[glom/gmenu] More use of GMenu with GtkBuilder



commit f103b1067436ed2ee6accd1f5ea884481d81caa4
Author: Murray Cumming <murrayc murrayc com>
Date:   Sat Jul 27 22:00:42 2013 +0200

    More use of GMenu with GtkBuilder

 glom/frame_glom.cc                                 |    2 -
 .../dialog_relationships_overview.cc               |  145 +++++++++++++-------
 .../dialog_relationships_overview.h                |   26 ++--
 glom/utility_widgets/imageglom.cc                  |    2 +-
 4 files changed, 105 insertions(+), 70 deletions(-)
---
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 465e616..11588ce 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -266,8 +266,6 @@ void Frame_Glom::set_mode_widget(Gtk::Widget& widget)
   AppWindow* pApp = dynamic_cast<AppWindow*>(get_app_window());
   if(pApp)
   {
-    //Glib::RefPtr<Gtk::UIManager> ui_manager = pApp->get_ui_manager();
-
     Notebook_Glom* notebook_current = dynamic_cast<Notebook_Glom*>(m_pBox_Mode->get_child());
     if(notebook_current)
     {
diff --git a/glom/mode_design/relationships_overview/dialog_relationships_overview.cc 
b/glom/mode_design/relationships_overview/dialog_relationships_overview.cc
index 1d87079..dcb9729 100644
--- a/glom/mode_design/relationships_overview/dialog_relationships_overview.cc
+++ b/glom/mode_design/relationships_overview/dialog_relationships_overview.cc
@@ -1,6 +1,6 @@
 /* Glom
  *
- * Copyright (C) 2001-2004 Murray Cumming
+ * Copyright (C) 2001-2013 Murray Cumming
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -26,6 +26,8 @@
 #include "printoperation_relationshipsoverview.h"
 #include "glom/appwindow.h"
 #include <goocanvas.h>
+#include <giomm/menu.h>
+#include <giomm/simpleactiongroup.h>
 #include <glibmm/i18n.h>
 #include <iostream>
 
@@ -52,40 +54,54 @@ Dialog_RelationshipsOverview::Dialog_RelationshipsOverview(BaseObjectType* cobje
   Gtk::Box* vbox = 0;
   builder->get_widget("vbox_placeholder_menubar", vbox);
 
-  m_refActionGroup = Gtk::ActionGroup::create();
+  Glib::RefPtr<Gio::SimpleActionGroup> action_group = Gio::SimpleActionGroup::create();
 
-  m_refActionGroup->add(Gtk::Action::create("Overview_MainMenu_File", _("_File")) );
-  m_refActionGroup->add(Gtk::Action::create("Overview_MainMenu_File_PageSetup", _("Page _Setup")),
+  action_group->add_action("pagesetup",
     sigc::mem_fun(*this, &Dialog_RelationshipsOverview::on_menu_file_page_setup) );
-  m_refActionGroup->add(Gtk::Action::create("Overview_MainMenu_File_Print", _("_Print")),
+  action_group->add_action("print",
     sigc::mem_fun(*this, &Dialog_RelationshipsOverview::on_menu_file_print) );
 
-  m_refActionGroup->add(Gtk::Action::create("Overview_MainMenu_View", _("_View")) );
-  m_action_showgrid = Gtk::ToggleAction::create("Overview_MainMenu_View_Grid", _("Show _Grid"));
-  m_refActionGroup->add(m_action_showgrid,
-    sigc::mem_fun(*this, &Dialog_RelationshipsOverview::on_menu_view_showgrid) );
+  m_action_showgrid = Gio::SimpleAction::create_bool("showgrid", false);
+  action_group->add_action(m_action_showgrid);
+  m_action_showgrid->signal_activate().connect(
+    sigc::mem_fun(*this, &Dialog_RelationshipsOverview::on_menu_view_showgrid)
+  );
 
-  Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
+  insert_action_group("relationshipsoverview", action_group);
 
-  m_refUIManager->insert_action_group(m_refActionGroup);
-  add_accel_group(m_refUIManager->get_accel_group());
+  Glib::RefPtr<Gtk::Builder> refUIBuilder = Gtk::Builder::create();
 
   try
   {
     static const Glib::ustring ui_description =
-    "<ui>"
-    "  <menubar name='Overview_MainMenu'>"
-    "    <menu action='Overview_MainMenu_File'>"
-    "      <menuitem action='Overview_MainMenu_File_PageSetup' />"
-    "      <menuitem action='Overview_MainMenu_File_Print' />"
-    "    </menu>"
-    "    <menu action='Overview_MainMenu_View'>"
-    "      <menuitem action='Overview_MainMenu_View_Grid' />"
-    "    </menu>"
-    "  </menubar>"
-    "</ui>";
-
-    m_refUIManager->add_ui_from_string(ui_description);
+      "<interface>"
+      "  <menu id='Overview_MainMenu'>"
+      "    <submenu>"
+      "      <attribute name='label' translatable='yes'>_File</attribute>"
+      "      <section>"
+      "        <item>"
+      "          <attribute name='label' translatable='yes'>Page _Setup</attribute>"
+      "          <attribute name='action'>relationshipsoverview.pagesetup</attribute>"
+      "        </item>"
+      "        <item>"
+      "          <attribute name='label' translatable='yes'>_Print</attribute>"
+      "          <attribute name='action'>relationshipsoverview.print</attribute>"
+      "        </item>"
+      "      </section>"
+      "    </submenu>"
+      "    <submenu>"
+      "      <attribute name='label' translatable='yes'>_View</attribute>"
+      "      <section>"
+      "        <item>"
+      "          <attribute name='label' translatable='yes'>Show _Grid</attribute>"
+      "          <attribute name='action'>relationshipsoverview.showgrid</attribute>"
+      "        </item>"
+      "      </section>"
+      "    </submenu>"
+      "  </menu>"
+      "</interface>";
+
+    refUIBuilder->add_from_string(ui_description);
   }
   catch(const Glib::Error& ex)
   {
@@ -93,9 +109,14 @@ Dialog_RelationshipsOverview::Dialog_RelationshipsOverview(BaseObjectType* cobje
   }
 
   //Get the menu:
-  m_menu = dynamic_cast<Gtk::MenuBar*>( m_refUIManager->get_widget("/Overview_MainMenu") );
-  if(!m_menu)
-    g_warning("menu not found");
+  Glib::RefPtr<Glib::Object> object =
+    refUIBuilder->get_object("Overview_MainMenu");
+  Glib::RefPtr<Gio::Menu> gmenu =
+    Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if(!gmenu)
+    g_warning("GMenu not found");
+
+  m_menu = new Gtk::MenuBar(gmenu);
 
   vbox->pack_start(*m_menu, Gtk::PACK_SHRINK);
   m_menu->show();
@@ -347,12 +368,12 @@ void Dialog_RelationshipsOverview::on_response(int /* id */)
   hide();
 }
 
-void Dialog_RelationshipsOverview::on_menu_file_print()
+void Dialog_RelationshipsOverview::on_menu_file_print(const Glib::VariantBase& /* parameter */)
 {
   print_or_preview(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG);
 }
 
-void Dialog_RelationshipsOverview::on_menu_file_page_setup()
+void Dialog_RelationshipsOverview::on_menu_file_page_setup(const Glib::VariantBase& /* parameter */)
 {
   //Show the page setup dialog, asking it to start with the existing settings:
   Glib::RefPtr<Gtk::PageSetup> new_page_setup =
@@ -363,9 +384,9 @@ void Dialog_RelationshipsOverview::on_menu_file_page_setup()
   m_refPageSetup = new_page_setup;
 }
 
-void Dialog_RelationshipsOverview::on_menu_view_showgrid()
+void Dialog_RelationshipsOverview::on_menu_view_showgrid(const Glib::VariantBase& /* parameter */)
 {
-  if(m_action_showgrid->get_active())
+  if(m_action_showgrid->get_state_bool())
   {
     m_canvas.set_grid_gap(40);
   }
@@ -375,7 +396,8 @@ void Dialog_RelationshipsOverview::on_menu_view_showgrid()
   }
 }
 
-void Dialog_RelationshipsOverview::on_menu_file_save()
+//TODO: Is this used?
+void Dialog_RelationshipsOverview::on_menu_file_save(const Glib::VariantBase& parameter)
 {
 }
 
@@ -460,6 +482,11 @@ void Dialog_RelationshipsOverview::on_table_show_context(guint button, guint32 a
       sigc::bind( sigc::mem_fun(*this, &Dialog_RelationshipsOverview::on_context_menu_edit_relationships), 
table ));
   }
 
+  if(!m_context_menu->get_attach_widget())
+  {
+    m_context_menu->attach_to_widget(*this);
+  }
+
   if(m_context_menu)
     m_context_menu->popup(button, activate_time);
 
@@ -467,30 +494,37 @@ void Dialog_RelationshipsOverview::on_table_show_context(guint button, guint32 a
 
 void Dialog_RelationshipsOverview::setup_context_menu()
 {
-  m_context_menu_action_group = Gtk::ActionGroup::create();
+  Glib::RefPtr<Gio::SimpleActionGroup> action_group = Gio::SimpleActionGroup::create();
 
-  m_context_menu_action_group->add(Gtk::Action::create("ContextMenu", "Context Menu") );
+  m_action_edit_fields = Gio::SimpleAction::create("editfields");
+  action_group->add_action(m_action_edit_fields);
 
-  m_action_edit_fields = Gtk::Action::create("ContextEditFields", _("Edit _Fields"));
-  m_context_menu_action_group->add(m_action_edit_fields);
+  m_action_edit_relationships = Gio::SimpleAction::create("editrelationships");
+  action_group->add_action(m_action_edit_relationships);
 
-  m_action_edit_relationships = Gtk::Action::create("ContextEditRelationships", _("Edit _Relationships"));
-  m_context_menu_action_group->add(m_action_edit_relationships);
+  insert_action_group("context", action_group);
 
-  m_context_menu_uimanager = Gtk::UIManager::create();
-  m_context_menu_uimanager->insert_action_group(m_context_menu_action_group);
+  m_context_menu_builder = Gtk::Builder::create();
 
   try
   {
     Glib::ustring ui_info =
-    "<ui>"
-    "  <popup name='ContextMenu'>"
-    "  <menuitem action='ContextEditFields'/>"
-    "  <menuitem action='ContextEditRelationships'/>"
-    "  </popup>"
-    "</ui>";
-
-    m_context_menu_uimanager->add_ui_from_string(ui_info);
+      "<interface>"
+      "  <menu id='ContextMenu'>"
+      "    <section>"
+      "      <item>"
+      "        <attribute name='label' translatable='yes'>Edit _Fields</attribute>"
+      "        <attribute name='action'>context.editfields</attribute>"
+      "      </item>"
+      "      <item>"
+      "        <attribute name='label' translatable='yes'>Edit _Relationships</attribute>"
+      "        <attribute name='action'>context.editrelationships</attribute>"
+      "      </item>"
+      "    </section>"
+      "  </menu>"
+      "</interface>";
+
+    m_context_menu_builder->add_from_string(ui_info);
   }
   catch(const Glib::Error& ex)
   {
@@ -498,10 +532,17 @@ void Dialog_RelationshipsOverview::setup_context_menu()
   }
 
   //Get the menu:
-  m_context_menu = dynamic_cast<Gtk::Menu*>( m_context_menu_uimanager->get_widget("/ContextMenu") );
+  Glib::RefPtr<Glib::Object> object =
+    m_context_menu_builder->get_object("ContextMenu");
+  Glib::RefPtr<Gio::Menu> gmenu =
+    Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if(!gmenu)
+    g_warning("GMenu not found");
+
+  m_context_menu = new Gtk::Menu(gmenu);
 }
 
-void Dialog_RelationshipsOverview::on_context_menu_edit_fields(Glib::RefPtr<CanvasGroupDbTable> table)
+void Dialog_RelationshipsOverview::on_context_menu_edit_fields(const Glib::VariantBase& /* parameter */, 
Glib::RefPtr<CanvasGroupDbTable> table)
 {
   AppWindow* pApp = AppWindow::get_appwindow();
   if(pApp && table)
@@ -512,7 +553,7 @@ void Dialog_RelationshipsOverview::on_context_menu_edit_fields(Glib::RefPtr<Canv
   }
 }
 
-void Dialog_RelationshipsOverview::on_context_menu_edit_relationships(Glib::RefPtr<CanvasGroupDbTable> table)
+void Dialog_RelationshipsOverview::on_context_menu_edit_relationships(const Glib::VariantBase& /* parameter 
*/, Glib::RefPtr<CanvasGroupDbTable> table)
 {
   AppWindow* pApp = AppWindow::get_appwindow();
   if(pApp && table)
diff --git a/glom/mode_design/relationships_overview/dialog_relationships_overview.h 
b/glom/mode_design/relationships_overview/dialog_relationships_overview.h
index 7027107..4f39486 100644
--- a/glom/mode_design/relationships_overview/dialog_relationships_overview.h
+++ b/glom/mode_design/relationships_overview/dialog_relationships_overview.h
@@ -1,6 +1,6 @@
 /* Glom
  *
- * Copyright (C) 2001-2004 Murray Cumming
+ * Copyright (C) 2001-2013 Murray Cumming
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -26,7 +26,6 @@
 #include "canvas_group_dbtable.h"
 #include <gtkmm/dialog.h>
 #include <gtkmm/widget.h>
-#include <gtkmm/uimanager.h>
 #include <gtkmm/menubar.h>
 #include <gtkmm/printoperation.h>
 #include <gtkmm/scrolledwindow.h>
@@ -64,24 +63,22 @@ private:
   void print_or_preview(Gtk::PrintOperationAction print_action);
   void on_response(int id);
 
-  void on_menu_file_print();
-  void on_menu_file_page_setup();
-  void on_menu_file_save();
-  void on_menu_view_showgrid();
+  void on_menu_file_print(const Glib::VariantBase& parameter);
+  void on_menu_file_page_setup(const Glib::VariantBase& parameter);
+  void on_menu_file_save(const Glib::VariantBase& parameter);
+  void on_menu_view_showgrid(const Glib::VariantBase& parameter);
 
   void on_table_moved(const Glib::RefPtr<CanvasItemMovable>& item, double x_offset, double y_offset);
   void on_table_show_context(guint button, guint32 activate_time, Glib::RefPtr<CanvasGroupDbTable> table);
 
-  void on_context_menu_edit_fields(Glib::RefPtr<CanvasGroupDbTable> table);
-  void on_context_menu_edit_relationships(Glib::RefPtr<CanvasGroupDbTable> table);
+  void on_context_menu_edit_fields(const Glib::VariantBase& parameter, Glib::RefPtr<CanvasGroupDbTable> 
table);
+  void on_context_menu_edit_relationships(const Glib::VariantBase& parameter, 
Glib::RefPtr<CanvasGroupDbTable> table);
 
   void on_scroll_value_changed();
 
   Glib::RefPtr<CanvasGroupDbTable> get_table_group(const Glib::ustring& table_name);
-  
-  Glib::RefPtr<Gtk::UIManager> m_refUIManager;
-  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
-  Glib::RefPtr<Gtk::ToggleAction> m_action_showgrid;
+
+  Glib::RefPtr<Gio::SimpleAction> m_action_showgrid;
   Gtk::MenuBar* m_menu;
 
   bool m_modified;
@@ -101,9 +98,8 @@ private:
   
   //Context menu:
   Gtk::Menu* m_context_menu;
-  Glib::RefPtr<Gtk::ActionGroup> m_context_menu_action_group;
-  Glib::RefPtr<Gtk::UIManager> m_context_menu_uimanager;
-  Glib::RefPtr<Gtk::Action> m_action_edit_fields, m_action_edit_relationships;
+  Glib::RefPtr<Gtk::Builder> m_context_menu_builder;
+  Glib::RefPtr<Gio::SimpleAction> m_action_edit_fields, m_action_edit_relationships;
   sigc::connection m_connection_edit_fields, m_connection_edit_relationships;
 
   //Printing:
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 93b0a72..086e367 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -947,7 +947,7 @@ void ImageGlom::setup_menu_usermode()
   m_refActionClear = m_refActionGroup_UserModePopup->add_action("clear",
     sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_clear) );
 
-  insert_action_group ("imagecontext", m_refActionGroup_UserModePopup);
+  insert_action_group("imagecontext", m_refActionGroup_UserModePopup);
 
 
   //Create the UI for the menu whose items will activate the actions,


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