[cairo] Dealing with drawingArea update (refresh) for multiple drawings.

Carlo Wood carlo at alinoe.com
Fri Aug 25 19:24:00 PDT 2006

On Fri, Aug 25, 2006 at 05:35:07PM -0300, Gustavo Sikora wrote:
> Some code of what I'm doing:
> gboolean expose_event_callback (GtkWidget *widget, GdkEventExpose *event)
> {
>    cairo_t *cr;
> 	/* get a cairo_t */
> 	cr = gdk_cairo_create (widget->window);
> 	cairo_rectangle (cr,
> 			event->area.x, event->area.y,
> 			event->area.width, event->area.height);
> 	cairo_clip (cr);
> 	newHeap->drawHeap(cr);
> 	cairo_destroy (cr);
>    return TRUE;
> }

Note that event->area is the 'extends' *) of the region that
was invalidated. That is, the smallest rectangle that includes
every part that was invalidated. This can be much larger than
what really was invalidated (think of moving another window over
the widget - that would expose an L-shaped region, and the
extends would thus be much larger).

A better way to write a general expose function with cairo
would be as follows:

gboolean expose_event_callback (GtkWidget *widget, GdkEventExpose *event)
  /* get a cairo_t */
  cairo_t* cr = gdk_cairo_create (widget->window);

  GdkRegion* region = event->region;
  gdk_cairo_region(cr, region);		// Set path equal to region

  cairo_destroy (cr);

  return TRUE;

Of course - this would still be doing a lot of possibly
unnecessary work inside drawHeap (you might find ways to
only redraw what was changed blah blah), but I can't put
that in this mail (I'm thinking about writing a tutorial
about that though...).  Anyhow, this sets the correct clipping

Carlo Wood <carlo at alinoe.com>

PS1 *) 'extends' can also be called 'clipbox', it is namely
the same rectangle as returned by gdk_region_get_clipbox for
the region.

PS2 A region is a list of rectangles.

More information about the cairo mailing list