[gtkmm-documentation] examples: Don't use application menus



commit e9407f165d91ab2bc5afca493cba2ed6e1aca462
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Fri May 29 17:06:18 2020 +0200

    examples: Don't use application menus
    
    Application menus have been removed from gtk and gtkmm.
    Use window menus (menubars).
    
    * examples/book/buildapp: Remove all app_menu.ui and move the menu items
    to gears_menu.ui. (Move from application menu to window menu.)
    * examples/book/application: Rename app_and_win_menus to menubar.
    * examples/book/menus/main_menu/exampleapplication.cc:
    Use ApplicationWindow::set_show_menubar().

 examples/Makefile.am                               | 14 ++--
 .../exampleapplication.cc                          | 25 +++----
 .../exampleapplication.h                           |  0
 .../examplewindow.cc                               |  0
 .../{app_and_win_menus => menubar}/examplewindow.h |  0
 .../{app_and_win_menus => menubar}/main.cc         |  0
 examples/book/buildapp/step2/window.ui             | 18 ++---
 examples/book/buildapp/step3/window.ui             | 19 ++---
 .../book/buildapp/step4/exampleapp.gresource.xml   |  2 +-
 examples/book/buildapp/step4/exampleapplication.cc | 20 +----
 examples/book/buildapp/step4/exampleappwindow.cc   | 87 +++++++++++++++++++++-
 examples/book/buildapp/step4/exampleappwindow.h    | 39 +++++++++-
 .../buildapp/step4/{app_menu.ui => gears_menu.ui}  |  3 +-
 examples/book/buildapp/step4/window.ui             | 24 +++---
 .../book/buildapp/step5/exampleapp.gresource.xml   |  2 +-
 examples/book/buildapp/step5/exampleapplication.cc | 20 +----
 examples/book/buildapp/step5/exampleappwindow.cc   | 18 ++++-
 examples/book/buildapp/step5/exampleappwindow.h    |  1 +
 .../buildapp/step5/{app_menu.ui => gears_menu.ui}  |  3 +-
 examples/book/buildapp/step5/window.ui             | 24 +++---
 .../book/buildapp/step6/exampleapp.gresource.xml   |  2 +-
 examples/book/buildapp/step6/exampleappwindow.cc   | 17 +++++
 examples/book/buildapp/step6/exampleappwindow.h    |  1 +
 .../buildapp/step6/{app_menu.ui => gears_menu.ui}  |  3 +-
 examples/book/buildapp/step6/window.ui             | 36 +++++----
 examples/book/buildapp/step7/app_menu.ui           | 18 -----
 .../book/buildapp/step7/exampleapp.gresource.xml   |  1 -
 examples/book/buildapp/step7/gears_menu.ui         | 11 ++-
 examples/book/buildapp/step7/window.ui             | 44 ++++++-----
 examples/book/buildapp/step8/app_menu.ui           | 18 -----
 .../book/buildapp/step8/exampleapp.gresource.xml   |  1 -
 examples/book/buildapp/step8/gears_menu.ui         | 11 ++-
 examples/book/buildapp/step8/window.ui             | 84 ++++++++++-----------
 examples/book/buildapp/step9/app_menu.ui           | 18 -----
 .../book/buildapp/step9/exampleapp.gresource.xml   |  1 -
 examples/book/buildapp/step9/gears_menu.ui         | 11 ++-
 examples/book/buildapp/step9/window.ui             |  3 -
 .../book/menus/main_menu/exampleapplication.cc     | 51 ++-----------
 examples/book/menus/popup/examplewindow.cc         |  1 +
 examples/book/meson.build                          |  2 +-
 40 files changed, 354 insertions(+), 299 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index b4dcde3..e7954db 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -12,7 +12,7 @@ check_PROGRAMS =                                      \
        book/actionbar/example                          \
        book/application/simple/example \
        book/application/command_line_handling/example \
-       book/application/app_and_win_menus/example \
+       book/application/menubar/example \
        book/aspectframe/example                        \
        book/assistant/example                          \
        book/base/base                                  \
@@ -159,12 +159,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_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_application_menubar_example_SOURCES =             \
+       book/application/menubar/exampleapplication.cc  \
+       book/application/menubar/exampleapplication.h   \
+       book/application/menubar/examplewindow.cc       \
+       book/application/menubar/examplewindow.h        \
+       book/application/menubar/main.cc
 
 book_aspectframe_example_SOURCES =             \
        book/aspectframe/examplewindow.cc       \
diff --git a/examples/book/application/app_and_win_menus/exampleapplication.cc 
b/examples/book/application/menubar/exampleapplication.cc
similarity index 95%
rename from examples/book/application/app_and_win_menus/exampleapplication.cc
rename to examples/book/application/menubar/exampleapplication.cc
index 4de079c..10a82dc 100644
--- a/examples/book/application/app_and_win_menus/exampleapplication.cc
+++ b/examples/book/application/menubar/exampleapplication.cc
@@ -34,29 +34,22 @@ void ExampleApplication::on_startup()
   //Call the base class's implementation:
   Gtk::Application::on_startup();
 
-  //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("quit",
-    sigc::mem_fun(*this, &ExampleApplication::on_action_quit) );
-
-
-  auto app_menu = Gio::Menu::create();
-  app_menu->append("_Something", "app.something");
-  app_menu->append("_Quit", "app.quit");
-
-  set_app_menu(app_menu);
-
-
-  //Create the per-Window menu:
+  //Create the menu:
   auto win_menu = Gio::Menu::create();
 
   auto submenu_file = Gio::Menu::create();
   submenu_file->append("_New", "win.new");
   submenu_file->append("_Close", "win.close");
+  submenu_file->append("_Something", "app.something");
+  submenu_file->append("_Quit", "app.quit");
   win_menu->append_submenu("File", submenu_file);
 
+  //We can use add_action because Gtk::Application derives from ActionMap:
+  add_action("something",
+    sigc::mem_fun(*this, &ExampleApplication::on_action_something) );
+  add_action("quit",
+    sigc::mem_fun(*this, &ExampleApplication::on_action_quit) );
+
   auto submenu_edit = Gio::Menu::create();
   submenu_edit->append("_Cut", "win.cut");
   auto item = Gio::MenuItem::create("_Copy", "win.copy");
diff --git a/examples/book/application/app_and_win_menus/exampleapplication.h 
b/examples/book/application/menubar/exampleapplication.h
similarity index 100%
rename from examples/book/application/app_and_win_menus/exampleapplication.h
rename to examples/book/application/menubar/exampleapplication.h
diff --git a/examples/book/application/app_and_win_menus/examplewindow.cc 
b/examples/book/application/menubar/examplewindow.cc
similarity index 100%
rename from examples/book/application/app_and_win_menus/examplewindow.cc
rename to examples/book/application/menubar/examplewindow.cc
diff --git a/examples/book/application/app_and_win_menus/examplewindow.h 
b/examples/book/application/menubar/examplewindow.h
similarity index 100%
rename from examples/book/application/app_and_win_menus/examplewindow.h
rename to examples/book/application/menubar/examplewindow.h
diff --git a/examples/book/application/app_and_win_menus/main.cc b/examples/book/application/menubar/main.cc
similarity index 100%
rename from examples/book/application/app_and_win_menus/main.cc
rename to examples/book/application/menubar/main.cc
diff --git a/examples/book/buildapp/step2/window.ui b/examples/book/buildapp/step2/window.ui
index eafa5e1..39bd69e 100644
--- a/examples/book/buildapp/step2/window.ui
+++ b/examples/book/buildapp/step2/window.ui
@@ -5,18 +5,18 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkStack" id="stack"/>
         </child>
diff --git a/examples/book/buildapp/step3/window.ui b/examples/book/buildapp/step3/window.ui
index eafa5e1..23792f0 100644
--- a/examples/book/buildapp/step3/window.ui
+++ b/examples/book/buildapp/step3/window.ui
@@ -5,18 +5,19 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkStack" id="stack"/>
         </child>
diff --git a/examples/book/buildapp/step4/exampleapp.gresource.xml 
b/examples/book/buildapp/step4/exampleapp.gresource.xml
index a4c7c2d..c6924e8 100644
--- a/examples/book/buildapp/step4/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step4/exampleapp.gresource.xml
@@ -2,6 +2,6 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
+    <file preprocess="xml-stripblanks">gears_menu.ui</file>
   </gresource>
 </gresources>
diff --git a/examples/book/buildapp/step4/exampleapplication.cc 
b/examples/book/buildapp/step4/exampleapplication.cc
index df4c41f..3fdbf1d 100644
--- a/examples/book/buildapp/step4/exampleapplication.cc
+++ b/examples/book/buildapp/step4/exampleapplication.cc
@@ -52,28 +52,10 @@ void ExampleApplication::on_startup()
   // Call the base class's implementation.
   Gtk::Application::on_startup();
 
-  // Add actions and keyboard accelerators for the application menu.
+  // Add actions and keyboard accelerators for the menu.
   add_action("preferences", sigc::mem_fun(*this, &ExampleApplication::on_action_preferences));
   add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_action_quit));
   set_accel_for_action("app.quit", "<Ctrl>Q");
-
-  auto refBuilder = Gtk::Builder::create();
-  try
-  {
-    refBuilder->add_from_resource("/org/gtkmm/exampleapp/app_menu.ui");
-  }
-  catch (const Glib::Error& ex)
-  {
-    std::cerr << "ExampleApplication::on_startup(): " << ex.what() << std::endl;
-    return;
-  }
-
-  auto app_menu = refBuilder->get_object<Gio::MenuModel>("appmenu");
-  if (app_menu)
-    set_app_menu(app_menu);
-  else
-    std::cerr << "ExampleApplication::on_startup(): No \"appmenu\" object in app_menu.ui"
-              << std::endl;
 }
 
 void ExampleApplication::on_activate()
diff --git a/examples/book/buildapp/step4/exampleappwindow.cc 
b/examples/book/buildapp/step4/exampleappwindow.cc
index 2091bf6..1795bcf 100644
--- a/examples/book/buildapp/step4/exampleappwindow.cc
+++ b/examples/book/buildapp/step4/exampleappwindow.cc
@@ -1,2 +1,85 @@
-#include "../step3/exampleappwindow.cc"
-// Equal to the corresponding file in step3
+/* gtkmm example Copyright (C) 2016 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 "exampleappwindow.h"
+#include <iostream>
+#include <stdexcept>
+
+ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
+  const Glib::RefPtr<Gtk::Builder>& refBuilder)
+: Gtk::ApplicationWindow(cobject),
+  m_refBuilder(refBuilder),
+  m_stack(nullptr),
+  m_gears(nullptr)
+{
+  // Get widgets from the Gtk::Builder file.
+  m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
+  if (!m_stack)
+    throw std::runtime_error("No \"stack\" object in window.ui");
+
+  m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
+  if (!m_gears)
+    throw std::runtime_error("No \"gears\" object in window.ui");
+
+  // Connect the menu to the MenuButton m_gears.
+  // (The connection between action and menu item is specified in gears_menu.ui.)
+  auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
+  auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
+  if (!menu)
+    throw std::runtime_error("No \"menu\" object in gears_menu.ui");
+
+  m_gears->set_menu_model(menu);
+}
+
+//static
+ExampleAppWindow* ExampleAppWindow::create()
+{
+  // Load the Builder file and instantiate its widgets.
+  auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
+
+  auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
+  if (!window)
+    throw std::runtime_error("No \"app_window\" object in window.ui");
+
+  return window;
+}
+
+void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
+{
+  const Glib::ustring basename = file->get_basename();
+
+  auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
+  scrolled->set_expand(true);
+  auto view = Gtk::make_managed<Gtk::TextView>();
+  view->set_editable(false);
+  view->set_cursor_visible(false);
+  scrolled->set_child(*view);
+  m_stack->add(*scrolled, basename, basename);
+
+  try
+  {
+    char* contents = nullptr;
+    gsize length = 0;
+    
+    file->load_contents(contents, length);
+    view->get_buffer()->set_text(contents, contents+length);
+    g_free(contents);
+  }
+  catch (const Glib::Error& ex)
+  {
+    std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
+      << "\"):\n  " << ex.what() << std::endl;
+  }
+}
diff --git a/examples/book/buildapp/step4/exampleappwindow.h b/examples/book/buildapp/step4/exampleappwindow.h
index d8bc1ee..a4b0f26 100644
--- a/examples/book/buildapp/step4/exampleappwindow.h
+++ b/examples/book/buildapp/step4/exampleappwindow.h
@@ -1,2 +1,37 @@
-#include "../step3/exampleappwindow.h"
-// Equal to the corresponding file in step3
+/* gtkmm example Copyright (C) 2016 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_EXAMPLEAPPWINDOW_H_
+#define GTKMM_EXAMPLEAPPWINDOW_H_
+
+#include <gtkmm.h>
+
+class ExampleAppWindow : public Gtk::ApplicationWindow
+{
+public:
+  ExampleAppWindow(BaseObjectType* cobject,
+    const Glib::RefPtr<Gtk::Builder>& refBuilder);
+
+  static ExampleAppWindow* create();
+
+  void open_file_view(const Glib::RefPtr<Gio::File>& file);
+
+protected:
+  Glib::RefPtr<Gtk::Builder> m_refBuilder;
+  Gtk::Stack* m_stack;
+  Gtk::MenuButton* m_gears;
+};
+
+#endif /* GTKMM_EXAMPLEAPPWINDOW_H */
diff --git a/examples/book/buildapp/step4/app_menu.ui b/examples/book/buildapp/step4/gears_menu.ui
similarity index 87%
rename from examples/book/buildapp/step4/app_menu.ui
rename to examples/book/buildapp/step4/gears_menu.ui
index e1e131c..46f42ba 100644
--- a/examples/book/buildapp/step4/app_menu.ui
+++ b/examples/book/buildapp/step4/gears_menu.ui
@@ -1,7 +1,6 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
-  <menu id="appmenu">
+  <menu id="menu">
     <section>
       <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
diff --git a/examples/book/buildapp/step4/window.ui b/examples/book/buildapp/step4/window.ui
index eafa5e1..1bf8334 100644
--- a/examples/book/buildapp/step4/window.ui
+++ b/examples/book/buildapp/step4/window.ui
@@ -5,18 +5,24 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkMenuButton" id="gears">
+            <property name="direction">none</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkStack" id="stack"/>
         </child>
diff --git a/examples/book/buildapp/step5/exampleapp.gresource.xml 
b/examples/book/buildapp/step5/exampleapp.gresource.xml
index 579ab8c..9c9e437 100644
--- a/examples/book/buildapp/step5/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step5/exampleapp.gresource.xml
@@ -2,7 +2,7 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
+    <file preprocess="xml-stripblanks">gears_menu.ui</file>
     <file preprocess="xml-stripblanks">prefs.ui</file>
   </gresource>
 </gresources>
diff --git a/examples/book/buildapp/step5/exampleapplication.cc 
b/examples/book/buildapp/step5/exampleapplication.cc
index 0d32a51..9253c70 100644
--- a/examples/book/buildapp/step5/exampleapplication.cc
+++ b/examples/book/buildapp/step5/exampleapplication.cc
@@ -53,28 +53,10 @@ void ExampleApplication::on_startup()
   // Call the base class's implementation.
   Gtk::Application::on_startup();
 
-  // Add actions and keyboard accelerators for the application menu.
+  // Add actions and keyboard accelerators for the menu.
   add_action("preferences", sigc::mem_fun(*this, &ExampleApplication::on_action_preferences));
   add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_action_quit));
   set_accel_for_action("app.quit", "<Ctrl>Q");
-
-  auto refBuilder = Gtk::Builder::create();
-  try
-  {
-    refBuilder->add_from_resource("/org/gtkmm/exampleapp/app_menu.ui");
-  }
-  catch (const Glib::Error& ex)
-  {
-    std::cerr << "ExampleApplication::on_startup(): " << ex.what() << std::endl;
-    return;
-  }
-
-  auto app_menu = refBuilder->get_object<Gio::MenuModel>("appmenu");
-  if (app_menu)
-    set_app_menu(app_menu);
-  else
-    std::cerr << "ExampleApplication::on_startup(): No \"appmenu\" object in app_menu.ui"
-              << std::endl;
 }
 
 void ExampleApplication::on_activate()
diff --git a/examples/book/buildapp/step5/exampleappwindow.cc 
b/examples/book/buildapp/step5/exampleappwindow.cc
index 50b62e1..8b6a23b 100644
--- a/examples/book/buildapp/step5/exampleappwindow.cc
+++ b/examples/book/buildapp/step5/exampleappwindow.cc
@@ -22,14 +22,30 @@ ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
 : Gtk::ApplicationWindow(cobject),
   m_refBuilder(refBuilder),
   m_settings(),
-  m_stack(nullptr)
+  m_stack(nullptr),
+  m_gears(nullptr)
 {
+  // Get widgets from the Gtk::Builder file.
   m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
   if (!m_stack)
     throw std::runtime_error("No \"stack\" object in window.ui");
 
+  m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
+  if (!m_gears)
+    throw std::runtime_error("No \"gears\" object in window.ui");
+
+  // Bind settings.
   m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
   m_settings->bind("transition", m_stack->property_transition_type());
+
+  // Connect the menu to the MenuButton m_gears.
+  // (The connection between action and menu item is specified in gears_menu.ui.)
+  auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
+  auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
+  if (!menu)
+    throw std::runtime_error("No \"menu\" object in gears_menu.ui");
+
+  m_gears->set_menu_model(menu);
 }
 
 //static
diff --git a/examples/book/buildapp/step5/exampleappwindow.h b/examples/book/buildapp/step5/exampleappwindow.h
index 284d8e6..b90b752 100644
--- a/examples/book/buildapp/step5/exampleappwindow.h
+++ b/examples/book/buildapp/step5/exampleappwindow.h
@@ -32,6 +32,7 @@ protected:
   Glib::RefPtr<Gtk::Builder> m_refBuilder;
   Glib::RefPtr<Gio::Settings> m_settings;
   Gtk::Stack* m_stack;
+  Gtk::MenuButton* m_gears;
 };
 
 #endif /* GTKMM_EXAMPLEAPPWINDOW_H */
diff --git a/examples/book/buildapp/step5/app_menu.ui b/examples/book/buildapp/step5/gears_menu.ui
similarity index 87%
rename from examples/book/buildapp/step5/app_menu.ui
rename to examples/book/buildapp/step5/gears_menu.ui
index e1e131c..46f42ba 100644
--- a/examples/book/buildapp/step5/app_menu.ui
+++ b/examples/book/buildapp/step5/gears_menu.ui
@@ -1,7 +1,6 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
-  <menu id="appmenu">
+  <menu id="menu">
     <section>
       <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
diff --git a/examples/book/buildapp/step5/window.ui b/examples/book/buildapp/step5/window.ui
index eafa5e1..1bf8334 100644
--- a/examples/book/buildapp/step5/window.ui
+++ b/examples/book/buildapp/step5/window.ui
@@ -5,18 +5,24 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkMenuButton" id="gears">
+            <property name="direction">none</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkStack" id="stack"/>
         </child>
diff --git a/examples/book/buildapp/step6/exampleapp.gresource.xml 
b/examples/book/buildapp/step6/exampleapp.gresource.xml
index 579ab8c..9c9e437 100644
--- a/examples/book/buildapp/step6/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step6/exampleapp.gresource.xml
@@ -2,7 +2,7 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
+    <file preprocess="xml-stripblanks">gears_menu.ui</file>
     <file preprocess="xml-stripblanks">prefs.ui</file>
   </gresource>
 </gresources>
diff --git a/examples/book/buildapp/step6/exampleappwindow.cc 
b/examples/book/buildapp/step6/exampleappwindow.cc
index 2be1f78..5f8b286 100644
--- a/examples/book/buildapp/step6/exampleappwindow.cc
+++ b/examples/book/buildapp/step6/exampleappwindow.cc
@@ -26,8 +26,10 @@ ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
   m_search(nullptr),
   m_searchbar(nullptr),
   m_searchentry(nullptr),
+  m_gears(nullptr),
   m_prop_binding()
 {
+  // Get widgets from the Gtk::Builder file.
   m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
   if (!m_stack)
     throw std::runtime_error("No \"stack\" object in window.ui");
@@ -44,9 +46,15 @@ ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
   if (!m_searchentry)
     throw std::runtime_error("No \"searchentry\" object in window.ui");
 
+  m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
+  if (!m_gears)
+    throw std::runtime_error("No \"gears\" object in window.ui");
+
+  // Bind settings.
   m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
   m_settings->bind("transition", m_stack->property_transition_type());
 
+  // Bind properties.
   m_prop_binding = Glib::Binding::bind_property(m_search->property_active(),
     m_searchbar->property_search_mode_enabled(), Glib::Binding::Flags::BIDIRECTIONAL);
 
@@ -55,6 +63,15 @@ ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
     sigc::mem_fun(*this, &ExampleAppWindow::on_search_text_changed));
   m_stack->property_visible_child().signal_changed().connect(
     sigc::mem_fun(*this, &ExampleAppWindow::on_visible_child_changed));
+
+  // Connect the menu to the MenuButton m_gears.
+  // (The connection between action and menu item is specified in gears_menu.ui.)
+  auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
+  auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
+  if (!menu)
+    throw std::runtime_error("No \"menu\" object in gears_menu.ui");
+
+  m_gears->set_menu_model(menu);
 }
 
 //static
diff --git a/examples/book/buildapp/step6/exampleappwindow.h b/examples/book/buildapp/step6/exampleappwindow.h
index 9bcd18b..954ca68 100644
--- a/examples/book/buildapp/step6/exampleappwindow.h
+++ b/examples/book/buildapp/step6/exampleappwindow.h
@@ -39,6 +39,7 @@ protected:
   Gtk::ToggleButton* m_search;
   Gtk::SearchBar* m_searchbar;
   Gtk::SearchEntry* m_searchentry;
+  Gtk::MenuButton* m_gears;
   Glib::RefPtr<Glib::Binding> m_prop_binding;
 };
 
diff --git a/examples/book/buildapp/step6/app_menu.ui b/examples/book/buildapp/step6/gears_menu.ui
similarity index 87%
rename from examples/book/buildapp/step6/app_menu.ui
rename to examples/book/buildapp/step6/gears_menu.ui
index e1e131c..46f42ba 100644
--- a/examples/book/buildapp/step6/app_menu.ui
+++ b/examples/book/buildapp/step6/gears_menu.ui
@@ -1,7 +1,6 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
-  <menu id="appmenu">
+  <menu id="menu">
     <section>
       <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
diff --git a/examples/book/buildapp/step6/window.ui b/examples/book/buildapp/step6/window.ui
index 6c90528..c99e281 100644
--- a/examples/book/buildapp/step6/window.ui
+++ b/examples/book/buildapp/step6/window.ui
@@ -5,24 +5,30 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkMenuButton" id="gears">
+            <property name="direction">none</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkToggleButton" id="search">
+            <property name="sensitive">False</property>
+            <property name="icon-name">edit-find-symbolic</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-            <child type="end">
-              <object class="GtkToggleButton" id="search">
-                <property name="sensitive">False</property>
-                <property name="icon-name">edit-find-symbolic</property>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkSearchBar" id="searchbar">
             <child>
diff --git a/examples/book/buildapp/step7/exampleapp.gresource.xml 
b/examples/book/buildapp/step7/exampleapp.gresource.xml
index dc39879..9c9e437 100644
--- a/examples/book/buildapp/step7/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step7/exampleapp.gresource.xml
@@ -2,7 +2,6 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
     <file preprocess="xml-stripblanks">gears_menu.ui</file>
     <file preprocess="xml-stripblanks">prefs.ui</file>
   </gresource>
diff --git a/examples/book/buildapp/step7/gears_menu.ui b/examples/book/buildapp/step7/gears_menu.ui
index 7e5df31..abb4d04 100644
--- a/examples/book/buildapp/step7/gears_menu.ui
+++ b/examples/book/buildapp/step7/gears_menu.ui
@@ -1,12 +1,21 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
   <menu id="menu">
     <section>
       <item>
         <attribute name="label" translatable="yes">_Words</attribute>
         <attribute name="action">win.show-words</attribute>
       </item>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+      </item>
     </section>
   </menu>
 </interface>
diff --git a/examples/book/buildapp/step7/window.ui b/examples/book/buildapp/step7/window.ui
index b2a4e64..9c80d20 100644
--- a/examples/book/buildapp/step7/window.ui
+++ b/examples/book/buildapp/step7/window.ui
@@ -5,32 +5,30 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkToggleButton" id="search">
+            <property name="sensitive">False</property>
+            <property name="icon-name">edit-find-symbolic</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkMenuButton" id="gears">
+            <property name="direction">none</property>
+          </object>
+        </child>
+      </object>
+    </child>
     <child>
       <object class="GtkBox" id="content_box">
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkHeaderBar" id="header">
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-            <child type="end">
-              <object class="GtkToggleButton" id="search">
-                <property name="sensitive">False</property>
-                <property name="icon-name">edit-find-symbolic</property>
-              </object>
-            </child>
-            <child type="end">
-              <object class="GtkMenuButton" id="gears">
-                <property name="direction">none</property>
-                <style>
-                  <class name="image-button"/>
-                </style>
-              </object>
-            </child>
-          </object>
-        </child>
         <child>
           <object class="GtkSearchBar" id="searchbar">
             <child>
diff --git a/examples/book/buildapp/step8/exampleapp.gresource.xml 
b/examples/book/buildapp/step8/exampleapp.gresource.xml
index dc39879..9c9e437 100644
--- a/examples/book/buildapp/step8/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step8/exampleapp.gresource.xml
@@ -2,7 +2,6 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
     <file preprocess="xml-stripblanks">gears_menu.ui</file>
     <file preprocess="xml-stripblanks">prefs.ui</file>
   </gresource>
diff --git a/examples/book/buildapp/step8/gears_menu.ui b/examples/book/buildapp/step8/gears_menu.ui
index fb0e929..fe74f79 100644
--- a/examples/book/buildapp/step8/gears_menu.ui
+++ b/examples/book/buildapp/step8/gears_menu.ui
@@ -1,6 +1,5 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
   <menu id="menu">
     <section>
       <item>
@@ -11,6 +10,16 @@
         <attribute name="label" translatable="yes">_Lines</attribute>
         <attribute name="action">win.show-lines</attribute>
       </item>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+      </item>
     </section>
   </menu>
 </interface>
diff --git a/examples/book/buildapp/step8/window.ui b/examples/book/buildapp/step8/window.ui
index 0b4f2e9..5232c64 100644
--- a/examples/book/buildapp/step8/window.ui
+++ b/examples/book/buildapp/step8/window.ui
@@ -5,43 +5,41 @@
     <property name="default-width">600</property>
     <property name="default-height">400</property>
     <property name="hide-on-close">True</property>
-    <child>
-      <object class="GtkBox" id="content_box">
-        <property name="orientation">vertical</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="show-title-buttons">True</property>
         <child>
-          <object class="GtkHeaderBar" id="header">
-            <child>
-              <object class="GtkLabel" id="lines_label">
-                <property name="visible">False</property>
-                <property name="label" translatable="yes">Lines:</property>
-              </object>
-            </child>
-            <child>
-              <object class="GtkLabel" id="lines">
-                <property name="visible">False</property>
-              </object>
-            </child>
-            <child type="title">
-              <object class="GtkStackSwitcher" id="tabs">
-                <property name="stack">stack</property>
-              </object>
-            </child>
-            <child type="end">
-              <object class="GtkToggleButton" id="search">
-                <property name="sensitive">False</property>
-                <property name="icon-name">edit-find-symbolic</property>
-              </object>
-            </child>
-            <child type="end">
-              <object class="GtkMenuButton" id="gears">
-                <property name="direction">none</property>
-                <style>
-                  <class name="image-button"/>
-                </style>
-              </object>
-            </child>
+          <object class="GtkLabel" id="lines_label">
+            <property name="visible">False</property>
+            <property name="label" translatable="yes">Lines:</property>
           </object>
         </child>
+        <child>
+          <object class="GtkLabel" id="lines">
+            <property name="visible">False</property>
+          </object>
+        </child>
+        <child type="title">
+          <object class="GtkStackSwitcher" id="tabs">
+            <property name="stack">stack</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkToggleButton" id="search">
+            <property name="sensitive">False</property>
+            <property name="icon-name">edit-find-symbolic</property>
+          </object>
+        </child>
+        <child type="end">
+          <object class="GtkMenuButton" id="gears">
+            <property name="direction">none</property>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkBox" id="content_box">
+        <property name="orientation">vertical</property>
         <child>
           <object class="GtkSearchBar" id="searchbar">
             <child>
@@ -56,15 +54,15 @@
               <object class="GtkRevealer" id="sidebar">
                 <property name="transition-type">slide-right</property>
                 <child>
-                 <object class="GtkScrolledWindow" id="sidebar-sw">
-                   <property name="hscrollbar-policy">never</property>
-                   <property name="vscrollbar-policy">automatic</property>
-                   <child>
-                     <object class="GtkListBox" id="words">
-                       <property name="selection-mode">none</property>
-                     </object>
-                   </child>
-                 </object>
+                  <object class="GtkScrolledWindow" id="sidebar-sw">
+                    <property name="hscrollbar-policy">never</property>
+                    <property name="vscrollbar-policy">automatic</property>
+                    <child>
+                      <object class="GtkListBox" id="words">
+                        <property name="selection-mode">none</property>
+                      </object>
+                    </child>
+                  </object>
                 </child>
               </object>
             </child>
diff --git a/examples/book/buildapp/step9/exampleapp.gresource.xml 
b/examples/book/buildapp/step9/exampleapp.gresource.xml
index 4ffa168..10dcd4f 100644
--- a/examples/book/buildapp/step9/exampleapp.gresource.xml
+++ b/examples/book/buildapp/step9/exampleapp.gresource.xml
@@ -2,7 +2,6 @@
 <gresources>
   <gresource prefix="/org/gtkmm/exampleapp">
     <file preprocess="xml-stripblanks">window.ui</file>
-    <file preprocess="xml-stripblanks">app_menu.ui</file>
     <file preprocess="xml-stripblanks">gears_menu.ui</file>
     <file preprocess="xml-stripblanks">prefs.ui</file>
     <file>exampleapp.png</file>
diff --git a/examples/book/buildapp/step9/gears_menu.ui b/examples/book/buildapp/step9/gears_menu.ui
index fb0e929..fe74f79 100644
--- a/examples/book/buildapp/step9/gears_menu.ui
+++ b/examples/book/buildapp/step9/gears_menu.ui
@@ -1,6 +1,5 @@
 <?xml version="1.0"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
   <menu id="menu">
     <section>
       <item>
@@ -11,6 +10,16 @@
         <attribute name="label" translatable="yes">_Lines</attribute>
         <attribute name="action">win.show-lines</attribute>
       </item>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+      </item>
     </section>
   </menu>
 </interface>
diff --git a/examples/book/buildapp/step9/window.ui b/examples/book/buildapp/step9/window.ui
index 20b5323..7337c7e 100644
--- a/examples/book/buildapp/step9/window.ui
+++ b/examples/book/buildapp/step9/window.ui
@@ -34,9 +34,6 @@
         <child type="end">
           <object class="GtkMenuButton" id="gears">
             <property name="direction">none</property>
-            <style>
-              <class name="image-button"/>
-            </style>
           </object>
         </child>
       </object>
diff --git a/examples/book/menus/main_menu/exampleapplication.cc 
b/examples/book/menus/main_menu/exampleapplication.cc
index 45ac460..1569aca 100644
--- a/examples/book/menus/main_menu/exampleapplication.cc
+++ b/examples/book/menus/main_menu/exampleapplication.cc
@@ -54,7 +54,7 @@ void ExampleApplication::on_startup()
 
   m_refBuilder = Gtk::Builder::create();
 
-  //Layout the actions in a menubar and an application menu:
+  //Layout the actions in a menubar and a menu:
   Glib::ustring ui_info =
     "<interface>"
     "  <!-- menubar -->"
@@ -143,45 +143,11 @@ void ExampleApplication::on_startup()
     "      <attribute name='label' translatable='yes'>_Help</attribute>"
     "      <section>"
     "        <item>"
-    "          <attribute name='label' translatable='yes'>_About</attribute>"
+    "          <attribute name='label' translatable='yes'>_About Window</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='label' translatable='yes'>_About App</attribute>"
     "          <attribute name='action'>app.about</attribute>"
     "        </item>"
     "      </section>"
@@ -201,14 +167,12 @@ void ExampleApplication::on_startup()
   //Get the menubar and the app menu, and add them to the application:
   auto object = m_refBuilder->get_object("menu-example");
   auto gmenu = std::dynamic_pointer_cast<Gio::Menu>(object);
-  object = m_refBuilder->get_object("appmenu");
-  auto appMenu = std::dynamic_pointer_cast<Gio::Menu>(object);
-  if (!(gmenu && appMenu)) {
-    g_warning("GMenu or AppMenu not found");
+  if (!gmenu)
+  {
+    g_warning("GMenu not found");
   }
   else
   {
-    set_app_menu(appMenu);
     set_menubar(gmenu);
   }
 }
@@ -234,6 +198,7 @@ void ExampleApplication::create_window()
   win->signal_hide().connect(sigc::bind(
     sigc::mem_fun(*this, &ExampleApplication::on_window_hide), win));
 
+  win->set_show_menubar();
   win->show();
 }
 
@@ -266,5 +231,5 @@ void ExampleApplication::on_menu_file_quit()
 
 void ExampleApplication::on_menu_help_about()
 {
-  std::cout << "App|Help|About was selected." << std::endl;
+  std::cout << "Help|About App was selected." << std::endl;
 }
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index 8b0f83d..3a07e7a 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -106,6 +106,7 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
 
 ExampleWindow::~ExampleWindow()
 {
+  m_MenuPopup.unparent();
 }
 
 void ExampleWindow::on_menu_file_popup_generic()
diff --git a/examples/book/meson.build b/examples/book/meson.build
index c481851..bccf985 100644
--- a/examples/book/meson.build
+++ b/examples/book/meson.build
@@ -16,7 +16,7 @@ examples_book = [
   [['actionbar'], 'example', exwindow_main],
   [['application', 'simple'], 'example', ['exampleapplication.cc'] + exwindow_main],
   [['application', 'command_line_handling'], 'example', ['exampleapplication.cc'] + exwindow_main],
-  [['application', 'app_and_win_menus'], 'example', ['exampleapplication.cc'] + exwindow_main],
+  [['application', 'menubar'], 'example', ['exampleapplication.cc'] + exwindow_main],
   [['aspectframe'], 'example', exwindow_main],
   [['assistant'], 'example', ['exampleassistant.cc'] + exwindow_main],
   [['base'], 'base', ['base.cc']],



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