]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkpaned.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkpaned.c
index da770d47543554253fd50fb9de8b0593b99d03e2..7c5ec381f3f0ab9eb0633692b53f4ce2009445dc 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
 #include "gtkorientable.h"
 #include "gtkwindow.h"
 #include "gtktypebuiltins.h"
+#include "gtkorientableprivate.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
-
+#include "a11y/gtkpanedaccessible.h"
 
 /**
  * SECTION:gtkpaned
- * @Short_description: Base class for widgets with two adjustable panes
+ * @Short_description: A widget with two adjustable panes
  * @Title: GtkPaned
  *
- * #GtkPaned is the base class for widgets with two panes, arranged either
- * horizontally (#GtkHPaned) or vertically (#GtkVPaned). Child widgets are
+ * #GtkPaned has two panes, arranged either
+ * horizontally or vertically. The division between
+ * the two panes is adjustable by the user by dragging
+ * a handle.
+ *
+ * Child widgets are
  * added to the panes of the widget with gtk_paned_pack1() and
  * gtk_paned_pack2(). The division between the two children is set by default
  * from the size requests of the children, but it can be adjusted by the
  * </example>
  */
 
+enum {
+  CHILD1,
+  CHILD2
+};
+
 struct _GtkPanedPrivate
 {
   GtkPaned       *first_paned;
@@ -644,6 +652,7 @@ gtk_paned_class_init (GtkPanedClass *class)
   add_move_binding (binding_set, GDK_KEY_KP_End, 0, GTK_SCROLL_END);
 
   g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
+  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PANED_ACCESSIBLE);
 }
 
 static GType
@@ -680,7 +689,6 @@ gtk_paned_init (GtkPaned *paned)
   priv->child1 = NULL;
   priv->child2 = NULL;
   priv->handle = NULL;
-  priv->cursor_type = GDK_CROSS;
 
   priv->handle_pos.width = 5;
   priv->handle_pos.height = 5;
@@ -713,6 +721,7 @@ gtk_paned_set_property (GObject        *object,
     {
     case PROP_ORIENTATION:
       priv->orientation = g_value_get_enum (value);
+      _gtk_orientable_set_style_classes (GTK_ORIENTABLE (paned));
 
       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
@@ -895,7 +904,10 @@ gtk_paned_get_preferred_size (GtkWidget      *widget,
   if (priv->child1 && gtk_widget_get_visible (priv->child1))
     {
       get_preferred_size_for_size (priv->child1, orientation, size, &child_min, &child_nat);
-      *minimum = child_min;
+      if (priv->child1_shrink && priv->orientation == orientation)
+        *minimum = 0;
+      else
+        *minimum = child_min;
       *natural = child_nat;
     }
 
@@ -905,7 +917,8 @@ gtk_paned_get_preferred_size (GtkWidget      *widget,
 
       if (priv->orientation == orientation)
         {
-          *minimum += child_min;
+          if (!priv->child2_shrink)
+            *minimum += child_min;
           *natural += child_nat;
         }
       else
@@ -978,20 +991,49 @@ flip_child (GtkWidget     *widget,
   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
 }
 
+static void
+gtk_paned_set_child_visible (GtkPaned  *paned,
+                             guint      id,
+                             gboolean   visible)
+{
+  GtkPanedPrivate *priv = paned->priv;
+  GtkWidget *child;
+
+  child = id == CHILD1 ? priv->child1 : priv->child2;
+
+  if (child == NULL)
+    return;
+
+  gtk_widget_set_child_visible (child, visible);
+
+  if (gtk_widget_get_mapped (GTK_WIDGET (paned)))
+    {
+      GdkWindow *window = id == CHILD1 ? priv->child1_window : priv->child2_window;
+
+      if (visible != gdk_window_is_visible (window))
+        {
+          if (visible)
+            gdk_window_show (window);
+          else
+            gdk_window_hide (window);
+        }
+    }
+}
+
 static void
 gtk_paned_child_allocate (GtkWidget           *child,
                           GdkWindow           *child_window, /* can be NULL */
-                          const GtkAllocation *allocation)
+                          const GtkAllocation *window_allocation,
+                          GtkAllocation       *child_allocation)
 {
-  GtkAllocation child_allocation = { 0, 0, allocation->width, allocation->height };
-
   if (child_window)
     gdk_window_move_resize (child_window,
-                            allocation->x, allocation->y,
-                            allocation->width, allocation->height);
+                            window_allocation->x, window_allocation->y,
+                            window_allocation->width, window_allocation->height);
 
-  gtk_widget_size_allocate (child, &child_allocation);
+  gtk_widget_size_allocate (child, child_allocation);
 }
+
 static void
 gtk_paned_size_allocate (GtkWidget     *widget,
                          GtkAllocation *allocation)
@@ -1004,8 +1046,8 @@ gtk_paned_size_allocate (GtkWidget     *widget,
   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
       priv->child2 && gtk_widget_get_visible (priv->child2))
     {
-      GtkAllocation child1_allocation;
-      GtkAllocation child2_allocation;
+      GtkAllocation child1_allocation, window1_allocation;
+      GtkAllocation child2_allocation, window2_allocation;
       GtkAllocation priv_child1_allocation;
       GdkRectangle old_handle_pos;
       gint handle_size;
@@ -1035,20 +1077,40 @@ gtk_paned_size_allocate (GtkWidget     *widget,
           priv->handle_pos.width = handle_size;
           priv->handle_pos.height = allocation->height;
 
-          child1_allocation.height = child2_allocation.height = allocation->height;
-          child1_allocation.width = MAX (1, priv->child1_size);
-          child1_allocation.x = allocation->x;
-          child1_allocation.y = child2_allocation.y = allocation->y;
+          window1_allocation.height = window2_allocation.height = allocation->height;
+          window1_allocation.width = MAX (1, priv->child1_size);
+          window1_allocation.x = allocation->x;
+          window1_allocation.y = window2_allocation.y = allocation->y;
 
-          child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
-          child2_allocation.width = MAX (1, allocation->x + allocation->width - child2_allocation.x);
+          window2_allocation.x = window1_allocation.x + priv->child1_size + priv->handle_pos.width;
+          window2_allocation.width = MAX (1, allocation->x + allocation->width - window2_allocation.x);
 
           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
             {
-              flip_child (widget, &(child2_allocation));
-              flip_child (widget, &(child1_allocation));
+              flip_child (widget, &(window2_allocation));
+              flip_child (widget, &(window1_allocation));
               flip_child (widget, &(priv->handle_pos));
             }
+
+          child1_allocation.x = child1_allocation.y = 0;
+          child1_allocation.width = window1_allocation.width;
+          child1_allocation.height = window1_allocation.height;
+          if (child1_width > child1_allocation.width)
+            {
+              if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_LTR)
+                child1_allocation.x -= child1_width - child1_allocation.width;
+              child1_allocation.width = child1_width;
+            }
+
+          child2_allocation.x = child2_allocation.y = 0;
+          child2_allocation.width = window2_allocation.width;
+          child2_allocation.height = window2_allocation.height;
+          if (child2_width > child2_allocation.width)
+            {
+              if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
+                child2_allocation.x -= child2_width - child2_allocation.width;
+              child2_allocation.width = child2_width;
+            }
         }
       else
         {
@@ -1071,13 +1133,28 @@ gtk_paned_size_allocate (GtkWidget     *widget,
           priv->handle_pos.width = allocation->width;
           priv->handle_pos.height = handle_size;
 
-          child1_allocation.width = child2_allocation.width = allocation->width;
-          child1_allocation.height = MAX (1, priv->child1_size);
-          child1_allocation.x = child2_allocation.x = allocation->x;
-          child1_allocation.y = allocation->y;
+          window1_allocation.width = window2_allocation.width = allocation->width;
+          window1_allocation.height = MAX (1, priv->child1_size);
+          window1_allocation.x = window2_allocation.x = allocation->x;
+          window1_allocation.y = allocation->y;
+
+          window2_allocation.y = window1_allocation.y + priv->child1_size + priv->handle_pos.height;
+          window2_allocation.height = MAX (1, allocation->y + allocation->height - window2_allocation.y);
+
+          child1_allocation.x = child1_allocation.y = 0;
+          child1_allocation.width = window1_allocation.width;
+          child1_allocation.height = window1_allocation.height;
+          if (child1_height > child1_allocation.height)
+            {
+              child1_allocation.y -= child1_height - child1_allocation.height;
+              child1_allocation.height = child1_height;
+            }
 
-          child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
-          child2_allocation.height = MAX (1, allocation->y + allocation->height - child2_allocation.y);
+          child2_allocation.x = child2_allocation.y = 0;
+          child2_allocation.width = window2_allocation.width;
+          child2_allocation.height = window2_allocation.height;
+          if (child2_height > child2_allocation.height)
+            child2_allocation.height = child2_height;
         }
 
       if (gtk_widget_get_mapped (widget) &&
@@ -1127,51 +1204,70 @@ gtk_paned_size_allocate (GtkWidget     *widget,
            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
             priv_child1_allocation.height < child1_allocation.height)))
        {
-          gtk_paned_child_allocate (priv->child2, priv->child2_window, &child2_allocation);
-          gtk_paned_child_allocate (priv->child1, priv->child1_window, &child1_allocation);
+          gtk_paned_child_allocate (priv->child2,
+                                    priv->child2_window,
+                                    &window2_allocation,
+                                    &child2_allocation);
+          gtk_paned_child_allocate (priv->child1,
+                                    priv->child1_window,
+                                    &window1_allocation,
+                                    &child1_allocation);
        }
       else
        {
-          gtk_paned_child_allocate (priv->child1, priv->child1_window, &child1_allocation);
-          gtk_paned_child_allocate (priv->child2, priv->child2_window, &child2_allocation);
+          gtk_paned_child_allocate (priv->child1,
+                                    priv->child1_window,
+                                    &window1_allocation,
+                                    &child1_allocation);
+          gtk_paned_child_allocate (priv->child2,
+                                    priv->child2_window,
+                                    &window2_allocation,
+                                    &child2_allocation);
        }
-      if (priv->child1_window)
-        gdk_window_show (priv->child1_window);
-      if (priv->child2_window)
-        gdk_window_show (priv->child2_window);
     }
   else
     {
-      GtkAllocation child_allocation;
+      GtkAllocation window_allocation, child_allocation;
 
       if (gtk_widget_get_realized (widget))
        gdk_window_hide (priv->handle);
 
-      if (priv->child1)
-       gtk_widget_set_child_visible (priv->child1, TRUE);
-      if (priv->child2)
-       gtk_widget_set_child_visible (priv->child2, TRUE);
-
-      child_allocation.x = allocation->x;
-      child_allocation.y = allocation->y;
+      window_allocation.x = allocation->x;
+      window_allocation.y = allocation->y;
+      window_allocation.width = allocation->width;
+      window_allocation.height = allocation->height;
+      child_allocation.x = child_allocation.y = 0;
       child_allocation.width = allocation->width;
       child_allocation.height = allocation->height;
 
       if (priv->child1 && gtk_widget_get_visible (priv->child1))
         {
-          gtk_paned_child_allocate (priv->child1, priv->child1_window, &child_allocation);
-          if (priv->child1_window)
-            gdk_window_show (priv->child1_window);
-          if (priv->child2_window)
-            gdk_window_hide (priv->child2_window);
+          gtk_paned_set_child_visible (paned, 0, TRUE);
+          if (priv->child2)
+            gtk_paned_set_child_visible (paned, 1, FALSE);
+
+          gtk_paned_child_allocate (priv->child1,
+                                    priv->child1_window,
+                                    &window_allocation,
+                                    &child_allocation);
         }
       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
         {
-          gtk_paned_child_allocate (priv->child2, priv->child2_window, &child_allocation);
-          if (priv->child2_window)
-            gdk_window_show (priv->child2_window);
-          if (priv->child1_window)
-            gdk_window_hide (priv->child1_window);
+          gtk_paned_set_child_visible (paned, 1, TRUE);
+          if (priv->child1)
+            gtk_paned_set_child_visible (paned, 0, FALSE);
+
+          gtk_paned_child_allocate (priv->child2,
+                                    priv->child2_window,
+                                    &window_allocation,
+                                    &child_allocation);
+        }
+      else
+        {
+          if (priv->child1)
+            gtk_paned_set_child_visible (paned, 0, FALSE);
+          if (priv->child2)
+            gtk_paned_set_child_visible (paned, 1, FALSE);
         }
     }
 }
@@ -1198,12 +1294,14 @@ gtk_paned_create_child_window (GtkPaned  *paned,
 
       gtk_widget_get_allocation (widget, &allocation);
       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
-          child == priv->child2)
+          child == priv->child2 && priv->child1 &&
+          gtk_widget_get_visible (priv->child1))
         attributes.x = priv->handle_pos.x + handle_size;
       else
         attributes.x = allocation.x;
       if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
-          child == priv->child2)
+          child == priv->child2 && priv->child1 &&
+          gtk_widget_get_visible (priv->child1))
         attributes.y = priv->handle_pos.y + handle_size;
       else
         attributes.y = allocation.y;
@@ -1222,7 +1320,7 @@ gtk_paned_create_child_window (GtkPaned  *paned,
 
   window = gdk_window_new (gtk_widget_get_window (widget),
                            &attributes, attributes_mask);
-  gdk_window_set_user_data (window, paned);
+  gtk_widget_register_window (widget, window);
   gtk_style_context_set_background (gtk_widget_get_style_context (widget), window);
 
   if (child)
@@ -1257,8 +1355,7 @@ gtk_paned_realize (GtkWidget *widget)
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_ENTER_NOTIFY_MASK |
                            GDK_LEAVE_NOTIFY_MASK |
-                           GDK_POINTER_MOTION_MASK |
-                           GDK_POINTER_MOTION_HINT_MASK);
+                           GDK_POINTER_MOTION_MASK);
   attributes_mask = GDK_WA_X | GDK_WA_Y;
   if (gtk_widget_is_sensitive (widget))
     {
@@ -1269,7 +1366,7 @@ gtk_paned_realize (GtkWidget *widget)
 
   priv->handle = gdk_window_new (window,
                                  &attributes, attributes_mask);
-  gdk_window_set_user_data (priv->handle, paned);
+  gtk_widget_register_window (widget, priv->handle);
   if (attributes_mask & GDK_WA_CURSOR)
     g_object_unref (attributes.cursor);
 
@@ -1285,19 +1382,19 @@ gtk_paned_unrealize (GtkWidget *widget)
 
   if (priv->child2)
     gtk_widget_set_parent_window (priv->child2, NULL);
-  gdk_window_set_user_data (priv->child2_window, NULL);
+  gtk_widget_unregister_window (widget, priv->child2_window);
   gdk_window_destroy (priv->child2_window);
   priv->child2_window = NULL;
 
   if (priv->child1)
     gtk_widget_set_parent_window (priv->child1, NULL);
-  gdk_window_set_user_data (priv->child1_window, NULL);
+  gtk_widget_unregister_window (widget, priv->child1_window);
   gdk_window_destroy (priv->child1_window);
   priv->child1_window = NULL;
 
   if (priv->handle)
     {
-      gdk_window_set_user_data (priv->handle, NULL);
+      gtk_widget_unregister_window (widget, priv->handle);
       gdk_window_destroy (priv->handle);
       priv->handle = NULL;
     }
@@ -1320,9 +1417,9 @@ gtk_paned_map (GtkWidget *widget)
       priv->child2 && gtk_widget_get_visible (priv->child2))
     gdk_window_show (priv->handle);
 
-  if (priv->child1 && gtk_widget_get_visible (priv->child1))
+  if (priv->child1 && gtk_widget_get_visible (priv->child1) && gtk_widget_get_child_visible (priv->child1))
     gdk_window_show (priv->child1_window);
-  if (priv->child2 && gtk_widget_get_visible (priv->child2))
+  if (priv->child2 && gtk_widget_get_visible (priv->child2) && gtk_widget_get_child_visible (priv->child2))
     gdk_window_show (priv->child2_window);
 
   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
@@ -1336,8 +1433,10 @@ gtk_paned_unmap (GtkWidget *widget)
 
   gdk_window_hide (priv->handle);
   
-  gdk_window_hide (priv->child1_window);
-  gdk_window_hide (priv->child2_window);
+  if (gdk_window_is_visible (priv->child1_window))
+    gdk_window_hide (priv->child1_window);
+  if (gdk_window_is_visible (priv->child2_window))
+    gdk_window_hide (priv->child2_window);
 
   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
 }
@@ -1423,7 +1522,10 @@ is_rtl (GtkPaned *paned)
 }
 
 static void
-update_drag (GtkPaned *paned)
+update_drag (GtkPaned         *paned,
+             /* relative to priv->handle */
+             int               xpos,
+             int               ypos)
 {
   GtkPanedPrivate *priv = paned->priv;
   GtkAllocation allocation;
@@ -1431,11 +1533,18 @@ update_drag (GtkPaned *paned)
   gint pos;
   gint handle_size;
   gint size;
+  gint x, y;
 
+  gdk_window_get_position (priv->handle, &x, &y);
+  gtk_widget_get_allocation (widget, &allocation);
   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
-    gtk_widget_get_pointer (widget, &pos, NULL);
+    {
+      pos = xpos + x - allocation.x;
+    }
   else
-    gtk_widget_get_pointer (widget, NULL, &pos);
+    {
+      pos = ypos + y - allocation.y;
+    }
 
   pos -= priv->drag_pos;
 
@@ -1445,7 +1554,6 @@ update_drag (GtkPaned *paned)
                            "handle-size", &handle_size,
                            NULL);
 
-      gtk_widget_get_allocation (widget, &allocation);
       size = allocation.width - pos - handle_size;
     }
   else
@@ -1467,7 +1575,7 @@ gtk_paned_enter (GtkWidget        *widget,
   GtkPanedPrivate *priv = paned->priv;
 
   if (priv->in_drag)
-    update_drag (paned);
+    update_drag (paned, event->x, event->y);
   else
     {
       priv->handle_prelit = TRUE;
@@ -1489,7 +1597,7 @@ gtk_paned_leave (GtkWidget        *widget,
   GtkPanedPrivate *priv = paned->priv;
 
   if (priv->in_drag)
-    update_drag (paned);
+    update_drag (paned, event->x, event->y);
   else
     {
       priv->handle_prelit = FALSE;
@@ -1529,15 +1637,14 @@ gtk_paned_button_press (GtkWidget      *widget,
   GtkPanedPrivate *priv = paned->priv;
 
   if (!priv->in_drag &&
-      (event->window == priv->handle) && (event->button == 1))
+      (event->window == priv->handle) && (event->button == GDK_BUTTON_PRIMARY))
     {
       /* We need a server grab here, not gtk_grab_add(), since
        * we don't want to pass events on to the widget's children */
       if (gdk_device_grab (event->device,
                            priv->handle,
                            GDK_OWNERSHIP_WINDOW, FALSE,
-                           GDK_POINTER_MOTION_HINT_MASK
-                           | GDK_BUTTON1_MOTION_MASK
+                           GDK_BUTTON1_MOTION_MASK
                            | GDK_BUTTON_RELEASE_MASK
                            | GDK_ENTER_NOTIFY_MASK
                            | GDK_LEAVE_NOTIFY_MASK,
@@ -1632,7 +1739,7 @@ gtk_paned_button_release (GtkWidget      *widget,
   GtkPaned *paned = GTK_PANED (widget);
   GtkPanedPrivate *priv = paned->priv;
 
-  if (priv->in_drag && (event->button == 1))
+  if (priv->in_drag && (event->button == GDK_BUTTON_PRIMARY))
     {
       stop_drag (paned);
 
@@ -1651,7 +1758,7 @@ gtk_paned_motion (GtkWidget      *widget,
 
   if (priv->in_drag)
     {
-      update_drag (paned);
+      update_drag (paned, event->x, event->y);
       return TRUE;
     }
   
@@ -1807,25 +1914,25 @@ gtk_paned_remove (GtkContainer *container,
 
   if (priv->child1 == widget)
     {
+      if (priv->child1_window && gdk_window_is_visible (priv->child1_window))
+        gdk_window_hide (priv->child1_window);
+
       gtk_widget_unparent (widget);
 
       priv->child1 = NULL;
 
-      if (priv->child1_window)
-        gdk_window_hide (priv->child1_window);
-
       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
        gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
     }
   else if (priv->child2 == widget)
     {
+      if (priv->child2_window && gdk_window_is_visible (priv->child2_window))
+        gdk_window_hide (priv->child2_window);
+
       gtk_widget_unparent (widget);
 
       priv->child2 = NULL;
 
-      if (priv->child2_window)
-        gdk_window_hide (priv->child2_window);
-
       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
        gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
     }
@@ -2012,10 +2119,10 @@ gtk_paned_calc_position (GtkPaned *paned,
                              priv->max_position);
 
   if (priv->child1)
-    gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
+    gtk_paned_set_child_visible (paned, 0, priv->child1_size != 0);
   
   if (priv->child2)
-    gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation); 
+    gtk_paned_set_child_visible (paned, 1, priv->child1_size != allocation); 
 
   g_object_freeze_notify (G_OBJECT (paned));
   if (priv->child1_size != old_position)