[niepce] gtk: Rework the actions: the win action group is implicit



commit 1b6d5de5ae5e0762287a4699b002ad7f1ae1b065
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Dec 28 22:23:57 2019 -0500

    gtk: Rework the actions: the win action group is implicit
    
    - Pass a Gio::ActionMap* since we can't get the RefPtr from Gtk::ApplicationWindow

 src/fwk/toolkit/Makefile.am           |  1 +
 src/fwk/toolkit/appframe.cpp          | 34 +++++++++++++++++++++++++++++++++
 src/fwk/toolkit/appframe.hpp          | 31 ++++++++++++++++++++++--------
 src/fwk/toolkit/application.cpp       | 10 +++++-----
 src/fwk/toolkit/frame.cpp             |  4 ++--
 src/fwk/toolkit/frame.hpp             |  4 ++--
 src/fwk/toolkit/gtkutils.cpp          |  4 ++--
 src/fwk/toolkit/gtkutils.hpp          | 15 +++++----------
 src/niepce/ui/moduleshell.cpp         | 36 +++++++++++++++++------------------
 src/niepce/ui/niepcewindow.cpp        | 30 +++++++++++++----------------
 src/niepce/ui/niepcewindow.hpp        |  1 -
 src/niepce/ui/workspacecontroller.cpp |  6 +++---
 12 files changed, 108 insertions(+), 68 deletions(-)
---
diff --git a/src/fwk/toolkit/Makefile.am b/src/fwk/toolkit/Makefile.am
index be5efc8..c67834a 100644
--- a/src/fwk/toolkit/Makefile.am
+++ b/src/fwk/toolkit/Makefile.am
@@ -31,6 +31,7 @@ testconfigdatabinder_LDADD = $(testing_ldadd)
 
 libniepceframework_a_SOURCES = configuration.hpp configuration.cpp \
        application.hpp application.cpp \
+       appframe.hpp appframe.cpp \
        dialog.hpp dialog.cpp \
        frame.hpp frame.cpp \
        controller.hpp controller.cpp \
diff --git a/src/fwk/toolkit/appframe.cpp b/src/fwk/toolkit/appframe.cpp
new file mode 100644
index 0000000..e39f3f7
--- /dev/null
+++ b/src/fwk/toolkit/appframe.cpp
@@ -0,0 +1,34 @@
+/*
+ * niepce - fwk/toolkit/appframe.cpp
+ *
+ * Copyright (C) 2019 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "appframe.hpp"
+
+namespace fwk {
+
+AppFrame::AppFrame(const std::string & layout_cfg_key)
+  : Frame(new Gtk::ApplicationWindow(), layout_cfg_key)
+{
+}
+
+void AppFrame::on_ready()
+{
+  gtkWindow().show();
+}
+
+}
diff --git a/src/fwk/toolkit/appframe.hpp b/src/fwk/toolkit/appframe.hpp
index b5a701b..55356dc 100644
--- a/src/fwk/toolkit/appframe.hpp
+++ b/src/fwk/toolkit/appframe.hpp
@@ -1,4 +1,21 @@
-
+/*
+ * niepce - fwk/toolkit/appframe.hpp
+ *
+ * Copyright (C) 2019 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 #pragma once
 
@@ -15,16 +32,14 @@ public:
   typedef std::shared_ptr<AppFrame> Ptr;
   typedef std::weak_ptr<AppFrame> WeakPtr;
 
-  AppFrame(const std::string & layout_cfg_key = "")
-    : Frame(new Gtk::ApplicationWindow(), layout_cfg_key)
-    {
-    }
+  AppFrame(const std::string & layout_cfg_key = "");
 
-  virtual void on_ready() override
+  virtual void on_ready() override;
+
+  Gtk::ApplicationWindow* gtkAppWindow()
     {
-      gtkWindow().show();
+      return dynamic_cast<Gtk::ApplicationWindow*>(&gtkWindow());
     }
-
   Glib::RefPtr<Gio::Menu> get_menu() const
     { return m_menu; }
 
diff --git a/src/fwk/toolkit/application.cpp b/src/fwk/toolkit/application.cpp
index d931a35..dac64ec 100644
--- a/src/fwk/toolkit/application.cpp
+++ b/src/fwk/toolkit/application.cpp
@@ -118,20 +118,20 @@ void Application::on_startup()
 
 void Application::init_actions()
 {
-    fwk::add_action(m_gtkapp, "OpenCatalog",
+    fwk::add_action(m_gtkapp.get(), "OpenCatalog",
                     sigc::mem_fun(*this,
                                   &Application::on_action_file_open),
                     "app", "<Primary>o");
-    fwk::add_action(m_gtkapp, "Preferences",
+    fwk::add_action(m_gtkapp.get(), "Preferences",
                     sigc::mem_fun(*this,
                                   &Application::on_action_preferences));
-    fwk::add_action(m_gtkapp, "Help",
+    fwk::add_action(m_gtkapp.get(), "Help",
                     sigc::mem_fun(*this,
                                   &Application::about));
-    fwk::add_action(m_gtkapp, "About",
+    fwk::add_action(m_gtkapp.get(), "About",
                     sigc::mem_fun(*this,
                                   &Application::about));
-    fwk::add_action(m_gtkapp, "Quit",
+    fwk::add_action(m_gtkapp.get(), "Quit",
                     sigc::mem_fun(*this,
                                   &Application::quit),
                     "app", "<Primary>q");
diff --git a/src/fwk/toolkit/frame.cpp b/src/fwk/toolkit/frame.cpp
index 567f2c3..2839f70 100644
--- a/src/fwk/toolkit/frame.cpp
+++ b/src/fwk/toolkit/frame.cpp
@@ -144,7 +144,7 @@ void Frame::redo_state()
 
 
 Glib::RefPtr<Gio::Action>
-Frame::create_undo_action(const Glib::RefPtr<Gio::ActionMap> & g)
+Frame::create_undo_action(Gio::ActionMap* g)
 {
     m_undo_action = fwk::add_action(g, "Undo",
                                     sigc::mem_fun(Application::app()->undo_history(),
@@ -158,7 +158,7 @@ Frame::create_undo_action(const Glib::RefPtr<Gio::ActionMap> & g)
 
 
 Glib::RefPtr<Gio::Action>
-Frame::create_redo_action(const Glib::RefPtr<Gio::ActionMap> & g)
+Frame::create_redo_action(Gio::ActionMap* g)
 {
     m_redo_action = fwk::add_action(g, "Redo",
                                     sigc::mem_fun(Application::app()->undo_history(),
diff --git a/src/fwk/toolkit/frame.hpp b/src/fwk/toolkit/frame.hpp
index 990fe98..9bceccc 100644
--- a/src/fwk/toolkit/frame.hpp
+++ b/src/fwk/toolkit/frame.hpp
@@ -102,8 +102,8 @@ protected:
 
     void undo_state();
     void redo_state();
-    Glib::RefPtr<Gio::Action> create_undo_action(const Glib::RefPtr<Gio::ActionMap> & g);
-    Glib::RefPtr<Gio::Action> create_redo_action(const Glib::RefPtr<Gio::ActionMap> & g);
+    Glib::RefPtr<Gio::Action> create_undo_action(Gio::ActionMap* g);
+    Glib::RefPtr<Gio::Action> create_redo_action(Gio::ActionMap* g);
 
     /** close signal handler */
     virtual bool _close();
diff --git a/src/fwk/toolkit/gtkutils.cpp b/src/fwk/toolkit/gtkutils.cpp
index eef53f8..848c537 100644
--- a/src/fwk/toolkit/gtkutils.cpp
+++ b/src/fwk/toolkit/gtkutils.cpp
@@ -24,7 +24,7 @@
 namespace fwk {
 
 Glib::RefPtr<Gio::SimpleAction>
-add_action(const Glib::RefPtr<Gio::ActionMap> & group,
+add_action(Gio::ActionMap* group,
            const char* name,
            const Gio::ActionMap::ActivateSlot& slot,
            const char* context,
@@ -42,7 +42,7 @@ add_action(const Glib::RefPtr<Gio::ActionMap> & group,
 }
 
 Glib::RefPtr<Gio::SimpleAction>
-add_menu_action(const Glib::RefPtr<Gio::ActionMap> & group,
+add_menu_action(Gio::ActionMap* group,
                 const char* name,
                 const Gio::ActionMap::ActivateSlot& slot,
                 const Glib::RefPtr<Gio::Menu> & menu,
diff --git a/src/fwk/toolkit/gtkutils.hpp b/src/fwk/toolkit/gtkutils.hpp
index 22db7ad..7406e24 100644
--- a/src/fwk/toolkit/gtkutils.hpp
+++ b/src/fwk/toolkit/gtkutils.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/toolkit/gtkutils.hpp
  *
- * Copyright (C) 2009-2014 Hubert Figuiere
+ * Copyright (C) 2009-2019 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,8 +18,7 @@
  */
 
 
-#ifndef __FWK_GTKUTILS_H__
-#define __FWK_GTKUTILS_H__
+#pragma once
 
 #include <string>
 
@@ -29,25 +28,24 @@
 #include <gtkmm/combobox.h>
 #include <gtkmm/liststore.h>
 
-
 namespace fwk {
 
 Glib::RefPtr<Gio::SimpleAction>
-add_action(const Glib::RefPtr<Gio::ActionMap> & group,
+add_action(Gio::ActionMap* group,
            const char* name,
            const Gio::ActionMap::ActivateSlot& slot,
            const char* context = nullptr,
            const char* accel = nullptr);
 
 /** Helper to add an action with a menu.
- * @param group the action group
+ * @param group the action group (raw pointer)
  * @param name the action name
  * @param slot the action slot
  * @param menu the menu append the action to.
  * @param label the label of the menu
  */
 Glib::RefPtr<Gio::SimpleAction>
-add_menu_action(const Glib::RefPtr<Gio::ActionMap> & group,
+add_menu_action(Gio::ActionMap* group,
                 const char* name,
                 const Gio::ActionMap::ActivateSlot& slot,
                 const Glib::RefPtr<Gio::Menu> & menu,
@@ -100,9 +98,6 @@ public:
 };
 
 }
-
-
-#endif
 /*
   Local Variables:
   mode:c++
diff --git a/src/niepce/ui/moduleshell.cpp b/src/niepce/ui/moduleshell.cpp
index 7738830..8e79ee4 100644
--- a/src/niepce/ui/moduleshell.cpp
+++ b/src/niepce/ui/moduleshell.cpp
@@ -55,13 +55,13 @@ Gtk::Widget * ModuleShell::buildWidget()
     m_menu = Gio::Menu::create();
 
     // "go-previous"
-    fwk::add_menu_action(m_actionGroup, "PrevImage",
+    fwk::add_menu_action(m_actionGroup.get(), "PrevImage",
                          sigc::mem_fun(*m_selection_controller,
                                        &SelectionController::select_previous),
                          m_menu, _("Back"), "shell", "Left");
 
     // "go-next"
-    fwk::add_menu_action(m_actionGroup, "NextImage",
+    fwk::add_menu_action(m_actionGroup.get(), "NextImage",
                          sigc::mem_fun(*m_selection_controller,
                                   &SelectionController::select_next),
                          m_menu, _("Forward"), "shell", "Right");
@@ -70,14 +70,14 @@ Gtk::Widget * ModuleShell::buildWidget()
     m_menu->append_section(section);
 
     // "object-rotate-left"
-    fwk::add_menu_action(m_actionGroup, "RotateLeft",
+    fwk::add_menu_action(m_actionGroup.get(), "RotateLeft",
                          sigc::bind(
                              sigc::mem_fun(*m_selection_controller,
                                            &SelectionController::rotate), -90),
                          section, _("Rotate Left"), "shell", "bracketleft");
 
     // "object-rotate-right"
-    fwk::add_menu_action(m_actionGroup, "RotateRight",
+    fwk::add_menu_action(m_actionGroup.get(), "RotateRight",
                          sigc::bind(
                              sigc::mem_fun(*m_selection_controller,
                                            &SelectionController::rotate), 90),
@@ -89,25 +89,25 @@ Gtk::Widget * ModuleShell::buildWidget()
     auto submenu = Gio::Menu::create();
     section->append_submenu(_("Set Label"), submenu);
 
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetLabel6",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_label),
                                     1),
                          submenu, _("Label 6"), "shell", "<Primary>6");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetLabel7",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_label),
                                     2),
                          submenu, _("Label 7"), "shell", "<Primary>7");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetLabel8",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_label),
                                     3),
                          submenu, _("Label 8"), "shell", "<Primary>8");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetLabel9",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_label),
@@ -117,37 +117,37 @@ Gtk::Widget * ModuleShell::buildWidget()
     submenu = Gio::Menu::create();
     section->append_submenu(_("Set Rating"), submenu);
 
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating0",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_rating),
                                     0),
                          submenu, _("Unrated"), "shell", "<Primary>0");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating1",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                              &SelectionController::set_rating),
                                     1),
                          submenu, _("Rating 1"), "shell", "<Primary>1");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating2",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_rating),
                                     2),
                          submenu, _("Rating 2"), "shell", "<Primary>2");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating3",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_rating),
                                     3),
                          submenu, _("Rating 3"), "shell", "<Primary>3");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating4",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_rating),
                                     4),
                          submenu, _("Rating 4"), "shell", "<Primary>4");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetRating5",
                          sigc::bind(sigc::mem_fun(*m_selection_controller,
                                                   &SelectionController::set_rating),
@@ -157,21 +157,21 @@ Gtk::Widget * ModuleShell::buildWidget()
     submenu = Gio::Menu::create();
     section->append_submenu(_("Set Flag"), submenu);
 
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetFlagReject",
                          sigc::bind(
                              sigc::mem_fun(*m_selection_controller,
                                            &SelectionController::set_flag),
                              -1),
                          submenu, _("Flag as Rejected"), "shell", "<Primary><Shift>x");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "SetFlagNone",
                          sigc::bind(
                              sigc::mem_fun(*m_selection_controller,
                                            &SelectionController::set_flag),
                              0),
                          submenu, _("Unflagged"), "shell", "<Primary><Shift>u");
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                           "SetFlagPick",
                          sigc::bind(
                              sigc::mem_fun(*m_selection_controller,
@@ -182,7 +182,7 @@ Gtk::Widget * ModuleShell::buildWidget()
     section = Gio::Menu::create();
     m_menu->append_section(section);
 
-    fwk::add_menu_action(m_actionGroup,
+    fwk::add_menu_action(m_actionGroup.get(),
                          "WriteMetadata",
                          sigc::mem_fun(*m_selection_controller,
                                        &SelectionController::write_metadata),
diff --git a/src/niepce/ui/niepcewindow.cpp b/src/niepce/ui/niepcewindow.cpp
index 28d2c7a..c20bb24 100644
--- a/src/niepce/ui/niepcewindow.cpp
+++ b/src/niepce/ui/niepcewindow.cpp
@@ -56,7 +56,6 @@ using fwk::UndoHistory;
 
 namespace ui {
 
-
 NiepceWindow::NiepceWindow()
     : fwk::AppFrame("mainWindow-frame")
     , m_vbox(Gtk::ORIENTATION_VERTICAL)
@@ -211,27 +210,28 @@ void NiepceWindow::init_actions()
     Glib::RefPtr<Gio::Menu> submenu;
     Glib::RefPtr<Gio::Menu> section;
 
-    m_action_group = Gio::SimpleActionGroup::create();
-    gtkWindow().insert_action_group("win", m_action_group);
-
-    fwk::add_action(m_action_group, "Close",
+    // Get the action group.
+    // Gtkmm doesn't allow GActionMap from GtkApplicationWindow
+    auto action_map = dynamic_cast<Gio::ActionMap*>(&gtkWindow());
+    DBG_ASSERT(action_map, "Not an action map");
+    fwk::add_action(action_map, "Close",
                     sigc::mem_fun(
                         gtkWindow(), &Gtk::Window::hide), "win", "<Primary>w");
 
     // XXX Move to shell?
-    create_undo_action(m_action_group);
-    create_redo_action(m_action_group);
+    create_undo_action(action_map);
+    create_redo_action(action_map);
 
-    fwk::add_action(m_action_group, "Cut",
+    fwk::add_action(action_map, "Cut",
                     Gio::ActionMap::ActivateSlot(),
                     "win", "<control>x");
-    fwk::add_action(m_action_group, "Copy",
+    fwk::add_action(action_map, "Copy",
                     Gio::ActionMap::ActivateSlot(),
                     "win", "<control>c");
-    fwk::add_action(m_action_group, "Paste",
+    fwk::add_action(action_map, "Paste",
                     Gio::ActionMap::ActivateSlot(),
                     "win" "<control>v");
-    fwk::add_action(m_action_group, "Delete",
+    fwk::add_action(action_map, "Delete",
                     sigc::mem_fun(*this, &NiepceWindow::on_action_edit_delete),
                     "win", "Delete");
 
@@ -244,11 +244,11 @@ void NiepceWindow::init_actions()
     section = Gio::Menu::create();
     m_main_menu->append_section(section);
     m_hide_tools_action
-        = fwk::add_menu_action(m_action_group, "ToggleToolsVisible",
+        = fwk::add_menu_action(action_map, "ToggleToolsVisible",
                                sigc::mem_fun(*this, &Frame::toggle_tools_visible),
                                section, _("Hide tools"), "win",
                                nullptr);
-    fwk::add_menu_action(m_action_group, "EditLabels",
+    fwk::add_menu_action(action_map, "EditLabels",
                          sigc::mem_fun(*this, &NiepceWindow::on_action_edit_labels),
                          section, _("Edit Labels..."), "win", nullptr);
     section->append(_("Preferences..."), "app.Preferences");
@@ -407,11 +407,7 @@ void NiepceWindow::set_title(const std::string & title)
     Frame::set_title(_("Niepce Digital - ") + title);
 }
 
-
 }
-
-
-
 /*
   Local Variables:
   mode:c++
diff --git a/src/niepce/ui/niepcewindow.hpp b/src/niepce/ui/niepcewindow.hpp
index 93efecd..88797cd 100644
--- a/src/niepce/ui/niepcewindow.hpp
+++ b/src/niepce/ui/niepcewindow.hpp
@@ -85,7 +85,6 @@ private:
     WorkspaceController::Ptr       m_workspacectrl;
     FilmStripController::Ptr       m_filmstrip;
     Gtk::Statusbar                 m_statusBar;
-    Glib::RefPtr<Gio::SimpleActionGroup> m_action_group;
     libraryclient::LibraryClientPtr m_libClient;
     fwk::Configuration::Ptr        m_library_cfg;
 };
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index cd3e8d3..5f84bef 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -436,14 +436,14 @@ Gtk::Widget * WorkspaceController::buildWidget()
 
     auto section = Gio::Menu::create();
     menu->append_section(section);
-    fwk::add_menu_action(m_action_group, "NewFolder",
+    fwk::add_menu_action(m_action_group.get(), "NewFolder",
                          sigc::mem_fun(*this,
                                        &WorkspaceController::action_new_folder),
                          section, _("New Folder..."), "workspace");
 
     section->append(_("New Project..."), "NewProject");
 
-    auto action = fwk::add_menu_action(m_action_group, "DeleteFolder",
+    auto action = fwk::add_menu_action(m_action_group.get(), "DeleteFolder",
                                        sigc::mem_fun(
                                            *this, &WorkspaceController::action_delete_folder),
                                        section, _("Delete Folder"), "workspace");
@@ -452,7 +452,7 @@ Gtk::Widget * WorkspaceController::buildWidget()
     section = Gio::Menu::create();
     menu->append_section(section);
 
-    fwk::add_menu_action(m_action_group, "Import",
+    fwk::add_menu_action(m_action_group.get(), "Import",
                          sigc::mem_fun(*this,
                                        &WorkspaceController::action_file_import),
                          section, _("_Import..."), "workspace");


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