]> git.draconx.ca Git - aspectbin.git/commitdiff
Add separate align properties for the body and side.
authorNick Bowler <nbowler@draconx.ca>
Sun, 15 Mar 2009 05:21:41 +0000 (01:21 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 15 Mar 2009 05:21:41 +0000 (01:21 -0400)
aspectbin.c
aspectbin.h

index c75fe3c549ae188305c91f4aa86e65e108c63177..07fca93d8e2aa36b5e98bdf20e7fbb43c5f79dea 100644 (file)
@@ -7,6 +7,11 @@ enum {
        PROP_FILL,
 };
 
+enum {
+       CHILD_PROP_0,
+       CHILD_PROP_ALIGN,
+};
+
 static void aspect_bin_size_request(GtkWidget *, GtkRequisition *);
 static void aspect_bin_size_allocate(GtkWidget *, GtkAllocation *);
 static void aspect_bin_add(GtkContainer *, GtkWidget *);
@@ -17,6 +22,10 @@ static GType aspect_bin_child_type(GtkContainer *);
 static void aspect_bin_get_property(GObject *, guint, GValue *, GParamSpec *);
 static void aspect_bin_set_property(GObject *, guint, const GValue *,
                                                           GParamSpec *);
+static void aspect_bin_set_child_property(GtkContainer *, GtkWidget *, guint,
+                                                const GValue *, GParamSpec *);
+static void aspect_bin_get_child_property(GtkContainer *, GtkWidget *, guint,
+                                                      GValue *, GParamSpec *);
 
 G_DEFINE_TYPE(AspectBin, aspect_bin, GTK_TYPE_CONTAINER)
 
@@ -24,12 +33,13 @@ static void aspect_bin_init(AspectBin *abin)
 {
        GTK_WIDGET_SET_FLAGS(abin, GTK_NO_WINDOW);
 
-       abin->body      = NULL;
-       abin->side      = NULL;
-       abin->ratio     = 1;
-       abin->align     = 0;
-       abin->constrain = FALSE;
-       abin->fill      = TRUE;
+       abin->body       = NULL;
+       abin->side       = NULL;
+       abin->ratio      = 1;
+       abin->body_align = 0.5;
+       abin->side_align = 0.5;
+       abin->constrain  = FALSE;
+       abin->fill       = TRUE;
 }
 
 static void aspect_bin_class_init(AspectBinClass *class)
@@ -46,6 +56,9 @@ static void aspect_bin_class_init(AspectBinClass *class)
        container_class->forall     = aspect_bin_forall;
        container_class->child_type = aspect_bin_child_type;
 
+       container_class->set_child_property = aspect_bin_set_child_property;
+       container_class->get_child_property = aspect_bin_get_child_property;
+
        object_class->set_property = aspect_bin_set_property;
        object_class->get_property = aspect_bin_get_property;
 
@@ -63,6 +76,13 @@ static void aspect_bin_class_init(AspectBinClass *class)
                        "Only expand the side to be as large as the body.",
                        FALSE,
                        G_PARAM_READWRITE));
+       gtk_container_class_install_child_property(container_class,
+               CHILD_PROP_ALIGN,
+               g_param_spec_float("align",
+                       "Alignment",
+                       "Alignment of the child within the available space.",
+                       0.0, 1.0, 0.5,
+                       G_PARAM_READWRITE));
 }
 
 GtkWidget *aspect_bin_new(void)
@@ -110,6 +130,57 @@ static void aspect_bin_get_property(GObject    *object,
        }
 }
 
+static void aspect_bin_set_child_property(GtkContainer *container,
+                                          GtkWidget    *child,
+                                          guint         prop_id,
+                                          const GValue *value,
+                                          GParamSpec   *pspec)
+{
+       AspectBin *abin = ASPECT_BIN(container);
+       gfloat align;
+
+       g_assert(child == abin->body || child == abin->side);
+
+       switch (prop_id) {
+       case CHILD_PROP_ALIGN:
+               align = g_value_get_float(value);
+               if (child == abin->body)
+                       abin->body_align = align;
+               else if (child == abin->side)
+                       abin->side_align = align;
+               gtk_widget_queue_resize(GTK_WIDGET(abin));
+               break;
+       default:
+               GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID(container,
+                                                            prop_id, pspec);
+       }
+}
+
+static void aspect_bin_get_child_property(GtkContainer *container,
+                                          GtkWidget    *child,
+                                          guint         prop_id,
+                                          GValue       *value,
+                                          GParamSpec   *pspec)
+{
+       AspectBin *abin = ASPECT_BIN(container);
+       gfloat align = 0;
+
+       g_assert(child == abin->body || child == abin->side);
+
+       switch (prop_id) {
+       case CHILD_PROP_ALIGN:
+               if (child == abin->body)
+                       align = abin->body_align;
+               else if (child == abin->side)
+                       align = abin->side_align;
+               g_value_set_float(value, align);
+               break;
+       default:
+               GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID(container,
+                                                            prop_id, pspec);
+       }
+}
+
 static void aspect_bin_add(GtkContainer *container, GtkWidget *widget)
 {
        AspectBin *abin;
@@ -219,12 +290,16 @@ aspect_bin_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
                } else if (abin->fill) {
                        csize.height = allocation->height;
                } else {
-                       csize.height = creq.height;
+                       csize.height = MIN(creq.height, allocation->height);
                }
        }
 
-       csize.y = (allocation->height - csize.height) * abin->align + 0.5;
-       asize.y = (allocation->height - asize.height) * abin->align + 0.5;
+       asize.y = (allocation->height - asize.height) * abin->body_align + 0.5;
+       csize.y = (allocation->height - csize.height) * abin->side_align + 0.5;
+
+       if (abin->constrain) {
+               csize.y = MIN(asize.y, allocation->height - csize.height);
+       }
 
        if (abin->body)
                gtk_widget_size_allocate(abin->body, &asize);
index 65113f7182e05e24024ab5a1902a7fea36753125..c0d805d36ee5e29ff8c7f02ba06aeaa95a171ea5 100644 (file)
@@ -18,9 +18,9 @@ struct AspectBin {
        GtkContainer parent;
 
        GtkWidget *body, *side;
+       gfloat     body_align, side_align;
        gfloat     ratio;
 
-       gfloat     align;
        gboolean   constrain;
        gboolean   fill;
 };