]> git.draconx.ca Git - aspectbin.git/blobdiff - aspectbin.c
Update .gitignore.
[aspectbin.git] / aspectbin.c
index 07fca93d8e2aa36b5e98bdf20e7fbb43c5f79dea..bc74a67d3c4558f08f26623926841854915d28d0 100644 (file)
@@ -1,8 +1,28 @@
+/* AspectBin - A GTK+ container for packing with consrained aspect ratio.
+ * Copyright (C) 2009 Nick Bowler
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * 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.
+ */
+
 #include <stdio.h>
 #include "aspectbin.h"
 
 enum {
        PROP_0,
+       PROP_RATIO,
        PROP_CONSTRAIN,
        PROP_FILL,
 };
@@ -62,6 +82,13 @@ static void aspect_bin_class_init(AspectBinClass *class)
        object_class->set_property = aspect_bin_set_property;
        object_class->get_property = aspect_bin_get_property;
 
+       g_object_class_install_property(object_class,
+               PROP_RATIO,
+               g_param_spec_float("ratio",
+                       "Aspect Ratio",
+                       "Width:height ratio of the body.",
+                       0.2, 5.0, 1.0,
+                       G_PARAM_READWRITE));
        g_object_class_install_property(object_class,
                PROP_FILL,
                g_param_spec_boolean("fill",
@@ -73,7 +100,7 @@ static void aspect_bin_class_init(AspectBinClass *class)
                PROP_CONSTRAIN,
                g_param_spec_boolean("constrain",
                        "Constrain",
-                       "Only expand the side to be as large as the body.",
+                       "Try not to place the side beyond the body's edges.",
                        FALSE,
                        G_PARAM_READWRITE));
        gtk_container_class_install_child_property(container_class,
@@ -96,8 +123,15 @@ static void aspect_bin_set_property(GObject      *object,
                                     GParamSpec   *pspec)
 {
        AspectBin *abin = ASPECT_BIN(object);
+       gfloat tmp;
 
        switch (prop_id) {
+       case PROP_RATIO:
+               tmp = abin->ratio;
+               abin->ratio = g_value_get_float(value);
+               if (tmp != abin->ratio)
+                       gtk_widget_queue_resize(GTK_WIDGET(abin));
+               break;
        case PROP_CONSTRAIN:
                abin->constrain = g_value_get_boolean(value);
                gtk_widget_queue_resize(GTK_WIDGET(abin));
@@ -119,6 +153,9 @@ static void aspect_bin_get_property(GObject    *object,
        AspectBin *abin = ASPECT_BIN(object);
 
        switch (prop_id) {
+       case PROP_RATIO:
+               g_value_set_float(value, abin->ratio);
+               break;
        case PROP_CONSTRAIN:
                g_value_set_boolean(value, abin->constrain);
                break;
@@ -295,10 +332,16 @@ aspect_bin_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
        }
 
        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->constrain && csize.height <= asize.height) {
+               csize.y = asize.y + (asize.height-csize.height)
+                       * abin->side_align + 0.5;
+       } else if (abin->constrain) {
+               csize.y = (allocation->height - csize.height)
+                       * abin->body_align + 0.5;
+       } else {
+               csize.y = (allocation->height - csize.height)
+                       * abin->side_align + 0.5;
        }
 
        if (abin->body)