[gspell/wip/entry: 9/11] Entry: recheck all when GspellEntryBuffer:spell-checker changes



commit 4681cfb6c60796780265498e3ddb0a13b98b06bf
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Oct 30 17:15:27 2016 +0100

    Entry: recheck all when GspellEntryBuffer:spell-checker changes
    
    One level deeper.

 gspell/gspell-entry.c  |   72 ++++++++++++++++++++++++++++++++++++++++++-----
 testsuite/test-entry.c |   66 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+), 8 deletions(-)
---
diff --git a/gspell/gspell-entry.c b/gspell/gspell-entry.c
index 4cacba3..1e31747 100644
--- a/gspell/gspell-entry.c
+++ b/gspell/gspell-entry.c
@@ -43,6 +43,7 @@ struct _GspellEntry
        GObject parent;
 
        GtkEntry *entry;
+       GtkEntryBuffer *buffer;
 
        /* List elements: GspellEntryWord*.
         * Used for unit tests.
@@ -281,14 +282,6 @@ emit_changed_signal (GspellEntry *gspell_entry)
        g_signal_emit_by_name (gspell_entry->entry, "changed");
 }
 
-static void
-notify_buffer_cb (GtkEntry    *gtk_entry,
-                 GParamSpec  *pspec,
-                 GspellEntry *gspell_entry)
-{
-       emit_changed_signal (gspell_entry);
-}
-
 static gboolean
 notify_attributes_idle_cb (gpointer user_data)
 {
@@ -320,6 +313,66 @@ notify_attributes_cb (GtkEntry    *gtk_entry,
 }
 
 static void
+notify_spell_checker_cb (GspellEntryBuffer *gspell_buffer,
+                        GParamSpec        *pspec,
+                        GspellEntry       *gspell_entry)
+{
+       emit_changed_signal (gspell_entry);
+}
+
+static void
+set_buffer (GspellEntry    *gspell_entry,
+           GtkEntryBuffer *gtk_buffer)
+{
+       GspellEntryBuffer *gspell_buffer;
+
+       if (gspell_entry->buffer == gtk_buffer)
+       {
+               return;
+       }
+
+       if (gspell_entry->buffer != NULL)
+       {
+               gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gspell_entry->buffer);
+
+               g_signal_handlers_disconnect_by_func (gspell_buffer,
+                                                     notify_spell_checker_cb,
+                                                     gspell_entry);
+
+               g_object_unref (gspell_entry->buffer);
+       }
+
+       gspell_entry->buffer = gtk_buffer;
+
+       if (gspell_entry->buffer != NULL)
+       {
+               gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gspell_entry->buffer);
+
+               g_signal_connect (gspell_buffer,
+                                 "notify::spell-checker",
+                                 G_CALLBACK (notify_spell_checker_cb),
+                                 gspell_entry);
+
+               g_object_ref (gspell_entry->buffer);
+       }
+}
+
+static void
+update_buffer (GspellEntry *gspell_entry)
+{
+       set_buffer (gspell_entry, gtk_entry_get_buffer (gspell_entry->entry));
+}
+
+static void
+notify_buffer_cb (GtkEntry    *gtk_entry,
+                 GParamSpec  *pspec,
+                 GspellEntry *gspell_entry)
+{
+       update_buffer (gspell_entry);
+       emit_changed_signal (gspell_entry);
+}
+
+static void
 set_entry (GspellEntry *gspell_entry,
           GtkEntry    *gtk_entry)
 {
@@ -350,6 +403,8 @@ set_entry (GspellEntry *gspell_entry,
                                  G_CALLBACK (notify_attributes_cb),
                                  gspell_entry);
 
+       update_buffer (gspell_entry);
+
        g_object_notify (G_OBJECT (gspell_entry), "entry");
 }
 
@@ -407,6 +462,7 @@ gspell_entry_dispose (GObject *object)
        GspellEntry *gspell_entry = GSPELL_ENTRY (object);
 
        gspell_entry->entry = NULL;
+       set_buffer (gspell_entry, NULL);
 
        if (gspell_entry->notify_attributes_idle_id != 0)
        {
diff --git a/testsuite/test-entry.c b/testsuite/test-entry.c
index 55baad2..9a3f200 100644
--- a/testsuite/test-entry.c
+++ b/testsuite/test-entry.c
@@ -232,6 +232,71 @@ test_buffer_change (void)
        g_object_unref (gtk_entry);
 }
 
+static void
+test_spell_checker_change (void)
+{
+       GtkEntry *gtk_entry;
+       GspellEntry *gspell_entry;
+       gint i;
+
+       gtk_entry = GTK_ENTRY (gtk_entry_new ());
+       g_object_ref_sink (gtk_entry);
+
+       gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
+       gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
+
+       for (i = 0; i < 2; i++)
+       {
+               GtkEntryBuffer *gtk_buffer;
+               GspellEntryBuffer *gspell_buffer;
+               GSList *expected_list;
+               const GSList *received_list;
+               const GspellLanguage *lang;
+               GspellChecker *checker;
+
+               gtk_buffer = gtk_entry_get_buffer (gtk_entry);
+               gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gtk_buffer);
+
+               /* Not yet a spell checker */
+               gtk_entry_set_text (gtk_entry, "auienrst");
+               expected_list = NULL;
+               received_list = _gspell_entry_get_misspelled_words (gspell_entry);
+               check_entry_word_list_equal (expected_list, received_list);
+
+               /* Set a spell checker */
+               lang = gspell_language_lookup ("en_US");
+               g_assert (lang != NULL);
+
+               checker = gspell_checker_new (lang);
+               gspell_entry_buffer_set_spell_checker (gspell_buffer, checker);
+               g_object_unref (checker);
+
+               expected_list = add_word (NULL, "auienrst", 0, 8);
+               received_list = _gspell_entry_get_misspelled_words (gspell_entry);
+               check_entry_word_list_equal (expected_list, received_list);
+               free_word_list (expected_list);
+
+               /* Set NULL spell checker */
+               gspell_entry_buffer_set_spell_checker (gspell_buffer, NULL);
+               expected_list = NULL;
+               received_list = _gspell_entry_get_misspelled_words (gspell_entry);
+               check_entry_word_list_equal (expected_list, received_list);
+
+               /* Change buffer for next iteration, to see if the signal
+                * connection for spell-checker changes still works.
+                */
+               {
+                       GtkEntryBuffer *new_gtk_buffer;
+
+                       new_gtk_buffer = gtk_entry_buffer_new (NULL, -1);
+                       gtk_entry_set_buffer (gtk_entry, new_gtk_buffer);
+                       g_object_unref (new_gtk_buffer);
+               }
+       }
+
+       g_object_unref (gtk_entry);
+}
+
 gint
 main (gint    argc,
       gchar **argv)
@@ -241,6 +306,7 @@ main (gint    argc,
        g_test_add_func ("/entry-utils/get-words", test_get_words);
        g_test_add_func ("/entry/inline-spell-checking-property", test_inline_spell_checking_property);
        g_test_add_func ("/entry/buffer-change", test_buffer_change);
+       g_test_add_func ("/entry/spell-checker-change", test_spell_checker_change);
 
        return g_test_run ();
 }


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