From fe2062925c5a194b111f52519eac6bdacf972f72 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 7 Mar 2013 13:53:16 +0100 Subject: GtkButton: Add baseline align support --- gtk/gtkbutton.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/gtk/gtkbutton.c b/gtk/gtkbutton.c index 3ac36a71b8..5165ba4dd9 100644 --- a/gtk/gtkbutton.c +++ b/gtk/gtkbutton.c @@ -170,6 +170,12 @@ static void gtk_button_get_preferred_width (GtkWidget *widget, static void gtk_button_get_preferred_height (GtkWidget *widget, gint *minimum_size, gint *natural_size); +static void gtk_button_get_preferred_height_and_baseline_for_width (GtkWidget *widget, + gint width, + gint *minimum_size, + gint *natural_size, + gint *minimum_baseline, + gint *natural_baseline); static guint button_signals[LAST_SIGNAL] = { 0 }; @@ -196,6 +202,7 @@ gtk_button_class_init (GtkButtonClass *klass) widget_class->get_preferred_width = gtk_button_get_preferred_width; widget_class->get_preferred_height = gtk_button_get_preferred_height; + widget_class->get_preferred_height_and_baseline_for_width = gtk_button_get_preferred_height_and_baseline_for_width; widget_class->destroy = gtk_button_destroy; widget_class->screen_changed = gtk_button_screen_changed; widget_class->realize = gtk_button_realize; @@ -1150,11 +1157,15 @@ gtk_button_construct_child (GtkButton *button) else box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing); + gtk_widget_set_valign (box, GTK_ALIGN_BASELINE); + if (priv->align_set) align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0); else align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0); + gtk_widget_set_valign (align, GTK_ALIGN_BASELINE); + if (priv->image_position == GTK_POS_LEFT || priv->image_position == GTK_POS_TOP) gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0); @@ -1172,6 +1183,8 @@ gtk_button_construct_child (GtkButton *button) else label = gtk_label_new (label_text); + gtk_widget_set_valign (label, GTK_ALIGN_BASELINE); + if (priv->image_position == GTK_POS_RIGHT || priv->image_position == GTK_POS_BOTTOM) gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0); @@ -1196,6 +1209,8 @@ gtk_button_construct_child (GtkButton *button) else label = gtk_label_new (priv->label_text); + gtk_widget_set_valign (label, GTK_ALIGN_BASELINE); + if (priv->align_set) gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign); @@ -1584,6 +1599,7 @@ gtk_button_size_allocate (GtkWidget *widget, GtkBorder border; gint focus_width; gint focus_pad; + gint baseline; context = gtk_widget_get_style_context (widget); @@ -1651,7 +1667,10 @@ gtk_button_size_allocate (GtkWidget *widget, child_allocation.width = MAX (1, child_allocation.width); child_allocation.height = MAX (1, child_allocation.height); - gtk_widget_size_allocate (child, &child_allocation); + baseline = gtk_widget_get_allocated_baseline (widget); + if (baseline != -1) + baseline -= child_allocation.y - allocation->y; + gtk_widget_size_allocate_with_baseline (child, &child_allocation, baseline); } } @@ -2063,7 +2082,9 @@ static void gtk_button_get_size (GtkWidget *widget, GtkOrientation orientation, gint *minimum_size, - gint *natural_size) + gint *natural_size, + gint *minimum_baseline, + gint *natural_baseline) { GtkButton *button = GTK_BUTTON (widget); GtkStyleContext *context; @@ -2074,6 +2095,7 @@ gtk_button_get_size (GtkWidget *widget, gint focus_width; gint focus_pad; gint minimum, natural; + gint top_offset; context = gtk_widget_get_style_context (widget); @@ -2084,6 +2106,8 @@ gtk_button_get_size (GtkWidget *widget, "focus-padding", &focus_pad, NULL); + top_offset = 0; + if (orientation == GTK_ORIENTATION_HORIZONTAL) { minimum = padding.left + padding.right + @@ -2097,9 +2121,14 @@ gtk_button_get_size (GtkWidget *widget, minimum = padding.top + padding.bottom + border.top + border.bottom; + top_offset = padding.top + border.top + focus_width + focus_pad; + if (gtk_widget_get_can_default (GTK_WIDGET (widget))) - minimum += default_border.top + default_border.bottom; - } + { + minimum += default_border.top + default_border.bottom; + top_offset += default_border.top; + } + } minimum += 2 * (focus_width + focus_pad); natural = minimum; @@ -2108,11 +2137,17 @@ gtk_button_get_size (GtkWidget *widget, gtk_widget_get_visible (child)) { gint child_min, child_nat; + gint child_min_baseline = -1, child_nat_baseline = -1; if (orientation == GTK_ORIENTATION_HORIZONTAL) gtk_widget_get_preferred_width (child, &child_min, &child_nat); else - gtk_widget_get_preferred_height (child, &child_min, &child_nat); + gtk_widget_get_preferred_height_and_baseline_for_width (child, -1, &child_min, &child_nat, &child_min_baseline, &child_nat_baseline); + + if (minimum_baseline && child_min_baseline >= 0) + *minimum_baseline = child_min_baseline + top_offset; + if (natural_baseline && child_nat_baseline >= 0) + *natural_baseline = child_nat_baseline + top_offset; minimum += child_min; natural += child_nat; @@ -2130,7 +2165,7 @@ gtk_button_get_preferred_width (GtkWidget *widget, gint *minimum_size, gint *natural_size) { - gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size); + gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size, NULL, NULL); } static void @@ -2138,7 +2173,20 @@ gtk_button_get_preferred_height (GtkWidget *widget, gint *minimum_size, gint *natural_size) { - gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size); + gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size, NULL, NULL); +} + +static void +gtk_button_get_preferred_height_and_baseline_for_width (GtkWidget *widget, + gint width, + gint *minimum_size, + gint *natural_size, + gint *minimum_baseline, + gint *natural_baseline) +{ + /* GtkButton is GTK_SIZE_REQUEST_CONSTANT mode, so width will be -1 all the time */ + g_assert (width == -1); + gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size, minimum_baseline, natural_baseline); } /** -- cgit v1.2.1