[gnome-text-editor] spellcheck: add enabled property



commit ffaa80a829ead70aa1583335a7ca8ff573293dba
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jul 1 15:06:08 2021 -0700

    spellcheck: add enabled property

 src/editor-document.c                  |  5 +++++
 src/editor-text-buffer-spell-adapter.c | 41 ++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
---
diff --git a/src/editor-document.c b/src/editor-document.c
index 40f6a85..3359086 100644
--- a/src/editor-document.c
+++ b/src/editor-document.c
@@ -1787,6 +1787,7 @@ void
 _editor_document_attach_actions (EditorDocument *self,
                                  GtkWidget      *widget)
 {
+  g_autoptr(GPropertyAction) check = NULL;
   g_autoptr(GPropertyAction) language = NULL;
   g_autoptr(GSimpleActionGroup) group = NULL;
 
@@ -1794,8 +1795,12 @@ _editor_document_attach_actions (EditorDocument *self,
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
   group = g_simple_action_group_new ();
+
   language = g_property_action_new ("language", self->spell_adapter, "language");
   g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (language));
 
+  check = g_property_action_new ("enabled", self->spell_adapter, "enabled");
+  g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (check));
+
   gtk_widget_insert_action_group (widget, "spelling", G_ACTION_GROUP (group));
 }
diff --git a/src/editor-text-buffer-spell-adapter.c b/src/editor-text-buffer-spell-adapter.c
index 3ff4044..5a36af2 100644
--- a/src/editor-text-buffer-spell-adapter.c
+++ b/src/editor-text-buffer-spell-adapter.c
@@ -66,6 +66,7 @@ enum {
   PROP_0,
   PROP_BUFFER,
   PROP_CHECKER,
+  PROP_ENABLED,
   PROP_LANGUAGE,
   N_PROPS
 };
@@ -330,6 +331,31 @@ editor_text_buffer_spell_adapter_set_buffer (EditorTextBufferSpellAdapter *self,
     }
 }
 
+static void
+editor_text_buffer_spell_adapter_set_enabled (EditorTextBufferSpellAdapter *self,
+                                              gboolean                      enabled)
+{
+  g_assert (EDITOR_IS_TEXT_BUFFER_SPELL_ADAPTER (self));
+
+  enabled = !!enabled;
+
+  if (self->enabled != enabled)
+    {
+      GtkTextIter begin, end;
+
+      self->enabled = enabled;
+
+      if (self->buffer && self->tag && !self->enabled)
+        {
+          gtk_text_buffer_get_bounds (self->buffer, &begin, &end);
+          gtk_text_buffer_remove_tag (self->buffer, self->tag, &begin, &end);
+        }
+
+      editor_text_buffer_spell_adapter_invalidate_all (self);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ENABLED]);
+    }
+}
+
 static void
 editor_text_buffer_spell_adapter_finalize (GObject *object)
 {
@@ -371,6 +397,10 @@ editor_text_buffer_spell_adapter_get_property (GObject    *object,
       g_value_set_object (value, editor_text_buffer_spell_adapter_get_checker (self));
       break;
 
+    case PROP_ENABLED:
+      g_value_set_boolean (value, self->enabled);
+      break;
+
     case PROP_LANGUAGE:
       g_value_set_string (value, editor_text_buffer_spell_adapter_get_language (self));
       break;
@@ -398,6 +428,10 @@ editor_text_buffer_spell_adapter_set_property (GObject      *object,
       editor_text_buffer_spell_adapter_set_checker (self, g_value_get_object (value));
       break;
 
+    case PROP_ENABLED:
+      editor_text_buffer_spell_adapter_set_enabled (self, g_value_get_boolean (value));
+      break;
+
     case PROP_LANGUAGE:
       editor_text_buffer_spell_adapter_set_language (self, g_value_get_string (value));
       break;
@@ -431,6 +465,13 @@ editor_text_buffer_spell_adapter_class_init (EditorTextBufferSpellAdapterClass *
                          EDITOR_TYPE_SPELL_CHECKER,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_ENABLED] =
+    g_param_spec_boolean ("enabled",
+                          "Enabled",
+                          "If spellcheck is enabled",
+                          TRUE,
+                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_LANGUAGE] =
     g_param_spec_string ("language",
                          "Language",


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