How to create a cairo_t on a Gtk 2 window

后端 未结 1 1530
予麋鹿
予麋鹿 2021-01-14 10:36

I have an old app, now compiling on Gtk 2, but I need to introduce the use of Cairo. I can\'t figure out how to create the necessary cairo context (cairo_t) from my Widgets

相关标签:
1条回答
  • 2021-01-14 10:44

    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 easier to use gdk_cairo_create() to 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.

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