From: Benjamin Otte Date: Thu, 9 Jun 2022 00:43:13 +0000 (+0200) Subject: inscription: Add ::markup X-Git-Tag: archive/raspbian/4.8.3+ds-2+rpi1~3^2~20^2~4^2~132^2~6 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=01fcfc5c2adc9a0fa4c2266b08647b7ddb492048;p=gtk4.git inscription: Add ::markup Utility property that sets both the ::text and ::attributes properties, mainly intended for use in ui files to ease translation support and bindings. --- diff --git a/gtk/gtkinscription.c b/gtk/gtkinscription.c index e3559e01b1..b8be525e5d 100644 --- a/gtk/gtkinscription.c +++ b/gtk/gtkinscription.c @@ -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); +} diff --git a/gtk/gtkinscription.h b/gtk/gtkinscription.h index f1f73dcc98..856ecd39e7 100644 --- a/gtk/gtkinscription.h +++ b/gtk/gtkinscription.h @@ -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);