patch: gtk_paned_set_proportion



  This patch adds API functions to GtkPaned to enable specifying a
percentage of space to be allocated to each child widget. This way, when
the widget is resized the child widgets both change their sizes
proportionally. This is change is 100% backwards compatible because the
old behaviour is the default. Only after calling
gtk_paned_set_proportion will the new code be activated.
  I don't have CVS write permissions, so please someone commit this, in
case the patch is accepted.


-- 
Gustavo Joćo Alves Marques Carneiro <ee96090 fe up pt>
 __                          _                        
/__      _ _|_  _.     _    /   _. ._ ._   _  o ._ _  
\_| |_| _>  |_ (_| \/ (_)   \_ (_| |  | | (/_ | | (_) 
Index: gtkhpaned.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkhpaned.c,v
retrieving revision 1.31
diff -u -r1.31 gtkhpaned.c
--- gtkhpaned.c	2001/07/18 23:39:22	1.31
+++ gtkhpaned.c	2001/08/11 18:22:27
@@ -360,6 +360,8 @@
       gtk_hpaned_xor_line (paned);
       paned->in_drag = FALSE;
       paned->position_set = TRUE;
+      if (paned->proportion >= 0)
+	  paned->proportion = ((float)paned->child1_size) / paned->last_allocation;
       gdk_pointer_ungrab (event->time);
       gtk_widget_queue_resize (GTK_WIDGET (paned));
       g_object_freeze_notify (object);
Index: gtkpaned.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkpaned.c,v
retrieving revision 1.37
diff -u -r1.37 gtkpaned.c
--- gtkpaned.c	2001/07/19 14:57:13	1.37
+++ gtkpaned.c	2001/08/11 18:22:28
@@ -30,7 +30,8 @@
 enum {
   PROP_0,
   PROP_POSITION,
-  PROP_POSITION_SET
+  PROP_POSITION_SET,
+  PROP_PROPORTION
 };
 
 static void    gtk_paned_class_init   (GtkPanedClass  *klass);
@@ -126,6 +127,15 @@
 							 _("TRUE if the Position property should be used"),
 							 FALSE,
 							 G_PARAM_READABLE | G_PARAM_WRITABLE));
+
+  g_object_class_install_property (object_class,
+				   PROP_PROPORTION,
+				   g_param_spec_float ("proportion",
+							 _("Propotion"),
+							 _("The percentage of space allocated to the first child. If negative, proportion based allocation is disabled."),
+							 -G_MAXFLOAT, 1.0, -1.0,
+							 G_PARAM_READABLE | G_PARAM_WRITABLE));
+
 				   
   gtk_widget_class_install_style_property (widget_class,
 					   g_param_spec_int ("handle_size",
@@ -165,6 +175,9 @@
   
   paned->handle_xpos = -1;
   paned->handle_ypos = -1;
+
+  /* proportion based size allocation is disabled by default. */
+  paned->proportion = -1;
 }
 
 static void
@@ -184,6 +197,9 @@
       paned->position_set = g_value_get_boolean (value);
       gtk_widget_queue_resize (GTK_WIDGET (paned));
       break;
+    case PROP_PROPORTION:
+      gtk_paned_set_proportion (paned, g_value_get_float (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -206,6 +222,9 @@
     case PROP_POSITION_SET:
       g_value_set_boolean (value, paned->position_set);
       break;
+    case PROP_PROPORTION:
+      g_value_set_float (value, gtk_paned_get_proportion (paned));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -496,6 +515,20 @@
   gtk_widget_queue_resize (GTK_WIDGET (paned));
 }
 
+
+static void
+gtk_paned_compute_position_prop(GtkPaned *paned,
+				gint      allocation,
+				gint      child1_req,
+				gint      child2_req)
+{
+  paned->child1_size = paned->proportion * allocation;
+  paned->last_allocation = allocation;
+  paned->min_position = 0;
+  paned->max_position = allocation;
+}
+
+
 void
 gtk_paned_compute_position(GtkPaned *paned,
 			   gint      allocation,
@@ -506,8 +539,12 @@
   
   g_return_if_fail (GTK_IS_PANED (paned));
 
-  old_position = paned->child1_size;
+  if (paned->proportion >= 0) {
+    gtk_paned_compute_position_prop (paned, allocation, child1_req, child2_req);
+    return;
+  }
 
+  old_position = paned->child1_size;
   paned->min_position = paned->child1_shrink ? 0 : child1_req;
 
   paned->max_position = allocation;
@@ -548,3 +585,44 @@
 
   paned->last_allocation = allocation;
 }
+
+
+/**
+ * gtk_paned_set_proportion:
+ * @paned:  a #GtkPaned widget
+ * @proportion: a float in the range 0..1 sets the percentage; a
+ * negative value will disable proportion-based allocation and return
+ * to the standard behaviour.
+ * 
+ * Set the percentage of space that is to be allocated to the first
+ * child. When a resize occurs, the same proportion of space will be
+ * maintained.
+ **/
+void gtk_paned_set_proportion (GtkPaned *paned,
+			       gfloat proportion)
+{
+  g_return_if_fail (GTK_IS_PANED (paned));
+  g_return_if_fail (proportion <= 1.0);
+
+  paned->proportion = proportion;
+  g_object_notify (G_OBJECT (paned), "proportion");
+  gtk_widget_queue_resize (GTK_WIDGET (paned));
+}
+
+
+/**
+ * gtk_paned_get_proportion:
+ * @paned:  a #GtkPaned widget
+ * 
+ * Get the proportion value.
+ * 
+ * Return value: a percentage value or a negative value if
+ * proportion-based allocation is not activated on this widget.
+ **/
+gfloat
+gtk_paned_get_proportion (GtkPaned  *paned)
+{
+  g_return_val_if_fail (GTK_IS_PANED (paned), -1);
+  return paned->proportion;
+}
+
Index: gtkpaned.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkpaned.h,v
retrieving revision 1.16
diff -u -r1.16 gtkpaned.h
--- gtkpaned.h	2001/04/02 15:51:26	1.16
+++ gtkpaned.h	2001/08/11 18:22:28
@@ -61,6 +61,8 @@
   guint16 handle_width;
   guint16 handle_height;
 
+  gfloat proportion;
+
   gint child1_size;
   gint last_allocation;
   gint min_position;
@@ -99,6 +101,9 @@
 gint    gtk_paned_get_position    (GtkPaned  *paned);
 void    gtk_paned_set_position    (GtkPaned  *paned,
 				   gint       position);
+void    gtk_paned_set_proportion  (GtkPaned  *paned,
+				   gfloat     proportion);
+gfloat  gtk_paned_get_proportion  (GtkPaned  *paned);
 
 /* Internal function */
 void    gtk_paned_compute_position (GtkPaned  *paned,
Index: gtkvpaned.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkvpaned.c,v
retrieving revision 1.33
diff -u -r1.33 gtkvpaned.c
--- gtkvpaned.c	2001/07/18 23:39:25	1.33
+++ gtkvpaned.c	2001/08/11 18:22:29
@@ -362,6 +362,8 @@
       gtk_vpaned_xor_line (paned);
       paned->in_drag = FALSE;
       paned->position_set = TRUE;
+      if (paned->proportion >= 0)
+	  paned->proportion = ((float)paned->child1_size) / paned->last_allocation;
       gdk_pointer_ungrab (event->time);
       gtk_widget_queue_resize (GTK_WIDGET (paned));
       g_object_freeze_notify (object);
Index: gtk-sections.txt
===================================================================
RCS file: /cvs/gnome/gtk+/docs/reference/gtk/gtk-sections.txt,v
retrieving revision 1.25
diff -u -r1.25 gtk-sections.txt
--- gtk-sections.txt	2001/06/25 01:51:56	1.25
+++ gtk-sections.txt	2001/08/11 18:25:10
@@ -1571,6 +1571,8 @@
 gtk_paned_set_gutter_size
 gtk_paned_set_position
 gtk_paned_get_position
+gtk_paned_set_proportion
+gtk_paned_get_proportion
 <SUBSECTION Standard>
 GTK_PANED
 GTK_IS_PANED


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]