gtk-demo: Convert drawingarea example to Cairo
This commit is contained in:
@ -25,6 +25,8 @@ scribble_configure_event (GtkWidget *widget,
|
|||||||
GdkEventConfigure *event,
|
GdkEventConfigure *event,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
|
cairo_t *cr;
|
||||||
|
|
||||||
if (pixmap)
|
if (pixmap)
|
||||||
g_object_unref (pixmap);
|
g_object_unref (pixmap);
|
||||||
|
|
||||||
@ -34,12 +36,12 @@ scribble_configure_event (GtkWidget *widget,
|
|||||||
-1);
|
-1);
|
||||||
|
|
||||||
/* Initialize the pixmap to white */
|
/* Initialize the pixmap to white */
|
||||||
gdk_draw_rectangle (pixmap,
|
cr = gdk_cairo_create (pixmap);
|
||||||
widget->style->white_gc,
|
|
||||||
TRUE,
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
||||||
0, 0,
|
cairo_paint (cr);
|
||||||
widget->allocation.width,
|
|
||||||
widget->allocation.height);
|
cairo_destroy (cr);
|
||||||
|
|
||||||
/* We've handled the configure event, no need for further processing. */
|
/* We've handled the configure event, no need for further processing. */
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@ -74,6 +76,7 @@ draw_brush (GtkWidget *widget,
|
|||||||
gdouble y)
|
gdouble y)
|
||||||
{
|
{
|
||||||
GdkRectangle update_rect;
|
GdkRectangle update_rect;
|
||||||
|
cairo_t *cr;
|
||||||
|
|
||||||
update_rect.x = x - 3;
|
update_rect.x = x - 3;
|
||||||
update_rect.y = y - 3;
|
update_rect.y = y - 3;
|
||||||
@ -81,11 +84,10 @@ draw_brush (GtkWidget *widget,
|
|||||||
update_rect.height = 6;
|
update_rect.height = 6;
|
||||||
|
|
||||||
/* Paint to the pixmap, where we store our state */
|
/* Paint to the pixmap, where we store our state */
|
||||||
gdk_draw_rectangle (pixmap,
|
cr = gdk_cairo_create (pixmap);
|
||||||
widget->style->black_gc,
|
|
||||||
TRUE,
|
gdk_cairo_rectangle (cr, &update_rect);
|
||||||
update_rect.x, update_rect.y,
|
cairo_fill (cr);
|
||||||
update_rect.width, update_rect.height);
|
|
||||||
|
|
||||||
/* Now invalidate the affected region of the drawing area. */
|
/* Now invalidate the affected region of the drawing area. */
|
||||||
gdk_window_invalidate_rect (widget->window,
|
gdk_window_invalidate_rect (widget->window,
|
||||||
@ -146,8 +148,7 @@ checkerboard_expose (GtkWidget *da,
|
|||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
gint i, j, xcount, ycount;
|
gint i, j, xcount, ycount;
|
||||||
GdkGC *gc1, *gc2;
|
cairo_t *cr;
|
||||||
GdkColor color;
|
|
||||||
|
|
||||||
#define CHECK_SIZE 10
|
#define CHECK_SIZE 10
|
||||||
#define SPACING 2
|
#define SPACING 2
|
||||||
@ -159,21 +160,9 @@ checkerboard_expose (GtkWidget *da,
|
|||||||
* works.
|
* works.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* It would be a bit more efficient to keep these
|
cr = gdk_cairo_create (da->window);
|
||||||
* GC's around instead of recreating on each expose, but
|
gdk_cairo_rectangle (cr, &event->area);
|
||||||
* this is the lazy/slow way.
|
cairo_clip (cr);
|
||||||
*/
|
|
||||||
gc1 = gdk_gc_new (da->window);
|
|
||||||
color.red = 30000;
|
|
||||||
color.green = 0;
|
|
||||||
color.blue = 30000;
|
|
||||||
gdk_gc_set_rgb_fg_color (gc1, &color);
|
|
||||||
|
|
||||||
gc2 = gdk_gc_new (da->window);
|
|
||||||
color.red = 65535;
|
|
||||||
color.green = 65535;
|
|
||||||
color.blue = 65535;
|
|
||||||
gdk_gc_set_rgb_fg_color (gc2, &color);
|
|
||||||
|
|
||||||
xcount = 0;
|
xcount = 0;
|
||||||
i = SPACING;
|
i = SPACING;
|
||||||
@ -186,20 +175,16 @@ checkerboard_expose (GtkWidget *da,
|
|||||||
GdkGC *gc;
|
GdkGC *gc;
|
||||||
|
|
||||||
if (ycount % 2)
|
if (ycount % 2)
|
||||||
gc = gc1;
|
cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
|
||||||
else
|
else
|
||||||
gc = gc2;
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
||||||
|
|
||||||
/* If we're outside event->area, this will do nothing.
|
/* If we're outside event->area, this will do nothing.
|
||||||
* It might be mildly more efficient if we handled
|
* It might be mildly more efficient if we handled
|
||||||
* the clipping ourselves, but again we're feeling lazy.
|
* the clipping ourselves, but again we're feeling lazy.
|
||||||
*/
|
*/
|
||||||
gdk_draw_rectangle (da->window,
|
cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
|
||||||
gc,
|
cairo_fill (cr);
|
||||||
TRUE,
|
|
||||||
i, j,
|
|
||||||
CHECK_SIZE,
|
|
||||||
CHECK_SIZE);
|
|
||||||
|
|
||||||
j += CHECK_SIZE + SPACING;
|
j += CHECK_SIZE + SPACING;
|
||||||
++ycount;
|
++ycount;
|
||||||
@ -209,8 +194,7 @@ checkerboard_expose (GtkWidget *da,
|
|||||||
++xcount;
|
++xcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_object_unref (gc1);
|
cairo_destroy (cr);
|
||||||
g_object_unref (gc2);
|
|
||||||
|
|
||||||
/* return TRUE because we've handled this event, so no
|
/* return TRUE because we've handled this event, so no
|
||||||
* further processing is required.
|
* further processing is required.
|
||||||
|
Reference in New Issue
Block a user