[gtkmm-documentation] Application example: Add a window menu.



commit be436cb5887214d3af04f5c360dfd31006a308df
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Sep 27 12:38:45 2013 +0200

    Application example: Add a window menu.
    
    * examples/book/application/app_menu/:
      Rename this to app_and_win_menus/ and add menubar,
      using Gtk::Application_set_menubar() in Gtk::Application::on_startup().

 examples/Makefile.am                               |   14 +++---
 .../exampleapplication.cc                          |   46 +++++++++++++++---
 .../exampleapplication.h                           |    6 +--
 .../application/app_and_win_menus/examplewindow.cc |   51 ++++++++++++++++++++
 .../examplewindow.h                                |    2 +
 .../{app_menu => app_and_win_menus}/main.cc        |    0
 .../book/application/app_menu/examplewindow.cc     |   24 ---------
 examples/book/menus/main_menu/examplewindow.cc     |    1 +
 8 files changed, 102 insertions(+), 42 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index adbfa10..cc7e021 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -10,7 +10,7 @@ check_PROGRAMS =                                      \
        book/alignment/example                          \
        book/application/simple/example \
        book/application/command_line_handling/example \
-       book/application/app_menu/example \
+       book/application/app_and_win_menus/example \
        book/aspectframe/example                        \
        book/assistant/example                          \
        book/base/base                                  \
@@ -153,12 +153,12 @@ book_application_command_line_handling_example_SOURCES =          \
        book/application/command_line_handling/examplewindow.h  \
        book/application/command_line_handling/main.cc
 
-book_application_app_menu_example_SOURCES =            \
-       book/application/app_menu/exampleapplication.cc \
-       book/application/app_menu/exampleapplication.h  \
-       book/application/app_menu/examplewindow.cc      \
-       book/application/app_menu/examplewindow.h       \
-       book/application/app_menu/main.cc
+book_application_app_and_win_menus_example_SOURCES =           \
+       book/application/app_and_win_menus/exampleapplication.cc        \
+       book/application/app_and_win_menus/exampleapplication.h \
+       book/application/app_and_win_menus/examplewindow.cc     \
+       book/application/app_and_win_menus/examplewindow.h      \
+       book/application/app_and_win_menus/main.cc
 
 book_alignment_example_SOURCES =               \
        book/alignment/examplewindow.cc         \
diff --git a/examples/book/application/app_menu/exampleapplication.cc 
b/examples/book/application/app_and_win_menus/exampleapplication.cc
similarity index 59%
rename from examples/book/application/app_menu/exampleapplication.cc
rename to examples/book/application/app_and_win_menus/exampleapplication.cc
index fcfe557..efed650 100644
--- a/examples/book/application/app_menu/exampleapplication.cc
+++ b/examples/book/application/app_and_win_menus/exampleapplication.cc
@@ -34,15 +34,41 @@ void ExampleApplication::on_startup()
   //Call the base class's implementation:
   Gtk::Application::on_startup();
 
-  m_action = Gio::SimpleAction::create("something");
-  m_action->signal_activate().connect(
+  //Create the Application menu:
+  //We can use add_action because Gtk::Application derives from ActionMap:
+  add_action("something",
     sigc::mem_fun(*this, &ExampleApplication::on_action_something) );
-  add_action(m_action);
+  add_action("quit",
+    sigc::mem_fun(*this, &ExampleApplication::on_action_quit) );
 
-  m_menu = Gio::Menu::create();
-  m_menu->append("Something", "app.something");
+  
+  Glib::RefPtr<Gio::Menu> app_menu = Gio::Menu::create();
+  app_menu->append("_Something", "app.something");
+  app_menu->append("_Quit", "app.quit");
 
-  set_app_menu(m_menu);
+  set_app_menu(app_menu);
+
+
+  //Create the per-Window menu:
+  Glib::RefPtr<Gio::Menu> win_menu = Gio::Menu::create();
+
+  Glib::RefPtr<Gio::Menu> submenu_file = Gio::Menu::create();
+  submenu_file->append("_New", "win.new");
+  submenu_file->append("_Close", "win.close");
+  win_menu->append_submenu("File", submenu_file);
+
+  Glib::RefPtr<Gio::Menu> submenu_edit = Gio::Menu::create();
+  submenu_edit->append("_Cut", "win.cut");
+  Glib::RefPtr<Gio::MenuItem> item = Gio::MenuItem::create("_Copy", "win.copy");
+  //TODO: This works, but doesn't seem to be documented. See 
https://bugzilla.gnome.org/show_bug.cgi?id=708908
+  item->set_attribute_value("accel", Glib::Variant<Glib::ustring>::create("<Primary>c"));
+  submenu_edit->append_item(item);
+  item = Gio::MenuItem::create("_Copy", "win.paste");
+  item->set_attribute_value("accel", Glib::Variant<Glib::ustring>::create("<Primary>v"));
+  submenu_edit->append_item(item);
+  win_menu->append_submenu("Edit", submenu_edit);
+
+  set_menubar(win_menu);
 }
 
 void ExampleApplication::create_window()
@@ -75,7 +101,13 @@ void ExampleApplication::on_activate()
   create_window();
 }
 
-void ExampleApplication::on_action_something(const Glib::VariantBase& /* parameter */)
+void ExampleApplication::on_action_something()
+{
+  std::cout << G_STRFUNC << std::endl;
+}
+
+void ExampleApplication::on_action_quit()
 {
   std::cout << G_STRFUNC << std::endl;
+  quit();
 }
diff --git a/examples/book/application/app_menu/exampleapplication.h 
b/examples/book/application/app_and_win_menus/exampleapplication.h
similarity index 89%
rename from examples/book/application/app_menu/exampleapplication.h
rename to examples/book/application/app_and_win_menus/exampleapplication.h
index d2a553c..bedd301 100644
--- a/examples/book/application/app_menu/exampleapplication.h
+++ b/examples/book/application/app_and_win_menus/exampleapplication.h
@@ -36,10 +36,8 @@ private:
   void create_window();
 
   void on_window_hide(Gtk::Window* window);
-  void on_action_something(const Glib::VariantBase& parameter);
-
-  Glib::RefPtr<Gio::Menu> m_menu;
-  Glib::RefPtr<Gio::SimpleAction> m_action;
+  void on_action_something();
+  void on_action_quit();
 };
 
 #endif /* GTKMM_EXAMPLEAPPLICATION_H */
diff --git a/examples/book/application/app_and_win_menus/examplewindow.cc 
b/examples/book/application/app_and_win_menus/examplewindow.cc
new file mode 100644
index 0000000..344e070
--- /dev/null
+++ b/examples/book/application/app_and_win_menus/examplewindow.cc
@@ -0,0 +1,51 @@
+/* gtkmm example Copyright (C) 2002 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <iostream>
+
+#include "examplewindow.h"
+
+ExampleWindow::ExampleWindow()
+{
+  set_title("Gtk::Application example");
+
+  // We can use add_action because Gtk::ApplicationWindow derives from ActionMap.
+  // This Action Map uses a "win." prefix for the actions.
+  // Therefore, for instance, "win.new", is used in ExampleApplication::on_startup()
+  // to layout the menu. 
+  add_action("new",
+    sigc::mem_fun(*this, &ExampleWindow::on_action_something) );
+  add_action("close",
+    sigc::mem_fun(*this, &ExampleWindow::on_action_close) );
+  add_action("cut",
+    sigc::mem_fun(*this, &ExampleWindow::on_action_something) );
+  add_action("copy",
+    sigc::mem_fun(*this, &ExampleWindow::on_action_something) );
+  add_action("paste",
+    sigc::mem_fun(*this, &ExampleWindow::on_action_something) );
+}
+
+void ExampleWindow::on_action_something()
+{
+  std::cout << G_STRFUNC << std::endl;
+}
+
+void ExampleWindow::on_action_close()
+{
+  std::cout << G_STRFUNC << std::endl;
+
+  hide();
+}
diff --git a/examples/book/application/app_menu/examplewindow.h 
b/examples/book/application/app_and_win_menus/examplewindow.h
similarity index 94%
rename from examples/book/application/app_menu/examplewindow.h
rename to examples/book/application/app_and_win_menus/examplewindow.h
index 52be41a..4e719b9 100644
--- a/examples/book/application/app_menu/examplewindow.h
+++ b/examples/book/application/app_and_win_menus/examplewindow.h
@@ -26,6 +26,8 @@ public:
   explicit ExampleWindow();
   
 private:
+  void on_action_something();
+  void on_action_close();
 };
 
 #endif /* GTKMM_EXAMPLEWINDOW_H */
diff --git a/examples/book/application/app_menu/main.cc b/examples/book/application/app_and_win_menus/main.cc
similarity index 100%
rename from examples/book/application/app_menu/main.cc
rename to examples/book/application/app_and_win_menus/main.cc
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index cd47fae..bb031e0 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -282,3 +282,4 @@ void ExampleWindow::on_menu_toggle()
   std::cout << message << std::endl;
 }
 
+


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