[gspell/wip/current-word-policy: 14/17] Implement the GspellCurrentWordPolicy private class



commit c032e4f5a9930ce22c159816a1460525076cc134
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Dec 8 11:24:06 2016 +0100

    Implement the GspellCurrentWordPolicy private class

 docs/reference/Makefile.am          |    1 +
 gspell/Makefile.am                  |    2 +
 gspell/gspell-current-word-policy.c |  237 +++++++++++++++++++++++++++++++++++
 gspell/gspell-current-word-policy.h |   85 +++++++++++++
 po/POTFILES.in                      |    1 +
 5 files changed, 326 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index d3f17df..e420954 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -33,6 +33,7 @@ IGNORE_HFILES =                                       \
        gspellregion.h                          \
        gspell-checker-private.h                \
        gspell-context-menu.h                   \
+       gspell-current-word-policy.h            \
        gspell-entry-private.h                  \
        gspell-entry-utils.h                    \
        gspell-init.h                           \
diff --git a/gspell/Makefile.am b/gspell/Makefile.am
index 9d21481..076a9fd 100644
--- a/gspell/Makefile.am
+++ b/gspell/Makefile.am
@@ -47,6 +47,7 @@ gspell_private_headers =                      \
        gspellregion.h                          \
        gspell-checker-private.h                \
        gspell-context-menu.h                   \
+       gspell-current-word-policy.h            \
        gspell-entry-private.h                  \
        gspell-entry-utils.h                    \
        gspell-init.h                           \
@@ -57,6 +58,7 @@ gspell_private_headers =                      \
 gspell_private_c_files =                       \
        gspellregion.c                          \
        gspell-context-menu.c                   \
+       gspell-current-word-policy.c            \
        gspell-entry-utils.c                    \
        gspell-init.c                           \
        gspell-inline-checker-text-buffer.c     \
diff --git a/gspell/gspell-current-word-policy.c b/gspell/gspell-current-word-policy.c
new file mode 100644
index 0000000..beb86d6
--- /dev/null
+++ b/gspell/gspell-current-word-policy.c
@@ -0,0 +1,237 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * 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.1 of the License, 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/>.
+ */
+
+#include "gspell-current-word-policy.h"
+
+/* An object that decides whether to check the current word. When a word is
+ * being typed, it should not be spell-checked, because it would be annoying to
+ * see the red wavy underline appearing and disappearing constantly.
+ *
+ * You need to feed the object with events, and get the result with
+ * _gspell_current_word_policy_get_check_current_word().
+ */
+
+typedef struct _GspellCurrentWordPolicyPrivate GspellCurrentWordPolicyPrivate;
+
+struct _GspellCurrentWordPolicyPrivate
+{
+       guint check_current_word : 1;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GspellCurrentWordPolicy, _gspell_current_word_policy, G_TYPE_OBJECT)
+
+static void
+_gspell_current_word_policy_dispose (GObject *object)
+{
+
+       G_OBJECT_CLASS (_gspell_current_word_policy_parent_class)->dispose (object);
+}
+
+static void
+_gspell_current_word_policy_finalize (GObject *object)
+{
+
+       G_OBJECT_CLASS (_gspell_current_word_policy_parent_class)->finalize (object);
+}
+
+static void
+_gspell_current_word_policy_class_init (GspellCurrentWordPolicyClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = _gspell_current_word_policy_dispose;
+       object_class->finalize = _gspell_current_word_policy_finalize;
+}
+
+static void
+_gspell_current_word_policy_init (GspellCurrentWordPolicy *policy)
+{
+       GspellCurrentWordPolicyPrivate *priv;
+
+       priv = _gspell_current_word_policy_get_instance_private (policy);
+       priv->check_current_word = TRUE;
+}
+
+GspellCurrentWordPolicy *
+_gspell_current_word_policy_new (void)
+{
+       return g_object_new (GSPELL_TYPE_CURRENT_WORD_POLICY, NULL);
+}
+
+gboolean
+_gspell_current_word_policy_get_check_current_word (GspellCurrentWordPolicy *policy)
+{
+       GspellCurrentWordPolicyPrivate *priv;
+
+       g_return_val_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy), TRUE);
+
+       priv = _gspell_current_word_policy_get_instance_private (policy);
+
+       return priv->check_current_word;
+}
+
+/* For other events, it's better to use the more specific feed functions if
+ * possible.
+ */
+void
+_gspell_current_word_policy_set_check_current_word (GspellCurrentWordPolicy *policy,
+                                                   gboolean                 check_current_word)
+{
+       GspellCurrentWordPolicyPrivate *priv;
+
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       priv = _gspell_current_word_policy_get_instance_private (policy);
+
+       priv->check_current_word = check_current_word != FALSE;
+}
+
+/* On GspellChecker::session-cleared signal. */
+void
+_gspell_current_word_policy_session_cleared (GspellCurrentWordPolicy *policy)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+}
+
+/* On GspellChecker::notify::language signal. */
+void
+_gspell_current_word_policy_language_changed (GspellCurrentWordPolicy *policy)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+}
+
+/* When another GspellChecker object is used. */
+void
+_gspell_current_word_policy_checker_changed (GspellCurrentWordPolicy *policy)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+}
+
+void
+_gspell_current_word_policy_cursor_moved (GspellCurrentWordPolicy *policy)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+}
+
+/* After a text insertion. */
+void
+_gspell_current_word_policy_several_chars_inserted (GspellCurrentWordPolicy *policy)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       /* If more than one character is inserted, it's probably not a normal
+        * keypress, e.g. a clipboard paste or DND. So it's better to check the
+        * current word in that case, to know ASAP if the word is correctly
+        * spelled.
+        */
+       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+}
+
+/* After a text insertion. */
+void
+_gspell_current_word_policy_single_char_inserted (GspellCurrentWordPolicy *policy,
+                                                 gunichar                 ch,
+                                                 gboolean                 empty_selection,
+                                                 gboolean                 at_cursor_pos)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       /* If e.g. a space or punctuation is inserted, we want to check the
+        * current word, since in that case we are not editing the current word.
+        * Maybe a word has been split in two, in which case the word on the
+        * left will anyway be checked, so it's better to know directly whether
+        * the word on the right is correctly spelled as well, so we know if we
+        * need to edit it or not.
+        * If there is a selection, it means that the text was inserted
+        * programmatically, so the user is not editing the current word
+        * manually.
+        */
+       if (g_unichar_isalnum (ch) &&
+           empty_selection &&
+           at_cursor_pos)
+       {
+               _gspell_current_word_policy_set_check_current_word (policy, FALSE);
+       }
+       else
+       {
+               _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+       }
+}
+
+/* Before a text deletion. "start" refers to the start of the deletion. "end"
+ * refers to the end of the deletion.
+ */
+void
+_gspell_current_word_policy_text_deleted (GspellCurrentWordPolicy *policy,
+                                         gboolean                 empty_selection,
+                                         gboolean                 spans_several_lines,
+                                         gboolean                 several_chars,
+                                         gboolean                 cursor_pos_at_start,
+                                         gboolean                 cursor_pos_at_end,
+                                         gboolean                 start_is_inside_word,
+                                         gboolean                 start_ends_word,
+                                         gboolean                 end_is_inside_word,
+                                         gboolean                 end_ends_word)
+{
+       g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
+
+       if (!empty_selection ||
+           spans_several_lines ||
+           several_chars)
+       {
+               _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+       }
+       /* Probably backspace key */
+       else if (cursor_pos_at_end)
+       {
+               if (start_is_inside_word || start_ends_word)
+               {
+                       _gspell_current_word_policy_set_check_current_word (policy, FALSE);
+               }
+               else
+               {
+                       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+               }
+       }
+       /* Probably delete key */
+       else if (cursor_pos_at_start)
+       {
+               if (end_is_inside_word || end_ends_word)
+               {
+                       _gspell_current_word_policy_set_check_current_word (policy, FALSE);
+               }
+               else
+               {
+                       _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+               }
+       }
+       /* Text deleted programmatically */
+       else
+       {
+               _gspell_current_word_policy_set_check_current_word (policy, TRUE);
+       }
+}
diff --git a/gspell/gspell-current-word-policy.h b/gspell/gspell-current-word-policy.h
new file mode 100644
index 0000000..9f386db
--- /dev/null
+++ b/gspell/gspell-current-word-policy.h
@@ -0,0 +1,85 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * 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.1 of the License, 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/>.
+ */
+
+#ifndef GSPELL_CURRENT_WORD_POLICY_H
+#define GSPELL_CURRENT_WORD_POLICY_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GSPELL_TYPE_CURRENT_WORD_POLICY (_gspell_current_word_policy_get_type ())
+G_DECLARE_DERIVABLE_TYPE (GspellCurrentWordPolicy, _gspell_current_word_policy,
+                         GSPELL, CURRENT_WORD_POLICY,
+                         GObject)
+
+struct _GspellCurrentWordPolicyClass
+{
+       GObjectClass parent_class;
+};
+
+G_GNUC_INTERNAL
+GspellCurrentWordPolicy *
+               _gspell_current_word_policy_new                         (void);
+
+G_GNUC_INTERNAL
+gboolean       _gspell_current_word_policy_get_check_current_word      (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_set_check_current_word      (GspellCurrentWordPolicy *policy,
+                                                                        gboolean                 
check_current_word);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_session_cleared             (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_language_changed            (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_checker_changed             (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_cursor_moved                (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_several_chars_inserted      (GspellCurrentWordPolicy *policy);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_single_char_inserted        (GspellCurrentWordPolicy *policy,
+                                                                        gunichar                 ch,
+                                                                        gboolean                 
empty_selection,
+                                                                        gboolean                 
at_cursor_pos);
+
+G_GNUC_INTERNAL
+void           _gspell_current_word_policy_text_deleted                (GspellCurrentWordPolicy *policy,
+                                                                        gboolean                 
empty_selection,
+                                                                        gboolean                 
spans_several_lines,
+                                                                        gboolean                 
several_chars,
+                                                                        gboolean                 
cursor_pos_at_start,
+                                                                        gboolean                 
cursor_pos_at_end,
+                                                                        gboolean                 
start_is_inside_word,
+                                                                        gboolean                 
start_ends_word,
+                                                                        gboolean                 
end_is_inside_word,
+                                                                        gboolean                 
end_ends_word);
+
+G_END_DECLS
+
+#endif /* GSPELL_CURRENT_WORD_POLICY_H */
+
+/* ex:set ts=8 noet: */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 266b24d..f10bd5a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -2,6 +2,7 @@
 gspell/gspell-checker.c
 gspell/gspell-checker-dialog.c
 gspell/gspell-context-menu.c
+gspell/gspell-current-word-policy.c
 gspell/gspell-entry.c
 gspell/gspell-entry-buffer.c
 gspell/gspell-entry-utils.c


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