[gtk/gbsneto/inscription-justify] Inscription: Derive row alignment from xalign




commit b8e827091d7dce154c14b616ef31d8851b26bc3d
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Jun 12 14:27:33 2022 -0300

    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.

 gtk/gtkinscription.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
---
diff --git a/gtk/gtkinscription.c b/gtk/gtkinscription.c
index c8937df9e9..04a5128f3d 100644
--- a/gtk/gtkinscription.c
+++ b/gtk/gtkinscription.c
@@ -233,6 +233,27 @@ gtk_inscription_set_property (GObject      *object,
     }
 }
 
+static void
+update_pango_alignment (GtkInscription *self)
+{
+  PangoAlignment align;
+  gboolean ltr;
+
+  g_assert (self->layout != NULL);
+  g_assert (self->xalign >= 0.0 && self->xalign <= 1.0);
+
+  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
 gtk_inscription_update_layout_attributes (GtkInscription *self,
                                           PangoAttrList  *css_attrs)
@@ -267,6 +288,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 *
 gtk_inscription_get_font_metrics (GtkInscription *self)
 {
@@ -491,6 +523,7 @@ gtk_inscription_class_init (GtkInscriptionClass *klass)
   gobject_class->set_property = gtk_inscription_set_property;
 
   widget_class->css_changed = gtk_inscription_css_changed;
+  widget_class->direction_changed = gtk_inscription_direction_changed;
   widget_class->measure = gtk_inscription_measure;
   widget_class->size_allocate = gtk_inscription_allocate;
   widget_class->snapshot = gtk_inscription_snapshot;
@@ -988,6 +1021,8 @@ gtk_inscription_set_xalign (GtkInscription *self,
 
   self->xalign = xalign;
 
+  update_pango_alignment (self);
+
   gtk_widget_queue_draw (GTK_WIDGET (self));
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_XALIGN]);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]