GtkDrawingArea - how to make it drawable?

后端 未结 2 622
情话喂你
情话喂你 2021-01-14 02:50

I\'m going out of my mind a bit here.

I\'m trying to draw some simple graphics on my GTK form using cairo.

#include 
#include 

        
相关标签:
2条回答
  • 2021-01-14 03:38

    The warning and the consequent crash in your first code is because of the fact that darea->window is NULL i.e. GdkWindow (darea->window) has not been created yet at the point in the program when you call gdk_cairo_create. GdkWindow for the drawing area is created once the drawing area widget is realized. Try adding gtk_widget_realize(darea); before the call gdk_cairo_create . Also instead of directly accessing window, may I suggest use of the accessor function gtk_widget_get_window.
    The reason why it did not assert & crash when you have added the same code inexpose-event callback is because GdkWindow associated with drawing area is already created at that point in the execution of the program.

    Edit:
    The reason provided above is only to explain why the code was not crashing in the expose-event callback & why the call gdk_cairo_create did not assert when the program was run. Treat the above response just as an explaination related to crash. Please refer ptomato's response for details regarding drawing mechanism

    Hope this helps!

    0 讨论(0)
  • 2021-01-14 03:44

    Drawing on a widget with Cairo only works in an expose event. That is because Cairo is not like a vector drawing program where the lines and shapes are objects that are remembered and can be manipulated; Cairo just paints the shapes onto the drawing area and forgets about them.

    So when you minimize and restore your window, or move another window over top of it, the shapes disappear. An expose event is generated to let you know that the shapes are gone and the widget needs to be redrawn. So you do your redrawing with Cairo in the expose event handler.

    0 讨论(0)
提交回复
热议问题