Advertisement
Guest User

GtkOverlay test

a guest
May 31st, 2012
435
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None
  1. #include <stdbool.h>
  2. #include <gtk/gtk.h>
  3.  
  4. static void close_window (void);
  5.  
  6. static cairo_surface_t *surface1 = NULL;
  7. static cairo_surface_t *surface2 = NULL;
  8.  
  9. static gboolean darea1_draw_cb (GtkWidget *widget,
  10.                                 cairo_t   *cr,
  11.                                 gpointer   data);
  12.  
  13. static gboolean darea2_draw_cb (GtkWidget *widget,
  14.                                 cairo_t   *cr,
  15.                                 gpointer   data);
  16.  
  17. static gboolean darea1_configure_event_cb (GtkWidget         *widget,
  18.                                            GdkEventConfigure *event,
  19.                                            gpointer           data);
  20.  
  21. static gboolean darea2_configure_event_cb (GtkWidget         *widget,
  22.                                            GdkEventConfigure *event,
  23.                                            gpointer           data);
  24.  
  25. int
  26. main (int argc, char *argv[])
  27. {
  28.     GtkWidget *window;
  29.     GtkWidget *viewport;
  30.     GtkWidget *scrolled;
  31.     GtkWidget *overlay;
  32.     GtkWidget *darea;
  33. ///    GtkSettings *settings;
  34.  
  35.     gtk_init (&argc, &argv);
  36.  
  37.     /* setup window */
  38.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  39.     gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
  40.     gtk_window_set_title (GTK_WINDOW (window), "Overlay");
  41.     gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  42.  
  43.     g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
  44.     g_signal_connect (window, "destroy", G_CALLBACK (close_window), &window);
  45.  
  46. ///    settings = gtk_settings_get_default ();
  47. ///    g_object_set (G_OBJECT (settings), "gtk-application-prefer-dark-theme", true, NULL);
  48.  
  49.     /* setup alignment */
  50.     viewport = gtk_viewport_new (NULL, NULL);
  51.  
  52.     /* setup scrolled window */
  53.     scrolled = gtk_scrolled_window_new (NULL, NULL);
  54.     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
  55.                                     GTK_POLICY_AUTOMATIC,
  56.                                     GTK_POLICY_AUTOMATIC);
  57.     gtk_container_add (GTK_CONTAINER (viewport), scrolled);
  58.  
  59.     /* setup the overlay */
  60.     overlay = gtk_overlay_new ();
  61.     gtk_container_add (GTK_CONTAINER (overlay), viewport);
  62.     gtk_container_add (GTK_CONTAINER (window), overlay);
  63.  
  64.     /* finally setup the widgets to place inside of the overlay */
  65.     darea = gtk_drawing_area_new ();
  66.     gtk_widget_set_size_request (darea, 200, 200);
  67.     gtk_widget_set_app_paintable (darea, true);
  68.     gtk_overlay_add_overlay (GTK_OVERLAY (overlay), darea);
  69.     gtk_widget_set_halign (darea, GTK_ALIGN_START);
  70.     gtk_widget_set_valign (darea, GTK_ALIGN_START);
  71.     gtk_widget_set_margin_left (darea, 10);
  72.     gtk_widget_set_margin_right (darea, 10);
  73.     gtk_widget_set_margin_top (darea, 10);
  74.     gtk_widget_set_margin_bottom (darea, 10);
  75.  
  76.     g_signal_connect (darea, "draw", G_CALLBACK (darea1_draw_cb), NULL);
  77.     g_signal_connect (darea, "configure-event", G_CALLBACK (darea1_configure_event_cb), NULL);
  78.  
  79.     darea = gtk_drawing_area_new ();
  80.     gtk_widget_set_size_request (darea, 300, 300);
  81.     gtk_widget_set_app_paintable (darea, true);
  82.     gtk_overlay_add_overlay (GTK_OVERLAY (overlay), darea);
  83.     gtk_widget_set_halign (darea, GTK_ALIGN_END);
  84.     gtk_widget_set_valign (darea, GTK_ALIGN_END);
  85.     gtk_widget_set_margin_left (darea, 20);
  86.     gtk_widget_set_margin_right (darea, 20);
  87.     gtk_widget_set_margin_top (darea, 20);
  88.     gtk_widget_set_margin_bottom (darea, 20);
  89.  
  90.     g_signal_connect (darea, "draw", G_CALLBACK (darea2_draw_cb), NULL);
  91.     g_signal_connect (darea, "configure-event", G_CALLBACK (darea2_configure_event_cb), NULL);
  92.  
  93.     gtk_widget_show_all (window);
  94.  
  95.     gtk_main ();
  96.  
  97.     return 0;
  98. }
  99.  
  100. static void
  101. close_window (void)
  102. {
  103.     if (surface1)
  104.         cairo_surface_destroy (surface1);
  105.     surface1 = NULL;
  106.  
  107.     if (surface2)
  108.         cairo_surface_destroy (surface2);
  109.     surface2 = NULL;
  110. }
  111.  
  112. static gboolean
  113. darea1_draw_cb (GtkWidget *widget,
  114.                 cairo_t   *cr,
  115.                 gpointer   data)
  116. {
  117.     g_debug ("Draw: drawing area 1");
  118.  
  119.     cairo_set_source_surface (cr, surface1, 0, 0);
  120.  
  121.     cairo_set_source_rgba (cr, 0.6, 0.2, 0.2, 0.5);
  122.     cairo_rectangle (cr, 10, 10, 150, 150);
  123.     cairo_fill (cr);
  124.  
  125.     cairo_paint (cr);
  126.  
  127.     return false;
  128. }
  129.  
  130. static gboolean
  131. darea2_draw_cb (GtkWidget *widget,
  132.                 cairo_t   *cr,
  133.                 gpointer   data)
  134. {
  135.     g_debug ("Draw: drawing area 2");
  136.  
  137.     cairo_set_source_surface (cr, surface2, 0, 0);
  138.  
  139.     cairo_set_source_rgba (cr, 0.4, 0.8, 0.4, 0.5);
  140.     cairo_rectangle (cr, 10, 10, 100, 100);
  141.     cairo_fill (cr);
  142.  
  143.     cairo_paint (cr);
  144.  
  145.     return false;
  146. }
  147.  
  148. static gboolean
  149. darea1_configure_event_cb (GtkWidget         *widget,
  150.                            GdkEventConfigure *event,
  151.                            gpointer           data)
  152. {
  153.     GtkAllocation allocation;
  154.     cairo_t *cr;
  155.  
  156.     g_debug ("Configure: drawing area 1");
  157.  
  158.     if (surface1)
  159.         cairo_surface_destroy (surface1);
  160.  
  161.     gtk_widget_get_allocation (widget, &allocation);
  162.     surface1 = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
  163.                                                   CAIRO_CONTENT_COLOR,
  164.                                                   allocation.width,
  165.                                                   allocation.height);
  166.  
  167.     /* initialize the surface to white */
  168.     cr = cairo_create (surface1);
  169.  
  170.     cairo_set_source_rgb (cr, 1, 1, 1);
  171.     cairo_paint (cr);
  172.  
  173.     cairo_destroy (cr);
  174.  
  175.     return true;
  176. }
  177.  
  178. static gboolean
  179. darea2_configure_event_cb (GtkWidget         *widget,
  180.                            GdkEventConfigure *event,
  181.                            gpointer           data)
  182. {
  183.     GtkAllocation allocation;
  184.     cairo_t *cr;
  185.  
  186.     g_debug ("Configure: drawing area 2");
  187.  
  188.     if (surface2)
  189.         cairo_surface_destroy (surface2);
  190.  
  191.     gtk_widget_get_allocation (widget, &allocation);
  192.     surface2 = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
  193.                                                   CAIRO_CONTENT_COLOR,
  194.                                                   allocation.width,
  195.                                                   allocation.height);
  196.  
  197.     /* initialize the surface to white */
  198.     cr = cairo_create (surface2);
  199.  
  200.     cairo_set_source_rgb (cr, 1, 1, 1);
  201.     cairo_paint (cr);
  202.  
  203.     cairo_destroy (cr);
  204.  
  205.     return true;
  206. }
Advertisement
RAW Paste Data Copied
Advertisement