[gspell/wip/text-buffer-class] text-buffer: have a real class



commit 44407ccbc53cadded05e9e886a7189248c9cebd1
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Mar 11 10:57:40 2016 +0100

    text-buffer: have a real class
    
    The API will be better with a real class. And having the spell-checker
    property (with the notify signal) is cleaner than the BufferNotifier
    singleton hack. (and the notify signal is available to apps, while the
    BufferNotifier was private).

 gspell/gspell-text-buffer.c |  235 +++++++++++++++++++++++++++++++++++--------
 gspell/gspell-text-buffer.h |   21 +++-
 2 files changed, 210 insertions(+), 46 deletions(-)
---
diff --git a/gspell/gspell-text-buffer.c b/gspell/gspell-text-buffer.c
index f03505d..fa9ab35 100644
--- a/gspell/gspell-text-buffer.c
+++ b/gspell/gspell-text-buffer.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of gspell, a spell-checking library.
  *
- * Copyright 2015 - Sébastien Wilmet
+ * Copyright 2015, 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
@@ -18,77 +18,228 @@
  */
 
 #include "gspell-text-buffer.h"
-#include "gspell-buffer-notifier.h"
 
 /**
  * SECTION:text-buffer
- * @Title: GtkTextBuffer support
- * @See_also: #GspellChecker
+ * @Title: GspellTextBuffer
  *
  * Spell checking support for #GtkTextBuffer.
  */
 
-#define SPELL_CHECKER_KEY "gspell-text-buffer-spell-checker-key"
+struct _GspellTextBuffer
+{
+       GObject parent;
+
+       GtkTextBuffer *buffer;
+       GspellChecker *spell_checker;
+};
+
+enum
+{
+       PROP_0,
+       PROP_BUFFER,
+       PROP_SPELL_CHECKER,
+};
+
+#define GSPELL_TEXT_BUFFER_KEY "gspell-text-buffer-key"
+
+G_DEFINE_TYPE (GspellTextBuffer, gspell_text_buffer, G_TYPE_OBJECT)
+
+static void
+gspell_text_buffer_get_property (GObject    *object,
+                                guint       prop_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+       GspellTextBuffer *buffer = GSPELL_TEXT_BUFFER (object);
+
+       switch (prop_id)
+       {
+               case PROP_BUFFER:
+                       g_value_set_object (value, gspell_text_buffer_get_buffer (buffer));
+                       break;
+
+               case PROP_SPELL_CHECKER:
+                       g_value_set_object (value, gspell_text_buffer_get_spell_checker (buffer));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gspell_text_buffer_set_property (GObject      *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+       GspellTextBuffer *buffer = GSPELL_TEXT_BUFFER (object);
+
+       switch (prop_id)
+       {
+               case PROP_BUFFER:
+                       g_assert (buffer->buffer == NULL);
+                       buffer->buffer = g_value_get_object (value);
+                       break;
+
+               case PROP_SPELL_CHECKER:
+                       gspell_text_buffer_set_spell_checker (buffer, g_value_get_object (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gspell_text_buffer_dispose (GObject *object)
+{
+       GspellTextBuffer *buffer = GSPELL_TEXT_BUFFER (object);
+
+       g_clear_object (&buffer->spell_checker);
+
+       G_OBJECT_CLASS (gspell_text_buffer_parent_class)->dispose (object);
+}
+
+static void
+gspell_text_buffer_class_init (GspellTextBufferClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->get_property = gspell_text_buffer_get_property;
+       object_class->set_property = gspell_text_buffer_set_property;
+       object_class->dispose = gspell_text_buffer_dispose;
+
+       /**
+        * GspellTextBuffer:buffer:
+        *
+        * The #GtkTextBuffer.
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_BUFFER,
+                                        g_param_spec_object ("buffer",
+                                                             "Buffer",
+                                                             "",
+                                                             GTK_TYPE_TEXT_BUFFER,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       /**
+        * GspellTextBuffer:spell-checker:
+        *
+        * The #GspellChecker.
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_SPELL_CHECKER,
+                                        g_param_spec_object ("spell-checker",
+                                                             "Spell Checker",
+                                                             "",
+                                                             GSPELL_TYPE_CHECKER,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gspell_text_buffer_init (GspellTextBuffer *buffer)
+{
+}
+
+static GspellTextBuffer *
+gspell_text_buffer_new (GtkTextBuffer *gtk_buffer)
+{
+       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (gtk_buffer), NULL);
+
+       return g_object_new (GSPELL_TYPE_TEXT_BUFFER,
+                            "buffer", gtk_buffer,
+                            NULL);
+}
 
 /**
- * gspell_text_buffer_set_spell_checker:
- * @buffer: a #GtkTextBuffer.
- * @checker: (nullable): a #GspellChecker, or %NULL to unset the spell checker.
+ * gspell_text_buffer_get_from_buffer:
+ * @gtk_buffer: a #GtkTextBuffer.
  *
- * Associates a spell checker to a #GtkTextBuffer. The @buffer will own a
- * reference to @checker, so you can release your reference to @checker if you
- * no longer need it.
+ * Returns the #GspellTextBuffer of @gtk_buffer. The returned object is
+ * guaranteed to be the same for the lifetime of @gtk_buffer.
+ *
+ * Returns: (transfer none): the #GspellTextBuffer of @gtk_buffer.
  */
-void
-gspell_text_buffer_set_spell_checker (GtkTextBuffer *buffer,
-                                     GspellChecker *checker)
+GspellTextBuffer *
+gspell_text_buffer_get_from_buffer (GtkTextBuffer *gtk_buffer)
 {
-       GspellBufferNotifier *notifier;
+       GspellTextBuffer *gspell_buffer;
 
-       g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-       g_return_if_fail (checker == NULL || GSPELL_IS_CHECKER (checker));
+       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (gtk_buffer), NULL);
 
-       if (checker != NULL)
+       gspell_buffer = g_object_get_data (G_OBJECT (gtk_buffer), GSPELL_TEXT_BUFFER_KEY);
+       if (gspell_buffer != NULL)
        {
-               g_object_set_data_full (G_OBJECT (buffer),
-                                       SPELL_CHECKER_KEY,
-                                       g_object_ref (checker),
-                                       g_object_unref);
-       }
-       else
-       {
-               g_object_set_data (G_OBJECT (buffer),
-                                  SPELL_CHECKER_KEY,
-                                  NULL);
+               g_return_val_if_fail (GSPELL_IS_TEXT_BUFFER (gspell_buffer), NULL);
+               return gspell_buffer;
        }
 
-       notifier = _gspell_buffer_notifier_get_instance ();
-       _gspell_buffer_notifier_text_buffer_checker_changed (notifier, buffer, checker);
+       gspell_buffer = gspell_text_buffer_new (gtk_buffer);
+       g_object_set_data_full (G_OBJECT (gtk_buffer),
+                               GSPELL_TEXT_BUFFER_KEY,
+                               gspell_buffer,
+                               g_object_unref);
+
+       return gspell_buffer;
+}
+
+/**
+ * gspell_text_buffer_get_buffer:
+ * @gspell_buffer: a #GspellTextBuffer.
+ *
+ * Returns: (transfer none): the #GtkTextBuffer of @gspell_buffer.
+ */
+GtkTextBuffer *
+gspell_text_buffer_get_buffer (GspellTextBuffer *gspell_buffer)
+{
+       g_return_val_if_fail (GSPELL_IS_TEXT_BUFFER (gspell_buffer), NULL);
+
+       return gspell_buffer->buffer;
 }
 
 /**
  * gspell_text_buffer_get_spell_checker:
- * @buffer: a #GtkTextBuffer.
+ * @buffer: a #GspellTextBuffer.
  *
- * Returns: (nullable) (transfer none): the associated #GspellChecker if one has
- * been set, or %NULL.
+ * Returns: (nullable) (transfer none): the #GspellChecker if one has been set,
+ *   or %NULL.
  */
 GspellChecker *
-gspell_text_buffer_get_spell_checker (GtkTextBuffer *buffer)
+gspell_text_buffer_get_spell_checker (GspellTextBuffer *buffer)
 {
-       gpointer data;
+       g_return_val_if_fail (GSPELL_IS_TEXT_BUFFER (buffer), NULL);
 
-       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+       return buffer->spell_checker;
+}
 
-       data = g_object_get_data (G_OBJECT (buffer), SPELL_CHECKER_KEY);
+/**
+ * gspell_text_buffer_set_spell_checker:
+ * @buffer: a #GspellTextBuffer.
+ * @spell_checker: (nullable): a #GspellChecker, or %NULL to unset the spell
+ *   checker.
+ *
+ * Sets a #GspellChecker to a #GspellTextBuffer. The @buffer will own a
+ * reference to @spell_checker, so you can release your reference to
+ * @spell_checker if you no longer need it.
+ */
+void
+gspell_text_buffer_set_spell_checker (GspellTextBuffer *buffer,
+                                     GspellChecker    *spell_checker)
+{
+       g_return_if_fail (GSPELL_IS_TEXT_BUFFER (buffer));
+       g_return_if_fail (spell_checker == NULL || GSPELL_IS_CHECKER (spell_checker));
 
-       if (data == NULL)
+       if (g_set_object (&buffer->spell_checker, spell_checker))
        {
-               return NULL;
+               g_object_notify (G_OBJECT (buffer), "spell-checker");
        }
-
-       g_return_val_if_fail (GSPELL_IS_CHECKER (data), NULL);
-       return data;
 }
 
 /* ex:set ts=8 noet: */
diff --git a/gspell/gspell-text-buffer.h b/gspell/gspell-text-buffer.h
index 7d72ad8..564dbc7 100644
--- a/gspell/gspell-text-buffer.h
+++ b/gspell/gspell-text-buffer.h
@@ -1,7 +1,7 @@
 /*
  * This file is part of gspell, a spell-checking library.
  *
- * Copyright 2015 - Sébastien Wilmet
+ * Copyright 2015, 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
@@ -27,10 +27,23 @@
 #include <gspell/gspell-checker.h>
 #include <gtk/gtk.h>
 
-void           gspell_text_buffer_set_spell_checker    (GtkTextBuffer *buffer,
-                                                        GspellChecker *checker);
+G_BEGIN_DECLS
 
-GspellChecker *        gspell_text_buffer_get_spell_checker    (GtkTextBuffer *buffer);
+#define GSPELL_TYPE_TEXT_BUFFER (gspell_text_buffer_get_type ())
+G_DECLARE_FINAL_TYPE (GspellTextBuffer, gspell_text_buffer,
+                     GSPELL, TEXT_BUFFER,
+                     GObject)
+
+GspellTextBuffer *     gspell_text_buffer_get_from_buffer      (GtkTextBuffer *gtk_buffer);
+
+GtkTextBuffer *                gspell_text_buffer_get_buffer           (GspellTextBuffer *gspell_buffer);
+
+GspellChecker *                gspell_text_buffer_get_spell_checker    (GspellTextBuffer *buffer);
+
+void                   gspell_text_buffer_set_spell_checker    (GspellTextBuffer *buffer,
+                                                                GspellChecker    *spell_checker);
+
+G_END_DECLS
 
 #endif /* __GSPELL_TEXT_BUFFER_H__ */
 


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