[gnote] Add action callback support to note addin



commit 4a3504b333c32b05bb46dcc4cc78e3f6e321348c
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Jan 2 15:26:56 2016 +0200

    Add action callback support to note addin

 src/noteaddin.cpp |   35 +++++++++++++++++++++++++++++++++++
 src/noteaddin.hpp |    7 +++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/src/noteaddin.cpp b/src/noteaddin.cpp
index 13e62a0..d79af9c 100644
--- a/src/noteaddin.cpp
+++ b/src/noteaddin.cpp
@@ -22,6 +22,7 @@
 
 #include <glibmm/i18n.h>
 
+#include "debug.hpp"
 #include "noteaddin.hpp"
 #include "notewindow.hpp"
 
@@ -67,6 +68,9 @@ namespace gnote {
     on_note_opened();
     NoteWindow * window = get_window();
 
+    window->signal_foregrounded.connect(sigc::mem_fun(*this, &NoteAddin::on_note_foregrounded));
+    window->signal_backgrounded.connect(sigc::mem_fun(*this, &NoteAddin::on_note_backgrounded));
+
     for(std::list<Gtk::MenuItem*>::const_iterator iter = m_text_menu_items.begin();
         iter != m_text_menu_items.end(); ++iter) {
       Gtk::Widget *item = *iter;
@@ -88,6 +92,32 @@ namespace gnote {
     }
   }
 
+  void NoteAddin::on_note_foregrounded()
+  {
+    auto host = get_window()->host();
+    if(!host) {
+      return;
+    }
+
+    for(auto & callback : m_action_callbacks) {
+      auto action = host->find_action(callback.first);
+      if(action) {
+        m_action_callbacks_cids.push_back(action->signal_activate().connect(callback.second));
+      }
+      else {
+        ERR_OUT("Action %s not found!", callback.first.c_str());
+      }
+    }
+  }
+
+  void NoteAddin::on_note_backgrounded()
+  {
+    for(auto cid : m_action_callbacks_cids) {
+      cid.disconnect();
+    }
+    m_action_callbacks_cids.clear();
+  }
+
   void NoteAddin::add_tool_item (Gtk::ToolItem *item, int position)
   {
     if (is_disposing())
@@ -131,5 +161,10 @@ namespace gnote {
   {
     return std::map<int, Gtk::Widget*>();
   }
+
+  void NoteAddin::register_main_window_action_callback(const Glib::ustring & action, sigc::slot<void, const 
Glib::VariantBase&> callback)
+  {
+    m_action_callbacks.emplace_back(action, callback);
+  }
   
 }
diff --git a/src/noteaddin.hpp b/src/noteaddin.hpp
index 1a48e07..7cfb4ad 100644
--- a/src/noteaddin.hpp
+++ b/src/noteaddin.hpp
@@ -75,6 +75,7 @@ public:
   virtual void on_note_opened () = 0;
 
   virtual std::map<int, Gtk::Widget*> get_actions_popover_widgets() const;
+  void register_main_window_action_callback(const Glib::ustring & action, sigc::slot<void, const 
Glib::VariantBase&> callback);
 
   const Note::Ptr & get_note() const
     {
@@ -111,11 +112,17 @@ public:
   void add_tool_item (Gtk::ToolItem *item, int position);
   void add_text_menu_item (Gtk::MenuItem * item);
 private:
+  void on_note_foregrounded();
+  void on_note_backgrounded();
+
   Note::Ptr                     m_note;
   sigc::connection              m_note_opened_cid;
   std::list<Gtk::MenuItem*>     m_text_menu_items;
   typedef std::map<Gtk::ToolItem*, int> ToolItemMap;
   ToolItemMap                   m_toolbar_items;
+  typedef std::pair<Glib::ustring, sigc::slot<void, const Glib::VariantBase&>> ActionCallback;
+  std::vector<ActionCallback>   m_action_callbacks;
+  std::vector<sigc::connection> m_action_callbacks_cids;
 };
 
 


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