[gtk/attribute-parsing: 2/2] entry: Add <attributes> support



commit efe16cf4bea88b658d7ac692a26b8c2b627d7f42
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Apr 6 22:13:10 2020 -0400

    entry: Add <attributes> support
    
    Make entries support the same <attributes> syntax
    as labels.
    
    Closes: #1335

 gtk/gtkentry.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
---
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index 13fd885acf..e823ab0d08 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -293,13 +293,86 @@ static void     gtk_entry_measure (GtkWidget           *widget,
                                    int                 *minimum_baseline,
                                    int                 *natural_baseline);
 
+static GtkBuildableIface *buildable_parent_iface = NULL;
+
+static void     gtk_entry_buildable_interface_init (GtkBuildableIface *iface);
+
 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
                          G_ADD_PRIVATE (GtkEntry)
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+                                                gtk_entry_buildable_interface_init)
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
                                                 gtk_entry_editable_init)
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
                                                 gtk_entry_cell_editable_init))
 
+
+static const GtkBuildableParser pango_parser =
+{
+  gtk_pango_attribute_start_element,
+};
+
+static gboolean
+gtk_entry_buildable_custom_tag_start (GtkBuildable       *buildable,
+                                      GtkBuilder         *builder,
+                                      GObject            *child,
+                                      const gchar        *tagname,
+                                      GtkBuildableParser *parser,
+                                      gpointer           *data)
+{
+  if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
+                                                tagname, parser, data))
+    return TRUE;
+
+  if (strcmp (tagname, "attributes") == 0)
+    {
+      GtkPangoAttributeParserData *parser_data;
+
+      parser_data = g_slice_new0 (GtkPangoAttributeParserData);
+      parser_data->builder = g_object_ref (builder);
+      parser_data->object = (GObject *) g_object_ref (buildable);
+      *parser = pango_parser;
+      *data = parser_data;
+      return TRUE;
+    }
+  return FALSE;
+}
+
+static void
+gtk_entry_buildable_custom_finished (GtkBuildable *buildable,
+                                     GtkBuilder   *builder,
+                                     GObject      *child,
+                                     const gchar  *tagname,
+                                     gpointer      user_data)
+{
+  GtkPangoAttributeParserData *data = user_data;
+
+  buildable_parent_iface->custom_finished (buildable, builder, child,
+                                           tagname, user_data);
+
+  if (strcmp (tagname, "attributes") == 0)
+    {
+      if (data->attrs)
+        {
+          gtk_entry_set_attributes (GTK_ENTRY (buildable), data->attrs);
+          pango_attr_list_unref (data->attrs);
+        }
+
+      g_object_unref (data->object);
+      g_object_unref (data->builder);
+      g_slice_free (GtkPangoAttributeParserData, data);
+    }
+}
+
+static void
+gtk_entry_buildable_interface_init (GtkBuildableIface *iface)
+{
+  buildable_parent_iface = g_type_interface_peek_parent (iface);
+
+  iface->custom_tag_start = gtk_entry_buildable_custom_tag_start;
+  iface->custom_finished = gtk_entry_buildable_custom_finished;
+}
+
 static gboolean
 gtk_entry_grab_focus (GtkWidget *widget)
 {


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