gtk-demo: Convert drawingarea example to Cairo

This commit is contained in:
Benjamin Otte 2010-07-17 00:18:35 +02:00
parent 4d07fa5772
commit 760ba34bd8

View File

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