[gnote] Make spell-check appear in popover



commit 841e43c9568b9c1f6967606e7ccd23a9824ad3ab
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Dec 12 20:22:59 2015 +0200

    Make spell-check appear in popover

 src/actionmanager.cpp |    1 +
 src/watchers.cpp      |   56 ++++++++++++++++++++++++++++++++++++++----------
 src/watchers.hpp      |   11 +++++++--
 3 files changed, 53 insertions(+), 15 deletions(-)
---
diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp
index 98e6dfc..45f1ffd 100644
--- a/src/actionmanager.cpp
+++ b/src/actionmanager.cpp
@@ -76,6 +76,7 @@ namespace gnote {
     register_main_window_action("close-window", NULL);
     register_main_window_action("delete-note", NULL);
     register_main_window_action("important-note", &Glib::Variant<bool>::variant_type());
+    register_main_window_action("enable-spell-check", &Glib::Variant<bool>::variant_type());
   }
 
 
diff --git a/src/watchers.cpp b/src/watchers.cpp
index 92a3892..2eced44 100644
--- a/src/watchers.cpp
+++ b/src/watchers.cpp
@@ -299,19 +299,29 @@ namespace gnote {
     if(settings->get_boolean(Preferences::ENABLE_SPELLCHECKING)) {
       attach ();
     }
+    else {
+      m_enabled = false;
+    }
+
+    NoteWindow *window = get_note()->get_window();
+    window->signal_foregrounded.connect(sigc::mem_fun(*this, 
&NoteSpellChecker::on_note_window_foregrounded));
+    window->signal_backgrounded.connect(sigc::mem_fun(*this, 
&NoteSpellChecker::on_note_window_backgrounded));
+  }
+
+  std::map<int, Gtk::Widget*> NoteSpellChecker::get_actions_popover_widgets() const
+  {
+    std::map<int, Gtk::Widget*> widgets = NoteAddin::get_actions_popover_widgets();
+    if(m_enabled) {
+      utils::add_item_to_ordered_map(widgets, 800,
+        utils::create_popover_button("win.enable-spell-check", _("Check spelling")));
+    }
+    return widgets;
   }
 
   void NoteSpellChecker::attach ()
   {
     attach_checker();
-
-    m_enable_action = utils::CheckAction::create("EnableSpellCheck");
-    m_enable_action->set_label(_("Check spelling"));
-    m_enable_action->set_tooltip(_("Check spelling in this note"));
-    m_enable_action->checked(get_language() != LANG_DISABLED);
-    m_enable_action->signal_activate()
-      .connect(sigc::mem_fun(*this, &NoteSpellChecker::on_spell_check_enable_action));
-    add_note_action(m_enable_action, 800);
+    get_note()->get_window()->signal_popover_widgets_changed();
   }
 
 
@@ -338,6 +348,10 @@ namespace gnote {
       }
       g_signal_connect(G_OBJECT(m_obj_ptr), "language-changed", G_CALLBACK(language_changed), this);
       gtk_spell_checker_attach(m_obj_ptr, get_window()->editor()->gobj());
+      m_enabled = true;
+    }
+    else {
+      m_enabled = false;
     }
   }
 
@@ -345,8 +359,8 @@ namespace gnote {
   void NoteSpellChecker::detach ()
   {
     detach_checker();
-    get_window()->remove_widget_action("EnableSpellCheck");
-    m_enable_action.reset();
+    m_enabled = false;
+    get_note()->get_window()->signal_popover_widgets_changed();
   }
 
 
@@ -455,13 +469,17 @@ namespace gnote {
     return lang;
   }
 
-  void NoteSpellChecker::on_spell_check_enable_action()
+  void NoteSpellChecker::on_spell_check_enable_action(const Glib::VariantBase & state)
   {
     Tag::Ptr tag = get_language_tag();
     if(tag) {
       get_note()->remove_tag(tag);
     }
-    if(m_enable_action->checked()) {
+    Glib::Variant<bool> new_state = Glib::VariantBase::cast_dynamic<Glib::Variant<bool> >(state);
+    MainWindow *main_window = dynamic_cast<MainWindow*>(get_note()->get_window()->host());
+    MainWindowAction::Ptr enable_action = main_window->find_action("enable-spell-check");
+    enable_action->set_state(new_state);
+    if(new_state.get()) {
       attach_checker();
     }
     else {
@@ -472,6 +490,20 @@ namespace gnote {
       detach_checker();
     }
   }
+
+  void NoteSpellChecker::on_note_window_foregrounded()
+  {
+    MainWindow *win = dynamic_cast<MainWindow*>(get_note()->get_window()->host());
+    MainWindowAction::Ptr enable_action = win->find_action("enable-spell-check");
+    enable_action->change_state(Glib::Variant<bool>::create(m_enabled));
+    m_enable_cid = enable_action->signal_change_state()
+      .connect(sigc::mem_fun(*this, &NoteSpellChecker::on_spell_check_enable_action));
+  }
+
+  void NoteSpellChecker::on_note_window_backgrounded()
+  {
+    m_enable_cid.disconnect();
+  }
 #endif
   
   ////////////////////////////////////////////////////////////////////////
diff --git a/src/watchers.hpp b/src/watchers.hpp
index 5da2c45..92a28a3 100644
--- a/src/watchers.hpp
+++ b/src/watchers.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2010-2014 Aurimas Cernius
+ * Copyright (C) 2010-2015 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -93,12 +93,14 @@ namespace gnote {
     virtual void initialize() override;
     virtual void shutdown() override;
     virtual void on_note_opened() override;
+    virtual std::map<int, Gtk::Widget*> get_actions_popover_widgets() const override;
 
     static bool gtk_spell_available()
       { return true; }
   protected:
     NoteSpellChecker()
       : m_obj_ptr(NULL)
+      , m_enabled(false)
       {}
   private:
     static const char *LANG_PREFIX;
@@ -114,11 +116,14 @@ namespace gnote {
     void on_language_changed(const gchar *lang);
     Tag::Ptr get_language_tag();
     std::string get_language();
-    void on_spell_check_enable_action();
+    void on_note_window_foregrounded();
+    void on_note_window_backgrounded();
+    void on_spell_check_enable_action(const Glib::VariantBase & state);
 
     GtkSpellChecker *m_obj_ptr;
     sigc::connection  m_tag_applied_cid;
-    utils::CheckAction::Ptr m_enable_action;
+    sigc::connection m_enable_cid;
+    bool m_enabled;
   };
 #else
   class NoteSpellChecker 


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