/* 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 "examplewindow.h" #include #include ExampleWindow::ExampleWindow(const Glib::RefPtr& app) : m_Box(Gtk::ORIENTATION_VERTICAL) { set_title("main_menu example"); set_default_size(200, 200); add(m_Box); //We can put a MenuBar at the top of the box and other stuff below it. //Define the actions: m_refActionGroup = Gio::SimpleActionGroup::create(); m_refActionGroup->add_action("new", sigc::mem_fun(*this, &ExampleWindow::on_action_file_new) ); m_refActionGroup->add_action("open", sigc::mem_fun(*this, &ExampleWindow::on_action_others) ); m_refActionRain = m_refActionGroup->add_action_bool("rain", sigc::mem_fun(*this, &ExampleWindow::on_action_toggle), false); m_refActionGroup->add_action("quit", sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) ); m_refActionGroup->add_action("cut", sigc::mem_fun(*this, &ExampleWindow::on_action_others) ); m_refActionGroup->add_action("copy", sigc::mem_fun(*this, &ExampleWindow::on_action_others) ); m_refActionGroup->add_action("paste", sigc::mem_fun(*this, &ExampleWindow::on_action_others) ); insert_action_group("example", m_refActionGroup); //Define how the actions are presented in the menus and toolbars: m_refBuilder = Gtk::Builder::create(); //Layout the actions in a menubar and toolbar: const char* ui_info = "" " " " " " _File" "
" " " " _New" " example.new" " <Primary>n" " " " " " _Open" " example.open" " <Primary>o" " " "
" "
" " " " Rain" " example.rain" " " "
" "
" " " " _Quit" " example.quit" " <Primary>q" " " "
" "
" " " " _Edit" " " " _Cut" " example.cut" " <Primary>x" " " " " " _Copy" " example.copy" " <Primary>c" " " " " " _Paste" " example.paste" " <Primary>v" " " " " "
" "
"; // 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", "n"); app->set_accel_for_action("example.open", "o"); app->set_accel_for_action("example.quit", "q"); app->set_accel_for_action("example.cut", "x"); app->set_accel_for_action("example.copy", "c"); app->set_accel_for_action("example.paste", "v"); try { m_refBuilder->add_from_string(ui_info); m_refBuilder->add_from_file("toolbar.glade"); } catch(const Glib::Error& ex) { std::cerr << "Building menus and toolbar failed: " << ex.what(); } //Get the menubar: auto object = m_refBuilder->get_object("menubar"); auto gmenu = Glib::RefPtr::cast_dynamic(object); if (!gmenu) g_warning("GMenu not found"); else { auto pMenuBar = Gtk::manage(new Gtk::MenuBar(gmenu)); //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 = nullptr; 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(); } ExampleWindow::~ExampleWindow() { } void ExampleWindow::on_action_file_quit() { hide(); //Closes the main window to stop the app->run(). } void ExampleWindow::on_action_file_new() { std::cout << "A File|New menu item was selected." << std::endl; } void ExampleWindow::on_action_others() { std::cout << "A menu item was selected." << std::endl; } void ExampleWindow::on_action_toggle() { std::cout << "The toggle menu item was selected." << std::endl; bool active = false; m_refActionRain->get_state(active); //The toggle action's state does not change automatically: active = !active; m_refActionRain->change_state(active); Glib::ustring message; if(active) message = "Toggle is active."; else message = "Toggle is not active"; std::cout << message << std::endl; }