Accessing Cairo Context Outside a GtkDrawingArea draw_callback

1.2k views Asked by At

Is it possible to access and change a GtkDrawingArea context outside of its draw_callback? If yes, I'd be very happy if you provided a short example(any language) or point me where I should look at. All examples I found until now were drawing inside the callback, that's why I'm questioning the possibility.

2

There are 2 answers

0
oldtechaa On

In GTK+2 this was certainly possible by using GDK and Cairo interaction to get a Cairo context from a GdkWindow. In GTK+3, which is likely what you're asking about, you can still do this in a way, with this and this.

This sounds like a classic manifestation of the XY problem. With the Cairo interaction of GTK+3, you can now just queue a draw signal for only the specific clip region you want. Just write your draw callback generically and draw only what's necessary in the clipped region.

I hope the docs will suffice, but if not, let me know. I just don't have the time to prepare an example this moment.

0
klozovin On

Responding this late is a bit weird, but I've just come across a similar problem, so here are my two cents for posteriority.

You generally don't want to issue Cairo commands to DrawingArea's context outside of the ::draw callback. At the same time, you usually want to draw stuff outside the ::draw handler (for example long running drawing calculations). If that's your case, then you should create an additional Cairo context (an offscreen one) and draw into that one (possibly in another thread if you're doing something long running). Then, inside the ::draw handler paint using that offscreen context as a surface (hopefully my terminology is correct with regards to context, surface, painting, drawing, but you get the idea).

Good example of that is in GTK+ documentation on custom drawing, take a look at the draw_cb function.