From 197e42efd8a0c6adea9fd49f5469687349e887d4 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 16 Dec 2015 20:39:51 +0100 Subject: label: Position the text properly The PangoLayout needs to be positioned according to the content allocation of the gadget, not the widget's allocation. --- gtk/gtkcssgadget.c | 35 +++++++++++++++++++++++++++++++++++ gtk/gtkcssgadgetprivate.h | 3 +++ gtk/gtklabel.c | 11 +++++++---- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/gtk/gtkcssgadget.c b/gtk/gtkcssgadget.c index ac355c5533..895855779d 100644 --- a/gtk/gtkcssgadget.c +++ b/gtk/gtkcssgadget.c @@ -698,3 +698,38 @@ gtk_css_gadget_get_border_allocation (GtkCssGadget *gadget, } } +void +gtk_css_gadget_get_content_allocation (GtkCssGadget *gadget, + GtkAllocation *allocation, + int *baseline) +{ + GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget); + GtkBorder margin, border, padding, extents; + GtkCssStyle *style; + + g_return_if_fail (GTK_IS_CSS_GADGET (gadget)); + + style = gtk_css_gadget_get_style (gadget); + get_box_margin (style, &margin); + get_box_border (style, &border); + get_box_padding (style, &padding); + extents.top = margin.top + border.top + padding.top; + extents.right = margin.right + border.right + padding.right; + extents.bottom = margin.bottom + border.bottom + padding.bottom; + extents.left = margin.left + border.left + padding.left; + + if (allocation) + { + allocation->x = priv->allocated_size.x + extents.left; + allocation->y = priv->allocated_size.y + extents.top; + allocation->width = priv->allocated_size.width - extents.left - extents.right; + allocation->height = priv->allocated_size.height - extents.top - extents.bottom; + } + if (baseline) + { + if (priv->allocated_baseline >= 0) + *baseline = priv->allocated_baseline - extents.top; + else + *baseline = -1; + } +} diff --git a/gtk/gtkcssgadgetprivate.h b/gtk/gtkcssgadgetprivate.h index 9d4b6e40ca..551064fde6 100644 --- a/gtk/gtkcssgadgetprivate.h +++ b/gtk/gtkcssgadgetprivate.h @@ -100,6 +100,9 @@ void gtk_css_gadget_draw (GtkCssGadget void gtk_css_gadget_get_border_allocation (GtkCssGadget *gadget, GtkAllocation *allocation, int *baseline); +void gtk_css_gadget_get_content_allocation (GtkCssGadget *gadget, + GtkAllocation *allocation, + int *baseline); G_END_DECLS diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c index bcbd3410fb..e87139aa40 100644 --- a/gtk/gtklabel.c +++ b/gtk/gtklabel.c @@ -3403,16 +3403,18 @@ gtk_label_update_layout_width (GtkLabel *label) if (priv->ellipsize || priv->wrap) { + GtkAllocation allocation; GtkBorder border; PangoRectangle logical; gint width, height; + gtk_css_gadget_get_content_allocation (priv->gadget, &allocation, NULL); G_GNUC_BEGIN_IGNORE_DEPRECATIONS _gtk_misc_get_padding_and_border (GTK_MISC (label), &border); G_GNUC_END_IGNORE_DEPRECATIONS - width = gtk_widget_get_allocated_width (GTK_WIDGET (label)) - border.left - border.right; - height = gtk_widget_get_allocated_height (GTK_WIDGET (label)) - border.top - border.bottom; + width = allocation.width - border.left - border.right; + height = allocation.height - border.top - border.bottom; if (priv->have_transform) { @@ -4011,12 +4013,13 @@ G_GNUC_END_IGNORE_DEPRECATIONS req_width += border.left + border.right; req_height += border.top + border.bottom; - gtk_widget_get_allocation (widget, &allocation); + gtk_css_gadget_get_content_allocation (priv->gadget, + &allocation, + &baseline); x = floor (allocation.x + border.left + xalign * (allocation.width - req_width) - logical.x); baseline_offset = 0; - baseline = gtk_widget_get_allocated_baseline (widget); if (baseline != -1 && !priv->have_transform) { layout_baseline = pango_layout_get_baseline (priv->layout) / PANGO_SCALE; -- cgit v1.2.1