[gtkmm-documentation] Main menu example: Use Gtk::Application to display an app menu



commit 5b3012835745c7979ea0bee486adc9f37c75d778
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Tue Nov 25 08:34:51 2014 +0100

    Main menu example: Use Gtk::Application to display an app menu
    
    * examples/Makefile.am: Add main_menu/exampleapplication.[h|cc].
    * examples/book/menus/main_menu/exampleapplication.[h|cc]: New files.
    * examples/book/menus/main_menu/examplewindow.[h|cc]:
    * examples/book/menus/main_menu/main.cc: Derive from Gtk::Application and
    Gtk::ApplicationWindow, similar to the application/app_and_win_menus example.
    
    Thanks to Jürgen Kleber, who attached similar code to
    https://mail.gnome.org/archives/gtkmm-list/2014-November/msg00012.html

 examples/Makefile.am                               |    2 +
 .../book/menus/main_menu/exampleapplication.cc     |  270 ++++++++++++++++++++
 examples/book/menus/main_menu/exampleapplication.h |   45 ++++
 examples/book/menus/main_menu/examplewindow.cc     |  228 +++--------------
 examples/book/menus/main_menu/examplewindow.h      |   11 +-
 examples/book/menus/main_menu/main.cc              |   19 +-
 6 files changed, 367 insertions(+), 208 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 0bffece..e664bb0 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -488,6 +488,8 @@ book_listbox_example_SOURCES =              \
        book/listbox/main.cc
 
 book_menus_main_menu_main_menu_SOURCES =       \
+       book/menus/main_menu/exampleapplication.cc      \
+       book/menus/main_menu/exampleapplication.h       \
        book/menus/main_menu/examplewindow.cc   \
        book/menus/main_menu/examplewindow.h    \
        book/menus/main_menu/main.cc
diff --git a/examples/book/menus/main_menu/exampleapplication.cc 
b/examples/book/menus/main_menu/exampleapplication.cc
new file mode 100644
index 0000000..bfcaf0b
--- /dev/null
+++ b/examples/book/menus/main_menu/exampleapplication.cc
@@ -0,0 +1,270 @@
+/* gtkmm example Copyright (C) 2014 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
+ * as published by the Free Software Foundation.
+ *
+ * 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 "exampleapplication.h"
+#include "examplewindow.h"
+#include <iostream>
+
+ExampleApplication::ExampleApplication()
+: Gtk::Application("org.gtkmm.example.main_menu")
+{
+  Glib::set_application_name("Main Menu Example");
+}
+
+Glib::RefPtr<ExampleApplication> ExampleApplication::create()
+{
+  return Glib::RefPtr<ExampleApplication>(new ExampleApplication());
+}
+
+void ExampleApplication::on_startup()
+{
+  //Call the base class's implementation:
+  Gtk::Application::on_startup();
+
+  //Create actions for menus and toolbars.
+  //We can use add_action() because Gtk::Application derives from Gio::ActionMap.
+
+  //File|New sub menu:
+  add_action("newstandard",
+    sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
+
+  add_action("newfoo",
+    sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
+
+  add_action("newgoo",
+    sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
+
+  //File menu:
+  add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_quit));
+
+  //Help menu:
+  add_action("about", sigc::mem_fun(*this, &ExampleApplication::on_menu_help_about));
+
+  m_refBuilder = Gtk::Builder::create();
+
+  //Layout the actions in a menubar and an application menu:
+  Glib::ustring ui_info =
+    "<interface>"
+    "  <!-- menubar -->"
+    "  <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'>app.newstandard</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;n</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Foo</attribute>"
+    "          <attribute name='action'>app.newfoo</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Goo</attribute>"
+    "          <attribute name='action'>app.newgoo</attribute>"
+    "        </item>"
+    "      </section>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Quit</attribute>"
+    "          <attribute name='action'>app.quit</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Edit</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Copy</attribute>"
+    "          <attribute name='action'>win.copy</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;c</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Paste</attribute>"
+    "          <attribute name='action'>win.paste</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;v</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Something</attribute>"
+    "          <attribute name='action'>win.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'>win.choice</attribute>"
+    "          <attribute name='target'>a</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice _B</attribute>"
+    "          <attribute name='action'>win.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'>win.choiceother</attribute>"
+    "          <attribute name='target' type='i'>1</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Choice 2</attribute>"
+    "          <attribute name='action'>win.choiceother</attribute>"
+    "          <attribute name='target' type='i'>2</attribute>"
+    "        </item>"
+    "      </section>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>Some Toggle</attribute>"
+    "          <attribute name='action'>win.sometoggle</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Help</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_About</attribute>"
+    "          <attribute name='action'>win.about</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "  </menu>"
+    ""
+    "  <!-- application menu -->"
+    "  <menu id='appmenu'>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_File</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Standard</attribute>"
+    "          <attribute name='action'>app.newstandard</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;n</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Foo</attribute>"
+    "          <attribute name='action'>app.newfoo</attribute>"
+    "        </item>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>New _Goo</attribute>"
+    "          <attribute name='action'>app.newgoo</attribute>"
+    "        </item>"
+    "      </section>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_Quit</attribute>"
+    "          <attribute name='action'>app.quit</attribute>"
+    "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "    <submenu>"
+    "      <attribute name='label' translatable='yes'>_Help</attribute>"
+    "      <section>"
+    "        <item>"
+    "          <attribute name='label' translatable='yes'>_About</attribute>"
+    "          <attribute name='action'>app.about</attribute>"
+    "        </item>"
+    "      </section>"
+    "    </submenu>"
+    "  </menu>"
+    "</interface>";
+
+  try
+  {
+    m_refBuilder->add_from_string(ui_info);
+  }
+  catch (const Glib::Error& ex)
+  {
+    std::cerr << "Building menus failed: " << ex.what();
+  }
+
+  //Get the menubar and the app menu, and add them to the application:
+  Glib::RefPtr<Glib::Object> object = m_refBuilder->get_object("menu-example");
+  Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  object = m_refBuilder->get_object("appmenu");
+  Glib::RefPtr<Gio::Menu> appMenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if (!(gmenu && appMenu)) {
+    g_warning("GMenu or AppMenu not found");
+  }
+  else
+  {
+    set_app_menu(appMenu);
+    set_menubar(gmenu);
+  }
+}
+
+void ExampleApplication::on_activate()
+{
+  //std::cout << "debug1: " << G_STRFUNC << std::endl;
+  // The application has been started, so let's show a window.
+  // A real application might want to reuse this window in on_open(),
+  // when asked to open a file, if no changes have been made yet.
+  create_window();
+}
+
+void ExampleApplication::create_window()
+{
+  ExampleWindow* win = new ExampleWindow();
+
+  //Make sure that the application runs for as long this window is still open:
+  add_window(*win);
+
+  //Delete the window when it is hidden.
+  //That's enough for this simple example.
+  win->signal_hide().connect(sigc::bind<Gtk::Window*>(
+    sigc::mem_fun(*this, &ExampleApplication::on_window_hide), win));
+
+  win->show_all();
+}
+
+void ExampleApplication::on_window_hide(Gtk::Window* window)
+{
+  delete window;
+}
+
+void ExampleApplication::on_menu_file_new_generic()
+{
+  std::cout << "A File|New menu item was selected." << std::endl;
+}
+
+void ExampleApplication::on_menu_file_quit()
+{
+  std::cout << G_STRFUNC << std::endl;
+  quit(); // Not really necessary, when Gtk::Widget::hide() is called.
+
+  // Gio::Application::quit() will make Gio::Application::run() return,
+  // but it's a crude way of ending the program. The window is not removed
+  // from the application. Neither the window's nor the application's
+  // destructors will be called, because there will be remaining reference
+  // counts in both of them. If we want the destructors to be called, we
+  // must remove the window from the application. One way of doing this
+  // is to hide the window.
+  std::vector<Gtk::Window*> windows = get_windows();
+  if (windows.size() > 0)
+    windows[0]->hide(); // In this simple case, we know there is only one window.
+}
+
+void ExampleApplication::on_menu_help_about()
+{
+  std::cout << "App|Help|About was selected." << std::endl;
+}
diff --git a/examples/book/menus/main_menu/exampleapplication.h 
b/examples/book/menus/main_menu/exampleapplication.h
new file mode 100644
index 0000000..59aa638
--- /dev/null
+++ b/examples/book/menus/main_menu/exampleapplication.h
@@ -0,0 +1,45 @@
+/* gtkmm example Copyright (C) 2014 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
+ * as published by the Free Software Foundation.
+ *
+ * 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/>.
+ */
+
+#ifndef GTKMM_EXAMPLEAPPLICATION_H
+#define GTKMM_EXAMPLEAPPLICATION_H
+
+#include <gtkmm.h>
+
+class ExampleApplication : public Gtk::Application
+{
+protected:
+  ExampleApplication();
+
+public:
+  static Glib::RefPtr<ExampleApplication> create();
+
+protected:
+  //Overrides of default signal handlers:
+  virtual void on_startup();
+  virtual void on_activate();
+
+private:
+  void create_window();
+
+  void on_window_hide(Gtk::Window* window);
+  void on_menu_file_new_generic();
+  void on_menu_file_quit();
+  void on_menu_help_about();
+
+  Glib::RefPtr<Gtk::Builder> m_refBuilder;
+};
+
+#endif /* GTKMM_EXAMPLEAPPLICATION_H */
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index bf6b6e7..e9cb976 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -1,5 +1,3 @@
-//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2002 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -20,216 +18,66 @@
 #include <iostream>
 
 ExampleWindow::ExampleWindow()
-: m_Box(Gtk::ORIENTATION_VERTICAL)
+: Gtk::ApplicationWindow(),
+  m_Box(Gtk::ORIENTATION_VERTICAL)
 {
-  set_title("main menu example");
-  set_default_size(200, 200);
-
-  add(m_Box); // put a MenuBar at the top of the box and other stuff below it.
-
-  //Create actions for menus and toolbars:
-  Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
-    Gio::SimpleActionGroup::create();
+  set_title("Main menu example");
+  set_default_size(300, 100);
 
-  //File|New sub menu:
-  refActionGroup->add_action("newstandard",
-    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+  // ExampleApplication displays the menubar. Other stuff, such as a toolbar,
+  // is put into the box.
+  add(m_Box);
 
-  refActionGroup->add_action("newfoo",
-    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:
-  refActionGroup->add_action("quit",
-    sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
+  // Create actions for menus and toolbars.
+  // We can use add_action() because Gtk::ApplicationWindow derives from Gio::ActionMap.
+  // This Action Map uses a "win." prefix for the actions.
+  // Therefore, for instance, "win.copy", is used in ExampleApplication::on_startup()
+  // to layout the menu. 
 
   //Edit menu:
-  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));
-
+  add_action("copy", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+  add_action("paste", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+  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_refChoice = 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);
-
-  m_refToggle = refActionGroup->add_action_bool("sometoggle",
-    sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle),
-    false);
+  m_refChoiceOther = add_action_radio_integer("choiceother",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other), 1);
 
+  m_refToggle = add_action_bool("sometoggle",
+    sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle), false);
 
   //Help menu:
-  refActionGroup->add_action("about",
-    sigc::mem_fun(*this, &ExampleWindow::on_menu_others) );
-
-  insert_action_group("example", refActionGroup);
-
-  m_refBuilder = Gtk::Builder::create();
-
-  //TODO: add_accel_group(m_refBuilder->get_accel_group());
-
-  //Layout the actions in a menubar and toolbar:
-  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>"
-    "          <attribute name='accel'>&lt;Primary&gt;n</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>"
-    "          <attribute name='accel'>&lt;Primary&gt;q</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>"
-    "          <attribute name='accel'>&lt;Primary&gt;c</attribute>"
-    "        </item>"
-    "        <item>"
-    "          <attribute name='label' translatable='yes'>_Paste</attribute>"
-    "          <attribute name='action'>example.paste</attribute>"
-    "          <attribute name='accel'>&lt;Primary&gt;v</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>"
-    "      <section>"
-    "        <item>"
-    "          <attribute name='label' translatable='yes'>Some Toggle</attribute>"
-    "          <attribute name='action'>example.sometoggle</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>";
-
-  try
-  {
-    m_refBuilder->add_from_string(ui_info);
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << "building menus failed: " <<  ex.what();
-  }
-
-  //Get the menubar and add it to a container widget:
-  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 = Gtk::manage(new Gtk::MenuBar(gmenu));
-  m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
-
+  add_action("about", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
 
   //Create the toolbar and add it to a container widget:
   Gtk::Toolbar* toolbar = Gtk::manage(new Gtk::Toolbar());
   Gtk::ToolButton* button = Gtk::manage(new Gtk::ToolButton());
   button->set_icon_name("document-new");
-  //We can't do this until we can break the ToolButton ABI: button->set_detailed_action_name("example.new");
-  gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button->gobj()), "example.newstandard");
+  // We can't do this until we can break the ToolButton ABI:
+  //   button->set_detailed_action_name("app.newstandard");
+  gtk_actionable_set_detailed_action_name(GTK_ACTIONABLE(button->gobj()),
+    "app.newstandard");
   toolbar->add(*button);
 
   button = Gtk::manage(new Gtk::ToolButton());
   button->set_icon_name("application-exit");
-  //We can't do this until we can break the ToolButton ABI: button->set_detailed_action_name("example.quit");
-  gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button->gobj()), "example.quit");
+  // We can't do this until we can break the ToolButton ABI:
+  //   button->set_detailed_action_name("app.quit");
+  gtk_actionable_set_detailed_action_name(GTK_ACTIONABLE(button->gobj()),
+    "app.quit");
   toolbar->add(*button);
 
   m_Box.pack_start(*toolbar, Gtk::PACK_SHRINK);
-
-  show_all_children();
 }
 
 ExampleWindow::~ExampleWindow()
 {
 }
 
-void ExampleWindow::on_menu_file_quit()
-{
-  hide(); //Closes the main window to stop the app->run().
-}
-
-void ExampleWindow::on_menu_file_new_generic()
-{
-   std::cout << "A File|New menu item was selected." << std::endl;
-}
-
 void ExampleWindow::on_menu_others()
 {
   std::cout << "A menu item was selected." << std::endl;
@@ -239,12 +87,12 @@ void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
 {
   //The radio action's state does not change automatically:
   m_refChoice->change_state(parameter);
-  
+
   Glib::ustring message;
-  if(parameter == "a")
+  if (parameter == "a")
     message = "Choice a was selected.";
   else
-    message = "Choice b was selected";
+    message = "Choice b was selected.";
 
   std::cout << message << std::endl;
 }
@@ -255,10 +103,10 @@ void ExampleWindow::on_menu_choices_other(int parameter)
   m_refChoiceOther->change_state(parameter);
 
   Glib::ustring message;
-  if(parameter == 1)
+  if (parameter == 1)
     message = "Choice 1 was selected.";
   else
-    message = "Choice 2 was selected";
+    message = "Choice 2 was selected.";
 
   std::cout << message << std::endl;
 }
@@ -269,14 +117,14 @@ void ExampleWindow::on_menu_toggle()
   m_refToggle->get_state(active);
 
   //The toggle action's state does not change automatically:
-  m_refToggle->change_state(!active);
   active = !active;
+  m_refToggle->change_state(active);
 
   Glib::ustring message;
-  if(active)
+  if (active)
     message = "Toggle is active.";
   else
-    message = "Toggle is not active";
+    message = "Toggle is not active.";
 
   std::cout << message << std::endl;
 }
diff --git a/examples/book/menus/main_menu/examplewindow.h b/examples/book/menus/main_menu/examplewindow.h
index 9bd84c5..4782757 100644
--- a/examples/book/menus/main_menu/examplewindow.h
+++ b/examples/book/menus/main_menu/examplewindow.h
@@ -1,5 +1,3 @@
-//$Id: examplewindow.h 705 2006-07-19 02:55:32Z jjongsma $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2002 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,7 +19,7 @@
 
 #include <gtkmm.h>
 
-class ExampleWindow : public Gtk::Window
+class ExampleWindow : public Gtk::ApplicationWindow
 {
 public:
   ExampleWindow();
@@ -29,8 +27,6 @@ public:
 
 protected:
   //Signal handlers:
-  void on_menu_file_new_generic();
-  void on_menu_file_quit();
   void on_menu_others();
 
   void on_menu_choices(const Glib::ustring& parameter);
@@ -40,10 +36,9 @@ protected:
   //Child widgets:
   Gtk::Box m_Box;
 
-  Glib::RefPtr<Gtk::Builder> m_refBuilder;
-
   //Two sets of choices:
-  Glib::RefPtr<Gio::SimpleAction> m_refChoice, m_refChoiceOther;
+  Glib::RefPtr<Gio::SimpleAction> m_refChoice;
+  Glib::RefPtr<Gio::SimpleAction> m_refChoiceOther;
 
   Glib::RefPtr<Gio::SimpleAction> m_refToggle;
 };
diff --git a/examples/book/menus/main_menu/main.cc b/examples/book/menus/main_menu/main.cc
index 3c36987..67c9d27 100644
--- a/examples/book/menus/main_menu/main.cc
+++ b/examples/book/menus/main_menu/main.cc
@@ -1,5 +1,3 @@
-//$Id: main.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
 /* gtkmm example Copyright (C) 2002 gtkmm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -16,15 +14,16 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-#include "examplewindow.h"
-#include <gtkmm/application.h>
+#include "exampleapplication.h"
 
-int main(int argc, char *argv[])
+int main(int argc, char* argv[])
 {
-  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
-
-  ExampleWindow window;
+  Glib::RefPtr<ExampleApplication> application = ExampleApplication::create();
 
-  //Shows the window and returns when it is closed.
-  return app->run(window);
+  // Start the application, showing the initial window,
+  // and opening extra windows for any files that it is asked to open,
+  // for instance as a command-line parameter.
+  // run() will return when the last window has been closed by the user.
+  const int status = application->run(argc, argv);
+  return status;
 }


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