]> Pileus Git - ~andy/gtk/blobdiff - demos/gtk-demo/offscreen_window.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / demos / gtk-demo / offscreen_window.c
index b07db50e918766ad52ad66f0bde79ac065d0f127..6a11786b8598b72f58f02f0df4e3f84140b01b21 100644 (file)
@@ -3,7 +3,6 @@
  * Offscreen windows can be used to transform parts of a widget
  * hierarchy. Note that the rotated button is fully functional.
  */
-#define _GNU_SOURCE
 #include <math.h>
 #include <gtk/gtk.h>
 
@@ -46,8 +45,8 @@ static void     gtk_rotated_bin_size_allocate (GtkWidget       *widget,
                                                GtkAllocation   *allocation);
 static gboolean gtk_rotated_bin_damage        (GtkWidget       *widget,
                                                GdkEventExpose  *event);
-static gboolean gtk_rotated_bin_expose        (GtkWidget       *widget,
-                                               GdkEventExpose  *offscreen);
+static gboolean gtk_rotated_bin_draw          (GtkWidget       *widget,
+                                               cairo_t         *cr);
 
 static void     gtk_rotated_bin_add           (GtkContainer    *container,
                                                GtkWidget       *child);
@@ -73,8 +72,9 @@ to_child (GtkRotatedBin *bin,
   double c, s;
   double w, h;
 
-  sincos (bin->angle, &s, &c);
-  child_area = bin->child->allocation;
+  s = sin (bin->angle);
+  c = cos (bin->angle);
+  gtk_widget_get_allocation (bin->child, &child_area);
 
   w = c * child_area.width + s * child_area.height;
   h = s * child_area.width + c * child_area.height;
@@ -112,8 +112,9 @@ to_parent (GtkRotatedBin *bin,
   double c, s;
   double w, h;
 
-  sincos (bin->angle, &s, &c);
-  child_area = bin->child->allocation;
+  s = sin (bin->angle);
+  c = cos (bin->angle);
+  gtk_widget_get_allocation (bin->child, &child_area);
 
   w = c * child_area.width + s * child_area.height;
   h = s * child_area.width + c * child_area.height;
@@ -149,7 +150,7 @@ gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
   widget_class->unrealize = gtk_rotated_bin_unrealize;
   widget_class->size_request = gtk_rotated_bin_size_request;
   widget_class->size_allocate = gtk_rotated_bin_size_allocate;
-  widget_class->expose_event = gtk_rotated_bin_expose;
+  widget_class->draw = gtk_rotated_bin_draw;
 
   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
                                    GTK_TYPE_ROTATED_BIN,
@@ -165,7 +166,7 @@ gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
 static void
 gtk_rotated_bin_init (GtkRotatedBin *bin)
 {
-  GTK_WIDGET_UNSET_FLAGS (bin, GTK_NO_WINDOW);
+  gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
 }
 
 GtkWidget *
@@ -183,11 +184,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)
@@ -223,19 +224,23 @@ static void
 gtk_rotated_bin_realize (GtkWidget *widget)
 {
   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
+  GtkAllocation allocation;
+  GtkStyle *style;
+  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
@@ -247,40 +252,43 @@ gtk_rotated_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);
+  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, bin->offscreen_window, GTK_STATE_NORMAL);
   gdk_window_show (bin->offscreen_window);
 }
 
@@ -330,7 +338,7 @@ gtk_rotated_bin_remove (GtkContainer *container,
   GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
   gboolean was_visible;
 
-  was_visible = GTK_WIDGET_VISIBLE (widget);
+  was_visible = gtk_widget_get_visible (widget);
 
   if (bin->child == widget)
     {
@@ -338,7 +346,7 @@ gtk_rotated_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));
     }
 }
@@ -366,7 +374,7 @@ gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
   bin->angle = angle;
   gtk_widget_queue_resize (GTK_WIDGET (bin));
 
-  /* TODO: Really needs to resent pointer events if over the rotated window */
+  gdk_window_geometry_changed (bin->offscreen_window);
 }
 
 static void
@@ -377,19 +385,23 @@ gtk_rotated_bin_size_request (GtkWidget      *widget,
   GtkRequisition child_requisition;
   double s, c;
   double w, h;
+  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);
 
-  sincos (bin->angle, &s, &c);
+  s = sin (bin->angle);
+  c = cos (bin->angle);
   w = c * child_requisition.width + s * child_requisition.height;
   h = s * child_requisition.width + c * child_requisition.height;
 
-  requisition->width = GTK_CONTAINER (widget)->border_width * 2 + w;
-  requisition->height = GTK_CONTAINER (widget)->border_width * 2 + h;
+  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
+  requisition->width = border_width * 2 + w;
+  requisition->height = border_width * 2 + h;
 }
 
 static void
@@ -397,31 +409,33 @@ gtk_rotated_bin_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
 {
   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
-  gint border_width;
+  guint border_width;
   gint w, h;
   gdouble s, c;
 
-  widget->allocation = *allocation;
+  gtk_widget_set_allocation (widget, allocation);
 
-  border_width = GTK_CONTAINER (widget)->border_width;
+  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;
 
-      sincos (bin->angle, &s, &c);
+      s = sin (bin->angle);
+      c = cos (bin->angle);
 
-      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;
@@ -433,7 +447,7 @@ gtk_rotated_bin_size_allocate (GtkWidget     *widget,
         child_allocation.width = MIN ((w - s * child_allocation.height) / c,
                                       (h - c * child_allocation.height) / s);
 
-      if (GTK_WIDGET_REALIZED (widget))
+      if (gtk_widget_get_realized (widget))
         gdk_window_move_resize (bin->offscreen_window,
                                 child_allocation.x,
                                 child_allocation.y,
@@ -449,69 +463,68 @@ static gboolean
 gtk_rotated_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_rotated_bin_expose (GtkWidget      *widget,
-                        GdkEventExpose *event)
+gtk_rotated_bin_draw (GtkWidget *widget,
+                      cairo_t   *cr)
 {
   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
-  gint width, height;
+  GdkWindow *window;
   gdouble s, c;
   gdouble w, h;
 
-  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;
-
-          if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
-            {
-              pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
-              child_area = bin->child->allocation;
-
-              cr = gdk_cairo_create (widget->window);
-
-              /* transform */
-              sincos (bin->angle, &s, &c);
-              w = c * child_area.width + s * child_area.height;
-              h = s * child_area.width + c * child_area.height;
-
-              cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
-              cairo_translate (cr, child_area.width / 2, child_area.height / 2);
-              cairo_rotate (cr, bin->angle);
-              cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
-
-              /* clip */
-              gdk_drawable_get_size (pixmap, &width, &height);
-              cairo_rectangle (cr, 0, 0, width, height);
-              cairo_clip (cr);
-              /* paint */
-              gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
-              cairo_paint (cr);
-
-              cairo_destroy (cr);
-            }
-        }
-      else if (event->window == bin->offscreen_window)
+      cairo_surface_t *surface;
+      GtkAllocation child_area;
+
+      if (bin->child && gtk_widget_get_visible (bin->child))
         {
-          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);
+          surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
+          gtk_widget_get_allocation (bin->child, &child_area);
+
+          /* transform */
+          s = sin (bin->angle);
+          c = cos (bin->angle);
+          w = c * child_area.width + s * child_area.height;
+          h = s * child_area.width + c * child_area.height;
+
+          cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
+          cairo_translate (cr, child_area.width / 2, child_area.height / 2);
+          cairo_rotate (cr, bin->angle);
+          cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
+
+          /* clip */
+          cairo_rectangle (cr,
+                           0, 0,
+                           gdk_window_get_width (bin->offscreen_window),
+                           gdk_window_get_height (bin->offscreen_window));
+          cairo_clip (cr);
+          /* paint */
+          cairo_set_source_surface (cr, surface, 0, 0);
+          cairo_paint (cr);
         }
     }
+  if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
+    {
+      gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
+                          GTK_STATE_NORMAL, GTK_SHADOW_NONE,
+                          widget, "blah",
+                          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;
 }
@@ -547,8 +560,9 @@ do_offscreen_window (GtkWidget *do_widget)
       gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 
-      vbox = gtk_vbox_new (0, FALSE);
-      scale = gtk_hscale_new_with_range (0, G_PI/2, 0.01);
+      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0, FALSE);
+      scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
+                                        0, G_PI/2, 0.01);
       gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
 
       button = gtk_button_new_with_label ("A Button");
@@ -562,7 +576,7 @@ do_offscreen_window (GtkWidget *do_widget)
       gtk_container_add (GTK_CONTAINER (bin), button);
     }
 
-  if (!GTK_WIDGET_VISIBLE (window))
+  if (!gtk_widget_get_visible (window))
     gtk_widget_show_all (window);
   else
     {