[gtkmm-documentation] Replace some Gtk::UIManager uses with Gtk::Builder and Gio::Menu.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm-documentation] Replace some Gtk::UIManager uses with Gtk::Builder and Gio::Menu.
- Date: Thu, 26 Sep 2013 10:36:54 +0000 (UTC)
commit b57f7bd5a06f4e5ed0dd7d193a4850f320746637
Author: Murray Cumming <murrayc murrayc com>
Date: Mon Jul 22 20:48:39 2013 +0200
Replace some Gtk::UIManager uses with Gtk::Builder and Gio::Menu.
Because GtkUIManager and GtkAction have been deprecated.
examples/book/menus/main_menu/examplewindow.cc | 258 +++++++++++++++-------
examples/book/menus/main_menu/examplewindow.h | 14 +-
examples/book/menus/popup/examplewindow.cc | 77 ++++---
examples/book/menus/popup/examplewindow.h | 3 +-
examples/book/printing/advanced/examplewindow.cc | 85 ++++---
examples/book/printing/advanced/examplewindow.h | 3 +-
examples/book/printing/simple/examplewindow.cc | 92 +++++----
examples/book/printing/simple/examplewindow.h | 3 +-
8 files changed, 338 insertions(+), 197 deletions(-)
---
diff --git a/examples/book/menus/main_menu/examplewindow.cc b/examples/book/menus/main_menu/examplewindow.cc
index bc2b40e..f90f56d 100644
--- a/examples/book/menus/main_menu/examplewindow.cc
+++ b/examples/book/menus/main_menu/examplewindow.cc
@@ -29,94 +29,163 @@ ExampleWindow::ExampleWindow()
add(m_Box); // put a MenuBar at the top of the box and other stuff below it.
//Create actions for menus and toolbars:
- m_refActionGroup = Gtk::ActionGroup::create();
+ Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
+ Gio::SimpleActionGroup::create();
//File|New sub menu:
- m_refActionGroup->add(Gtk::Action::create("FileNewStandard",
- Gtk::Stock::NEW, "_New", "Create a new file"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+ refActionGroup->add_action("newstandard",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
- m_refActionGroup->add(Gtk::Action::create("FileNewFoo",
- Gtk::Stock::NEW, "New Foo", "Create a new foo"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+ refActionGroup->add_action("newfoo",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
- m_refActionGroup->add(Gtk::Action::create("FileNewGoo",
- Gtk::Stock::NEW, "_New Goo", "Create a new goo"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
+ refActionGroup->add_action("newgoo",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
//File menu:
- m_refActionGroup->add(Gtk::Action::create("FileMenu", "File"));
- //Sub-menu.
- m_refActionGroup->add(Gtk::Action::create("FileNew", Gtk::Stock::NEW));
- m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
+ refActionGroup->add_action("quit",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
//Edit menu:
- m_refActionGroup->add(Gtk::Action::create("EditMenu", "Edit"));
- m_refActionGroup->add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
- m_refActionGroup->add(Gtk::Action::create("EditPaste", Gtk::Stock::PASTE),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
- m_refActionGroup->add(Gtk::Action::create("EditSomething", "Something"),
- Gtk::AccelKey("<control><alt>S"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
-
-
- //Choices menu, to demonstrate Radio items
- m_refActionGroup->add( Gtk::Action::create("ChoicesMenu", "Choices") );
- Gtk::RadioAction::Group group_userlevel;
- m_refChoiceOne = Gtk::RadioAction::create(group_userlevel, "ChoiceOne", "One");
- m_refActionGroup->add(m_refChoiceOne,
- sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_one) );
- m_refChoiceTwo = Gtk::RadioAction::create(group_userlevel, "ChoiceTwo", "Two");
- m_refActionGroup->add(m_refChoiceTwo,
- sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_two) );
+ refActionGroup->add_action("copy",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+ refActionGroup->add_action("paste",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+ refActionGroup->add_action("something",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
+
+
+ //Choices menus, to demonstrate Radio items,
+ //using our convenience methods for string and int radio values:
+ m_refChoice = refActionGroup->add_action_radio_string("choice",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_choices),
+ "a");
+
+ m_refChoiceOther = refActionGroup->add_action_radio_integer("choiceother",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other),
+ 1);
+
+ m_refToggle = refActionGroup->add_action_bool("sometoggle",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle),
+ false);
+
//Help menu:
- m_refActionGroup->add( Gtk::Action::create("HelpMenu", "Help") );
- m_refActionGroup->add( Gtk::Action::create("HelpAbout", Gtk::Stock::HELP),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_others) );
+ refActionGroup->add_action("about",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_others) );
- m_refUIManager = Gtk::UIManager::create();
- m_refUIManager->insert_action_group(m_refActionGroup);
+ insert_action_group("example", refActionGroup);
- add_accel_group(m_refUIManager->get_accel_group());
+ 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 =
- "<ui>"
- " <menubar name='MenuBar'>"
- " <menu action='FileMenu'>"
- " <menu action='FileNew'>"
- " <menuitem action='FileNewStandard'/>"
- " <menuitem action='FileNewFoo'/>"
- " <menuitem action='FileNewGoo'/>"
- " </menu>"
- " <separator/>"
- " <menuitem action='FileQuit'/>"
- " </menu>"
- " <menu action='EditMenu'>"
- " <menuitem action='EditCopy'/>"
- " <menuitem action='EditPaste'/>"
- " <menuitem action='EditSomething'/>"
- " </menu>"
- " <menu action='ChoicesMenu'>"
- " <menuitem action='ChoiceOne'/>"
- " <menuitem action='ChoiceTwo'/>"
- " </menu>"
- " <menu action='HelpMenu'>"
- " <menuitem action='HelpAbout'/>"
- " </menu>"
- " </menubar>"
+ Glib::ustring ui_info =
+ "<interface>"
+ " <menu id='menu-example'>"
+ " <submenu>"
+ " <attribute name='label' translatable='yes'>_File</attribute>"
+ " <section>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>New _Standard</attribute>"
+ " <attribute name='action'>example.newstandard</attribute>"
+ " <attribute name='accel'><Primary>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'><Primary>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'><Primary>c</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>_Paste</attribute>"
+ " <attribute name='action'>example.paste</attribute>"
+ " <attribute name='accel'><Primary>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>";
+
+/* TODO:
" <toolbar name='ToolBar'>"
" <toolitem action='FileNewStandard'/>"
" <toolitem action='FileQuit'/>"
" </toolbar>"
- "</ui>";
+*/
try
{
- m_refUIManager->add_ui_from_string(ui_info);
+ m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
@@ -124,13 +193,22 @@ ExampleWindow::ExampleWindow()
}
//Get the menubar and toolbar widgets, and add them to a container widget:
- Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
- if(pMenubar)
- m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+ 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");
- Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar") ;
+ //Menubar:
+ Gtk::MenuBar* pMenubar = new Gtk::MenuBar(gmenu);
+ m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+
+/* TODO:
+ Gtk::Widget* pToolbar = m_refBuilder->get_widget("/ToolBar") ;
if(pToolbar)
m_Box.pack_start(*pToolbar, Gtk::PACK_SHRINK);
+*/
show_all_children();
}
@@ -154,24 +232,48 @@ void ExampleWindow::on_menu_others()
std::cout << "A menu item was selected." << std::endl;
}
-void ExampleWindow::on_menu_choices_one()
+void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
+{
+ //The radio action's state does not change automatically:
+ m_refChoice->change_state(parameter);
+
+ Glib::ustring message;
+ if(parameter == "a")
+ message = "Choice a was selected.";
+ else
+ message = "Choice b was selected";
+
+ std::cout << message << std::endl;
+}
+
+void ExampleWindow::on_menu_choices_other(int parameter)
{
+ //The radio action's state does not change automatically:
+ m_refChoice->change_state(parameter);
+
Glib::ustring message;
- if(m_refChoiceOne->get_active())
+ if(parameter == 1)
message = "Choice 1 was selected.";
else
- message = "Choice 1 was deselected";
+ message = "Choice 2 was selected";
std::cout << message << std::endl;
}
-void ExampleWindow::on_menu_choices_two()
+void ExampleWindow::on_menu_toggle()
{
+ bool active = false;
+ m_refToggle->get_state(active);
+
+ //The toggle action's state does not change automatically:
+ m_refToggle->change_state(!active);
+ active = !active;
+
Glib::ustring message;
- if(m_refChoiceTwo->get_active())
- message = "Choice 2 was selected.";
+ if(active)
+ message = "Toggle is active.";
else
- message = "Choice 2 was deselected";
+ 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 361e277..9bd84c5 100644
--- a/examples/book/menus/main_menu/examplewindow.h
+++ b/examples/book/menus/main_menu/examplewindow.h
@@ -33,15 +33,19 @@ protected:
void on_menu_file_quit();
void on_menu_others();
- void on_menu_choices_one();
- void on_menu_choices_two();
+ void on_menu_choices(const Glib::ustring& parameter);
+ void on_menu_choices_other(int parameter);
+ void on_menu_toggle();
//Child widgets:
Gtk::Box m_Box;
- Glib::RefPtr<Gtk::UIManager> m_refUIManager;
- Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
- Glib::RefPtr<Gtk::RadioAction> m_refChoiceOne, m_refChoiceTwo;
+ Glib::RefPtr<Gtk::Builder> m_refBuilder;
+
+ //Two sets of choices:
+ Glib::RefPtr<Gio::SimpleAction> m_refChoice, m_refChoiceOther;
+
+ Glib::RefPtr<Gio::SimpleAction> m_refToggle;
};
#endif //GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/menus/popup/examplewindow.cc b/examples/book/menus/popup/examplewindow.cc
index 8be6760..63b9ef6 100644
--- a/examples/book/menus/popup/examplewindow.cc
+++ b/examples/book/menus/popup/examplewindow.cc
@@ -1,6 +1,4 @@
-//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-
-
-/* gtkmm example Copyright (C) 2002 gtkmm development team
+/* gtkmm example Copyright (C) 2002-2013 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
@@ -42,49 +40,52 @@ ExampleWindow::ExampleWindow()
//Fill menu:
- m_refActionGroup = Gtk::ActionGroup::create();
+ Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
+ Gio::SimpleActionGroup::create();
//File|New sub menu:
//These menu actions would normally already exist for a main menu, because a
//context menu should not normally contain menu items that are only available
//via a context menu.
- m_refActionGroup->add(Gtk::Action::create("ContextMenu", "Context Menu"));
- m_refActionGroup->add(Gtk::Action::create("ContextEdit", "Edit"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+ refActionGroup->add_action("edit",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
- m_refActionGroup->add(Gtk::Action::create("ContextProcess", "Process"),
- Gtk::AccelKey("<control>P"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+ refActionGroup->add_action("process", //TODO: How to specify "<control>P" as an accelerator.
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
- m_refActionGroup->add(Gtk::Action::create("ContextRemove", "Remove"),
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
+ refActionGroup->add_action("remove",
+ sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
- //TODO:
- /*
- //Add a ImageMenuElem:
- menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("_Something", m_Image,
- sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic) ) ) ;
- */
+ insert_action_group("examplepopup", refActionGroup);
- m_refUIManager = Gtk::UIManager::create();
- m_refUIManager->insert_action_group(m_refActionGroup);
- add_accel_group(m_refUIManager->get_accel_group());
+ m_refBuilder = Gtk::Builder::create();
//Layout the actions in a menubar and toolbar:
Glib::ustring ui_info =
- "<ui>"
- " <popup name='PopupMenu'>"
- " <menuitem action='ContextEdit'/>"
- " <menuitem action='ContextProcess'/>"
- " <menuitem action='ContextRemove'/>"
- " </popup>"
- "</ui>";
+ "<interface>"
+ " <menu id='menu-examplepopup'>"
+ " <section>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Edit</attribute>"
+ " <attribute name='action'>examplepopup.edit</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Process</attribute>"
+ " <attribute name='action'>examplepopup.process</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Remove</attribute>"
+ " <attribute name='action'>examplepopup.remove</attribute>"
+ " </item>"
+ " </section>"
+ " </menu>"
+ "</interface>";
try
{
- m_refUIManager->add_ui_from_string(ui_info);
+ m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
@@ -92,10 +93,14 @@ ExampleWindow::ExampleWindow()
}
//Get the menu:
- m_pMenuPopup = dynamic_cast<Gtk::Menu*>(
- m_refUIManager->get_widget("/PopupMenu"));
- if(!m_pMenuPopup)
- g_warning("menu not found");
+ Glib::RefPtr<Glib::Object> object =
+ m_refBuilder->get_object("menu-examplepopup");
+ Glib::RefPtr<Gio::Menu> gmenu =
+ Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+ if(!gmenu)
+ g_warning("GMenu not found");
+
+ m_pMenuPopup = new Gtk::Menu(gmenu);
show_all_children();
}
@@ -113,9 +118,15 @@ bool ExampleWindow::on_button_press_event(GdkEventButton* event)
{
if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
{
+ if(!m_pMenuPopup->get_attach_widget())
+ {
+ m_pMenuPopup->attach_to_widget(*this);
+ }
+
if(m_pMenuPopup)
m_pMenuPopup->popup(event->button, event->time);
+
return true; //It has been handled.
}
else
diff --git a/examples/book/menus/popup/examplewindow.h b/examples/book/menus/popup/examplewindow.h
index 13a2450..d57d4ad 100644
--- a/examples/book/menus/popup/examplewindow.h
+++ b/examples/book/menus/popup/examplewindow.h
@@ -37,8 +37,7 @@ protected:
Gtk::EventBox m_EventBox;
Gtk::Label m_Label;
- Glib::RefPtr<Gtk::UIManager> m_refUIManager;
- Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
+ Glib::RefPtr<Gtk::Builder> m_refBuilder;
Gtk::Menu* m_pMenuPopup;
};
diff --git a/examples/book/printing/advanced/examplewindow.cc
b/examples/book/printing/advanced/examplewindow.cc
index 7708790..ee6be50 100644
--- a/examples/book/printing/advanced/examplewindow.cc
+++ b/examples/book/printing/advanced/examplewindow.cc
@@ -77,62 +77,67 @@ ExampleWindow::~ExampleWindow()
void ExampleWindow::build_main_menu()
{
//Create actions for menus and toolbars:
- m_refActionGroup = Gtk::ActionGroup::create();
+ Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
+ Gio::SimpleActionGroup::create();
//File menu:
- m_refActionGroup->add(
- Gtk::Action::create("FileMenu", "_File"));
-
- m_refActionGroup->add(
- Gtk::Action::create("New", Gtk::Stock::NEW),
+ refActionGroup->add_action("new",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new));
- m_refActionGroup->add(
- Gtk::Action::create("PageSetup", "Page _Setup"),
+ refActionGroup->add_action("pagesetup",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_page_setup));
- m_refActionGroup->add(
- Gtk::Action::create("PrintPreview", "Print Preview"),
+ refActionGroup->add_action("printpreview",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_print_preview));
- m_refActionGroup->add(
- Gtk::Action::create("Print", Gtk::Stock::PRINT),
+ refActionGroup->add_action("print",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_print));
- m_refActionGroup->add(
- Gtk::Action::create("Quit", Gtk::Stock::QUIT),
+ refActionGroup->add_action("quit",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
- m_refUIManager = Gtk::UIManager::create();
- m_refUIManager->insert_action_group(m_refActionGroup);
+ insert_action_group("example", refActionGroup);
+
+ m_refBuilder = Gtk::Builder::create();
- add_accel_group(m_refUIManager->get_accel_group());
+ //TODO: add_accel_group(m_refBuilder->get_accel_group());
//Layout the actions in a menubar and toolbar:
Glib::ustring ui_info =
- "<ui>"
- " <menubar name='MenuBar'>"
- " <menu action='FileMenu'>"
- " <menuitem action='New'/>"
- " <menuitem action='PageSetup'/>"
- " <menuitem action='PrintPreview'/>"
- " <menuitem action='Print'/>"
- " <separator/>"
- " <menuitem action='Quit'/>"
- " </menu>"
- " </menubar>"
- " <toolbar name='ToolBar'>"
+ "<interface>"
+ " <menu id='menu-example'>"
+ " <submenu>"
+ " <attribute name='label' translatable='yes'>_File</attribute>"
+ " <section>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Page _Setup</attribute>"
+ " <attribute name='action'>example.pagesetup</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Print Preview</attribute>"
+ " <attribute name='action'>example.printpreview</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>_Print</attribute>"
+ " <attribute name='action'>example.print</attribute>"
+ " </item>"
+ " </section>"
+ " </submenu>"
+ " </menu>"
+/* TODO:
+ " <toolbar name='ToolBar'>"
" <toolitem action='New'/>"
" <toolitem action='Print'/>"
" <separator/>"
" <toolitem action='Quit'/>"
" </toolbar>"
- "</ui>";
+*/
+ "</interface>";
try
{
- m_refUIManager->add_ui_from_string(ui_info);
+ m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
@@ -141,13 +146,21 @@ void ExampleWindow::build_main_menu()
//Get the menubar and toolbar widgets, and add them to a container widget:
- Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
- if(pMenubar)
- m_VBox.pack_start(*pMenubar, Gtk::PACK_SHRINK);
-
- Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar") ;
+ Glib::RefPtr<Glib::Object> object =
+ m_refBuilder->get_object("menu-example");
+ Glib::RefPtr<Gio::Menu> gmenu =
+ Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+ if(!gmenu)
+ g_warning("GMenu not found");
+
+ Gtk::MenuBar* pMenubar = new Gtk::MenuBar(gmenu);
+ m_VBox.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+
+/* TODO:
+ Gtk::Widget* pToolbar = m_refBuilder->get_widget("/ToolBar") ;
if(pToolbar)
m_VBox.pack_start(*pToolbar, Gtk::PACK_SHRINK);
+*/
}
void
diff --git a/examples/book/printing/advanced/examplewindow.h b/examples/book/printing/advanced/examplewindow.h
index 43e343d..31d3a28 100644
--- a/examples/book/printing/advanced/examplewindow.h
+++ b/examples/book/printing/advanced/examplewindow.h
@@ -70,8 +70,7 @@ protected:
unsigned m_ContextId;
Gtk::Statusbar m_Statusbar;
- Glib::RefPtr<Gtk::UIManager> m_refUIManager;
- Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
+ Glib::RefPtr<Gtk::Builder> m_refBuilder;
};
#endif //GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/printing/simple/examplewindow.cc b/examples/book/printing/simple/examplewindow.cc
index f784d3f..5504b8b 100644
--- a/examples/book/printing/simple/examplewindow.cc
+++ b/examples/book/printing/simple/examplewindow.cc
@@ -78,76 +78,90 @@ ExampleWindow::~ExampleWindow()
void ExampleWindow::build_main_menu()
{
//Create actions for menus and toolbars:
- m_refActionGroup = Gtk::ActionGroup::create();
+ Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup =
+ Gio::SimpleActionGroup::create();
//File menu:
- m_refActionGroup->add(
- Gtk::Action::create("FileMenu", "_File"));
-
- m_refActionGroup->add(
- Gtk::Action::create("New", Gtk::Stock::NEW),
+ refActionGroup->add_action("new",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new));
- m_refActionGroup->add(
- Gtk::Action::create("PageSetup", "Page _Setup"),
+ refActionGroup->add_action("pagesetup",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_page_setup));
- m_refActionGroup->add(
- Gtk::Action::create("PrintPreview", "Print Preview"),
+ refActionGroup->add_action("printpreview",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_print_preview));
- m_refActionGroup->add(
- Gtk::Action::create("Print", Gtk::Stock::PRINT),
+ refActionGroup->add_action("print",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_print));
- m_refActionGroup->add(
- Gtk::Action::create("Quit", Gtk::Stock::QUIT),
+ refActionGroup->add_action("quit",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
- m_refUIManager = Gtk::UIManager::create();
- m_refUIManager->insert_action_group(m_refActionGroup);
+ insert_action_group("example", refActionGroup);
- add_accel_group(m_refUIManager->get_accel_group());
+ 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 =
- "<ui>"
- " <menubar name='MenuBar'>"
- " <menu action='FileMenu'>"
- " <menuitem action='New'/>"
- " <menuitem action='PageSetup'/>"
- " <menuitem action='PrintPreview'/>"
- " <menuitem action='Print'/>"
- " <separator/>"
- " <menuitem action='Quit'/>"
- " </menu>"
- " </menubar>"
- " <toolbar name='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'>Page _Setup</attribute>"
+ " <attribute name='action'>example.pagesetup</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>Print Preview</attribute>"
+ " <attribute name='action'>example.printpreview</attribute>"
+ " </item>"
+ " <item>"
+ " <attribute name='label' translatable='yes'>_Print</attribute>"
+ " <attribute name='action'>example.print</attribute>"
+ " </item>"
+ " </section>"
+ " </submenu>"
+ " </menu>"
+/* TODO:
+ " <toolbar name='ToolBar'>"
" <toolitem action='New'/>"
" <toolitem action='Print'/>"
" <separator/>"
" <toolitem action='Quit'/>"
" </toolbar>"
- "</ui>";
+*/
+ "</interface>";
try
- {
- m_refUIManager->add_ui_from_string(ui_info);
+ {
+ m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
std::cerr << "building menus failed: " << ex.what();
}
- //Get the menubar and toolbar widgets, and add them to a container widget:
- Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
- if(pMenubar)
- m_VBox.pack_start(*pMenubar, Gtk::PACK_SHRINK);
- Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar") ;
+ //Get the menubar and toolbar widgets, and add them 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");
+
+ Gtk::MenuBar* pMenubar = new Gtk::MenuBar(gmenu);
+ m_VBox.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+
+/* TODO:
+ Gtk::Widget* pToolbar = m_refBuilder->get_widget("/ToolBar") ;
if(pToolbar)
m_VBox.pack_start(*pToolbar, Gtk::PACK_SHRINK);
+*/
}
void ExampleWindow::on_printoperation_status_changed(
diff --git a/examples/book/printing/simple/examplewindow.h b/examples/book/printing/simple/examplewindow.h
index ce80a18..1297fbf 100644
--- a/examples/book/printing/simple/examplewindow.h
+++ b/examples/book/printing/simple/examplewindow.h
@@ -71,8 +71,7 @@ protected:
unsigned m_ContextId;
Gtk::Statusbar m_Statusbar;
- Glib::RefPtr<Gtk::UIManager> m_refUIManager;
- Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
+ Glib::RefPtr<Gtk::Builder> m_refBuilder;
};
#endif //GTKMM_EXAMPLEWINDOW_H
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]