[gtk/wip/otte/inscription: 10/14] inscription: Add ::overflow




commit 79a7139a571fe47147d9d6bdd0f3d8e5d7c7e218
Author: Benjamin Otte <otte redhat com>
Date:   Thu Jun 9 05:17:40 2022 +0200

    inscription: Add ::overflow

 gtk/gtkinscription.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkinscription.h |  12 ++++++
 2 files changed, 124 insertions(+)
---
diff --git a/gtk/gtkinscription.c b/gtk/gtkinscription.c
index 0201b54fed..81865efddb 100644
--- a/gtk/gtkinscription.c
+++ b/gtk/gtkinscription.c
@@ -25,6 +25,7 @@
 #include "gtkcssstylechangeprivate.h"
 #include "gtkpango.h"
 #include "gtksnapshot.h"
+#include "gtktypebuiltins.h"
 #include "gtkwidgetprivate.h"
 
 /**
@@ -69,6 +70,7 @@ struct _GtkInscription
   float xalign;
   float yalign;
   PangoAttrList *attrs;
+  GtkInscriptionOverflow overflow;
 
   PangoLayout *layout;
 };
@@ -82,6 +84,7 @@ enum
   PROP_MIN_LINES,
   PROP_NAT_CHARS,
   PROP_NAT_LINES,
+  PROP_OVERFLOW,
   PROP_TEXT,
   PROP_XALIGN,
   PROP_YALIGN,
@@ -143,6 +146,10 @@ gtk_inscription_get_property (GObject    *object,
       g_value_set_uint (value, self->nat_lines);
       break;
 
+    case PROP_OVERFLOW:
+      g_value_set_enum (value, self->overflow);
+      break;
+
     case PROP_TEXT:
       g_value_set_string (value, self->text);
       break;
@@ -195,6 +202,10 @@ gtk_inscription_set_property (GObject      *object,
       gtk_inscription_set_nat_lines (self, g_value_get_uint (value));
       break;
 
+    case PROP_OVERFLOW:
+      gtk_inscription_set_overflow (self, g_value_get_enum (value));
+      break;
+
     case PROP_TEXT:
       gtk_inscription_set_text (self, g_value_get_string (value));
       break;
@@ -402,6 +413,36 @@ gtk_inscription_allocate (GtkWidget *widget,
   GtkInscription *self = GTK_INSCRIPTION (widget);
 
   pango_layout_set_width (self->layout, width * PANGO_SCALE);
+
+  switch (self->overflow)
+    {
+    case GTK_INSCRIPTION_OVERFLOW_CLIP:
+      pango_layout_set_height (self->layout, -1);
+      /* figure out if we're single line (clip horizontally)
+       * or multiline (clip vertically):
+       * If we can't fit 2 rows, we're single line.
+       */
+      {
+        PangoLayoutIter *iter = pango_layout_get_iter (self->layout);
+        if (pango_layout_iter_next_line (iter))
+          {
+            PangoRectangle rect;
+            pango_layout_iter_get_line_extents (iter, NULL, &rect);
+            if (rect.y + rect.height > height * PANGO_SCALE)
+              pango_layout_set_width (self->layout, -1);
+          }
+      }
+      break;
+
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_START:
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_MIDDLE:
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_END:
+      pango_layout_set_height (self->layout, height * PANGO_SCALE);
+      break;
+    default:
+      g_assert_not_reached();
+      break;
+    }
 }
 
 static void
@@ -552,6 +593,19 @@ gtk_inscription_class_init (GtkInscriptionClass *klass)
                          DEFAULT_NAT_LINES,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkInscription:overflow: (attributes org.gtk.Property.get=gtk_inscription_get_overflow 
org.gtk.Property.set=gtk_inscription_set_overflow)
+   *
+   * The overflow method to use.
+   *
+   * Since: 4.8
+   */
+  properties[PROP_OVERFLOW] =
+    g_param_spec_enum ("overflow", NULL, NULL,
+                       GTK_TYPE_INSCRIPTION_OVERFLOW,
+                       GTK_INSCRIPTION_OVERFLOW_CLIP,
+                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   /**
    * GtkInscription:text: (attributes org.gtk.Property.get=gtk_inscription_get_text 
org.gtk.Property.set=gtk_inscription_set_text)
    *
@@ -610,6 +664,7 @@ gtk_inscription_init (GtkInscription *self)
   self->yalign = DEFAULT_YALIGN;
 
   self->layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), NULL);
+  pango_layout_set_wrap (self->layout, PANGO_WRAP_WORD_CHAR);
 }
 
 /**
@@ -1013,6 +1068,63 @@ gtk_inscription_get_attributes (GtkInscription *self)
   return self->attrs;
 }
 
+/**
+ * gtk_inscription_set_overflow: (attributes org.gtk.Method.set_property=overflow)
+ * @self: a `GtkInscription`
+ * @overflow: the overflow method to use
+ *
+ * Sets what to do when the text doesn't fit.
+ */
+void
+gtk_inscription_set_overflow (GtkInscription         *self,
+                              GtkInscriptionOverflow  overflow)
+{
+  g_return_if_fail (GTK_IS_INSCRIPTION (self));
+
+  if (self->overflow == overflow)
+    return;
+
+  self->overflow = overflow;
+
+  switch (self->overflow)
+    {
+    case GTK_INSCRIPTION_OVERFLOW_CLIP:
+      pango_layout_set_ellipsize (self->layout, PANGO_ELLIPSIZE_NONE);
+      break;
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_START:
+      pango_layout_set_ellipsize (self->layout, PANGO_ELLIPSIZE_START);
+      break;
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_MIDDLE:
+      pango_layout_set_ellipsize (self->layout, PANGO_ELLIPSIZE_MIDDLE);
+      break;
+    case GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_END:
+      pango_layout_set_ellipsize (self->layout, PANGO_ELLIPSIZE_END);
+      break;
+    default:
+      g_assert_not_reached();
+      break;
+    }
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_OVERFLOW]);
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+/**
+ * gtk_inscription_get_overflow: (attributes org.gtk.Method.get_property=overflow)
+ * @self: a `GtkInscription`
+ *
+ * Gets the inscription's overflow method.
+ *
+ * Returns: the overflow method
+ */
+GtkInscriptionOverflow
+gtk_inscription_get_overflow (GtkInscription *self)
+{
+  g_return_val_if_fail (GTK_IS_INSCRIPTION (self), GTK_INSCRIPTION_OVERFLOW_CLIP);
+
+  return self->overflow;
+}
+
 /**
  * gtk_inscription_set_markup: (attributes org.gtk.Method.set_property=markup)
  * @self: a `GtkInscription`
diff --git a/gtk/gtkinscription.h b/gtk/gtkinscription.h
index 856ecd39e7..10171e599e 100644
--- a/gtk/gtkinscription.h
+++ b/gtk/gtkinscription.h
@@ -30,6 +30,13 @@ G_BEGIN_DECLS
 
 #define GTK_TYPE_INSCRIPTION         (gtk_inscription_get_type ())
 
+typedef enum {
+  GTK_INSCRIPTION_OVERFLOW_CLIP,
+  GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_START,
+  GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_MIDDLE,
+  GTK_INSCRIPTION_OVERFLOW_ELLIPSIZE_END
+} GtkInscriptionOverflow;
+
 GDK_AVAILABLE_IN_4_8
 G_DECLARE_FINAL_TYPE (GtkInscription, gtk_inscription, GTK, INSCRIPTION, GtkWidget)
 
@@ -49,6 +56,11 @@ void                    gtk_inscription_set_attributes          (GtkInscription
 GDK_AVAILABLE_IN_4_8
 void                    gtk_inscription_set_markup              (GtkInscription         *self,
                                                                  const char             *markup);
+GDK_AVAILABLE_IN_4_8
+GtkInscriptionOverflow  gtk_inscription_get_overflow            (GtkInscription         *self);
+GDK_AVAILABLE_IN_4_8
+void                    gtk_inscription_set_overflow            (GtkInscription         *self,
+                                                                 GtkInscriptionOverflow  overflow);
 
 GDK_AVAILABLE_IN_4_8
 guint                   gtk_inscription_get_min_chars           (GtkInscription         *self);


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