/* * Enhancing the main menu example of gtkmm-tutorial/unstable presented in * https://developer.gnome.org/gtkmm-tutorial/unstable/\ * sec-menus-examples.html.de#menu-example-main * to show an application menu as suggested in * https://wiki.gnome.org/ThreePointThree/Features/ApplicationMenu, * described in * https://wiki.gnome.org/HowDoI/ApplicationMenu * and realised using gtkmm in * https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/\ * application/app_and_win_menus * * Started on 2014-11-10 * Current version: 2014-11-10 * Performed by: Jürgen Kleber (jkleber2701 t-online de) * * Original file downloaded on Sunday, 9th of November, 2014 from * https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/\ * application/app_and_win_menus/exampleapplication.cc */ #include "exampleapplication.h" #include "examplewindow.h" #include ExampleApplication::ExampleApplication() : Gtk::Application("org.gtkmm.examples.application") { Glib::set_application_name("Gtk::Application Example"); win = 0; } Glib::RefPtr ExampleApplication::create() { return Glib::RefPtr(new ExampleApplication()); } void ExampleApplication::on_startup() { //Call the base class's implementation: Gtk::Application::on_startup(); //File|New sub menu: newstandard_action = add_action("newstandard", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic)); newfoo_action = add_action("newfoo", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic)); newgoo_action = add_action("newgoo", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic)); //File menu: quit_action = add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_quit)); //Help menu: about_action = add_action("about", sigc::mem_fun(*this, &ExampleApplication::on_menu_others)); m_refBuilder = Gtk::Builder::create(); //Layout the actions in a menubar and toolbar: Glib::ustring ui_info = "" " " " " " _File" "
" " " " New _Standard" " app.newstandard" " <Primary>n" " " " " " New _Foo" " app.newfoo" " " " " " New _Goo" " app.newgoo" " " "
" "
" " " " _Quit" " app.quit" " <Primary>q" " " "
" "
" " " " _Edit" "
" " " " _Copy" " win.copy" " <Primary>c" " " " " " _Paste" " win.paste" " <Primary>v" " " " " " _Something" " win.something" " " "
" "
" " " " _Choices" "
" " " " Choice _A" " win.choice" " a" " " " " " Choice _B" " win.choice" " b" " " "
" "
" " " " _Other Choices" "
" " " " Choice 1" " win.choiceother" " 1" " " " " " Choice 2" " win.choiceother" " 2" " " "
" "
" " " " Some Toggle" " win.sometoggle" " " "
" "
" " " " _Help" "
" " " " _About" " app.about" " " "
" "
" "
" " " " " " _File" "
" " " " New _Standard" " app.newstandard" " <Primary>n" " " " " " New _Foo" " app.newfoo" " " " " " New _Goo" " app.newgoo" " " "
" "
" " " " _Quit" " app.quit" " <Primary>q" " " "
" "
" " " " _Help" "
" " " " _About" " app.about" " " "
" "
" "
" "
"; try { m_refBuilder->add_from_string(ui_info); } catch (const Glib::Error& ex) { std::cerr << "building menus failed: " << ex.what(); } //Get the menubar and add it to a container widget: Glib::RefPtr object = m_refBuilder->get_object("menu-example"); Glib::RefPtr gmenu = Glib::RefPtr::cast_dynamic( object); object = m_refBuilder->get_object("appmenu"); Glib::RefPtr appMenu = Glib::RefPtr::cast_dynamic( object); if (!(gmenu && appMenu)) { g_warning("GMenu or AppMenu not found"); } else { set_app_menu(appMenu); set_menubar(gmenu); } } void ExampleApplication::create_window() { win = new ExampleWindow(); //Make sure that the application runs for as long this window is still open: add_window(*win); //Delete the window when it is hidden. //That's enough for this simple example. win->signal_hide().connect( sigc::bind( sigc::mem_fun(*this, &ExampleApplication::on_window_hide), win)); win->show_all(); } void ExampleApplication::on_window_hide(Gtk::Window* window) { delete window; } void ExampleApplication::on_activate() { //std::cout << "debug1: " << G_STRFUNC << std::endl; // The application has been started, so let's show a window. // A real application might want to reuse this "empty" window in on_open(), // when asked to open a file, if no changes have been made yet. create_window(); } void ExampleApplication::on_menu_file_new_generic() { std::cout << "A File|New menu item was selected." << std::endl; } void ExampleApplication::on_menu_file_quit() { std::cout << G_STRFUNC << std::endl; quit(); // Not really necessary, when Gtk::Widget::hide() is called. // Gio::Application::quit() will make Gio::Application::run() return, // but it's a crude way of ending the program. The window is not removed // from the application. Neither the window's nor the application's // destructors will be called, because there will be remaining reference // counts in both of them. If we want the destructors to be called, we // must remove the window from the application. One way of doing this // is to hide the window. std::vector windows = get_windows(); if (windows.size() > 0) windows[0]->hide(); // In this simple case, we know there is only one window. } void ExampleApplication::on_menu_others() { std::cout << "A menu item was selected." << std::endl; }