[pygtk] Questions about GtkDrawingArea

Ben Escoto bescoto@stanford.edu
Fri, 18 Aug 2000 23:54:57 -0700


>>>>> "S" == Scott F Johnston <scott@fleetingimage.com>
>>>>> wrote the following on Fri, 18 Aug 2000 14:36:23 -0700

  S> If you look at the GtkDrawingArea widget, you'll see that it
  S> exposes a bunch of drawing methods:

     ...

  S> The scribble example is choosing to paint into a pixmap and then
  S> present that pixmap on a drawingarea.

  S> Your application can draw directly onto the drawing area without
  S> using a pixmap, if that is preferred.  Pixmaps are just amoung
  S> the things you can draw onto a drawingarea.

     That's true, but at least when I try it, all the drawings on the
area must be redone everytime the window gets exposed (and thus
everytime someone, for instance, switches back the the screen or
workspace).  For example, this seems to be wrong and results in
nothing getting shown:

....
darea = GtkDrawingArea()
darea.size(500, 500)
darea.show()
darea.draw_rectangle(darea.get_style().fg_gc[0], 1, 0,0, 100,100)
....
mainloop()


instead, the drawing must be done when the window is exposed, as in:

def expose_event(widget, event):
    widget.draw_rectangle(widget.get_style().fg_gc[0], 1, 0,0, 100,100)
....
darea = GtkDrawingArea()
darea.size(500, 500)
darea.show()
darea.connect("expose_event", expose_event)
darea.set_events(GDK.EXPOSURE_MASK)
....
mainloop()


    This second snippet results in the rectangle being visible, but it
must be redrawn everytime the window become re-exposed.  As a result
(I think), every time I switch back to a screen or workspace with an
application like this, I can see the default gtk background appear,
and then the rectangle gets drawn on top.  So I don't think the
problem is in pixmaps vs rectangles, etc.

     Also, using the other functions instead of a pixmap seems like a
pain since the function responding to an expose event is only supposed
to redraw the part of the window that is exposed.  If, for instance, I
drew a rectangle and then a circle onto the drawing area, then the
expose_event function would have to figure out which of these needs to
be redrawn and only update the relevant parts.  Having a pixmap the
size of the drawing area seems easier, since then you can just redraw
the area with the corresponding part of the pixmap, as is done in the
scribble example:

def expose_event(widget, event):
    area = event.area
    gc = widget.get_style().fg_gc[0]
    widget.draw_pixmap(gc, pixmap, area[0], area[1], area[0], area[1],
                       area[2], area[3])
    return FALSE


--
Ben Escoto