Inscription: Derive row alignment from xalign

Texts usually want the alignment of each row to match the xalign of
the text itself.

Derive the alignment of the PangoLayout from the xalign property of
the inscription. Because Pango doesn't provide float row alignment,
map left, center and right from the xalign in 1 / 3 steps.
This commit is contained in:
Georges Basile Stavracas Neto 2022-06-12 14:27:33 -03:00 committed by Benjamin Otte
parent 221a18704b
commit c93a01d627

View File

@ -233,6 +233,24 @@ gtk_inscription_set_property (GObject *object,
} }
} }
static void
update_pango_alignment (GtkInscription *self)
{
PangoAlignment align;
gboolean ltr;
ltr = _gtk_widget_get_direction (GTK_WIDGET (self)) != GTK_TEXT_DIR_RTL;
if (self->xalign < 0.33)
align = ltr ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
else if (self->xalign < 0.67)
align = PANGO_ALIGN_CENTER;
else
align = ltr ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
pango_layout_set_alignment (self->layout, align);
}
static void static void
gtk_inscription_update_layout_attributes (GtkInscription *self, gtk_inscription_update_layout_attributes (GtkInscription *self,
PangoAttrList *css_attrs) PangoAttrList *css_attrs)
@ -267,6 +285,17 @@ gtk_inscription_css_changed (GtkWidget *widget,
} }
} }
static void
gtk_inscription_direction_changed (GtkWidget *widget,
GtkTextDirection previous_direction)
{
GtkInscription *self = GTK_INSCRIPTION (widget);
GTK_WIDGET_CLASS (gtk_inscription_parent_class)->direction_changed (widget, previous_direction);
update_pango_alignment (self);
}
static PangoFontMetrics * static PangoFontMetrics *
gtk_inscription_get_font_metrics (GtkInscription *self) gtk_inscription_get_font_metrics (GtkInscription *self)
{ {
@ -491,6 +520,7 @@ gtk_inscription_class_init (GtkInscriptionClass *klass)
gobject_class->set_property = gtk_inscription_set_property; gobject_class->set_property = gtk_inscription_set_property;
widget_class->css_changed = gtk_inscription_css_changed; widget_class->css_changed = gtk_inscription_css_changed;
widget_class->direction_changed = gtk_inscription_direction_changed;
widget_class->measure = gtk_inscription_measure; widget_class->measure = gtk_inscription_measure;
widget_class->size_allocate = gtk_inscription_allocate; widget_class->size_allocate = gtk_inscription_allocate;
widget_class->snapshot = gtk_inscription_snapshot; widget_class->snapshot = gtk_inscription_snapshot;
@ -700,6 +730,7 @@ gtk_inscription_init (GtkInscription *self)
self->layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), NULL); self->layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), NULL);
pango_layout_set_wrap (self->layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_wrap (self->layout, PANGO_WRAP_WORD_CHAR);
update_pango_alignment (self);
} }
/* for a11y */ /* for a11y */
@ -988,6 +1019,8 @@ gtk_inscription_set_xalign (GtkInscription *self,
self->xalign = xalign; self->xalign = xalign;
update_pango_alignment (self);
gtk_widget_queue_draw (GTK_WIDGET (self)); gtk_widget_queue_draw (GTK_WIDGET (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_XALIGN]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_XALIGN]);