From e6565598a4a1c371cfc091c00ba905b5e96ecc15 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 14 Mar 2009 19:34:18 -0400 Subject: [PATCH] Add height constraining and positioning options. --- aspectbin.c | 20 +++++++++++++++----- aspectbin.h | 5 ++++- main.c | 5 ++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/aspectbin.c b/aspectbin.c index 339c8ae..c9fe6a5 100644 --- a/aspectbin.c +++ b/aspectbin.c @@ -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) diff --git a/aspectbin.h b/aspectbin.h index bca5ba7..e4148c6 100644 --- a/aspectbin.h +++ b/aspectbin.h @@ -18,7 +18,10 @@ struct AspectBin { GtkBin bin; GtkWidget *child; - gfloat ratio; + gfloat ratio; + + gfloat align; + gboolean constrain; }; struct AspectBinClass { diff --git a/main.c b/main.c index 72b4d97..0485862 100644 --- a/main.c +++ b/main.c @@ -17,8 +17,11 @@ int main(int argc, char **argv) button1 = gtk_button_new_with_label("Square"); button2 = gtk_button_new_with_label("Rectangle"); aspect_bin_set_body_widget(ASPECT_BIN(aspectbin), button1, 1); - gtk_container_add(GTK_CONTAINER(aspectbin), button2); + ASPECT_BIN(aspectbin)->constrain = TRUE; + ASPECT_BIN(aspectbin)->align = 0.5; + + gtk_container_add(GTK_CONTAINER(aspectbin), button2); gtk_container_add(GTK_CONTAINER(window), aspectbin); gtk_widget_show_all(window); -- 2.43.2