[Usability]Re: Padding API



Attached is a patch against gtk+ HEAD to add padding to GtkAlignment.
I have not tested it.  Instead of using the deprecated GtkSideType,
I've used GtkPositionType.

I'm pretty sure this breaks the ABI. I'll look fixing that as Havoc
recommended (using object data) if no one beats me to it.


Cheers,
Greg Merchan
Index: gtk/gtkalignment.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkalignment.c,v
retrieving revision 1.26
diff -u -r1.26 gtkalignment.c
--- gtk/gtkalignment.c	9 Oct 2002 22:25:17 -0000	1.26
+++ gtk/gtkalignment.c	16 Dec 2002 21:29:45 -0000
@@ -34,7 +34,9 @@
   PROP_YALIGN,
   PROP_XSCALE,
   PROP_YSCALE,
-  
+  PROP_PAD_POS,
+  PROP_PADDING,
+
   PROP_LAST
 };
 
@@ -133,6 +135,23 @@
                                                       1.0,
                                                       1.0,
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
+  g_object_class_install_property (gobject_class,
+                                   PROP_PAD_POS,
+                                   g_param_spec_enum("pad_pos",
+						     _("Pad Position"),
+						     _("Which side of the alignment to pad."),
+                                                      GTK_TYPE_POSITION,,
+                                                      GTK_POS_TOP,
+                                                      G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class,
+				   PROP_PADDING,
+				   g_param_spec_uint ("padding",
+						      _("Padding"),
+						      _("The amount of padding to the side of the alignment."),
+						      0,
+						      G_MAXINT,
+						      0,
+						      G_PARAM_READWRITE));
 }
 
 static void
@@ -145,6 +164,8 @@
   alignment->yalign = 0.5;
   alignment->xscale = 1.0;
   alignment->yscale = 1.0;
+  alignment->pad_pos = GTK_POS_TOP;
+  alignment->padding = 0;
 }
 
 GtkWidget*
@@ -165,6 +186,21 @@
   return GTK_WIDGET (alignment);
 }
 
+GtkWidget*
+gtk_alignment_new_padding (gfloat      xalign,
+			   gfloat      yalign,
+			   gfloat      xscale,
+			   gfloat      yscale,
+			   GtkSideType side,
+			   guint       padding)
+{
+	GtkWidget *alignment;
+	alignment = g_alignment_new (xalign, yalign, xscale, yscale);
+	gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
+				   pad_pos, padding);
+	return alignment;
+}
+
 static void
 gtk_alignment_set_property (GObject         *object,
 			    guint            prop_id,
@@ -205,6 +241,16 @@
 			 alignment->xscale,
 			 g_value_get_float (value));
       break;
+    case PROP_PAD_POS:
+      gtk_alignment_set_padding (alignment,
+				 g_value_get_enum (value),
+				 alignment->padding);
+      break;
+    case PROP_PADDING:
+      gtk_alignment_set_padding (alignment,
+				 alignment->pad_pos,
+				 g_value_get_uint (valu));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -235,6 +281,12 @@
     case PROP_YSCALE:
       g_value_set_float(value, alignment->yscale);
       break;
+    case PROP_PAD_POS:
+      g_value_set_enum (value, alignment->pad_pos);
+      break;
+    case PROP_PADDING:
+      g_value_set_uint (value, alignment->padding);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -289,6 +341,34 @@
     }
 }
 
+void
+gtk_alignment_set_padding (GtkAlignment    *alignment,
+			   GtkPositionType  pad_pos,
+			   guint            padding)
+{
+  g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
+
+  if (   (alignment->pad_pos != pad_pos)
+      || (alignment->padding != padding))
+    {
+      g_object_freeze_notify (G_OBJECT (alignment));
+      if (alignment->pad_pos != pad_pos)
+        {
+           alignment->pad_pos = pad_pos;
+           g_object_notify (G_OBJECT (alignment), "pad_pos");
+        }
+      if (alignment->padding != padding)
+        {
+           alignment->padding = padding;
+           g_object_notify (G_OBJECT (alignment), "padding");
+        }
+      g_object_thaw_notify (G_OBJECT (alignment));
+
+      if (GTK_BIN (alignment)->child)
+        gtk_widget_queue_resize (GTK_BIN (alignment)->child);
+      gtk_widget_queue_draw (GTK_WIDGET (alignment));
+    }
+}
 
 static void
 gtk_alignment_size_request (GtkWidget      *widget,
@@ -303,6 +383,17 @@
   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
 
+  if (   (alignment->pad_pos == GTK_POS_LEFT)
+      || (alignment->pad_pos == GTK_POS_RIGHT))
+    {
+	    requisition->width += alignment->padding;
+    }
+  else if (   (alignment->pad_pos == GTK_POS_TOP)
+	   || (alignment->pad_pos == GTK_POS_BOTTOM))
+    {
+	    requisition->height += alignment->padding;
+    }
+
   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
     {
       GtkRequisition child_requisition;
@@ -332,11 +423,24 @@
   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
     {
       gtk_widget_get_child_requisition (bin->child, &child_requisition);
+
       
       x = GTK_CONTAINER (alignment)->border_width;
       y = GTK_CONTAINER (alignment)->border_width;
-      width = MAX (allocation->width - 2 * x, 0);
-      height = MAX (allocation->height - 2 * y, 0);
+
+      switch (alignment->pad_pos)
+        {
+	case GTK_POS_LEFT:
+	case GTK_POS_RIGHT:
+	  width = MAX (allocation->width - 2 * x - alignment->padding, 0);
+	  height = MAX (allocation->height - 2 * y, 0);
+	  break;
+	case GTK_POS_TOP:
+	case GTK_POS_BOTTOM:
+	  width = MAX (allocation->width - 2 * x, 0);
+	  height = MAX (allocation->height - 2 * y - alignment->padding, 0);
+	  break;
+	}
       
       if (width > child_requisition.width)
 	child_allocation.width = (child_requisition.width *
@@ -352,8 +456,25 @@
       else
 	child_allocation.height = height;
 
-      child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
-      child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
+      switch (alignment->pad_pos)
+        {
+	case GTK_POS_LEFT:
+	  child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x + alignment->padding;
+	  child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
+	  break;
+	case GTK_POS_RIGHT:
+	  child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
+	  child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
+	  break;
+	case GTK_POS_TOP:
+	  child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
+	  child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y + alignment->padding;
+	  break;
+	case GTK_POS_BOTTOM:
+	  child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
+	  child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
+	  break;
+	}
 
       gtk_widget_size_allocate (bin->child, &child_allocation);
     }
Index: gtk/gtkalignment.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkalignment.h,v
retrieving revision 1.10
diff -u -r1.10 gtkalignment.h
--- gtk/gtkalignment.h	9 Oct 2002 22:25:17 -0000	1.10
+++ gtk/gtkalignment.h	16 Dec 2002 21:29:45 -0000
@@ -56,6 +56,9 @@
   gfloat yalign;
   gfloat xscale;
   gfloat yscale;
+
+  GtkPositionType pad_pos;
+  guint padding;
 };
 
 struct _GtkAlignmentClass
@@ -64,16 +67,25 @@
 };
 
 
-GType      gtk_alignment_get_type   (void) G_GNUC_CONST;
-GtkWidget* gtk_alignment_new        (gfloat             xalign,
-				     gfloat             yalign,
-				     gfloat             xscale,
-				     gfloat             yscale);
-void       gtk_alignment_set        (GtkAlignment      *alignment,
-				     gfloat             xalign,
-				     gfloat             yalign,
-				     gfloat             xscale,
-				     gfloat             yscale);
+GType      gtk_alignment_get_type    (void) G_GNUC_CONST;
+GtkWidget* gtk_alignment_new         (gfloat             xalign,
+				      gfloat             yalign,
+				      gfloat             xscale,
+				      gfloat             yscale);
+GtkWidget* gtk_alignment_new_padding (gfloat             xalign,
+				      gfloat             yalign,
+				      gfloat             xscale,
+				      gfloat             yscale,
+				      GtkPositionType    pad_pos,
+				      guint              padding);
+void       gtk_alignment_set         (GtkAlignment      *alignment,
+				      gfloat             xalign,
+				      gfloat             yalign,
+				      gfloat             xscale,
+				      gfloat             yscale);
+void       gtk_alignment_set_padding (GtkAlignment      *alignment,
+				      GtkPositionType    pad_pos,
+				      guint              padding);
 
 
 #ifdef __cplusplus


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