From b164df74506505ac0f4559744ad9b59b5ea57ebf Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Sat, 20 Apr 2013 17:52:16 +0900 Subject: [PATCH] Revert "gtkbin: Remove the silliest code on earth" This reverts commit f4438a1ffc6aaab92fb6b751cd16e95c2abaa0e3. The calculation of the delta between parent and child widget is required in order to automate height-for-width and width-for-height requests for various GtkBin widgets. GtkButton, GtkCheckButton, GtkRadioButton, etc, all have different requests for space around the content which can not be satisfied with a simple calculation of GtkContainer border-width. --- gtk/gtkbin.c | 78 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/gtk/gtkbin.c b/gtk/gtkbin.c index b64f54156d..3764938f2d 100644 --- a/gtk/gtkbin.c +++ b/gtk/gtkbin.c @@ -189,6 +189,44 @@ gtk_bin_get_effective_border_width (GtkBin *bin) return gtk_container_get_border_width (GTK_CONTAINER (bin)); } +/* GtkBin widgets define the padding and borders independantly so + * we cannot provide a generic get_size() for the same reason + * we never implemented size_request() here. + * + * But for cases where the GtkBin class's padding is constant and + * does not vary based on allocation (most cases), we can at least + * deduce a common code path for the get_width_for_height()/get_height_for_width() + * cases by using the delta of the base size requsts. + */ +static void +get_child_padding_delta (GtkBin *bin, + gint *delta_h, + gint *delta_v) +{ + GtkBinPrivate *priv = bin->priv; + gint hmin, vmin, hnat, vnat, child_hmin, child_vmin; + + /* we can't use gtk_widget_get_preferred_width() wrapper + * because we want our "original" request, not any external + * adjustments from set_size_request() or whatever. we have + * to ask for natural also because NULL isn't allowed for the + * direct vfuncs + */ + GTK_WIDGET_GET_CLASS (bin)->get_preferred_width (GTK_WIDGET (bin), &hmin, &hnat); + GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), + GTK_ORIENTATION_HORIZONTAL, &hmin, &hnat); + + GTK_WIDGET_GET_CLASS (bin)->get_preferred_height (GTK_WIDGET (bin), &vmin, &vnat); + GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), + GTK_ORIENTATION_VERTICAL, &vmin, &vnat); + + gtk_widget_get_preferred_width (priv->child, &child_hmin, NULL); + gtk_widget_get_preferred_height (priv->child, &child_vmin, NULL); + + *delta_h = hmin - child_hmin; + *delta_v = vmin - child_vmin; +} + static void gtk_bin_get_preferred_width (GtkWidget *widget, gint *minimum_width, @@ -249,25 +287,25 @@ gtk_bin_get_preferred_width_for_height (GtkWidget *widget, { GtkBin *bin = GTK_BIN (widget); GtkBinPrivate *priv = bin->priv; - gint border_width; + gint hdelta, vdelta, child_min, child_nat; *minimum_width = 0; *natural_width = 0; - border_width = gtk_bin_get_effective_border_width (bin); - if (priv->child && gtk_widget_get_visible (priv->child)) { - gint child_min, child_nat; - gtk_widget_get_preferred_width_for_height (priv->child, height - 2 * border_width, + get_child_padding_delta (bin, &hdelta, &vdelta); + + gtk_widget_get_preferred_width_for_height (priv->child, + height - vdelta, &child_min, &child_nat); - *minimum_width = child_min; - *natural_width = child_nat; + if (minimum_width) + *minimum_width = child_min + hdelta; + + if (natural_width) + *natural_width = child_nat + hdelta; } - - *minimum_width += 2 * border_width; - *natural_width += 2 * border_width; } static void @@ -278,25 +316,25 @@ gtk_bin_get_preferred_height_for_width (GtkWidget *widget, { GtkBin *bin = GTK_BIN (widget); GtkBinPrivate *priv = bin->priv; - gint border_width; + gint hdelta, vdelta, child_min, child_nat; *minimum_height = 0; *natural_height = 0; - border_width = gtk_bin_get_effective_border_width (bin); - if (priv->child && gtk_widget_get_visible (priv->child)) { - gint child_min, child_nat; - gtk_widget_get_preferred_height_for_width (priv->child, width - 2 * border_width, + get_child_padding_delta (bin, &hdelta, &vdelta); + + gtk_widget_get_preferred_height_for_width (priv->child, + width - hdelta, &child_min, &child_nat); - *minimum_height = child_min; - *natural_height = child_nat; + if (minimum_height) + *minimum_height = child_min + vdelta; + + if (natural_height) + *natural_height = child_nat + vdelta; } - - *minimum_height += 2 * border_width; - *natural_height += 2 * border_width; } static void