Custom drawing in Gtk3 with Cairo is explained in https://developer.gnome.org/gtk3/stable/ch01s05.html
Here draw_brush
in the handler for motion-notify-event draws small rectangles as the mouse is dragged. In the original code there is no other drawing. Suppose I draw a filled blue rectangle in draw_cb
by adding the following code:
cairo_set_source_rgb(cr,0.1,0.1,0.8);
cairo_rectangle(cr,80,80,50,50);
cairo_fill(cr);
and similarly another filled red rectangle in clear_surface
which is called from configure_event_cb
, I get a strange behavior where the blue rectangle is not over-written by draw-brush, but the red rectangle gets over-written, as seen in the picture below:
Can anyone explain this behavior so that I can correctly make custom drawings in the application I am developing.
So, in clear_surface
/ configure_event_cb
, you draw to surface
, which is also the surface where the brush draws to. Since the brush is drawn later, it ends up ontop of the red rectangle that you draw here.
In draw_cb
, this temporary surface that is used for the drawing is copied to the screen. If you draw a blue rectangle afterwards to the screen, this blue rectangle ends up ontop of what you drew before.
So basically: The reason is the two different targets for drawing that are used here. One is the "actual stuff" on screen, which can disappear at any time. The other is an internal surface created in configure_event_cb
that does not go away unexpectedly.