]> Pileus Git - ~andy/gtk/blobdiff - tests/gtkoffscreenbox.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / gtkoffscreenbox.c
index a889296135d2e26117cdc3cec2100754441a5629..7aeb82cdfd397931dfee78f0bb742c790b4b3e04 100644 (file)
 
 static void        gtk_offscreen_box_realize       (GtkWidget       *widget);
 static void        gtk_offscreen_box_unrealize     (GtkWidget       *widget);
-static void        gtk_offscreen_box_size_request  (GtkWidget       *widget,
-                                                    GtkRequisition  *requisition);
+static void        gtk_offscreen_box_get_preferred_width  (GtkWidget *widget,
+                                                           gint      *minimum,
+                                                           gint      *natural);
+static void        gtk_offscreen_box_get_preferred_height (GtkWidget *widget,
+                                                           gint      *minimum,
+                                                           gint      *natural);
 static void        gtk_offscreen_box_size_allocate (GtkWidget       *widget,
                                                     GtkAllocation   *allocation);
 static gboolean    gtk_offscreen_box_damage        (GtkWidget       *widget,
                                                     GdkEventExpose  *event);
-static gboolean    gtk_offscreen_box_expose        (GtkWidget       *widget,
-                                                    GdkEventExpose  *offscreen);
+static gboolean    gtk_offscreen_box_draw          (GtkWidget       *widget,
+                                                    cairo_t         *cr);
 
 static void        gtk_offscreen_box_add           (GtkContainer    *container,
                                                     GtkWidget       *child);
@@ -47,9 +51,11 @@ to_child_2 (GtkOffscreenBox *offscreen_box,
   x = widget_x;
   y = widget_y;
 
-  gtk_widget_get_allocation (offscreen_box->child1, &child_area);
   if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
-    y -= child_area.height;
+    {
+      gtk_widget_get_allocation (offscreen_box->child1, &child_area);
+      y -= child_area.height;
+    }
 
   gtk_widget_get_allocation (offscreen_box->child2, &child_area);
 
@@ -99,9 +105,11 @@ to_parent_2 (GtkOffscreenBox *offscreen_box,
   x += child_area.width / 2;
   y += child_area.height / 2;
 
-  gtk_widget_get_allocation (offscreen_box->child1, &child_area);
   if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
-    y += child_area.height;
+    {
+      gtk_widget_get_allocation (offscreen_box->child1, &child_area);
+      y += child_area.height;
+    }
 
   *x_out = x;
   *y_out = y;
@@ -115,9 +123,10 @@ gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass)
 
   widget_class->realize = gtk_offscreen_box_realize;
   widget_class->unrealize = gtk_offscreen_box_unrealize;
-  widget_class->size_request = gtk_offscreen_box_size_request;
+  widget_class->get_preferred_width = gtk_offscreen_box_get_preferred_width;
+  widget_class->get_preferred_height = gtk_offscreen_box_get_preferred_height;
   widget_class->size_allocate = gtk_offscreen_box_size_allocate;
-  widget_class->expose_event = gtk_offscreen_box_expose;
+  widget_class->draw = gtk_offscreen_box_draw;
 
   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
                                    GTK_TYPE_OFFSCREEN_BOX,
@@ -230,12 +239,20 @@ offscreen_window_from_parent2 (GdkWindow       *window,
              offscreen_x, offscreen_y);
 }
 
+static cairo_surface_t *
+gdk_offscreen_box_create_alpha_image_surface (GdkWindow *offscreen,
+                                              gint       width,
+                                              gint       height)
+{
+  return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+}
+
 static void
 gtk_offscreen_box_realize (GtkWidget *widget)
 {
   GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
   GtkAllocation allocation, child_area;
-  GtkStyle *style;
+  GtkStyleContext *context;
   GdkWindow *window;
   GdkWindowAttr attributes;
   gint attributes_mask;
@@ -319,16 +336,19 @@ gtk_offscreen_box_realize (GtkWidget *widget)
     gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
   gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
                                     window);
+
+  g_signal_connect (offscreen_box->offscreen_window2, "create-surface",
+                    G_CALLBACK (gdk_offscreen_box_create_alpha_image_surface),
+                    offscreen_box);
   g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
                    G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
   g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
                    G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
 
-  gtk_widget_style_attach (widget);
-  style = gtk_widget_get_style (widget);
-  gtk_style_set_background (style, window, GTK_STATE_NORMAL);
-  gtk_style_set_background (style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
-  gtk_style_set_background (style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
+  context = gtk_widget_get_style_context (widget);
+  gtk_style_context_set_background (context, window);
+  gtk_style_context_set_background (context, offscreen_box->offscreen_window1);
+  gtk_style_context_set_background (context, offscreen_box->offscreen_window2);
 
   gdk_window_show (offscreen_box->offscreen_window1);
   gdk_window_show (offscreen_box->offscreen_window2);
@@ -478,7 +498,7 @@ gtk_offscreen_box_size_request (GtkWidget      *widget,
     {
       GtkRequisition child_requisition;
 
-      gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child1),
+      gtk_widget_get_preferred_size ( (offscreen_box->child1),
                                  &child_requisition, NULL);
 
       w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
@@ -489,7 +509,7 @@ gtk_offscreen_box_size_request (GtkWidget      *widget,
     {
       GtkRequisition child_requisition;
 
-      gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child2),
+      gtk_widget_get_preferred_size ( (offscreen_box->child2),
                                  &child_requisition, NULL);
 
       w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
@@ -501,6 +521,30 @@ gtk_offscreen_box_size_request (GtkWidget      *widget,
   requisition->height = border_width * 2 + h;
 }
 
+static void
+gtk_offscreen_box_get_preferred_width (GtkWidget *widget,
+                                       gint      *minimum,
+                                       gint      *natural)
+{
+  GtkRequisition requisition;
+
+  gtk_offscreen_box_size_request (widget, &requisition);
+
+  *minimum = *natural = requisition.width;
+}
+
+static void
+gtk_offscreen_box_get_preferred_height (GtkWidget *widget,
+                                        gint      *minimum,
+                                        gint      *natural)
+{
+  GtkRequisition requisition;
+
+  gtk_offscreen_box_size_request (widget, &requisition);
+
+  *minimum = *natural = requisition.height;
+}
+
 static void
 gtk_offscreen_box_size_allocate (GtkWidget     *widget,
                                 GtkAllocation *allocation)
@@ -529,8 +573,8 @@ gtk_offscreen_box_size_allocate (GtkWidget     *widget,
       GtkRequisition child_requisition;
       GtkAllocation child_allocation;
 
-      gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child1),
-                                 &child_requisition, NULL);
+      gtk_widget_get_preferred_size (offscreen_box->child1,
+                                     &child_requisition, NULL);
       child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
       child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
       child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
@@ -554,8 +598,8 @@ gtk_offscreen_box_size_allocate (GtkWidget     *widget,
       GtkRequisition child_requisition;
       GtkAllocation child_allocation;
 
-      gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child2),
-                                 &child_requisition, NULL);
+      gtk_widget_get_preferred_size (offscreen_box->child2,
+                                     &child_requisition, NULL);
       child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
       child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
       child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
@@ -586,88 +630,69 @@ gtk_offscreen_box_damage (GtkWidget      *widget,
 }
 
 static gboolean
-gtk_offscreen_box_expose (GtkWidget      *widget,
-                         GdkEventExpose *event)
+gtk_offscreen_box_draw (GtkWidget *widget,
+                        cairo_t   *cr)
 {
   GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
   GdkWindow *window;
 
-  if (gtk_widget_is_drawable (widget))
+  window = gtk_widget_get_window (widget);
+  if (gtk_cairo_should_draw_window (cr, window))
+    {
+      cairo_surface_t *surface;
+      GtkAllocation child_area;
+
+      if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
+        {
+          surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
+
+          cairo_set_source_surface (cr, surface, 0, 0);
+          cairo_paint (cr);
+
+          gtk_widget_get_allocation (offscreen_box->child1, &child_area);
+          cairo_translate (cr, 0, child_area.height);
+        }
+
+      if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
+        {
+          surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
+
+          gtk_widget_get_allocation (offscreen_box->child2, &child_area);
+
+          /* transform */
+          cairo_translate (cr, child_area.width / 2, child_area.height / 2);
+          cairo_rotate (cr, offscreen_box->angle);
+          cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
+
+          /* paint */
+          cairo_set_source_surface (cr, surface, 0, 0);
+          cairo_paint (cr);
+        }
+    }
+  else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
+    {
+      gtk_render_background (gtk_widget_get_style_context (widget), cr,
+                             0, 0,
+
+                             gdk_window_get_width (offscreen_box->offscreen_window1),
+                             gdk_window_get_height (offscreen_box->offscreen_window1));
+
+      if (offscreen_box->child1)
+        gtk_container_propagate_draw (GTK_CONTAINER (widget),
+                                      offscreen_box->child1,
+                                      cr);
+    }
+  else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
     {
-      window = gtk_widget_get_window (widget);
-      if (event->window == window)
-       {
-          cairo_surface_t *surface;
-          GtkAllocation child_area;
-          cairo_t *cr;
-         int start_y = 0;
-
-         if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
-           {
-             surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
-              gtk_widget_get_allocation (offscreen_box->child1, &child_area);
-
-             cr = gdk_cairo_create (window);
-
-              cairo_set_source_surface (cr, surface, 0, 0);
-              cairo_paint (cr);
-
-              cairo_destroy (cr);
-
-              start_y += child_area.height;
-           }
-
-         if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
-           {
-              gint w, h;
-
-             surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
-              gtk_widget_get_allocation (offscreen_box->child2, &child_area);
-
-             cr = gdk_cairo_create (window);
-
-              /* transform */
-             cairo_translate (cr, 0, start_y);
-             cairo_translate (cr, child_area.width / 2, child_area.height / 2);
-             cairo_rotate (cr, offscreen_box->angle);
-             cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
-
-              /* clip */
-              gdk_drawable_get_size (offscreen_box->offscreen_window2, &w, &h);
-              cairo_rectangle (cr, 0, 0, w, h);
-              cairo_clip (cr);
-
-              /* paint */
-             cairo_set_source_surface (cr, surface, 0, 0);
-             cairo_paint (cr);
-
-              cairo_destroy (cr);
-           }
-       }
-      else if (event->window == offscreen_box->offscreen_window1)
-       {
-         gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
-                             GTK_STATE_NORMAL, GTK_SHADOW_NONE,
-                             &event->area, widget, "blah",
-                             0, 0, -1, -1);
-
-         if (offscreen_box->child1)
-           gtk_container_propagate_expose (GTK_CONTAINER (widget),
-                                           offscreen_box->child1,
-                                            event);
-       }
-      else if (event->window == offscreen_box->offscreen_window2)
-       {
-         gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
-                             GTK_STATE_NORMAL, GTK_SHADOW_NONE,
-                             &event->area, widget, "blah",
-                             0, 0, -1, -1);
-
-         if (offscreen_box->child2)
-           gtk_container_propagate_expose (GTK_CONTAINER (widget),
-                                           offscreen_box->child2,
-                                            event);
-       }
+      gtk_render_background (gtk_widget_get_style_context (widget), cr,
+                             0, 0,
+                             gdk_window_get_width (offscreen_box->offscreen_window2),
+                             gdk_window_get_height (offscreen_box->offscreen_window2));
+
+      if (offscreen_box->child2)
+        gtk_container_propagate_draw (GTK_CONTAINER (widget),
+                                      offscreen_box->child2,
+                                      cr);
     }
 
   return FALSE;