[gspell/wip/entry: 3/5] Entry: re-apply our attributes if the :attributes property has changed



commit 92eec9687ca266c1c147d8064dafcc9636afed89
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Oct 28 17:00:05 2016 +0200

    Entry: re-apply our attributes if the :attributes property has changed
    
    And add UI test with a button to make the GtkEntry in bold.

 gspell/gspell-entry.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++-
 tests/test-entry.c    |   58 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 2 deletions(-)
---
diff --git a/gspell/gspell-entry.c b/gspell/gspell-entry.c
index 9e16ac6..e97654a 100644
--- a/gspell/gspell-entry.c
+++ b/gspell/gspell-entry.c
@@ -41,6 +41,10 @@ struct _GspellEntry
        GObject parent;
 
        GtkEntry *entry;
+
+       gulong notify_attributes_handler_id;
+       guint notify_attributes_idle_id;
+
        guint inline_spell_checking : 1;
 };
 
@@ -55,6 +59,19 @@ enum
 
 G_DEFINE_TYPE (GspellEntry, gspell_entry, G_TYPE_OBJECT)
 
+static void
+set_attributes (GspellEntry   *gspell_entry,
+               PangoAttrList *attributes)
+{
+       g_signal_handler_block (gspell_entry->entry,
+                               gspell_entry->notify_attributes_handler_id);
+
+       gtk_entry_set_attributes (gspell_entry->entry, attributes);
+
+       g_signal_handler_unblock (gspell_entry->entry,
+                                 gspell_entry->notify_attributes_handler_id);
+}
+
 static gboolean
 remove_underlines_filter (PangoAttribute *attr,
                          gpointer        user_data)
@@ -102,7 +119,7 @@ insert_underline (GspellEntry *gspell_entry,
        if (attr_list == NULL)
        {
                attr_list = pango_attr_list_new ();
-               gtk_entry_set_attributes (gspell_entry->entry, attr_list);
+               set_attributes (gspell_entry, attr_list);
                pango_attr_list_unref (attr_list);
        }
 
@@ -139,7 +156,7 @@ recheck_all (GspellEntry *gspell_entry)
         * with multi-byte characters (displaying them as unknown char boxes).
         */
        attr_list = gtk_entry_get_attributes (gspell_entry->entry);
-       gtk_entry_set_attributes (gspell_entry->entry, attr_list);
+       set_attributes (gspell_entry, attr_list);
 }
 
 /* Connect to the ::changed signal before/after, so that other features (in
@@ -164,6 +181,36 @@ changed_after_cb (GtkEditable *editable,
        recheck_all (gspell_entry);
 }
 
+static gboolean
+notify_attributes_idle_cb (gpointer user_data)
+{
+       GspellEntry *gspell_entry = GSPELL_ENTRY (user_data);
+
+       /* Re-apply our attributes. Do it in an idle function, to not be inside
+        * a notify::attributes signal emission. If we call recheck_all() during
+        * the signal emission, there is an infinite loop.
+        */
+       recheck_all (gspell_entry);
+
+       gspell_entry->notify_attributes_idle_id = 0;
+       return G_SOURCE_REMOVE;
+}
+
+static void
+notify_attributes_cb (GtkEntry    *gtk_entry,
+                     GParamSpec  *pspec,
+                     GspellEntry *gspell_entry)
+{
+       if (gspell_entry->notify_attributes_idle_id == 0)
+       {
+               gspell_entry->notify_attributes_idle_id =
+                       g_idle_add_full (G_PRIORITY_HIGH_IDLE,
+                                        notify_attributes_idle_cb,
+                                        gspell_entry,
+                                        NULL);
+       }
+}
+
 static void
 set_entry (GspellEntry *gspell_entry,
           GtkEntry    *gtk_entry)
@@ -183,6 +230,13 @@ set_entry (GspellEntry *gspell_entry,
                                G_CALLBACK (changed_after_cb),
                                gspell_entry);
 
+       g_assert (gspell_entry->notify_attributes_handler_id == 0);
+       gspell_entry->notify_attributes_handler_id =
+               g_signal_connect (gtk_entry,
+                                 "notify::attributes",
+                                 G_CALLBACK (notify_attributes_cb),
+                                 gspell_entry);
+
        g_object_notify (G_OBJECT (gspell_entry), "entry");
 }
 
@@ -241,6 +295,12 @@ gspell_entry_dispose (GObject *object)
 
        gspell_entry->entry = NULL;
 
+       if (gspell_entry->notify_attributes_idle_id != 0)
+       {
+               g_source_remove (gspell_entry->notify_attributes_idle_id);
+               gspell_entry->notify_attributes_idle_id = 0;
+       }
+
        G_OBJECT_CLASS (gspell_entry_parent_class)->dispose (object);
 }
 
diff --git a/tests/test-entry.c b/tests/test-entry.c
index 3f2b588..aaa5569 100644
--- a/tests/test-entry.c
+++ b/tests/test-entry.c
@@ -63,12 +63,70 @@ create_entry (void)
 }
 
 static void
+bold_toggled_cb (GtkToggleButton *button,
+                TestSpell       *spell)
+{
+       /* Do not care about other users of the GtkEntry:attributes property. An
+        * application or another library might do something similar, but
+        * GspellEntry should still work.
+        */
+
+       if (gtk_toggle_button_get_active (button))
+       {
+               PangoAttribute *attr_bold;
+               PangoAttrList *attr_list;
+
+               attr_bold = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
+
+               attr_list = pango_attr_list_new ();
+               pango_attr_list_insert (attr_list, attr_bold);
+               gtk_entry_set_attributes (spell->entry, attr_list);
+               pango_attr_list_unref (attr_list);
+       }
+       else
+       {
+               gtk_entry_set_attributes (spell->entry, NULL);
+       }
+}
+
+static GtkWidget *
+create_sidebar (TestSpell *spell)
+{
+       GtkWidget *vgrid;
+       GtkWidget *bold_toggle_button;
+
+       vgrid = gtk_grid_new ();
+       gtk_grid_set_row_spacing (GTK_GRID (vgrid), 6);
+       gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid),
+                                       GTK_ORIENTATION_VERTICAL);
+
+       bold_toggle_button = gtk_toggle_button_new_with_label ("Bold");
+       gtk_container_add (GTK_CONTAINER (vgrid), bold_toggle_button);
+
+       g_signal_connect (bold_toggle_button,
+                         "toggled",
+                         G_CALLBACK (bold_toggled_cb),
+                         spell);
+
+       gtk_widget_show_all (vgrid);
+
+       return vgrid;
+}
+
+static void
 test_spell_init (TestSpell *spell)
 {
        g_object_set (spell,
                      "margin", 6,
                      NULL);
 
+       gtk_grid_set_column_spacing (GTK_GRID (spell), 6);
+       gtk_orientable_set_orientation (GTK_ORIENTABLE (spell),
+                                       GTK_ORIENTATION_HORIZONTAL);
+
+       gtk_container_add (GTK_CONTAINER (spell),
+                          create_sidebar (spell));
+
        spell->entry = create_entry ();
        gtk_container_add (GTK_CONTAINER (spell),
                           GTK_WIDGET (spell->entry));


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