[gspell/wip/inline-checker: 2/3] Create GspellInlineCheckerText class



commit daae9ea1a3e404f00452db770fd0b3ed1797a57b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Dec 29 17:11:25 2015 +0100

    Create GspellInlineCheckerText class
    
    Higher-level class than GspellInlineCheckerTextBuffer.

 docs/reference/Makefile.am                 |    1 +
 docs/reference/gspell-1.0-sections.txt     |   10 +-
 gspell/Makefile.am                         |    6 +-
 gspell/gspell-inline-checker-text-buffer.c |   39 +-----
 gspell/gspell-inline-checker-text-buffer.h |   10 +-
 gspell/gspell-inline-checker-text.c        |  232 ++++++++++++++++++++++++++++
 gspell/gspell-inline-checker-text.h        |   50 ++++++
 gspell/gspell.h                            |    2 +-
 po/POTFILES.in                             |    1 +
 tests/test-spell.c                         |   10 +-
 10 files changed, 301 insertions(+), 60 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 9b9aab8..508c04e 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -21,6 +21,7 @@ CFILE_GLOB = $(top_srcdir)/gspell/*.c
 # e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h private_code
 IGNORE_HFILES =                                        \
        gspell-buffer-notifier.h                \
+       gspell-inline-checker-text-buffer.h     \
        gspell-osx.h                            \
        gspell-utils.h                          \
        gtktextregion.h
diff --git a/docs/reference/gspell-1.0-sections.txt b/docs/reference/gspell-1.0-sections.txt
index 26da2d9..878b0f0 100644
--- a/docs/reference/gspell-1.0-sections.txt
+++ b/docs/reference/gspell-1.0-sections.txt
@@ -32,13 +32,11 @@ GSPELL_TYPE_CHECKER_DIALOG
 
 <SECTION>
 <FILE>inline-checker-text</FILE>
-<TITLE>GspellInlineCheckerTextBuffer</TITLE>
-GspellInlineCheckerTextBuffer
-gspell_inline_checker_text_buffer_new
-gspell_inline_checker_text_buffer_attach_view
-gspell_inline_checker_text_buffer_detach_view
+<TITLE>GspellInlineCheckerText</TITLE>
+GspellInlineCheckerText
+gspell_inline_checker_text_new
 <SUBSECTION Standard>
-GSPELL_TYPE_INLINE_CHECKER_TEXT_BUFFER
+GSPELL_TYPE_INLINE_CHECKER_TEXT
 </SECTION>
 
 <SECTION>
diff --git a/gspell/Makefile.am b/gspell/Makefile.am
index 8984c37..2178ee4 100644
--- a/gspell/Makefile.am
+++ b/gspell/Makefile.am
@@ -19,7 +19,7 @@ gspell_public_headers =                               \
        gspell.h                                \
        gspell-checker.h                        \
        gspell-checker-dialog.h                 \
-       gspell-inline-checker-text-buffer.h     \
+       gspell-inline-checker-text.h            \
        gspell-language.h                       \
        gspell-language-chooser.h               \
        gspell-language-chooser-button.h        \
@@ -31,7 +31,7 @@ gspell_public_headers =                               \
 gspell_public_c_files =                                \
        gspell-checker.c                        \
        gspell-checker-dialog.c                 \
-       gspell-inline-checker-text-buffer.c     \
+       gspell-inline-checker-text.c            \
        gspell-language.c                       \
        gspell-language-chooser.c               \
        gspell-language-chooser-button.c        \
@@ -43,12 +43,14 @@ gspell_public_c_files =                             \
 gspell_private_headers =                       \
        gconstructor.h                          \
        gspell-buffer-notifier.h                \
+       gspell-inline-checker-text-buffer.h     \
        gspell-utils.h                          \
        gtktextregion.h
 
 gspell_private_c_files =                       \
        gspell-buffer-notifier.c                \
        gspell-init.c                           \
+       gspell-inline-checker-text-buffer.c     \
        gspell-utils.c                          \
        gtktextregion.c
 
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index dbd055d..3df0bbe 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -26,35 +26,12 @@
 #include "gspell-inline-checker-text-buffer.h"
 #include <string.h>
 #include <glib/gi18n-lib.h>
+#include "gspell-checker.h"
 #include "gspell-buffer-notifier.h"
 #include "gspell-text-buffer.h"
 #include "gspell-utils.h"
 #include "gtktextregion.h"
 
-/**
- * SECTION:inline-checker-text
- * @Short_description: Inline spell checker for GtkTextView
- * @Title: GspellInlineCheckerTextBuffer
- * @See_also: #GspellChecker
- *
- * The #GspellInlineCheckerTextBuffer is an inline spell checker for the
- * #GtkTextView widget. Misspelled words are highlighted with a
- * %PANGO_UNDERLINE_ERROR, usually a red wavy underline. Right-clicking a
- * misspelled word pops up a context menu of suggested replacements. The context
- * menu also contains an “Ignore All” item to add the misspelled word to the
- * session dictionary. And an “Add” item to add the word to the personal
- * dictionary.
- *
- * The spell is checked only on the visible regions of the attached
- * #GtkTextView's.
- *
- * You need to call gspell_text_buffer_set_spell_checker() to associate a
- * #GspellChecker to the #GtkTextBuffer. You can call
- * gspell_text_buffer_set_spell_checker() at any time, a
- * #GspellInlineCheckerTextBuffer re-checks the buffer when the #GspellChecker
- * changes.
- */
-
 struct _GspellInlineCheckerTextBuffer
 {
        GObject parent;
@@ -1129,13 +1106,6 @@ gspell_inline_checker_text_buffer_class_init (GspellInlineCheckerTextBufferClass
        object_class->set_property = gspell_inline_checker_text_buffer_set_property;
        object_class->dispose = gspell_inline_checker_text_buffer_dispose;
 
-       /**
-        * GspellInlineCheckerTextBuffer:buffer:
-        *
-        * The #GtkTextBuffer. If a same buffer is used for several views, the
-        * misspelled words are visible in all views, because #GtkTextTag's are
-        * added to the buffer.
-        */
        g_object_class_install_property (object_class,
                                         PROP_BUFFER,
                                         g_param_spec_object ("buffer",
@@ -1152,12 +1122,6 @@ gspell_inline_checker_text_buffer_init (GspellInlineCheckerTextBuffer *spell)
 {
 }
 
-/**
- * gspell_inline_checker_text_buffer_new:
- * @buffer: a #GtkTextBuffer.
- *
- * Returns: a new #GspellInlineCheckerTextBuffer object.
- */
 GspellInlineCheckerTextBuffer *
 gspell_inline_checker_text_buffer_new (GtkTextBuffer *buffer)
 {
@@ -1236,7 +1200,6 @@ gspell_inline_checker_text_buffer_detach_view (GspellInlineCheckerTextBuffer *sp
 {
        g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));
        g_return_if_fail (GTK_IS_TEXT_VIEW (view));
-       g_return_if_fail (gtk_text_view_get_buffer (view) == spell->buffer);
        g_return_if_fail (g_slist_find (spell->views, view) != NULL);
 
        g_signal_handlers_disconnect_by_data (view, spell);
diff --git a/gspell/gspell-inline-checker-text-buffer.h b/gspell/gspell-inline-checker-text-buffer.h
index 017b0e5..997611e 100644
--- a/gspell/gspell-inline-checker-text-buffer.h
+++ b/gspell/gspell-inline-checker-text-buffer.h
@@ -25,24 +25,24 @@
 #ifndef __GSPELL_INLINE_CHECKER_TEXT_BUFFER_H__
 #define __GSPELL_INLINE_CHECKER_TEXT_BUFFER_H__
 
-#if !defined (__GSPELL_H_INSIDE__) && !defined (GSPELL_COMPILATION)
-#error "Only <gspell/gspell.h> can be included directly."
-#endif
-
-#include <gspell/gspell-checker.h>
 #include <gtk/gtk.h>
 
 #define GSPELL_TYPE_INLINE_CHECKER_TEXT_BUFFER (gspell_inline_checker_text_buffer_get_type ())
+
+G_GNUC_INTERNAL
 G_DECLARE_FINAL_TYPE (GspellInlineCheckerTextBuffer, gspell_inline_checker_text_buffer,
                      GSPELL, INLINE_CHECKER_TEXT_BUFFER,
                      GObject)
 
+G_GNUC_INTERNAL
 GspellInlineCheckerTextBuffer *
        gspell_inline_checker_text_buffer_new           (GtkTextBuffer *buffer);
 
+G_GNUC_INTERNAL
 void   gspell_inline_checker_text_buffer_attach_view   (GspellInlineCheckerTextBuffer *spell,
                                                         GtkTextView                   *view);
 
+G_GNUC_INTERNAL
 void   gspell_inline_checker_text_buffer_detach_view   (GspellInlineCheckerTextBuffer *spell,
                                                         GtkTextView                   *view);
 
diff --git a/gspell/gspell-inline-checker-text.c b/gspell/gspell-inline-checker-text.c
new file mode 100644
index 0000000..37894bd
--- /dev/null
+++ b/gspell/gspell-inline-checker-text.c
@@ -0,0 +1,232 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2015 - 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-inline-checker-text.h"
+#include "gspell-inline-checker-text-buffer.h"
+
+/**
+ * SECTION:inline-checker-text
+ * @Short_description: Inline spell checker for GtkTextView
+ * @Title: GspellInlineCheckerText
+ * @See_also: #GspellChecker
+ *
+ * The #GspellInlineCheckerText is an inline spell checker for the
+ * #GtkTextView widget. Misspelled words are highlighted with a
+ * %PANGO_UNDERLINE_ERROR, usually a red wavy underline. Right-clicking a
+ * misspelled word pops up a context menu of suggested replacements. The context
+ * menu also contains an “Ignore All” item to add the misspelled word to the
+ * session dictionary. And an “Add” item to add the word to the personal
+ * dictionary.
+ *
+ * The spell is checked only on the visible region of the #GtkTextView. Note
+ * that if a same #GtkTextBuffer is used for several views, the misspelled words
+ * are visible in all views, because the highlighting is achieved with a
+ * #GtkTextTag added to the buffer.
+ *
+ * You need to call gspell_text_buffer_set_spell_checker() to associate a
+ * #GspellChecker to the #GtkTextBuffer. You can call
+ * gspell_text_buffer_set_spell_checker() at any time, a
+ * #GspellInlineCheckerText re-checks the buffer when the #GspellChecker
+ * changes.
+ *
+ * #GspellInlineCheckerText supports buffer changes.
+ */
+
+typedef struct _GspellInlineCheckerTextPrivate GspellInlineCheckerTextPrivate;
+
+struct _GspellInlineCheckerTextPrivate
+{
+       GtkTextView *view;
+       GspellInlineCheckerTextBuffer *inline_checker;
+};
+
+enum
+{
+       PROP_0,
+       PROP_VIEW,
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GspellInlineCheckerText, gspell_inline_checker_text, G_TYPE_OBJECT)
+
+static void
+update_inline_checker (GspellInlineCheckerText *self)
+{
+       GspellInlineCheckerTextPrivate *priv;
+       GtkTextBuffer *buffer;
+
+       priv = gspell_inline_checker_text_get_instance_private (self);
+
+       if (priv->view == NULL)
+       {
+               return;
+       }
+
+       if (priv->inline_checker != NULL)
+       {
+               gspell_inline_checker_text_buffer_detach_view (priv->inline_checker,
+                                                              priv->view);
+               g_object_unref (priv->inline_checker);
+       }
+
+       buffer = gtk_text_view_get_buffer (priv->view);
+       priv->inline_checker = gspell_inline_checker_text_buffer_new (buffer);
+       gspell_inline_checker_text_buffer_attach_view (priv->inline_checker,
+                                                      priv->view);
+}
+
+static void
+notify_buffer_cb (GtkTextView             *view,
+                 GParamSpec              *pspec,
+                 GspellInlineCheckerText *self)
+{
+       update_inline_checker (self);
+}
+
+static void
+set_view (GspellInlineCheckerText *self,
+         GtkTextView             *view)
+{
+       GspellInlineCheckerTextPrivate *priv;
+
+       g_return_if_fail (GTK_IS_TEXT_VIEW (view));
+
+       priv = gspell_inline_checker_text_get_instance_private (self);
+
+       g_assert (priv->view == NULL);
+       g_assert (priv->inline_checker == NULL);
+
+       priv->view = g_object_ref (view);
+
+       g_signal_connect_object (priv->view,
+                                "notify::buffer",
+                                G_CALLBACK (notify_buffer_cb),
+                                self,
+                                0);
+
+       update_inline_checker (self);
+}
+
+static void
+gspell_inline_checker_text_get_property (GObject    *object,
+                                        guint       prop_id,
+                                        GValue     *value,
+                                        GParamSpec *pspec)
+{
+       GspellInlineCheckerTextPrivate *priv;
+
+       priv = gspell_inline_checker_text_get_instance_private (GSPELL_INLINE_CHECKER_TEXT (object));
+
+       switch (prop_id)
+       {
+               case PROP_VIEW:
+                       g_value_set_object (value, priv->view);
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gspell_inline_checker_text_set_property (GObject      *object,
+                                        guint         prop_id,
+                                        const GValue *value,
+                                        GParamSpec   *pspec)
+{
+       GspellInlineCheckerText *self = GSPELL_INLINE_CHECKER_TEXT (object);
+
+       switch (prop_id)
+       {
+               case PROP_VIEW:
+                       set_view (self, g_value_get_object (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gspell_inline_checker_text_dispose (GObject *object)
+{
+       GspellInlineCheckerTextPrivate *priv;
+
+       priv = gspell_inline_checker_text_get_instance_private (GSPELL_INLINE_CHECKER_TEXT (object));
+
+       if (priv->view != NULL && priv->inline_checker != NULL)
+       {
+               gspell_inline_checker_text_buffer_detach_view (priv->inline_checker,
+                                                              priv->view);
+       }
+
+       g_clear_object (&priv->view);
+       g_clear_object (&priv->inline_checker);
+
+       G_OBJECT_CLASS (gspell_inline_checker_text_parent_class)->dispose (object);
+}
+
+static void
+gspell_inline_checker_text_class_init (GspellInlineCheckerTextClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->get_property = gspell_inline_checker_text_get_property;
+       object_class->set_property = gspell_inline_checker_text_set_property;
+       object_class->dispose = gspell_inline_checker_text_dispose;
+
+       /**
+        * GspellInlineCheckerText:view:
+        *
+        * The #GtkTextView.
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_VIEW,
+                                        g_param_spec_object ("view",
+                                                             "View",
+                                                             "",
+                                                             GTK_TYPE_TEXT_VIEW,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gspell_inline_checker_text_init (GspellInlineCheckerText *self)
+{
+}
+
+/**
+ * gspell_inline_checker_text_new:
+ * @view: a #GtkTextView.
+ *
+ * Returns: a new #GspellInlineCheckerText object.
+ */
+GspellInlineCheckerText *
+gspell_inline_checker_text_new (GtkTextView *view)
+{
+       g_return_val_if_fail (GTK_IS_TEXT_VIEW (view), NULL);
+
+       return g_object_new (GSPELL_TYPE_INLINE_CHECKER_TEXT,
+                            "view", view,
+                            NULL);
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gspell/gspell-inline-checker-text.h b/gspell/gspell-inline-checker-text.h
new file mode 100644
index 0000000..8a6e456
--- /dev/null
+++ b/gspell/gspell-inline-checker-text.h
@@ -0,0 +1,50 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2015 - 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_INLINE_CHECKER_TEXT_H__
+#define __GSPELL_INLINE_CHECKER_TEXT_H__
+
+#if !defined (__GSPELL_H_INSIDE__) && !defined (GSPELL_COMPILATION)
+#error "Only <gspell/gspell.h> can be included directly."
+#endif
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GSPELL_TYPE_INLINE_CHECKER_TEXT (gspell_inline_checker_text_get_type ())
+G_DECLARE_DERIVABLE_TYPE (GspellInlineCheckerText, gspell_inline_checker_text,
+                         GSPELL, INLINE_CHECKER_TEXT,
+                         GObject)
+
+struct _GspellInlineCheckerTextClass
+{
+       GObjectClass parent_class;
+
+       /* Padding for future expansion */
+       gpointer padding[8];
+};
+
+GspellInlineCheckerText *      gspell_inline_checker_text_new          (GtkTextView *view);
+
+G_END_DECLS
+
+#endif /* __GSPELL_INLINE_CHECKER_TEXT_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/gspell/gspell.h b/gspell/gspell.h
index 1a7e60b..b635a81 100644
--- a/gspell/gspell.h
+++ b/gspell/gspell.h
@@ -24,7 +24,7 @@
 
 #include <gspell/gspell-checker.h>
 #include <gspell/gspell-checker-dialog.h>
-#include <gspell/gspell-inline-checker-text-buffer.h>
+#include <gspell/gspell-inline-checker-text.h>
 #include <gspell/gspell-language.h>
 #include <gspell/gspell-language-chooser.h>
 #include <gspell/gspell-language-chooser-button.h>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 76e7662..656133b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -2,6 +2,7 @@
 gspell/gspell-buffer-notifier.c
 gspell/gspell-checker.c
 gspell/gspell-checker-dialog.c
+gspell/gspell-inline-checker-text.c
 gspell/gspell-inline-checker-text-buffer.c
 gspell/gspell-language.c
 gspell/gspell-language-chooser.c
diff --git a/tests/test-spell.c b/tests/test-spell.c
index feb0e16..3e79e7f 100644
--- a/tests/test-spell.c
+++ b/tests/test-spell.c
@@ -29,7 +29,7 @@ struct _TestSpell
        GtkGrid parent;
 
        GtkTextView *view;
-       GspellInlineCheckerTextBuffer *inline_spell;
+       GspellInlineCheckerText *inline_spell;
 };
 
 G_DEFINE_TYPE (TestSpell, test_spell, GTK_TYPE_GRID)
@@ -87,20 +87,14 @@ highlight_checkbutton_toggled_cb (GtkToggleButton *checkbutton,
 {
        if (gtk_toggle_button_get_active (checkbutton))
        {
-               GtkTextBuffer *buffer;
-
                g_assert (spell->inline_spell == NULL);
 
-               buffer = gtk_text_view_get_buffer (spell->view);
-
                /* A real application needs to check if
                 * gspell_checker_get_language() != NULL. If it is NULL, the
                 * inline spell checker should not be created and a warning
                 * should be printed to say that no dictionaries are available.
                 */
-               spell->inline_spell = gspell_inline_checker_text_buffer_new (buffer);
-
-               gspell_inline_checker_text_buffer_attach_view (spell->inline_spell, spell->view);
+               spell->inline_spell = gspell_inline_checker_text_new (spell->view);
        }
        else
        {


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