How to create a cairo_t on a Gtk 2 window

社会主义新天地 提交于 2019-12-01 08:25:50

I hope the tutorial did not do cairo drawing in main()... The meaningful place to draw onto a GTK2 widget is in the expose-event (and if you want to force a redraw from somewhere else, just call gtk_widget_queue_draw()). It is easer to use gdk_cairo_create() get a cairo context.

Something like this:

static gboolean
on_expose_event (GtkWidget *widget,
                 GdkEventExpose *event,
                 gpointer data)
{
    cairo_t *cr;

    cr = gdk_cairo_create (gtk_widget_get_window (widget));
    cairo_move_to (cr, 30, 30);
    cairo_show_text (cr, "Text");

    cairo_destroy (cr);

    return FALSE;
}

g_signal_connect(darea, "expose-event",
                 G_CALLBACK(on_expose_event), NULL);

Jan Bodnar has a more complete example (in the end).

This is all a lot nicer in GTK3 in my opinion. Still, even if your goal is to port to GTK3 it may make sense to change the drawing to use cairo first as you're doing -- changing to GTK3 afterwards should just simplify the code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!