[gtkmm-documentation/gmenu] gmenu: Port the main_menu example



commit ef562e2ad8afa5d26338d711c522e5abff26682c
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Aug 7 15:07:47 2013 +0200

    gmenu: Port the main_menu example

 examples/book/menus/main_menu/examplewindow.cc |  231 ++++++++++++++++--------
 examples/book/menus/main_menu/examplewindow.h  |   11 +-
 2 files changed, 157 insertions(+), 85 deletions(-)
---
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index bc2b40e..7debfc7 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -29,94 +29,149 @@ ExampleWindow::ExampleWindow()
   add(m_Box); // put a MenuBar at the top of the box and other stuff below it.
 
   //Create actions for menus and toolbars:
-  m_refActionGroup = Gtk::ActionGroup::create();
+  Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
+    Gio::SimpleActionGroup::create();
 
   //File|New sub menu:
-  m_refActionGroup->add(Gtk::Action::create("FileNewStandard",
-              Gtk::Stock::NEW, "_New", "Create a new file"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+  refActionGroup->add_action("newstandard",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
 
-  m_refActionGroup->add(Gtk::Action::create("FileNewFoo",
-              Gtk::Stock::NEW, "New Foo", "Create a new foo"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+  refActionGroup->add_action("newfoo",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
 
-  m_refActionGroup->add(Gtk::Action::create("FileNewGoo",
-              Gtk::Stock::NEW, "_New Goo", "Create a new goo"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+  refActionGroup->add_action("newgoo",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
 
   //File menu:
-  m_refActionGroup->add(Gtk::Action::create("FileMenu", "File"));
-  //Sub-menu.
-  m_refActionGroup->add(Gtk::Action::create("FileNew", Gtk::Stock::NEW));
-  m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
+  refActionGroup->add_action("quit",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
 
   //Edit menu:
-  m_refActionGroup->add(Gtk::Action::create("EditMenu", "Edit"));
-  m_refActionGroup->add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
-  m_refActionGroup->add(Gtk::Action::create("EditPaste", Gtk::Stock::PASTE),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
-  m_refActionGroup->add(Gtk::Action::create("EditSomething", "Something"),
-          Gtk::AccelKey("<control><alt>S"),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
-
-
-  //Choices menu, to demonstrate Radio items
-  m_refActionGroup->add( Gtk::Action::create("ChoicesMenu", "Choices") );
-  Gtk::RadioAction::Group group_userlevel;
-  m_refChoiceOne = Gtk::RadioAction::create(group_userlevel, "ChoiceOne", "One");
-  m_refActionGroup->add(m_refChoiceOne,
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_one) );
-  m_refChoiceTwo = Gtk::RadioAction::create(group_userlevel, "ChoiceTwo", "Two");
-  m_refActionGroup->add(m_refChoiceTwo,
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_two) );
+  refActionGroup->add_action("copy",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+  refActionGroup->add_action("paste",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+  refActionGroup->add_action("something",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+
+
+  //Choices menus, to demonstrate Radio items,
+  //using our convenience methods for string and int radio values:
+  m_refChoice = refActionGroup->add_action_radio_string("choice",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_choices),
+    "a");
+
+  m_refChoiceOther = refActionGroup->add_action_radio_integer("choiceother",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other),
+    1);
+
 
   //Help menu:
-  m_refActionGroup->add( Gtk::Action::create("HelpMenu", "Help") );
-  m_refActionGroup->add( Gtk::Action::create("HelpAbout", Gtk::Stock::HELP),
-          sigc::mem_fun(*this, &ExampleWindow::on_menu_others) );
+  refActionGroup->add_action("about",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_others) );
+
+  insert_action_group("example", refActionGroup);
 
-  m_refUIManager = Gtk::UIManager::create();
-  m_refUIManager->insert_action_group(m_refActionGroup);
+  m_refBuilder = Gtk::Builder::create();
 
-  add_accel_group(m_refUIManager->get_accel_group());
+  //TODO: add_accel_group(m_refBuilder->get_accel_group());
 
   //Layout the actions in a menubar and toolbar:
-  Glib::ustring ui_info = 
-        "<ui>"
-        "  <menubar name='MenuBar'>"
-        "    <menu action='FileMenu'>"
-        "      <menu action='FileNew'>"
-        "        <menuitem action='FileNewStandard'/>"
-        "        <menuitem action='FileNewFoo'/>"
-        "        <menuitem action='FileNewGoo'/>"
-        "      </menu>"
-        "      <separator/>"
-        "      <menuitem action='FileQuit'/>"
-        "    </menu>"
-        "    <menu action='EditMenu'>"
-        "      <menuitem action='EditCopy'/>"
-        "      <menuitem action='EditPaste'/>"
-        "      <menuitem action='EditSomething'/>"
-        "    </menu>"
-        "    <menu action='ChoicesMenu'>"
-        "      <menuitem action='ChoiceOne'/>"
-        "      <menuitem action='ChoiceTwo'/>"
-        "    </menu>"
-        "    <menu action='HelpMenu'>"
-        "      <menuitem action='HelpAbout'/>"
-        "    </menu>"
-        "  </menubar>"
+  Glib::ustring ui_info =
+    "<interface>"
+    "  <menu id='menu-example'>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_File</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Standard</attribute>"
+    "          <attribute name='action'>example.newstandard</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Foo</attribute>"
+    "          <attribute name='action'>example.newfoo</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Goo</attribute>"
+    "          <attribute name='action'>example.newgoo</attribute>"
+    "        </item>"
+    "      </section>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Quit</attribute>"
+    "          <attribute name='action'>example.quit</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Edit</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Copy</attribute>"
+    "          <attribute name='action'>example.copy</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Paste</attribute>"
+    "          <attribute name='action'>example.paste</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Something</attribute>"
+    "          <attribute name='action'>example.something</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Choices</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice _A</attribute>"
+    "          <attribute name='action'>example.choice</attribute>"
+    "          <attribute name='target'>a</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice _B</attribute>"
+    "          <attribute name='action'>example.choice</attribute>"
+    "          <attribute name='target'>b</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Other Choices</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice 1</attribute>"
+    "          <attribute name='action'>example.choiceother</attribute>"
+    "          <attribute name='target' type='i'>1</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice 2</attribute>"
+    "          <attribute name='action'>example.choiceother</attribute>"
+    "          <attribute name='target' type='i'>2</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Help</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_About</attribute>"
+    "          <attribute name='action'>example.about</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "  </menu>"
+    "</interface>";
+
+/* TODO:
         "  <toolbar  name='ToolBar'>"
         "    <toolitem action='FileNewStandard'/>"
         "    <toolitem action='FileQuit'/>"
         "  </toolbar>"
-        "</ui>";
+*/
 
   try
   {
-    m_refUIManager->add_ui_from_string(ui_info);
+    m_refBuilder->add_from_string(ui_info);
   }
   catch(const Glib::Error& ex)
   {
@@ -124,13 +179,22 @@ ExampleWindow::ExampleWindow()
   }
 
   //Get the menubar and toolbar widgets, and add them to a container widget:
-  Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
-  if(pMenubar)
-    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
-
-  Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar") ;
+  Glib::RefPtr<Glib::Object> object =
+    m_refBuilder->get_object("menu-example");
+  Glib::RefPtr<Gio::Menu> gmenu =
+    Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if(!gmenu)
+    g_warning("GMenu not found");
+
+  //Menubar:
+  Gtk::MenuBar* pMenubar = new Gtk::MenuBar(gmenu);
+  m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+
+/* TODO:
+  Gtk::Widget* pToolbar = m_refBuilder->get_widget("/ToolBar") ;
   if(pToolbar)
     m_Box.pack_start(*pToolbar, Gtk::PACK_SHRINK);
+*/
 
   show_all_children();
 }
@@ -154,24 +218,31 @@ void ExampleWindow::on_menu_others()
   std::cout << "A menu item was selected." << std::endl;
 }
 
-void ExampleWindow::on_menu_choices_one()
+void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
 {
+  //The radio action's state does not change automatically:
+  m_refChoice->set_state(
+    Glib::Variant<Glib::ustring>::create(parameter) );
+  
   Glib::ustring message;
-  if(m_refChoiceOne->get_active())
-    message = "Choice 1 was selected.";
+  if(parameter == "a")
+    message = "Choice a was selected.";
   else
-    message = "Choice 1 was deselected";
+    message = "Choice b was selected";
 
   std::cout << message << std::endl;
 }
 
-void ExampleWindow::on_menu_choices_two()
+void ExampleWindow::on_menu_choices_other(int parameter)
 {
+  m_refChoice->set_state(
+   Glib::Variant<int>::create(parameter) );
+
   Glib::ustring message;
-  if(m_refChoiceTwo->get_active())
-    message = "Choice 2 was selected.";
+  if(parameter == 1)
+    message = "Choice 1 was selected.";
   else
-    message = "Choice 2 was deselected";
+    message = "Choice 2 was selected";
 
   std::cout << message << std::endl;
 }
diff --git a/examples/book/menus/main_menu/examplewindow.h b/examples/book/menus/main_menu/examplewindow.h
index 361e277..b676ded 100644
--- a/examples/book/menus/main_menu/examplewindow.h
+++ b/examples/book/menus/main_menu/examplewindow.h
@@ -33,15 +33,16 @@ protected:
   void on_menu_file_quit();
   void on_menu_others();
 
-  void on_menu_choices_one();
-  void on_menu_choices_two();
+  void on_menu_choices(const Glib::ustring& parameter);
+  void on_menu_choices_other(int parameter);
 
   //Child widgets:
   Gtk::Box m_Box;
 
-  Glib::RefPtr<Gtk::UIManager> m_refUIManager;
-  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
-  Glib::RefPtr<Gtk::RadioAction> m_refChoiceOne, m_refChoiceTwo;
+  Glib::RefPtr<Gtk::Builder> m_refBuilder;
+
+  //Two sets of choices:
+  Glib::RefPtr<Gio::SimpleAction> m_refChoice, m_refChoiceOther;
 };
 
 #endif //GTKMM_EXAMPLEWINDOW_H


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