[gtkmm-documentation/gmenu] Trying to replace GtkUIManager with GMenu.



commit fe84f0c639b78297c3d990edf12fb842632a8bc8
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jul 22 20:48:39 2013 +0200

    Trying to replace GtkUIManager with GMenu.

 examples/book/menus/popup/examplewindow.cc |   78 ++++++++++++++++------------
 examples/book/menus/popup/examplewindow.h  |    6 +-
 2 files changed, 47 insertions(+), 37 deletions(-)
---
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index 8be6760..1c20293 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -1,6 +1,4 @@
-//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
-/* gtkmm example Copyright (C) 2002 gtkmm development team
+/* gtkmm example Copyright (C) 2002-2013 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2
@@ -42,49 +40,51 @@ ExampleWindow::ExampleWindow()
 
   //Fill menu:
 
-  m_refActionGroup = Gtk::ActionGroup::create();
+  m_refActionGroup = Gio::SimpleActionGroup::create();
 
   //File|New sub menu:
   //These menu actions would normally already exist for a main menu, because a
   //context menu should not normally contain menu items that are only available
   //via a context menu.
-  m_refActionGroup->add(Gtk::Action::create("ContextMenu", "Context Menu"));
 
-  m_refActionGroup->add(Gtk::Action::create("ContextEdit", "Edit"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+  m_refActionGroup->add_action("edit",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
 
-  m_refActionGroup->add(Gtk::Action::create("ContextProcess", "Process"),
-          Gtk::AccelKey("<control>P"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+  m_refActionGroup->add_action("edit", //TODO: How to specify "<control>P" as an accelerator. 
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
 
-  m_refActionGroup->add(Gtk::Action::create("ContextRemove", "Remove"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+  m_refActionGroup->add_action("remove",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
 
-  //TODO:
-  /*
-    //Add a ImageMenuElem:
-    menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("_Something", m_Image,
-      sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic) ) ) ;
-  */
+  insert_action_group("menu-examplepopup", m_refActionGroup);
 
-  m_refUIManager = Gtk::UIManager::create();
-  m_refUIManager->insert_action_group(m_refActionGroup);
 
-  add_accel_group(m_refUIManager->get_accel_group());
+  m_refBuilder = Gtk::Builder::create();
 
   //Layout the actions in a menubar and toolbar:
   Glib::ustring ui_info =
-        "<ui>"
-        "  <popup name='PopupMenu'>"
-        "    <menuitem action='ContextEdit'/>"
-        "    <menuitem action='ContextProcess'/>"
-        "    <menuitem action='ContextRemove'/>"
-        "  </popup>"
-        "</ui>";
+        "<interface>"
+        "  <menu id='menu-examplepopup'>"
+        "    <section>"
+        "      <item>"
+        "        <attribute name='label' translatable='yes'>Edit</attribute>"
+        "        <attribute name='action'>examplepopup.edit</attribute>"
+        "      </item>"
+        "      <item>"
+        "        <attribute name='label' translatable='yes'>Process</attribute>"
+        "        <attribute name='action'>examplepopup.process</attribute>"
+        "      </item>"
+        "      <item>"
+        "        <attribute name='label' translatable='yes'>Remove</attribute>"
+        "        <attribute name='action'>examplepopup.remove</attribute>"
+        "      </item>"
+        "    </section>"
+        "  </menu>"
+        "</interface>";
 
   try
   {
-    m_refUIManager->add_ui_from_string(ui_info);
+    m_refBuilder->add_from_string(ui_info);
   }
   catch(const Glib::Error& ex)
   {
@@ -92,10 +92,14 @@ ExampleWindow::ExampleWindow()
   }
 
   //Get the menu:
-  m_pMenuPopup = dynamic_cast<Gtk::Menu*>(
-          m_refUIManager->get_widget("/PopupMenu")); 
-  if(!m_pMenuPopup)
-    g_warning("menu not found");
+  Glib::RefPtr<Glib::Object> object =
+    m_refBuilder->get_object("menu-examplepopup");
+  Glib::RefPtr<Gio::Menu> gmenu =
+    Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if(!gmenu)
+    g_warning("GMenu not found");
+
+  m_pMenuPopup = new Gtk::Menu(gmenu);
 
   show_all_children();
 }
@@ -104,7 +108,7 @@ ExampleWindow::~ExampleWindow()
 {
 }
 
-void ExampleWindow::on_menu_file_popup_generic()
+void ExampleWindow::on_menu_file_popup_generic(const Glib::VariantBase& /* parameter */)
 {
    std::cout << "A popup menu item was selected." << std::endl;
 }
@@ -113,9 +117,15 @@ bool ExampleWindow::on_button_press_event(GdkEventButton* event)
 {
   if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
   {
+    if(!m_pMenuPopup->get_attach_widget())
+    {
+      m_pMenuPopup->attach_to_widget(*this);
+    }
+
     if(m_pMenuPopup)
       m_pMenuPopup->popup(event->button, event->time);
 
+
     return true; //It has been handled.
   }
   else
diff --git a/examples/book/menus/popup/examplewindow.h b/examples/book/menus/popup/examplewindow.h
index 13a2450..2f83bdd 100644
--- a/examples/book/menus/popup/examplewindow.h
+++ b/examples/book/menus/popup/examplewindow.h
@@ -30,15 +30,15 @@ public:
 protected:
   //Signal handlers:
   virtual bool on_button_press_event(GdkEventButton* event);
-  void on_menu_file_popup_generic();
+  void on_menu_file_popup_generic(const Glib::VariantBase& /* parameter */);
 
   //Child widgets:
   Gtk::Box m_Box;
   Gtk::EventBox m_EventBox;
   Gtk::Label m_Label;
 
-  Glib::RefPtr<Gtk::UIManager> m_refUIManager;
-  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
+  Glib::RefPtr<Gtk::Builder> m_refBuilder;
+  Glib::RefPtr<Gio::SimpleActionGroup> m_refActionGroup;
 
   Gtk::Menu* m_pMenuPopup;
 };


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