]> git.draconx.ca Git - aspectbin.git/blobdiff - aspectbin.c
Add height constraining and positioning options.
[aspectbin.git] / aspectbin.c
index 339c8aec916a7ca610d71b827c664c7d375f6821..c9fe6a5abff987182b321c5679818334fd1dfd6b 100644 (file)
@@ -10,8 +10,10 @@ G_DEFINE_TYPE(AspectBin, aspect_bin, GTK_TYPE_BIN)
 
 static void aspect_bin_init(AspectBin *ab)
 {
-       ab->child = NULL;
-       ab->ratio = 1;
+       ab->child     = NULL;
+       ab->ratio     = 1;
+       ab->constrain = FALSE;
+       ab->align     = 0;
 }
 
 static void aspect_bin_class_init(AspectBinClass *abc)
@@ -94,6 +96,7 @@ aspect_bin_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
        GtkRequisition creq = {0};
        GtkAllocation csize = {0}, asize = {0};
        
+       /* First find the best fit for the constrained child. */
        if (abin->child && GTK_WIDGET_VISIBLE(abin->child)) {
                asize.height = allocation->height;
                asize.width  = asize.height * abin->ratio + 0.5;
@@ -104,20 +107,27 @@ aspect_bin_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
                }
        }
 
+       /* Now try to fit the other child. */
        if (bin->child && GTK_WIDGET_VISIBLE(bin->child)) {
                gtk_widget_get_child_requisition(bin->child, &creq);
 
                if (allocation->width - asize.width < creq.width) {
+                       /* It didn't fit, squish the constrained guy. */
                        asize.width  = allocation->width - creq.width;
                        asize.height = asize.width / abin->ratio + 0.5;
                }
 
-               csize.width = allocation->width - asize.width;
-               csize.height = allocation->height;
+               csize.width  = allocation->width - asize.width;
+               csize.height = MIN(allocation->height, creq.height);
+               if (abin->constrain) {
+                       csize.height = MAX(csize.height, asize.height);
+               }
                csize.x = asize.width;
-               csize.y = 0;
        }
 
+       csize.y = (allocation->height - csize.height) * abin->align + 0.5;
+       asize.y = (allocation->height - asize.height) * abin->align + 0.5;
+
        if (bin->child)
                gtk_widget_size_allocate(bin->child, &csize);
        if (abin->child)