是否可以在GTK绘图区域绘制GTK小部件?

时间:2017-11-17 15:35:20

标签: ruby gtk gtk3 glade

我最近搬到Linux并尝试使用Ruby GTK3开发桌面应用程序。我希望能够将一个glade文件渲染到DrawingArea上(向用户预览Glade文件中的窗口是什么样的)。

这可能吗?例如,可以在GTK DrawingArea /画布上绘制GTK按钮或窗口。我猜它是,我认为这是Glade应用程序所做的,但我不知道从哪里开始研究。

任何指针都将非常感激。

1 个答案:

答案 0 :(得分:0)

是的,与Glade毫无关系。迟到总比没有好。希望其他人可以喜欢它:

#include <gtk/gtk.h>
#include <math.h>

GtkWidget *window;
GtkWidget *layout;
GtkWidget *canvas;

GtkWidget   *image;
GtkWidget   *eventbox;

static gboolean on_window_draw (GtkWidget *da, GdkEventExpose *event, gpointer data)
{
    GdkWindow *window;
    GdkDrawingContext *drawingContext;
    cairo_region_t *cairoRegion;
    cairo_t *cr;

    cairoRegion = cairo_region_create();
    window = gtk_layout_get_bin_window(GTK_LAYOUT(layout)); 
    drawingContext = gdk_window_begin_draw_frame(window, cairoRegion);
    cr = gdk_drawing_context_get_cairo_context(drawingContext);

    cairo_set_line_width(cr, 9);  
    cairo_set_source_rgb(cr, 0.69, 0.19, 0);

    cairo_translate(cr, 300/2, 200/2);
    cairo_arc(cr, 0, 0, 50, 0, 2 * M_PI);
    cairo_stroke_preserve(cr);

    cairo_set_source_rgb(cr, 0.3, 0.4, 0.6); 
    cairo_fill(cr);

    gdk_window_end_draw_frame(window, drawingContext);
    cairo_region_destroy(cairoRegion);

    return FALSE;
}

int main ( int argc, char **argv) 
{   
    GtkWidget *button;

    gtk_init (&argc , &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (window, 300, 200);
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit) , NULL);

    canvas = gtk_drawing_area_new ();
    layout = gtk_layout_new(NULL,NULL);
    gtk_container_add (GTK_CONTAINER (layout), canvas);
    gtk_container_add (GTK_CONTAINER (window), layout);

    button = gtk_button_new_with_label("I feel alone...");
    gtk_container_add(GTK_CONTAINER(layout), button);
    gtk_layout_move(GTK_LAYOUT(layout), button, 90, 50);
    g_signal_connect (canvas, "draw", (GCallback) on_window_draw, NULL);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}