[gnote] Update fixed width addin for popover text menu



commit 036d7218453fd38c24ccd3e827425300eddf5edb
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Mar 12 23:11:22 2016 +0200

    Update fixed width addin for popover text menu

 src/addins/fixedwidth/Makefile.am              |    1 -
 src/addins/fixedwidth/fixedwidth.desktop.in.in |    4 +-
 src/addins/fixedwidth/fixedwidthmenuitem.cpp   |   87 ------------------------
 src/addins/fixedwidth/fixedwidthmenuitem.hpp   |   61 -----------------
 src/addins/fixedwidth/fixedwidthnoteaddin.cpp  |   68 +++++++++++++++++--
 src/addins/fixedwidth/fixedwidthnoteaddin.hpp  |   10 +++-
 6 files changed, 75 insertions(+), 156 deletions(-)
---
diff --git a/src/addins/fixedwidth/Makefile.am b/src/addins/fixedwidth/Makefile.am
index 88bb306..11849ea 100644
--- a/src/addins/fixedwidth/Makefile.am
+++ b/src/addins/fixedwidth/Makefile.am
@@ -13,7 +13,6 @@ addins_DATA = $(desktop_files)
 
 libfixedwidth_la_SOURCES = fixedwidthnoteaddin.hpp fixedwidthnoteaddin.cpp \
        fixedwidthtag.hpp \
-       fixedwidthmenuitem.hpp fixedwidthmenuitem.cpp \
        $(NULL)
 
 EXTRA_DIST = $(desktop_in_files)
diff --git a/src/addins/fixedwidth/fixedwidth.desktop.in.in b/src/addins/fixedwidth/fixedwidth.desktop.in.in
index 80fac32..6d8cdac 100644
--- a/src/addins/fixedwidth/fixedwidth.desktop.in.in
+++ b/src/addins/fixedwidth/fixedwidth.desktop.in.in
@@ -4,8 +4,10 @@ _Name=Fixed Width
 _Description=Adds fixed-width font style.
 _Authors=Hubert Figuiere and the Tomboy Project
 Category=Formatting
-Version=0.3
+Version=0.4
 DefaultEnabled=true
 Module=libfixedwidth
 LibgnoteRelease= LIBGNOTE_RELEASE@
 LibgnoteVersionInfo= LIBGNOTE_VERSION_INFO@
+[Actions]
+actions_bool=fixedwidth-enable
diff --git a/src/addins/fixedwidth/fixedwidthnoteaddin.cpp b/src/addins/fixedwidth/fixedwidthnoteaddin.cpp
index a149065..6c49c40 100644
--- a/src/addins/fixedwidth/fixedwidthnoteaddin.cpp
+++ b/src/addins/fixedwidth/fixedwidthnoteaddin.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2010,2013 Aurimas Cernius
+ * Copyright (C) 2010,2013,2016 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * Original C# file
  * (C) 2006 Ryan Lortie <desrt desrt ca>
@@ -25,8 +25,11 @@
 // Add a 'fixed width' item to the font styles menu.
 
 
+#include <glibmm/i18n.h>
+
 #include "sharp/modulefactory.hpp"
-#include "fixedwidthmenuitem.hpp"
+#include "notewindow.hpp"
+#include "utils.hpp"
 #include "fixedwidthnoteaddin.hpp"
 #include "fixedwidthtag.hpp"
 
@@ -48,6 +51,13 @@ namespace fixedwidth {
                                get_note()->get_tag_table()->add (m_tag);
                        }
 
+    auto button = gnote::utils::create_popover_button("win.fixedwidth-enable", "");
+    auto label = dynamic_cast<Gtk::Label*>(dynamic_cast<Gtk::Bin*>(button)->get_child());
+    Glib::ustring lbl = "<tt>";
+    lbl += _("Fixed Wid_th");
+    lbl += "</tt>";
+    label->set_markup_with_mnemonic(lbl);
+    add_text_menu_item(button);
   }
 
 
@@ -62,11 +72,59 @@ namespace fixedwidth {
 
   void FixedWidthNoteAddin::on_note_opened ()
   {
-    // Add here instead of in Initialize to avoid creating unopened
-    // notes' windows/buffers.
-    add_text_menu_item (new FixedWidthMenuItem (this));
+    get_window()->text_menu()->signal_show().connect(
+      sigc::mem_fun(*this, &FixedWidthNoteAddin::menu_shown));
+    dynamic_cast<gnote::NoteTextMenu*>(get_window()->text_menu())->signal_set_accels
+      .connect(sigc::mem_fun(*this, &FixedWidthNoteAddin::set_accels));
+
+    gnote::NoteWindow *note_window = get_window();
+    note_window->signal_foregrounded.connect(
+      sigc::mem_fun(*this, &FixedWidthNoteAddin::on_note_foregrounded));
+    note_window->signal_backgrounded.connect(
+      sigc::mem_fun(*this, &FixedWidthNoteAddin::on_note_backgrounded));
+  }
+
+  void FixedWidthNoteAddin::menu_shown()
+  {
+    auto host = get_window()->host();
+    if(host == NULL) {
+      return;
+    }
+
+    host->find_action("fixedwidth-enable")->set_state(
+      Glib::Variant<bool>::create(get_buffer()->is_active_tag ("monospace")));
+  }
+
+
+  void FixedWidthNoteAddin::on_note_foregrounded()
+  {
+    m_menu_item_cid = get_window()->host()->find_action("fixedwidth-enable")->signal_change_state()
+      .connect(sigc::mem_fun(*this, &FixedWidthNoteAddin::on_menu_item_state_changed));
+  }
+
+
+  void FixedWidthNoteAddin::on_note_backgrounded()
+  {
+    m_menu_item_cid.disconnect();
+  }
+
+  void FixedWidthNoteAddin::on_menu_item_state_changed(const Glib::VariantBase & state)
+  {
+    get_window()->host()->find_action("fixedwidth-enable")->set_state(state);
+    on_accel();
   }
 
+  void FixedWidthNoteAddin::set_accels(const gnote::utils::GlobalKeybinder & keybinder)
+  {
+    const_cast<gnote::utils::GlobalKeybinder&>(keybinder).add_accelerator(
+      sigc::mem_fun(*this, &FixedWidthNoteAddin::on_accel),
+      GDK_KEY_T, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
+  }
+
+  void FixedWidthNoteAddin::on_accel()
+  {
+    get_buffer()->toggle_active_tag("monospace");
+  }
 
 
 }
diff --git a/src/addins/fixedwidth/fixedwidthnoteaddin.hpp b/src/addins/fixedwidth/fixedwidthnoteaddin.hpp
index 1f8ef1d..e35ca05 100644
--- a/src/addins/fixedwidth/fixedwidthnoteaddin.hpp
+++ b/src/addins/fixedwidth/fixedwidthnoteaddin.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2010,2013 Aurimas Cernius
+ * Copyright (C) 2010,2013,2016 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * Original C# file
  * (C) 2006 Ryan Lortie <desrt desrt ca>
@@ -61,7 +61,15 @@ namespace fixedwidth {
     virtual void shutdown() override;
     virtual void on_note_opened() override;
   private:
+    void menu_shown();
+    void on_note_foregrounded();
+    void on_note_backgrounded();
+    void on_menu_item_state_changed(const Glib::VariantBase & state);
+    void set_accels(const gnote::utils::GlobalKeybinder & keybinder);
+    void on_accel();
+
     Glib::RefPtr<Gtk::TextTag> m_tag;
+    sigc::connection           m_menu_item_cid;
   };
 
 


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