[gtkmm-documentation] Update the menus/main_menu and menus_and_toolbars examples



commit 1e6ad36c413806c4abbe8e1aadf8a9caadefce6e
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Tue Nov 25 18:50:28 2014 +0100

    Update the menus/main_menu and menus_and_toolbars examples
    
    * docs/tutorial/C/figures/main_menu.png:
    * docs/tutorial/C/figures/menus_and_toolbars.png: Updated pictures.
    * examples/book/menus/main_menu/examplewindow.[h|cc]: Use Gtk::Builder to
    create the toolbar.
    * examples/book/menus_and_toolbars/examplewindow.[h|cc]: Use Gtk::Builder to
    create the toolbar. Add the Rain tool button, which was deleted when
    Gtk::UIManager was abandoned. Use Gtk::Application::set_accel_for_action().
    * examples/book/menus_and_toolbars/main.cc: Add the app to ExampleWindow's
    constructor.

 docs/tutorial/C/figures/main_menu.png             |  Bin 7158 -> 11187 bytes
 docs/tutorial/C/figures/menus_and_toolbars.png    |  Bin 7937 -> 8345 bytes
 examples/book/menus/main_menu/examplewindow.cc    |   73 ++++++++++---
 examples/book/menus/main_menu/examplewindow.h     |    2 +
 examples/book/menus_and_toolbars/examplewindow.cc |  117 +++++++++++++++------
 examples/book/menus_and_toolbars/examplewindow.h  |    4 +-
 examples/book/menus_and_toolbars/main.cc          |    4 +-
 7 files changed, 143 insertions(+), 57 deletions(-)
---
diff --git a/docs/tutorial/C/figures/main_menu.png b/docs/tutorial/C/figures/main_menu.png
index 974910f..fd8700b 100644
Binary files a/docs/tutorial/C/figures/main_menu.png and b/docs/tutorial/C/figures/main_menu.png differ
diff --git a/docs/tutorial/C/figures/menus_and_toolbars.png b/docs/tutorial/C/figures/menus_and_toolbars.png
index 6eca40a..2ea3074 100644
Binary files a/docs/tutorial/C/figures/menus_and_toolbars.png and 
b/docs/tutorial/C/figures/menus_and_toolbars.png differ
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index e9cb976..1f365c8 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -54,24 +54,61 @@ ExampleWindow::ExampleWindow()
   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("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("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);
+
+  m_refBuilder = Gtk::Builder::create();
+
+  Glib::ustring ui_info =
+    "<?xml version='1.0' encoding='UTF-8'?>"
+    "<!-- Generated with glade 3.18.3 -->"
+    "<interface>"
+    "  <requires lib='gtk+' version='3.12'/>"
+    "  <object class='GtkToolbar' id='toolbar'>"
+    "    <property name='visible'>True</property>"
+    "    <property name='can_focus'>False</property>"
+    "    <child>"
+    "      <object class='GtkToolButton' id='toolbutton_new'>"
+    "        <property name='visible'>True</property>"
+    "        <property name='can_focus'>False</property>"
+    "        <property name='tooltip_text' translatable='yes'>New Standard</property>"
+    "        <property name='action_name'>app.newstandard</property>"
+    "        <property name='icon_name'>document-new</property>"
+    "      </object>"
+    "      <packing>"
+    "        <property name='expand'>False</property>"
+    "        <property name='homogeneous'>True</property>"
+    "      </packing>"
+    "    </child>"
+    "    <child>"
+    "      <object class='GtkToolButton' id='toolbutton_quit'>"
+    "        <property name='visible'>True</property>"
+    "        <property name='can_focus'>False</property>"
+    "        <property name='tooltip_text' translatable='yes'>Quit</property>"
+    "        <property name='action_name'>app.quit</property>"
+    "        <property name='icon_name'>application-exit</property>"
+    "      </object>"
+    "      <packing>"
+    "        <property name='expand'>False</property>"
+    "        <property name='homogeneous'>True</property>"
+    "      </packing>"
+    "    </child>"
+    "  </object>"
+    "</interface>";
+
+  try
+  {
+    m_refBuilder->add_from_string(ui_info);
+  }
+  catch (const Glib::Error& ex)
+  {
+    std::cerr << "Building toolbar failed: " <<  ex.what();
+  }
+
+  Gtk::Toolbar* toolbar = 0;
+  m_refBuilder->get_widget("toolbar", toolbar);
+  if (!toolbar)
+    g_warning("GtkToolbar not found");
+  else
+    m_Box.pack_start(*toolbar, Gtk::PACK_SHRINK);
 }
 
 ExampleWindow::~ExampleWindow()
diff --git a/examples/book/menus/main_menu/examplewindow.h b/examples/book/menus/main_menu/examplewindow.h
index 4782757..5cd11ab 100644
--- a/examples/book/menus/main_menu/examplewindow.h
+++ b/examples/book/menus/main_menu/examplewindow.h
@@ -36,6 +36,8 @@ protected:
   //Child widgets:
   Gtk::Box m_Box;
 
+  Glib::RefPtr<Gtk::Builder> m_refBuilder;
+
   //Two sets of choices:
   Glib::RefPtr<Gio::SimpleAction> m_refChoice;
   Glib::RefPtr<Gio::SimpleAction> m_refChoiceOther;
diff --git a/examples/book/menus_and_toolbars/examplewindow.cc 
b/examples/book/menus_and_toolbars/examplewindow.cc
index 246aab2..32c9710 100644
--- a/examples/book/menus_and_toolbars/examplewindow.cc
+++ b/examples/book/menus_and_toolbars/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,7 +18,7 @@
 #include <gtkmm.h>
 #include <iostream>
 
-ExampleWindow::ExampleWindow()
+ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
 : m_Box(Gtk::ORIENTATION_VERTICAL)
 {
   set_title("main_menu example");
@@ -55,9 +53,8 @@ ExampleWindow::ExampleWindow()
 
   //Define how the actions are presented in the menus and toolbars:
   Glib::RefPtr<Gtk::Builder> m_refBuilder = Gtk::Builder::create();
-  //TODO? add_accel_group(m_refBuilder->get_accel_group());
 
-  //Layout the actions in a menubar:
+  //Layout the actions in a menubar and toolbar:
   const char* ui_info =
     "<interface>"
     "  <menu id='menubar'>"
@@ -94,6 +91,7 @@ ExampleWindow::ExampleWindow()
     "      <item>"
     "        <attribute name='label' translatable='yes'>_Cut</attribute>"
     "        <attribute name='action'>example.cut</attribute>"
+    "        <attribute name='accel'>&lt;Primary&gt;x</attribute>"
     "      </item>"
     "      <item>"
     "        <attribute name='label' translatable='yes'>_Copy</attribute>"
@@ -106,7 +104,70 @@ ExampleWindow::ExampleWindow()
     "        <attribute name='accel'>&lt;Primary&gt;v</attribute>"
     "      </item>"
     "    </submenu>"
-    "  </menu>";
+    "  </menu>"
+    ""
+    "<!-- Generated with glade 3.18.3 -->"
+    "  <object class='GtkImage' id='image_rain'>"
+    "    <property name='visible'>True</property>"
+    "    <property name='can_focus'>False</property>"
+    "    <property name='pixbuf'>rain.png</property>"
+    "  </object>"
+    "  <object class='GtkToolbar' id='toolbar'>"
+    "    <property name='visible'>True</property>"
+    "    <property name='can_focus'>False</property>"
+    "    <child>"
+    "      <object class='GtkToolButton' id='toolbutton_new'>"
+    "        <property name='visible'>True</property>"
+    "        <property name='can_focus'>False</property>"
+    "        <property name='tooltip_text' translatable='yes'>New</property>"
+    "        <property name='action_name'>example.new</property>"
+    "        <property name='icon_name'>document-new</property>"
+    "      </object>"
+    "      <packing>"
+    "        <property name='expand'>False</property>"
+    "        <property name='homogeneous'>True</property>"
+    "      </packing>"
+    "    </child>"
+    "    <child>"
+    "      <object class='GtkToggleToolButton' id='toolbutton_rain'>"
+    "        <property name='visible'>True</property>"
+    "        <property name='can_focus'>False</property>"
+    "        <property name='tooltip_text' translatable='yes'>Stay dry in the rain</property>"
+    "        <property name='action_name'>example.rain</property>"
+    "        <property name='label_widget'>image_rain</property>"
+    "      </object>"
+    "      <packing>"
+    "        <property name='expand'>False</property>"
+    "        <property name='homogeneous'>True</property>"
+    "      </packing>"
+    "    </child>"
+    "    <child>"
+    "      <object class='GtkToolButton' id='toolbutton_quit'>"
+    "        <property name='visible'>True</property>"
+    "        <property name='can_focus'>False</property>"
+    "        <property name='tooltip_text' translatable='yes'>Quit</property>"
+    "        <property name='action_name'>example.quit</property>"
+    "        <property name='icon_name'>application-exit</property>"
+    "      </object>"
+    "      <packing>"
+    "        <property name='expand'>False</property>"
+    "        <property name='homogeneous'>True</property>"
+    "      </packing>"
+    "    </child>"
+    "  </object>"
+    "</interface>";
+
+  // When the menubar is a child of a Gtk::Window, keyboard accelerators are not
+  // automatically fetched from the Gio::Menu.
+  // See the examples/book/menus/main_menu example for an alternative way of
+  // adding the menubar when using Gtk::ApplicationWindow.
+  // Gtk::Application::set_accel_for_action() is new in gtkmm 3.11.9.
+  app->set_accel_for_action("example.new", "<Primary>n");
+  app->set_accel_for_action("example.open", "<Primary>o");
+  app->set_accel_for_action("example.quit", "<Primary>q");
+  app->set_accel_for_action("example.cut", "<Primary>x");
+  app->set_accel_for_action("example.copy", "<Primary>c");
+  app->set_accel_for_action("example.paste", "<Primary>v");
 
   try
   {
@@ -114,39 +175,29 @@ ExampleWindow::ExampleWindow()
   }
   catch(const Glib::Error& ex)
   {
-    std::cerr << "building menus failed: " <<  ex.what();
+    std::cerr << "Building menus and toolbar failed: " <<  ex.what();
   }
 
   //Get the menubar:
-  Glib::RefPtr<Glib::Object> object =
-    m_refBuilder->get_object("menubar");
-  Glib::RefPtr<Gio::Menu> gmenu =
-    Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
-  if(!gmenu)
+  Glib::RefPtr<Glib::Object> object = m_refBuilder->get_object("menubar");
+  Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+  if (!gmenu)
     g_warning("GMenu not found");
+  else
+  {
+    Gtk::MenuBar* pMenuBar = Gtk::manage(new Gtk::MenuBar(gmenu));
 
-  Gtk::MenuBar* pMenuBar = new Gtk::MenuBar(gmenu);
-
-  //Add the MenuBar to the window:
-  m_Box.pack_start(*pMenuBar, Gtk::PACK_SHRINK);
-
-
-  //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.new");
-  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");
-  toolbar->add(*button);
-
-  m_Box.pack_start(*toolbar, Gtk::PACK_SHRINK);
+    //Add the MenuBar to the window:
+    m_Box.pack_start(*pMenuBar, Gtk::PACK_SHRINK);
+  }
 
+  //Get the toolbar and add it to a container widget:
+  Gtk::Toolbar* toolbar = 0;
+  m_refBuilder->get_widget("toolbar", toolbar);
+  if (!toolbar)
+    g_warning("GtkToolbar not found");
+  else
+    m_Box.pack_start(*toolbar, Gtk::PACK_SHRINK);
 
   show_all_children();
 }
diff --git a/examples/book/menus_and_toolbars/examplewindow.h 
b/examples/book/menus_and_toolbars/examplewindow.h
index 796eac8..fcb6ce2 100644
--- a/examples/book/menus_and_toolbars/examplewindow.h
+++ b/examples/book/menus_and_toolbars/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
@@ -24,7 +22,7 @@
 class ExampleWindow : public Gtk::Window
 {
 public:
-  ExampleWindow();
+  ExampleWindow(const Glib::RefPtr<Gtk::Application>& app);
   virtual ~ExampleWindow();
 
 private:
diff --git a/examples/book/menus_and_toolbars/main.cc b/examples/book/menus_and_toolbars/main.cc
index c277339..c35ad14 100644
--- a/examples/book/menus_and_toolbars/main.cc
+++ b/examples/book/menus_and_toolbars/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
@@ -23,7 +21,7 @@ int main(int argc, char *argv[])
 {
   Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
 
-  ExampleWindow window;
+  ExampleWindow window(app);
 
   //Shows the window and returns when it is closed.
   return app->run(window);


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