diff options
author | Benjamin Otte <otte@redhat.com> | 2022-06-09 02:43:13 +0200 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2022-06-11 02:15:08 +0200 |
commit | 01fcfc5c2adc9a0fa4c2266b08647b7ddb492048 (patch) | |
tree | 31122e8647a1d572c5efe3b24050f033bb76c7f8 /gtk/gtkinscription.c | |
parent | 3f4c88aad1a9060015c809b3a2cdb78545feabba (diff) | |
download | gtk+-01fcfc5c2adc9a0fa4c2266b08647b7ddb492048.tar.gz |
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.
Diffstat (limited to 'gtk/gtkinscription.c')
-rw-r--r-- | gtk/gtkinscription.c | 66 |
1 files changed, 66 insertions, 0 deletions
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; @@ -447,6 +452,24 @@ gtk_inscription_class_init (GtkInscriptionClass *klass) 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) * * The number of characters that should fit into the inscription at minimum. @@ -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); +} |