/* gtkmm example Copyright (C) 2014 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 .
*/
#include "exampleapplication.h"
#include "examplewindow.h"
#include
ExampleApplication::ExampleApplication()
: Gtk::Application("org.gtkmm.example.main_menu")
{
Glib::set_application_name("Main Menu Example");
}
Glib::RefPtr ExampleApplication::create()
{
return Glib::make_refptr_for_instance(new ExampleApplication());
}
void ExampleApplication::on_startup()
{
//Call the base class's implementation:
Gtk::Application::on_startup();
//Create actions for menus and toolbars.
//We can use add_action() because Gtk::Application derives from Gio::ActionMap.
//File|New sub menu:
add_action("newstandard",
sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
add_action("newfoo",
sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
add_action("newgoo",
sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
//File menu:
add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_quit));
//Help menu:
add_action("about", sigc::mem_fun(*this, &ExampleApplication::on_menu_help_about));
// Set accelerator keys:
set_accel_for_action("app.newstandard", "n");
set_accel_for_action("app.quit", "q");
set_accel_for_action("win.copy", "c");
set_accel_for_action("win.paste", "v");
m_refBuilder = Gtk::Builder::create();
//Layout the actions in a menubar and a menu:
Glib::ustring ui_info =
""
" "
" "
"";
try
{
m_refBuilder->add_from_string(ui_info);
}
catch (const Glib::Error& ex)
{
std::cerr << "Building menus failed: " << ex.what();
}
//Get the menubar and the app menu, and add them to the application:
auto gmenu = m_refBuilder->get_object("menu-example");
if (!gmenu)
{
g_warning("GMenu not found");
}
else
{
set_menubar(gmenu);
}
}
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 window in on_open(),
// when asked to open a file, if no changes have been made yet.
create_window();
}
void ExampleApplication::create_window()
{
// Since gtkmm 4.8, a Gtk::Window can be managed. If managed and not created
// by Gtk::Builder, it's deleted when its underlying C instance is destroyed.
// The underlying C instance is destroyed when it's closed, unless
// Gtk::Window::set_hide_on_close() has been called.
auto win = Gtk::make_managed();
//Make sure that the application runs for as long this window is still open:
add_window(*win);
win->set_show_menubar();
win->set_visible(true);
}
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::Window::close() 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 close the window.
auto windows = get_windows();
for (auto window : windows)
window->close();
}
void ExampleApplication::on_menu_help_about()
{
std::cout << "Help|About App was selected." << std::endl;
}