如何将C程序从GTK2移植到GTK3

时间:2016-10-22 08:15:55

标签: gtk

有一个gtk2程序,它自定义一个圆形按钮,当移植到Gtk3时,圆形按钮无法正确绘制。例如下面的图像。 如何将C程序从GTK2移植到GTK3

circle button build with GTK2

circle button build with GTK3

源代码

#include <gtk/gtk.h> 

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *hbox;
    GtkWidget *eventbox;


#if  !GTK_CHECK_VERSION (3, 0, 0)
    GdkGC *gc;

    GdkColormap *colormap;

    GdkBitmap *window_shape_bitmap;

    GdkColor black;
    GdkColor white;
#endif

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title(GTK_WINDOW(window), "circle button test!");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    hbox = gtk_hbox_new(FALSE, 10);

    gtk_container_add(GTK_CONTAINER(window), hbox);
    gtk_widget_show(hbox);

    eventbox = gtk_event_box_new();
    gtk_box_pack_start(GTK_BOX(hbox), eventbox, TRUE, TRUE, 0);
    gtk_widget_show(eventbox);

    button = gtk_button_new_with_label("Hello,World!Hello,World!");

    gtk_container_add(GTK_CONTAINER(eventbox), button);
    gtk_widget_show(button);

    GtkWidget *button2 = gtk_button_new_with_label("Hello,World!Hello,World!");
    gtk_box_pack_start(GTK_BOX(hbox), button2, TRUE, TRUE, 0);
    gtk_widget_show(button2);

    gtk_widget_show_all(window);

#if  !GTK_CHECK_VERSION (3, 0, 0)
    colormap = gdk_colormap_get_system ();
    gdk_color_black (colormap, & black);
    gdk_color_white (colormap, & white);

    window_shape_bitmap = (GdkBitmap *) gdk_pixmap_new (NULL, 400, 300, 1);

    gc = gdk_gc_new (window_shape_bitmap);
    gdk_gc_set_foreground (gc, & black);
    gdk_gc_set_background (gc, & white);
    gdk_draw_rectangle (window_shape_bitmap, gc, TRUE, 0, 0, 400, 300);

    gdk_gc_set_foreground (gc, & white);
    gdk_gc_set_background (gc, & black);
    gdk_draw_arc(window_shape_bitmap, gc,TRUE, 50, 50, 200, 150, 0, 360*64);
    gtk_widget_shape_combine_mask(eventbox, window_shape_bitmap, 0, 0);
#else
    cairo_region_t *regionX = NULL;
    cairo_rectangle_int_t r;

    regionX = cairo_region_create();
        r.x = 50;
        r.y = 50;
        r.width = 100;
        r.height = 100;
        if (CAIRO_STATUS_SUCCESS != cairo_region_union_rectangle(regionX, &r))
        {
            g_warning("cairo_region_union_rectangle not success");
        }

    gtk_widget_shape_combine_region (GTK_WIDGET(eventbox), regionX);
    //gtk_widget_input_shape_combine_region (GTK_WIDGET(eventbox), regionX);


    cairo_region_destroy(regionX);
#endif

    gtk_main();
    return 0;
} 

0 个答案:

没有答案