]> Pileus Git - ~andy/gtk/blobdiff - demos/gtk-demo/offscreen_window2.c
gtk-demo: Move resources into the tests they belong
[~andy/gtk] / demos / gtk-demo / offscreen_window2.c
index d7e3c9524d027bd0acbbdee5b863f2c02c354c49..b28270448b5d1296fc3b5fd6e2a470b0a9b4c4ed 100644 (file)
@@ -35,14 +35,18 @@ GtkWidget* gtk_mirror_bin_new       (void);
 
 static void     gtk_mirror_bin_realize       (GtkWidget       *widget);
 static void     gtk_mirror_bin_unrealize     (GtkWidget       *widget);
-static void     gtk_mirror_bin_size_request  (GtkWidget       *widget,
-                                               GtkRequisition  *requisition);
+static void     gtk_mirror_bin_get_preferred_width  (GtkWidget *widget,
+                                                     gint      *minimum,
+                                                     gint      *natural);
+static void     gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
+                                                     gint      *minimum,
+                                                     gint      *natural);
 static void     gtk_mirror_bin_size_allocate (GtkWidget       *widget,
                                                GtkAllocation   *allocation);
 static gboolean gtk_mirror_bin_damage        (GtkWidget       *widget,
                                                GdkEventExpose  *event);
-static gboolean gtk_mirror_bin_expose        (GtkWidget       *widget,
-                                               GdkEventExpose  *offscreen);
+static gboolean gtk_mirror_bin_draw          (GtkWidget       *widget,
+                                              cairo_t         *cr);
 
 static void     gtk_mirror_bin_add           (GtkContainer    *container,
                                                GtkWidget       *child);
@@ -86,9 +90,10 @@ gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
 
   widget_class->realize = gtk_mirror_bin_realize;
   widget_class->unrealize = gtk_mirror_bin_unrealize;
-  widget_class->size_request = gtk_mirror_bin_size_request;
+  widget_class->get_preferred_width = gtk_mirror_bin_get_preferred_width;
+  widget_class->get_preferred_height = gtk_mirror_bin_get_preferred_height;
   widget_class->size_allocate = gtk_mirror_bin_size_allocate;
-  widget_class->expose_event = gtk_mirror_bin_expose;
+  widget_class->draw = gtk_mirror_bin_draw;
 
   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
                                    GTK_TYPE_MIRROR_BIN,
@@ -104,7 +109,7 @@ gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
 static void
 gtk_mirror_bin_init (GtkMirrorBin *bin)
 {
-  GTK_WIDGET_UNSET_FLAGS (bin, GTK_NO_WINDOW);
+  gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
 }
 
 GtkWidget *
@@ -122,11 +127,11 @@ pick_offscreen_child (GdkWindow     *offscreen_window,
  GtkAllocation child_area;
  double x, y;
 
- if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
+ if (bin->child && gtk_widget_get_visible (bin->child))
     {
       to_child (bin, widget_x, widget_y, &x, &y);
 
-      child_area = bin->child->allocation;
+      gtk_widget_get_allocation (bin->child, &child_area);
 
       if (x >= 0 && x < child_area.width &&
           y >= 0 && y < child_area.height)
@@ -162,19 +167,23 @@ static void
 gtk_mirror_bin_realize (GtkWidget *widget)
 {
   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
+  GtkAllocation allocation;
+  GtkStyleContext *context;
+  GdkWindow *window;
   GdkWindowAttr attributes;
   gint attributes_mask;
-  gint border_width;
+  guint border_width;
   GtkRequisition child_requisition;
 
-  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
+  gtk_widget_set_realized (widget, TRUE);
 
-  border_width = GTK_CONTAINER (widget)->border_width;
+  gtk_widget_get_allocation (widget, &allocation);
+  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
 
-  attributes.x = widget->allocation.x + border_width;
-  attributes.y = widget->allocation.y + border_width;
-  attributes.width = widget->allocation.width - 2 * border_width;
-  attributes.height = widget->allocation.height - 2 * border_width;
+  attributes.x = allocation.x + border_width;
+  attributes.y = allocation.y + border_width;
+  attributes.width = allocation.width - 2 * border_width;
+  attributes.height = allocation.height - 2 * border_width;
   attributes.window_type = GDK_WINDOW_CHILD;
   attributes.event_mask = gtk_widget_get_events (widget)
                         | GDK_EXPOSURE_MASK
@@ -186,40 +195,42 @@ gtk_mirror_bin_realize (GtkWidget *widget)
                         | GDK_LEAVE_NOTIFY_MASK;
 
   attributes.visual = gtk_widget_get_visual (widget);
-  attributes.colormap = gtk_widget_get_colormap (widget);
   attributes.wclass = GDK_INPUT_OUTPUT;
 
-  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
+  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
 
-  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
-                                   &attributes, attributes_mask);
-  gdk_window_set_user_data (widget->window, widget);
-  g_signal_connect (widget->window, "pick-embedded-child",
+  window = gdk_window_new (gtk_widget_get_parent_window (widget),
+                           &attributes, attributes_mask);
+  gtk_widget_set_window (widget, window);
+  gdk_window_set_user_data (window, widget);
+  g_signal_connect (window, "pick-embedded-child",
                     G_CALLBACK (pick_offscreen_child), bin);
 
   attributes.window_type = GDK_WINDOW_OFFSCREEN;
 
   child_requisition.width = child_requisition.height = 0;
-  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
+  if (bin->child && gtk_widget_get_visible (bin->child))
     {
-      attributes.width = bin->child->allocation.width;
-      attributes.height = bin->child->allocation.height;
+      GtkAllocation child_allocation;
+
+      gtk_widget_get_allocation (bin->child, &child_allocation);
+      attributes.width = child_allocation.width;
+      attributes.height = child_allocation.height;
     }
   bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
                                           &attributes, attributes_mask);
   gdk_window_set_user_data (bin->offscreen_window, widget);
   if (bin->child)
     gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
-  gdk_offscreen_window_set_embedder (bin->offscreen_window, widget->window);
+  gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
   g_signal_connect (bin->offscreen_window, "to-embedder",
                     G_CALLBACK (offscreen_window_to_parent), bin);
   g_signal_connect (bin->offscreen_window, "from-embedder",
                     G_CALLBACK (offscreen_window_from_parent), bin);
 
-  widget->style = gtk_style_attach (widget->style, widget->window);
-
-  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
-  gtk_style_set_background (widget->style, bin->offscreen_window, GTK_STATE_NORMAL);
+  context = gtk_widget_get_style_context (widget);
+  gtk_style_context_set_background (context, window);
+  gtk_style_context_set_background (context, bin->offscreen_window);
   gdk_window_show (bin->offscreen_window);
 }
 
@@ -269,7 +280,7 @@ gtk_mirror_bin_remove (GtkContainer *container,
   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
   gboolean was_visible;
 
-  was_visible = GTK_WIDGET_VISIBLE (widget);
+  was_visible = gtk_widget_get_visible (widget);
 
   if (bin->child == widget)
     {
@@ -277,7 +288,7 @@ gtk_mirror_bin_remove (GtkContainer *container,
 
       bin->child = NULL;
 
-      if (was_visible && GTK_WIDGET_VISIBLE (container))
+      if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
         gtk_widget_queue_resize (GTK_WIDGET (container));
     }
 }
@@ -302,15 +313,42 @@ gtk_mirror_bin_size_request (GtkWidget      *widget,
 {
   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
   GtkRequisition child_requisition;
+  guint border_width;
 
   child_requisition.width = 0;
   child_requisition.height = 0;
 
-  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
-    gtk_widget_size_request (bin->child, &child_requisition);
+  if (bin->child && gtk_widget_get_visible (bin->child))
+    gtk_widget_get_preferred_size ( (bin->child),
+                               &child_requisition, NULL);
+
+  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
+  requisition->width = border_width * 2 + child_requisition.width + 10;
+  requisition->height = border_width * 2 + child_requisition.height * 2 + 10;
+}
+
+static void
+gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
+                                    gint      *minimum,
+                                    gint      *natural)
+{
+  GtkRequisition requisition;
+
+  gtk_mirror_bin_size_request (widget, &requisition);
+
+  *minimum = *natural = requisition.width;
+}
+
+static void
+gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
+                                     gint      *minimum,
+                                     gint      *natural)
+{
+  GtkRequisition requisition;
+
+  gtk_mirror_bin_size_request (widget, &requisition);
 
-  requisition->width = GTK_CONTAINER (widget)->border_width * 2 + child_requisition.width + 10;
-  requisition->height = GTK_CONTAINER (widget)->border_width * 2 + child_requisition.height * 2 + 10;
+  *minimum = *natural = requisition.width;
 }
 
 static void
@@ -318,33 +356,35 @@ gtk_mirror_bin_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
 {
   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
-  gint border_width;
   gint w, h;
-  widget->allocation = *allocation;
+  guint border_width;
 
-  border_width = GTK_CONTAINER (widget)->border_width;
+  gtk_widget_set_allocation (widget, allocation);
+
+  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
 
   w = allocation->width - border_width * 2;
   h = allocation->height - border_width * 2;
 
-  if (GTK_WIDGET_REALIZED (widget))
-    gdk_window_move_resize (widget->window,
+  if (gtk_widget_get_realized (widget))
+    gdk_window_move_resize (gtk_widget_get_window (widget),
                             allocation->x + border_width,
                             allocation->y + border_width,
                             w, h);
 
-  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
+  if (bin->child && gtk_widget_get_visible (bin->child))
     {
       GtkRequisition child_requisition;
       GtkAllocation child_allocation;
 
-      gtk_widget_get_child_requisition (bin->child, &child_requisition);
+      gtk_widget_get_preferred_size (bin->child,
+                                     &child_requisition, NULL);
       child_allocation.x = 0;
       child_allocation.y = 0;
       child_allocation.height = child_requisition.height;
       child_allocation.width = child_requisition.width;
 
-      if (GTK_WIDGET_REALIZED (widget))
+      if (gtk_widget_get_realized (widget))
         gdk_window_move_resize (bin->offscreen_window,
                                 allocation->x + border_width,
                                 allocation->y + border_width,
@@ -357,85 +397,70 @@ static gboolean
 gtk_mirror_bin_damage (GtkWidget      *widget,
                         GdkEventExpose *event)
 {
-  gdk_window_invalidate_rect (widget->window, NULL, FALSE);
+  gdk_window_invalidate_rect (gtk_widget_get_window (widget),
+                              NULL, FALSE);
 
   return TRUE;
 }
 
 static gboolean
-gtk_mirror_bin_expose (GtkWidget      *widget,
-                        GdkEventExpose *event)
+gtk_mirror_bin_draw (GtkWidget *widget,
+                     cairo_t   *cr)
 {
   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
-  gint width, height;
+  GdkWindow *window;
 
-  if (GTK_WIDGET_DRAWABLE (widget))
+  window = gtk_widget_get_window (widget);
+  if (gtk_cairo_should_draw_window (cr, window))
     {
-      if (event->window == widget->window)
-        {
-          GdkPixmap *pixmap;
-          GtkAllocation child_area;
-          cairo_t *cr;
-          cairo_matrix_t matrix;
-          cairo_pattern_t *mask;
-
-          if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
-            {
-              pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
-              gdk_drawable_get_size (pixmap, &width, &height);
-              child_area = bin->child->allocation;
-
-              cr = gdk_cairo_create (widget->window);
-
-              cairo_save (cr);
-
-              cairo_rectangle (cr, 0, 0, width, height);
-              cairo_clip (cr);
+      cairo_surface_t *surface;
+      cairo_matrix_t matrix;
+      cairo_pattern_t *mask;
+      int height;
 
-              /* paint the offscreen child */
-              gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
-              cairo_paint (cr);
-
-              cairo_restore (cr);
+      if (bin->child && gtk_widget_get_visible (bin->child))
+        {
+          surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
+          height = gdk_window_get_height (bin->offscreen_window);
 
-              cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
-              cairo_matrix_scale (&matrix, 1.0, -1.0);
-              cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
-              cairo_transform (cr, &matrix);
+          /* paint the offscreen child */
+          cairo_set_source_surface (cr, surface, 0, 0);
+          cairo_paint (cr);
 
-              cairo_rectangle (cr, 0, height, width, height);
-              cairo_clip (cr);
+          cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
+          cairo_matrix_scale (&matrix, 1.0, -1.0);
+          cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
+          cairo_transform (cr, &matrix);
 
-              gdk_cairo_set_source_pixmap (cr, pixmap, 0, height);
+          cairo_set_source_surface (cr, surface, 0, height);
 
-              /* create linear gradient as mask-pattern to fade out the source */
-              mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
-              cairo_pattern_add_color_stop_rgba (mask, 0.0,  0.0, 0.0, 0.0, 0.0);
-              cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
-              cairo_pattern_add_color_stop_rgba (mask, 0.5,  0.0, 0.0, 0.0, 0.25);
-              cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
-              cairo_pattern_add_color_stop_rgba (mask, 1.0,  0.0, 0.0, 0.0, 1.0);
+          /* create linear gradient as mask-pattern to fade out the source */
+          mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
+          cairo_pattern_add_color_stop_rgba (mask, 0.0,  0.0, 0.0, 0.0, 0.0);
+          cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
+          cairo_pattern_add_color_stop_rgba (mask, 0.5,  0.0, 0.0, 0.0, 0.25);
+          cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
+          cairo_pattern_add_color_stop_rgba (mask, 1.0,  0.0, 0.0, 0.0, 1.0);
 
-              /* paint the reflection */
-              cairo_mask (cr, mask);
+          /* paint the reflection */
+          cairo_mask (cr, mask);
 
-              cairo_pattern_destroy (mask);
-              cairo_destroy (cr);
-            }
-        }
-      else if (event->window == bin->offscreen_window)
-        {
-          gtk_paint_flat_box (widget->style, event->window,
-                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
-                              &event->area, widget, "blah",
-                              0, 0, -1, -1);
-
-          if (bin->child)
-            gtk_container_propagate_expose (GTK_CONTAINER (widget),
-                                            bin->child,
-                                            event);
+          cairo_pattern_destroy (mask);
         }
     }
+  else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
+    {
+      gtk_render_background (gtk_widget_get_style_context (widget),
+                             cr,
+                             0, 0,
+                             gdk_window_get_width (bin->offscreen_window),
+                             gdk_window_get_height (bin->offscreen_window));
+
+      if (bin->child)
+        gtk_container_propagate_draw (GTK_CONTAINER (widget),
+                                      bin->child,
+                                      cr);
+    }
 
   return FALSE;
 }
@@ -463,13 +488,13 @@ do_offscreen_window2 (GtkWidget *do_widget)
 
       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 
-      vbox = gtk_vbox_new (0, FALSE);
+      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
 
       bin = gtk_mirror_bin_new ();
 
       group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
 
-      hbox = gtk_hbox_new (FALSE, 6);
+      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
       backbutton = gtk_button_new ();
       gtk_container_add (GTK_CONTAINER (backbutton),
                          gtk_image_new_from_stock (GTK_STOCK_GO_BACK, 4));
@@ -489,7 +514,7 @@ do_offscreen_window2 (GtkWidget *do_widget)
       gtk_box_pack_start (GTK_BOX (hbox), applybutton, FALSE, FALSE, 0);
     }
 
-  if (!GTK_WIDGET_VISIBLE (window))
+  if (!gtk_widget_get_visible (window))
     gtk_widget_show_all (window);
   else
     {
@@ -499,4 +524,3 @@ do_offscreen_window2 (GtkWidget *do_widget)
 
   return window;
 }
-