]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkpaned.c
Adapt cast macros to standard.
[~andy/gtk] / gtk / gtkpaned.c
index d679982b80b667944acee0ba6c9a1721d1c1ab8d..3a6929fc0b56f62fafca29cbbc42a1d7a6dbf9f1 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 #include "gtkpaned.h"
 
+enum {
+  ARG_0,
+  ARG_HANDLE_SIZE,
+  ARG_GUTTER_SIZE
+};
 
 static void gtk_paned_class_init (GtkPanedClass    *klass);
 static void gtk_paned_init       (GtkPaned         *paned);
+static void gtk_paned_set_arg   (GtkObject        *object,
+                                 GtkArg           *arg,
+                                 guint             arg_id);
+static void gtk_paned_get_arg   (GtkObject        *object,
+                                 GtkArg           *arg,
+                                 guint             arg_id);
 static void gtk_paned_realize    (GtkWidget *widget);
 static void gtk_paned_map        (GtkWidget      *widget);
 static void gtk_paned_unmap      (GtkWidget      *widget);
@@ -79,6 +98,9 @@ gtk_paned_class_init (GtkPanedClass *class)
   
   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
   
+  object_class->set_arg = gtk_paned_set_arg;
+  object_class->get_arg = gtk_paned_get_arg;
+
   widget_class->realize = gtk_paned_realize;
   widget_class->map = gtk_paned_map;
   widget_class->unmap = gtk_paned_unmap;
@@ -89,6 +111,11 @@ gtk_paned_class_init (GtkPanedClass *class)
   container_class->remove = gtk_paned_remove;
   container_class->forall = gtk_paned_forall;
   container_class->child_type = gtk_paned_child_type;
+
+  gtk_object_add_arg_type ("GtkPaned::handle_size", GTK_TYPE_UINT,
+                          GTK_ARG_READWRITE, ARG_HANDLE_SIZE);
+  gtk_object_add_arg_type ("GtkPaned::gutter_size", GTK_TYPE_UINT,
+                          GTK_ARG_READWRITE, ARG_GUTTER_SIZE);
 }
 
 static GtkType
@@ -113,12 +140,51 @@ gtk_paned_init (GtkPaned *paned)
   paned->handle_size = 10;
   paned->gutter_size = 6;
   paned->position_set = FALSE;
+  paned->last_allocation = -1;
   paned->in_drag = FALSE;
   
   paned->handle_xpos = -1;
   paned->handle_ypos = -1;
 }
 
+static void
+gtk_paned_set_arg (GtkObject *object,
+                  GtkArg    *arg,
+                  guint      arg_id)
+{
+  GtkPaned *paned = GTK_PANED (object);
+  
+  switch (arg_id)
+    {
+    case ARG_HANDLE_SIZE:
+      gtk_paned_set_handle_size (paned, GTK_VALUE_UINT (*arg));
+      break;
+    case ARG_GUTTER_SIZE:
+      gtk_paned_set_gutter_size (paned, GTK_VALUE_UINT (*arg));
+      break;
+    }
+}
+
+static void
+gtk_paned_get_arg (GtkObject *object,
+                  GtkArg    *arg,
+                  guint      arg_id)
+{
+  GtkPaned *paned = GTK_PANED (object);
+  
+  switch (arg_id)
+    {
+    case ARG_HANDLE_SIZE:
+      GTK_VALUE_UINT (*arg) = paned->handle_size;
+      break;
+    case ARG_GUTTER_SIZE:
+      GTK_VALUE_UINT (*arg) = paned->gutter_size;
+      break;
+    default:
+      arg->type = GTK_TYPE_INVALID;
+      break;
+    }
+}
 
 static void
 gtk_paned_realize (GtkWidget *widget)
@@ -185,8 +251,6 @@ gtk_paned_map (GtkWidget *widget)
   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
   paned = GTK_PANED (widget);
   
-  gdk_window_show (widget->window);
-  
   if (paned->child1 &&
       GTK_WIDGET_VISIBLE (paned->child1) &&
       !GTK_WIDGET_MAPPED (paned->child1))
@@ -195,6 +259,8 @@ gtk_paned_map (GtkWidget *widget)
       GTK_WIDGET_VISIBLE (paned->child2) &&
       !GTK_WIDGET_MAPPED (paned->child2))
     gtk_widget_map (paned->child2);
+
+  gdk_window_show (widget->window);
 }
 
 static void
@@ -287,55 +353,75 @@ void
 gtk_paned_add1 (GtkPaned     *paned,
                GtkWidget    *widget)
 {
-  g_return_if_fail (widget != NULL);
+  gtk_paned_pack1 (paned, widget, FALSE, TRUE);
+}
+
+void
+gtk_paned_add2 (GtkPaned  *paned,
+               GtkWidget *widget)
+{
+  gtk_paned_pack2 (paned, widget, TRUE, TRUE);
+}
+
+void
+gtk_paned_pack1 (GtkPaned     *paned,
+                GtkWidget    *child,
+                gboolean      resize,
+                gboolean      shrink)
+{
+  g_return_if_fail (paned != NULL);
+  g_return_if_fail (GTK_IS_PANED (paned));
+  g_return_if_fail (GTK_IS_WIDGET (child));
   
   if (!paned->child1)
     {
-      gtk_widget_set_parent (widget, GTK_WIDGET (paned));
-      
-      if (GTK_WIDGET_VISIBLE (widget->parent))
+      paned->child1 = child;
+      paned->child1_resize = resize;
+      paned->child1_shrink = shrink;
+
+      gtk_widget_set_parent (child, GTK_WIDGET (paned));
+
+      if (GTK_WIDGET_REALIZED (child->parent))
+       gtk_widget_realize (child);
+
+      if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
        {
-         if (GTK_WIDGET_REALIZED (widget->parent) &&
-             !GTK_WIDGET_REALIZED (widget))
-           gtk_widget_realize (widget);
-         
-         if (GTK_WIDGET_MAPPED (widget->parent) &&
-             !GTK_WIDGET_MAPPED (widget))
-           gtk_widget_map (widget);
+         if (GTK_WIDGET_MAPPED (child->parent))
+           gtk_widget_map (child);
+
+         gtk_widget_queue_resize (child);
        }
-      
-      paned->child1 = widget;
-      
-      if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (paned))
-        gtk_widget_queue_resize (widget);
     }
 }
 
 void
-gtk_paned_add2 (GtkPaned  *paned,
-               GtkWidget *widget)
+gtk_paned_pack2 (GtkPaned  *paned,
+                GtkWidget *child,
+                gboolean   resize,
+                gboolean   shrink)
 {
-  g_return_if_fail (widget != NULL);
+  g_return_if_fail (paned != NULL);
+  g_return_if_fail (GTK_IS_PANED (paned));
+  g_return_if_fail (GTK_IS_WIDGET (child));
   
   if (!paned->child2)
     {
-      gtk_widget_set_parent (widget, GTK_WIDGET (paned));
+      paned->child2 = child;
+      paned->child2_resize = resize;
+      paned->child2_shrink = shrink;
       
-      if (GTK_WIDGET_VISIBLE (widget->parent))
+      gtk_widget_set_parent (child, GTK_WIDGET (paned));
+
+      if (GTK_WIDGET_REALIZED (child->parent))
+       gtk_widget_realize (child);
+
+      if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
        {
-         if (GTK_WIDGET_REALIZED (widget->parent) &&
-             !GTK_WIDGET_REALIZED (widget))
-           gtk_widget_realize (widget);
-         
-         if (GTK_WIDGET_MAPPED (widget->parent) &&
-             !GTK_WIDGET_MAPPED (widget))
-           gtk_widget_map (widget);
+         if (GTK_WIDGET_MAPPED (child->parent))
+           gtk_widget_map (child);
+
+         gtk_widget_queue_resize (child);
        }
-      
-      paned->child2 = widget;
-      
-      if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (paned))
-        gtk_widget_queue_resize (widget);
     }
 }
 
@@ -411,6 +497,30 @@ gtk_paned_forall (GtkContainer *container,
     (* callback) (paned->child2, callback_data);
 }
 
+void
+gtk_paned_set_position    (GtkPaned  *paned,
+                          gint       position)
+{
+  g_return_if_fail (paned != NULL);
+  g_return_if_fail (GTK_IS_PANED (paned));
+
+  if (position >= 0)
+    {
+      /* We don't clamp here - the assumption is that
+       * if the total allocation changes at the same time
+       * as the position, the position set is with reference
+       * to the new total size. If only the position changes,
+       * then clamping will occur in gtk_paned_compute_position()
+       */
+      paned->child1_size = position;
+      paned->position_set = TRUE;
+    }
+  else
+    paned->position_set = FALSE;
+
+  gtk_widget_queue_resize (GTK_WIDGET (paned));
+}
+
 void
 gtk_paned_set_handle_size (GtkPaned *paned,
                           guint16   size)
@@ -443,3 +553,49 @@ gtk_paned_set_gutter_size (GtkPaned *paned,
   if (GTK_WIDGET_VISIBLE (GTK_WIDGET (paned)))
     gtk_widget_queue_resize (GTK_WIDGET (paned));
 }
+
+void
+gtk_paned_compute_position (GtkPaned *paned,
+                           gint      allocation,
+                           gint      child1_req,
+                           gint      child2_req)
+{
+  g_return_if_fail (paned != NULL);
+  g_return_if_fail (GTK_IS_PANED (paned));
+
+  paned->min_position = paned->child1_shrink ? 0 : child1_req;
+
+  paned->max_position = allocation;
+  if (!paned->child2_shrink)
+    paned->max_position -= child2_req;
+
+  if (!paned->position_set)
+    {
+      if (paned->child1_resize && !paned->child2_resize)
+       paned->child1_size = allocation - child2_req;
+      else if (!paned->child1_resize && paned->child2_resize)
+       paned->child1_size = child1_req;
+      else
+       paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
+    }
+  else
+    {
+      /* If the position was set before the initial allocation.
+       * (paned->last_allocation < 0) just clamp it and leave it.
+       */
+      if (paned->last_allocation >= 0)
+       {
+         if (paned->child1_resize && !paned->child2_resize)
+           paned->child1_size += (allocation - paned->last_allocation);
+         else if (!(!paned->child1_resize && paned->child2_resize))
+           paned->child1_size = allocation * ((gdouble)paned->child1_size / (paned->last_allocation));
+       }
+    }
+
+  paned->child1_size = CLAMP (paned->child1_size,
+                             paned->min_position,
+                             paned->max_position);
+
+  paned->last_allocation = allocation;
+  
+}