[niepce] use a Gtk::Stack



commit ace384bdf3cdad42e05a9d96cfc090de6d296091
Author: Hubert Figuière <hub figuiere net>
Date:   Wed Oct 29 23:00:27 2014 -0400

    use a Gtk::Stack
    
    Make sure m_widget is cleared.
    
    Require gtkmm 3.14 for Gtk+ 3.14

 configure.ac                        |    2 +-
 src/niepce/ui/gridviewmodule.cpp    |    5 +++
 src/niepce/ui/gridviewmodule.hpp    |    2 +-
 src/niepce/ui/moduleshell.cpp       |   44 ++++++++++++++---------
 src/niepce/ui/moduleshell.hpp       |    9 +++--
 src/niepce/ui/moduleshellwidget.cpp |   67 ++++++++++-------------------------
 src/niepce/ui/moduleshellwidget.hpp |   22 ++++++-----
 7 files changed, 70 insertions(+), 81 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f31c5b7..1645962 100644
--- a/configure.ac
+++ b/configure.ac
@@ -13,7 +13,7 @@ dnl all the library version.
 dnl if one is harcoded elsewhere, it is a bug
 LIBGIOMM_VERSION=2.16
 LIBGLIBMM_VERSION=2.32
-LIBGTKMM_VERSION=3.12
+LIBGTKMM_VERSION=3.14
 EXEMPI_VERSION=2.2.0
 SQLITE_VERSION=3.0
 GEGL_VERSION=0.2.0
diff --git a/src/niepce/ui/gridviewmodule.cpp b/src/niepce/ui/gridviewmodule.cpp
index ba8dcd5..989ff7d 100644
--- a/src/niepce/ui/gridviewmodule.cpp
+++ b/src/niepce/ui/gridviewmodule.cpp
@@ -48,6 +48,11 @@ GridViewModule::GridViewModule(const IModuleShell & shell,
     DBG_ASSERT(m_uidataprovider, "provider is NULL");
 }
 
+GridViewModule::~GridViewModule()
+{
+    m_widget = nullptr;
+}
+
 void
 GridViewModule::on_lib_notification(const eng::LibNotification &ln)
 {
diff --git a/src/niepce/ui/gridviewmodule.hpp b/src/niepce/ui/gridviewmodule.hpp
index 091d7ea..e848f8b 100644
--- a/src/niepce/ui/gridviewmodule.hpp
+++ b/src/niepce/ui/gridviewmodule.hpp
@@ -57,7 +57,7 @@ public:
 
   GridViewModule(const IModuleShell & shell,
                  const Glib::RefPtr<ImageListStore> & store);
-
+  virtual ~GridViewModule();
 
   void on_lib_notification(const eng::LibNotification &);
   void display_none();
diff --git a/src/niepce/ui/moduleshell.cpp b/src/niepce/ui/moduleshell.cpp
index 5631fa9..b592077 100644
--- a/src/niepce/ui/moduleshell.cpp
+++ b/src/niepce/ui/moduleshell.cpp
@@ -36,6 +36,11 @@
 
 namespace ui {
 
+ModuleShell::~ModuleShell()
+{
+    m_widget = nullptr;
+}
+
 Gtk::Widget * ModuleShell::buildWidget()
 {
     if(m_widget) {
@@ -193,7 +198,7 @@ Gtk::Widget * ModuleShell::buildWidget()
 
     m_gridview = GridViewModule::Ptr(
         new GridViewModule(*this, m_selection_controller->get_list_store()));
-    add_library_module(m_gridview, _("Library"));
+    add_library_module(m_gridview, "grid", _("Library"));
 
     m_selection_controller->add_selectable(m_gridview);
     m_selection_controller->signal_selected
@@ -202,10 +207,10 @@ Gtk::Widget * ModuleShell::buildWidget()
         .connect(sigc::mem_fun(*this, &ModuleShell::on_image_activated));
 
     m_darkroom = dr::DarkroomModule::Ptr(new dr::DarkroomModule(*this));
-    add_library_module(m_darkroom, _("Darkroom"));
+    add_library_module(m_darkroom, "darkroom", _("Darkroom"));
 
     m_mapm = mapm::MapModule::Ptr(new mapm::MapModule(*this));
-    add_library_module(m_mapm, _("Map"));
+    add_library_module(m_mapm, "map", _("Map"));
 
     m_shell.signal_activated.connect(sigc::mem_fun(*this, &ModuleShell::on_module_activated));
     m_shell.signal_deactivated.connect(sigc::mem_fun(*this, &ModuleShell::on_module_deactivated));
@@ -222,13 +227,14 @@ void ModuleShell::action_edit_delete()
 }
 
 void ModuleShell::add_library_module(const ILibraryModule::Ptr & module,
-                                                   const std::string & label)
+                                     const std::string & name,
+                                     const std::string & label)
 {
     auto w = module->buildWidget();
     if(w) {
         add(module);
-        m_shell.append_page(*w, label);
-        m_modules.push_back(module);
+        m_shell.appendPage(*w, name, label);
+        m_modules.insert(std::make_pair(name, module));
     }
 }
 
@@ -256,25 +262,29 @@ void ModuleShell::on_image_activated(eng::library_id_t id)
     if(iter) {
         auto libfile = (*iter)[store->columns().m_libfile];
         m_darkroom->set_image(libfile);
-        m_shell.activate_page(1);
+        m_shell.activatePage("darkroom");
     }
 }
 
-void ModuleShell::on_module_deactivated(int idx)
+void ModuleShell::on_module_deactivated(const std::string & name) const
 {
-    DBG_ASSERT((idx >= 0) && ((unsigned)idx < m_modules.size()), "wrong module index");
-    m_module_menu->remove_all();
-    m_modules[idx]->set_active(false);
+    auto module = m_modules.find(name);
+    if (module != m_modules.end()) {
+        m_module_menu->remove_all();
+        module->second->set_active(false);
+    }
 }
 
-void ModuleShell::on_module_activated(int idx)
+void ModuleShell::on_module_activated(const std::string & name) const
 {
-    DBG_ASSERT((idx >= 0) && ((unsigned)idx < m_modules.size()), "wrong module index");
-    auto menu = m_modules[idx]->getMenu();
-    if (menu) {
-        m_module_menu->append_section(menu);
+    auto module = m_modules.find(name);
+    if (module != m_modules.end()) {
+        auto menu = module->second->getMenu();
+        if (menu) {
+            m_module_menu->append_section(menu);
+        }
+        module->second->set_active(true);
     }
-    m_modules[idx]->set_active(true);
 }
 
 
diff --git a/src/niepce/ui/moduleshell.hpp b/src/niepce/ui/moduleshell.hpp
index f59a5c5..124643b 100644
--- a/src/niepce/ui/moduleshell.hpp
+++ b/src/niepce/ui/moduleshell.hpp
@@ -54,7 +54,7 @@ public:
         , m_actionGroup(Gio::SimpleActionGroup::create())
         {
         }
-
+    virtual ~ModuleShell();
 
     const GridViewModule::Ptr & get_gridview() const
         {
@@ -89,10 +89,11 @@ public:
     void action_edit_delete();
 protected:
     virtual void add_library_module(const ILibraryModule::Ptr & module,
+                                    const std::string & name,
                                     const std::string & label);
     virtual void on_ready();
-    void on_module_deactivated(int idx);
-    void on_module_activated(int idx);
+    void on_module_deactivated(const std::string & name) const;
+    void on_module_activated(const std::string & name) const;
 private:
     libraryclient::LibraryClient::Ptr m_libraryclient;
     Glib::RefPtr<Gio::SimpleActionGroup> m_actionGroup;
@@ -103,7 +104,7 @@ private:
     Glib::RefPtr<Gio::Menu>       m_module_menu;
 
     ui::SelectionController::Ptr  m_selection_controller;
-    std::vector<ILibraryModule::Ptr> m_modules;
+    std::map<std::string, ILibraryModule::Ptr> m_modules;
     // these should be dynamic
     GridViewModule::Ptr           m_gridview;
     dr::DarkroomModule::Ptr       m_darkroom;
diff --git a/src/niepce/ui/moduleshellwidget.cpp b/src/niepce/ui/moduleshellwidget.cpp
index db484c2..12abbd9 100644
--- a/src/niepce/ui/moduleshellwidget.cpp
+++ b/src/niepce/ui/moduleshellwidget.cpp
@@ -18,6 +18,7 @@
  */
 
 #include <gtkmm/togglebutton.h>
+#include <gtkmm/stackswitcher.h>
 
 #include "fwk/base/debug.hpp"
 #include "ui/moduleshellwidget.hpp"
@@ -28,69 +29,39 @@ ModuleShellWidget::ModuleShellWidget()
     : Gtk::Box(Gtk::ORIENTATION_VERTICAL)
     , m_mainbox(Gtk::ORIENTATION_HORIZONTAL)
     , m_mainbar(Gtk::ORIENTATION_HORIZONTAL)
-    , m_currentpage(-1)
 {
     set_spacing(4);
     m_mainbar.set_layout(Gtk::BUTTONBOX_START);
     m_mainbar.set_spacing(4);
     m_menubutton.set_direction(Gtk::ARROW_NONE);
-    m_notebook.set_show_tabs(false);
     m_mainbox.pack_end(m_menubutton, Gtk::PACK_SHRINK);
     m_mainbox.pack_start(m_mainbar, Gtk::PACK_EXPAND_WIDGET);
     pack_start(m_mainbox, Gtk::PACK_SHRINK);
-    pack_start(m_notebook);
-}
 
-int
-ModuleShellWidget::append_page(Gtk::Widget & w, const Glib::ustring & label)
-{
-    int idx;
-    
-    Gtk::ToggleButton* button = Gtk::manage(new Gtk::ToggleButton(label));
-    m_mainbar.pack_start(*button);
-    idx = m_notebook.append_page(w, label);
-    sigc::connection conn = button->signal_toggled().connect(
-        sigc::bind(sigc::mem_fun(this, &ModuleShellWidget::set_current_page),
-                   idx, button));
-    if(m_currentpage == -1) {
-        set_current_page(idx, button);
-    }
-    if((int)m_buttons.size() < idx + 1) {
-        m_buttons.resize(idx + 1);
-    }
-    m_buttons[idx] = std::make_pair(button, conn);
-    return idx;
+    m_mainbox.pack_start(m_switcher);
+    pack_start(m_stack);
+
+    m_switcher.set_stack(m_stack);
 }
-       
-void ModuleShellWidget::activate_page(int idx)
+
+
+
+void
+ModuleShellWidget::appendPage(Gtk::Widget & w, const Glib::ustring & name,
+                              const Glib::ustring & label)
 {
-    if(m_currentpage != idx) {
-        Gtk::ToggleButton * btn = m_buttons[idx].first;
-        set_current_page(idx, btn);
-    }
+    m_stack.add(w, name, label);
 }
 
-
-void ModuleShellWidget::set_current_page(int idx, Gtk::ToggleButton * btn)
+void ModuleShellWidget::activatePage(const std::string & name)
 {
-    if(m_currentpage == idx) {
-        // just preempt. Make sure the button is still active.
-        // otherwise it cause an infinite loop.
-        m_buttons[m_currentpage].second.block();
-        m_buttons[m_currentpage].first->set_active(true);
-        m_buttons[m_currentpage].second.unblock();
-        return;
-    }
-    m_notebook.set_current_page(idx);
-    if(m_currentpage >= 0) {
-        m_buttons[m_currentpage].second.block();
-        m_buttons[m_currentpage].first->set_active(false);
-        m_buttons[m_currentpage].second.unblock();
+    Glib::ustring current_name
+        = m_stack.get_visible_child_name();
+    if(current_name != name) {
+        signal_deactivated(current_name);
+        m_stack.set_visible_child(name);
+        signal_activated(name);
     }
-    btn->set_active(true);
-    signal_deactivated(m_currentpage);
-    m_currentpage = idx;
-    signal_activated(idx);
 }
 
 }
diff --git a/src/niepce/ui/moduleshellwidget.hpp b/src/niepce/ui/moduleshellwidget.hpp
index bca0274..d88aa6a 100644
--- a/src/niepce/ui/moduleshellwidget.hpp
+++ b/src/niepce/ui/moduleshellwidget.hpp
@@ -23,10 +23,11 @@
 #include <vector>
 #include <utility>
 
-#include <gtkmm/notebook.h>
 #include <gtkmm/box.h>
 #include <gtkmm/buttonbox.h>
 #include <gtkmm/menubutton.h>
+#include <gtkmm/stack.h>
+#include <gtkmm/stackswitcher.h>
 
 namespace Gtk {
 class ToggleButton;
@@ -41,24 +42,25 @@ class ModuleShellWidget
 public:
     ModuleShellWidget();
 
-    int append_page(Gtk::Widget & w, const Glib::ustring & label);
-    void activate_page(int);
+    void appendPage(Gtk::Widget & w,
+                     const Glib::ustring & name,
+                     const Glib::ustring & label);
+    void activatePage(const std::string &);
 
     Gtk::MenuButton & getMenuButton()
         { return m_menubutton; }
+//    Gtk::Stack* getStack() const
+//        { return m_stack; }
 
-    sigc::signal<void, int> signal_activated;
-    sigc::signal<void, int> signal_deactivated;
-protected:
+    sigc::signal<void, const std::string &> signal_activated;
+    sigc::signal<void, const std::string &> signal_deactivated;
 
-    void set_current_page(int, Gtk::ToggleButton *);
 private:
     Gtk::Box                m_mainbox;
     Gtk::ButtonBox          m_mainbar;
     Gtk::MenuButton         m_menubutton;
-    Gtk::Notebook           m_notebook;
-    int                     m_currentpage;
-    std::vector<std::pair<Gtk::ToggleButton*, sigc::connection> > m_buttons;
+    Gtk::Stack              m_stack;
+    Gtk::StackSwitcher      m_switcher;
 };
 
 }


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