inscription: Add ::markup
authorBenjamin Otte <otte@redhat.com>
Thu, 9 Jun 2022 00:43:13 +0000 (02:43 +0200)
committerBenjamin Otte <otte@redhat.com>
Sat, 11 Jun 2022 00:15:08 +0000 (02:15 +0200)
Utility property that sets both the ::text and ::attributes properties,
mainly intended for use in ui files to ease translation support and bindings.

gtk/gtkinscription.c
gtk/gtkinscription.h

index e3559e01b11cc9c5583fcee173259c4495fef978..b8be525e5df59397fcf0e2ab399b7423bfbdb8dc 100644 (file)
@@ -77,6 +77,7 @@ enum
 {
   PROP_0,
   PROP_ATTRIBUTES,
+  PROP_MARKUP,
   PROP_MIN_CHARS,
   PROP_MIN_LINES,
   PROP_NAT_CHARS,
@@ -174,6 +175,10 @@ gtk_inscription_set_property (GObject      *object,
       gtk_inscription_set_attributes (self, g_value_get_boxed (value));
       break;
 
+    case PROP_MARKUP:
+      gtk_inscription_set_markup (self, g_value_get_string (value));
+      break;
+
     case PROP_MIN_CHARS:
       gtk_inscription_set_min_chars (self, g_value_get_uint (value));
       break;
@@ -446,6 +451,24 @@ gtk_inscription_class_init (GtkInscriptionClass *klass)
                           PANGO_TYPE_ATTR_LIST,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkInscription:markup: (attributes org.gtk.Property.set=gtk_inscription_set_markup)
+   *
+   * Utility property that sets both the [property@Gtk.Inscription:text] and
+   * [property@Gtk.Inscription:attributes] properties, mainly intended for use in
+   * GtkBuilder ui files to ease translation support and bindings.
+   *
+   * This function uses [func@Pango.parse_markup] to parse the markup into text and
+   * attributes. The markup must be valid. If you cannot ensure that, consider using
+   * [func@Pango.parse_markup] and setting the two properties yourself.
+   *
+   * Since: 4.8
+   */
+  properties[PROP_MARKUP] =
+    g_param_spec_string ("markup", NULL, NULL,
+                         NULL,
+                         G_PARAM_WRITABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   /**
    * GtkInscription:min-chars: (attributes org.gtk.Property.get=gtk_inscription_get_min_chars org.gtk.Property.set=gtk_inscription_set_min_chars)
    *
@@ -996,3 +1019,46 @@ gtk_inscription_get_attributes (GtkInscription *self)
   return self->attrs;
 }
 
+/**
+ * gtk_inscription_set_markup: (attributes org.gtk.Method.set_property=markup)
+ * @self: a `GtkInscription`
+ * @markup: (nullable): The markup to display
+ *
+ * Utility function to set the text and attributes to be displayed.
+ *
+ * See the [property@Gtk.Inscription:markup] property.
+ *
+ * Since: 4.8
+ */
+void
+gtk_inscription_set_markup (GtkInscription *self,
+                            const char     *markup)
+{
+  PangoAttrList *attrs;
+  char *text;
+  GError *error = NULL;
+
+  g_return_if_fail (GTK_IS_INSCRIPTION (self));
+
+  if (markup == NULL)
+    {
+      text = NULL;
+      attrs = NULL;
+    }
+  else if (!pango_parse_markup (markup, -1,
+                           0,
+                           &attrs, &text,
+                           NULL,
+                           &error))
+    {
+      g_warning ("Failed to set text '%s' from markup due to error parsing markup: %s",
+                 markup, error->message);
+      return;
+    }
+
+  gtk_inscription_set_text (self, text);
+  gtk_inscription_set_attributes (self, attrs);
+
+  g_clear_pointer (&text, g_free);
+  g_clear_pointer (&attrs, pango_attr_list_unref);
+}
index f1f73dcc9872802be8639e89a7192e9964ece017..856ecd39e72bc3d1601a2f09d0f88631d640f184 100644 (file)
@@ -46,6 +46,9 @@ PangoAttrList *         gtk_inscription_get_attributes          (GtkInscription
 GDK_AVAILABLE_IN_4_8
 void                    gtk_inscription_set_attributes          (GtkInscription         *self,
                                                                  PangoAttrList          *attrs);
+GDK_AVAILABLE_IN_4_8
+void                    gtk_inscription_set_markup              (GtkInscription         *self,
+                                                                 const char             *markup);
 
 GDK_AVAILABLE_IN_4_8
 guint                   gtk_inscription_get_min_chars           (GtkInscription         *self);