[cairo] Can't draw more

Gary Jaffe gfj555 at gmail.com
Sat Nov 24 05:50:16 PST 2007



Kalle Vahlman wrote:
> 2007/11/24, Gary Jaffe <gfj555 at gmail.com>:

[snip...]

>> I get the following error.
>>
>> cairo.Error: the target surface has been finished
>>
>> Obviously, I'm missing something here.
> 
> Judging by the error, you are hanging on to the cairo context created
> in the expose handler and using it outside the handler. The way GTK+
> does the expose functions is that it creates a temporary drawable to
> take in the drawing and then updates it all in one go to the real
> drawable. Thus you created a cairo surface for the temporary drawable
> and it already went away.

Yes, that's exactly what I'm trying to do.
> 
> Drawing directly outside the expose handler is not a good idea with
> GTK+ anyway, as it brings problems with preserving the contents (GTK+
> likely will quickly overwrite anything you draw outside the handler).
> 
>>  Can someone please clue me in on
>> how to draw more on the background after the background is already drawn
>> or point me to some documentation that will tell me?
> 
> The "correct" way to draw something in GTK+ is to invalidate the area
> you want to draw to and then handle that in the eventually incoming
> expose event. It *is* a bit clunky and makes the expose handler more
> complex, but at least you get automatical double-buffering with the
> scheme.
> 
> http://pygtk.org/docs/pygtk/class-gtkwidget.html#method-gtkwidget--queue-draw
> 
> Oh, and just in case you were wondering, this doesn't mean you need to
> redraw the whole widget every time. You should always check the
> exposed area from the event structure you get in the expose handler to
> see what part of the widget needs redrawing and at minimum set the
> cairo clip area to that to minimize the work done.
> 
I'm trying to create something that allows the user to draw on a
gtk.DrawingArea.  I see there is an example of doing this in the pygtk
tutorial called scribblesimple.py at
<http://www.pygtk.org/pygtk2tutorial/sec-EventHandling.html#simplescribblefig>, 

although that script doesn't use cairo.  It does use a working pixmap to
draw on, and then the expose_event transfers the modified area of the
pixmap to the gtk.DrawingArea with the draw_drawable() method.

Is drawing on a working pixmap and then transferring the modified part
of the pixmap to a cairo surface a reasonable way to go?

Thanks for your help.
Gary



More information about the cairo mailing list