[gtk/wip/chergert/spellcheck] stub program and some scaffolding



commit 99e4f05dfaf3a55998aac8f7fc587955df546087
Author: Christian Hergert <chergert redhat com>
Date:   Fri Mar 12 13:36:05 2021 -0800

    stub program and some scaffolding

 gtk/gtk.h                     |  2 +
 gtk/gtkspellchecker.c         | 86 ++++++++++++++++++++++++++++++++++++++-----
 gtk/gtkspellchecker.h         |  2 +-
 gtk/gtkspellcheckerprivate.h  | 58 +++++++++++++++++++++++++++++
 gtk/gtkspelllanguage.c        | 16 ++++++++
 gtk/gtkspelllanguageprivate.h |  2 +-
 tests/meson.build             |  1 +
 tests/testspelling.c          | 68 ++++++++++++++++++++++++++++++++++
 8 files changed, 224 insertions(+), 11 deletions(-)
---
diff --git a/gtk/gtk.h b/gtk/gtk.h
index d5f50a86b3..11982e6122 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -236,6 +236,8 @@
 #include <gtk/gtkstacksidebar.h>
 #include <gtk/gtksizegroup.h>
 #include <gtk/gtksizerequest.h>
+#include <gtk/gtkspellchecker.h>
+#include <gtk/gtkspellcorrection.h>
 #include <gtk/gtkspinbutton.h>
 #include <gtk/gtkspinner.h>
 #include <gtk/gtkstack.h>
diff --git a/gtk/gtkspellchecker.c b/gtk/gtkspellchecker.c
index 60fd2881ec..660612090d 100644
--- a/gtk/gtkspellchecker.c
+++ b/gtk/gtkspellchecker.c
@@ -19,14 +19,8 @@
 
 #include "config.h"
 
-#include "gtkspellchecker.h"
-#include "gtkspelllanguageprivate.h"
-
-struct _GtkSpellChecker
-{
-  GObject    parent_instance;
-  GPtrArray *languages;
-};
+#include "gtkspellcheckerprivate.h"
+#include "gtkspellcorrection.h"
 
 enum {
   PROP_0,
@@ -43,6 +37,7 @@ _gtk_spell_checker_set_languages (GtkSpellChecker    *self,
                                   const char * const *languages)
 {
   g_assert (GTK_IS_SPELL_CHECKER (self));
+  g_assert (self->languages != NULL);
 
   if (languages == NULL)
     return;
@@ -146,6 +141,7 @@ gtk_spell_checker_class_init (GtkSpellCheckerClass *klass)
 static void
 gtk_spell_checker_init (GtkSpellChecker *self)
 {
+  self->languages = g_ptr_array_new_with_free_func (g_object_unref);
 }
 
 /**
@@ -168,7 +164,8 @@ gtk_spell_checker_get_default (void)
       if (defaults == NULL || defaults[0] == NULL)
         defaults = fallbacks;
 
-      checker = gtk_spell_checker_new_for_languages (defaults);
+      if ((checker = gtk_spell_checker_new_for_languages (defaults)))
+        g_object_add_weak_pointer (G_OBJECT (checker), (gpointer *)&checker);
     }
 
   return checker;
@@ -213,5 +210,76 @@ GtkSpellChecker *
 gtk_spell_checker_new_for_language (const char *language)
 {
   const char *languages[] = { language, NULL };
+
   return gtk_spell_checker_new_for_languages (languages);
 }
+
+gboolean
+gtk_spell_checker_contains_word (GtkSpellChecker *self,
+                                 const char      *word,
+                                 gssize           word_length)
+{
+  g_return_val_if_fail (GTK_IS_SPELL_CHECKER (self), FALSE);
+  g_return_val_if_fail (word != NULL, FALSE);
+
+  return _gtk_spell_checker_contains_word (self, word, word_length);
+}
+
+GListModel *
+gtk_spell_checker_list_corrections (GtkSpellChecker *self,
+                                    const char      *word,
+                                    gssize           word_length)
+{
+  GListStore *group;
+  GHashTable *found;
+
+  g_return_val_if_fail (GTK_IS_SPELL_CHECKER (self), NULL);
+  g_return_val_if_fail (word != NULL, NULL);
+
+  if (self->languages->len == 0)
+    return NULL;
+
+  if (word_length < 0)
+    word_length = strlen (word);
+
+  if (self->languages->len == 1)
+    return _gtk_spell_language_list_corrections (g_ptr_array_index (self->languages, 0),
+                                                 word,
+                                                 word_length);
+
+  group = g_list_store_new (GTK_TYPE_SPELL_CORRECTION);
+  found = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+  for (guint i = 0; i < self->languages->len; i++)
+    {
+      GtkSpellLanguage *language = g_ptr_array_index (self->languages, i);
+      GListModel *list = _gtk_spell_language_list_corrections (language, word, word_length);
+      guint n_items = list ? g_list_model_get_n_items (list) : 0;
+
+      for (guint j = 0; j < n_items; j++)
+        {
+          GtkSpellCorrection *item = g_list_model_get_item (list, j);
+          const char *text = gtk_spell_correction_get_text (item);
+
+          if (!g_hash_table_contains (found, text))
+            {
+              g_hash_table_insert (found, g_strdup (text), NULL);
+              g_list_store_append (group, item);
+            }
+
+          g_object_unref (item);
+        }
+
+      g_clear_object (&list);
+    }
+
+  g_hash_table_unref (found);
+
+  if (g_list_model_get_n_items (G_LIST_MODEL (group)) == 0)
+    {
+      g_object_unref (group);
+      return NULL;
+    }
+
+  return G_LIST_MODEL (group);
+}
diff --git a/gtk/gtkspellchecker.h b/gtk/gtkspellchecker.h
index 122ff2c1b0..76f036ba7a 100644
--- a/gtk/gtkspellchecker.h
+++ b/gtk/gtkspellchecker.h
@@ -44,7 +44,7 @@ GtkSpellChecker    *gtk_spell_checker_new_for_languages (const char * const *lan
 GDK_AVAILABLE_IN_4_2
 const char * const *gtk_spell_checker_list_languages    (void);
 GDK_AVAILABLE_IN_4_2
-gboolean            gtk_spell_checker_check_word        (GtkSpellChecker    *self,
+gboolean            gtk_spell_checker_contains_word     (GtkSpellChecker    *self,
                                                          const char         *word,
                                                          gssize              word_length);
 GDK_AVAILABLE_IN_4_2
diff --git a/gtk/gtkspellcheckerprivate.h b/gtk/gtkspellcheckerprivate.h
new file mode 100644
index 0000000000..567d773217
--- /dev/null
+++ b/gtk/gtkspellcheckerprivate.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#ifndef __GTK_SPELL_CHECKER_PRIVATE_H__
+#define __GTK_SPELL_CHECKER_PRIVATE_H__
+
+#include "gtkspellchecker.h"
+#include "gtkspelllanguageprivate.h"
+
+G_BEGIN_DECLS
+
+struct _GtkSpellChecker
+{
+  GObject    parent_instance;
+  GPtrArray *languages;
+};
+
+static inline gboolean
+_gtk_spell_checker_contains_word (GtkSpellChecker *self,
+                                  const char      *word,
+                                  gssize           word_length)
+{
+  if (self->languages->len == 0)
+    return TRUE;
+
+  if (word_length < 0)
+    word_length = strlen (word);
+
+  for (guint i = 0; i < self->languages->len; i++)
+    {
+      GtkSpellLanguage *language = g_ptr_array_index (self->languages, i);
+
+      if (_gtk_spell_language_contains_word (language, word, word_length))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+G_END_DECLS
+
+#endif /* __GTK_SPELL_CHECKER_PRIVATE_H__ */
diff --git a/gtk/gtkspelllanguage.c b/gtk/gtkspelllanguage.c
index 432252021e..505efaa9eb 100644
--- a/gtk/gtkspelllanguage.c
+++ b/gtk/gtkspelllanguage.c
@@ -157,3 +157,19 @@ _gtk_spell_language_get_defaults (void)
 
   return (const char * const *)defaults;
 }
+
+gboolean
+_gtk_spell_language_contains_word (GtkSpellLanguage *self,
+                                   const char       *word,
+                                   gssize            word_length)
+{
+  return FALSE;
+}
+
+GListModel *
+_gtk_spell_language_list_corrections (GtkSpellLanguage *self,
+                                      const char       *word,
+                                      gssize            word_length)
+{
+  return NULL;
+}
diff --git a/gtk/gtkspelllanguageprivate.h b/gtk/gtkspelllanguageprivate.h
index 300dc28f1c..b1804628e0 100644
--- a/gtk/gtkspelllanguageprivate.h
+++ b/gtk/gtkspelllanguageprivate.h
@@ -31,7 +31,7 @@ G_DECLARE_FINAL_TYPE (GtkSpellLanguage, gtk_spell_language, GTK, SPELL_LANGUAGE,
 const char * const *_gtk_spell_language_get_defaults     (void);
 GtkSpellLanguage   *_gtk_spell_language_new_from_code    (const char         *code);
 const char         *_gtk_spell_language_get_code         (GtkSpellLanguage   *self);
-gboolean            _gtk_spell_language_check_word       (GtkSpellLanguage   *self,
+gboolean            _gtk_spell_language_contains_word    (GtkSpellLanguage   *self,
                                                           const char         *word,
                                                           gssize              word_length);
 GListModel         *_gtk_spell_language_list_corrections (GtkSpellLanguage   *self,
diff --git a/tests/meson.build b/tests/meson.build
index c0e5f836b0..dbe4d62e19 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -68,6 +68,7 @@ gtk_tests = [
   ['testscale'],
   ['testselectionmode'],
   ['testsounds'],
+  ['testspelling'],
   ['testspinbutton'],
   ['testtreechanging'],
   ['testtreednd'],
diff --git a/tests/testspelling.c b/tests/testspelling.c
new file mode 100644
index 0000000000..213331d397
--- /dev/null
+++ b/tests/testspelling.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <gtk/gtk.h>
+
+int
+main (int argc, char *argv[])
+{
+  GtkSpellChecker *checker;
+
+  gtk_init ();
+
+  checker = gtk_spell_checker_get_default ();
+
+  for (guint i = 1; i < argc; i++)
+    {
+      const char *word = argv[i];
+      GListModel *corrections;
+      guint n_items = 0;
+
+      if (gtk_spell_checker_contains_word (checker, word, -1))
+        {
+          g_print ("Dictionary contains the word “%s”\n", word);
+          continue;
+        }
+
+      if ((corrections = gtk_spell_checker_list_corrections (checker, word, -1)))
+        {
+          n_items = g_list_model_get_n_items (G_LIST_MODEL (corrections));
+
+          if (n_items > 0)
+            g_print ("Corrections for “%s”:\n", word);
+
+          for (guint j = 0; j < n_items; j++)
+            {
+              GtkSpellCorrection *correction = g_list_model_get_item (corrections, j);
+              const char *text = gtk_spell_correction_get_text (correction);
+
+              g_print ("  %s\n", text);
+            }
+
+          g_object_unref (corrections);
+        }
+
+      if (n_items == 0)
+        g_print ("No corrections for “%s” were found.\n", word);
+    }
+
+  g_assert_finalize_object (checker);
+
+  return 0;
+}


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