Re: Using existing cairo context for GtkDrawingArea





El 09/12/06 08:18, asubedi escribió:
Hi,

Thanks for the reply.

On 12/8/06, Ivan Baldo <ibaldo adinet com uy> wrote:
    Hello.
    You can't AFAIK.
    GTK is double buffered, for every expose event, it draws to a
different place. When you draw on the expose event, you are not drawing
into the visible area of the screen, when the expose event is finished,
GTK switches the drawing you just did offscreen to appear on the screen
instead of the old one.
    So you must create a new Cairo context for every expose event, but
don't worry, this is highly optimized and no time is wasted on the new
context for every expose event.
    If you are having performance problems, you may try to use a cache
for some parts of your drawing, you can use the
cairo_surface_create_similar function for that, you can create a Cairo
context for that surface and draw things there, then you can use that
surface on the Cairo context of the expose event. You can cache mostly
static things and the more dynamic ones you can draw in the expose event.
    HTH, goodbye.

This seems reasonable, but my problem is different. I do not want the
user of my widget to put his/her drawing commands in the expose_event
handler.  The member functions of the widget will be things like
draw_line(x1, y1, x2, y2), draw_point(x, y), etc., which will be
converted to corresponding cairo calls. So I would like to create a
cairo context, draw on it using the above functions. And in the expose
event I would like to draw on the DrawingArea using the previously
created cairo context--which seems impossible. But is there a way to
copy a cairo context to the one created from DrawingArea?

In the first expose event of your widget you can create a cairo surface with cairo_surface_create_similar() and then a cairo context for it, save pointers for both the surface and the cairo context. Then, when the user calls your functions you draw in that saved cairo context and generate a new expose event; in the expose event handler you just draw the saved surface and thats it. Thats one way to accomplish what you say, another way is to manually save all the operations that the user has requested for your widget in some sort of array of commands and parameters and in every expose event just draw that with the Cairo calls. Another way is to manually generate an svg file with the functions that the user calls for the widget and in the expose event render it with librsvg.
   HTH, goodbye.

P.s.: please, answer to the mailing list so other people can help too, maybe they have better ideas, etc. and also they can answer sooner sometimes.


--
Ivan Baldo - ibaldo adinet com uy - http://ibaldo.codigolibre.net/
ICQ 10215364 - Phone/FAX (598) (2) 613 3223.
Caldas 1781, Malvin, Montevideo, Uruguay, South America.
We believe that we are free, but in reality we are not! Are we?
Alternatives: ibaldo codigolibre net - http://go.to/ibaldo





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]