| 1 |
/* gtkmm example Copyright (C) 2016 gtkmm development team |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "exampleapplication.h" |
| 18 |
#include "exampleappwindow.h" |
| 19 |
#include <iostream> |
| 20 |
#include <exception> |
| 21 |
|
| 22 |
ExampleApplication::ExampleApplication() |
| 23 |
: Gtk::Application("org.gtkmm.examples.application", Gio::Application::Flags::HANDLES_OPEN) |
| 24 |
{ |
| 25 |
// Delete a window when it is removed from the application. |
| 26 |
signal_window_removed().connect([](Gtk::Window* window) { delete window; }); |
| 27 |
} |
| 28 |
|
| 29 |
Glib::RefPtr<ExampleApplication> ExampleApplication::create() |
| 30 |
{ |
| 31 |
return Glib::make_refptr_for_instance<ExampleApplication>(new ExampleApplication()); |
| 32 |
} |
| 33 |
|
| 34 |
ExampleAppWindow* ExampleApplication::create_appwindow() |
| 35 |
{ |
| 36 |
auto appwindow = ExampleAppWindow::create(); |
| 37 |
|
| 38 |
// Make sure that the application runs for as long this window is still open. |
| 39 |
add_window(*appwindow); |
| 40 |
|
| 41 |
// A window can be added to an application with Gtk::Application::add_window() |
| 42 |
// or Gtk::Window::set_application(). When all added windows have been removed, |
| 43 |
// the application stops running (Gtk::Application::run() returns), |
| 44 |
// unless Gio::Application::hold() has been called. |
| 45 |
|
| 46 |
return appwindow; |
| 47 |
} |
| 48 |
|
| 49 |
void ExampleApplication::on_startup() |
| 50 |
{ |
| 51 |
// Call the base class's implementation. |
| 52 |
Gtk::Application::on_startup(); |
| 53 |
|
| 54 |
// Add actions and keyboard accelerators for the menu. |
| 55 |
add_action("preferences", sigc::mem_fun(*this, &ExampleApplication::on_action_preferences)); |
| 56 |
add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_action_quit)); |
| 57 |
set_accel_for_action("app.quit", "<Ctrl>Q"); |
| 58 |
} |
| 59 |
|
| 60 |
void ExampleApplication::on_activate() |
| 61 |
{ |
| 62 |
try |
| 63 |
{ |
| 64 |
// The application has been started, so let's show a window. |
| 65 |
auto appwindow = create_appwindow(); |
| 66 |
appwindow->present(); |
| 67 |
} |
| 68 |
// If create_appwindow() throws an exception (perhaps from Gtk::Builder), |
| 69 |
// no window has been created, no window has been added to the application, |
| 70 |
// and therefore the application will stop running. |
| 71 |
catch (const Glib::Error& ex) |
| 72 |
{ |
| 73 |
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl; |
| 74 |
} |
| 75 |
catch (const std::exception& ex) |
| 76 |
{ |
| 77 |
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void ExampleApplication::on_open(const Gio::Application::type_vec_files& files, |
| 82 |
const Glib::ustring& /* hint */) |
| 83 |
{ |
| 84 |
// The application has been asked to open some files, |
| 85 |
// so let's open a new view for each one. |
| 86 |
ExampleAppWindow* appwindow = nullptr; |
| 87 |
auto windows = get_windows(); |
| 88 |
if (windows.size() > 0) |
| 89 |
appwindow = dynamic_cast<ExampleAppWindow*>(windows[0]); |
| 90 |
|
| 91 |
try |
| 92 |
{ |
| 93 |
if (!appwindow) |
| 94 |
appwindow = create_appwindow(); |
| 95 |
|
| 96 |
for (const auto& file : files) |
| 97 |
appwindow->open_file_view(file); |
| 98 |
|
| 99 |
appwindow->present(); |
| 100 |
} |
| 101 |
catch (const Glib::Error& ex) |
| 102 |
{ |
| 103 |
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl; |
| 104 |
} |
| 105 |
catch (const std::exception& ex) |
| 106 |
{ |
| 107 |
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void ExampleApplication::on_action_preferences() |
| 112 |
{ |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
void ExampleApplication::on_action_quit() |
| 117 |
{ |
| 118 |
// Gio::Application::quit() will make Gio::Application::run() return, |
| 119 |
// but it's a crude way of ending the program. The window is not removed |
| 120 |
// from the application. Neither the window's nor the application's |
| 121 |
// destructors will be called, because there will be remaining reference |
| 122 |
// counts in both of them. If we want the destructors to be called, we |
| 123 |
// must remove the window from the application. See ExampleApplication's constructor. |
| 124 |
// A window can be removed from an application with Gtk::Application::remove_window() |
| 125 |
// or Gtk::Window::unset_application(). |
| 126 |
auto windows = get_windows(); |
| 127 |
for (auto window : windows) |
| 128 |
remove_window(*window); |
| 129 |
|
| 130 |
// Not really necessary, when Gtk::Application::remove_window() is called, |
| 131 |
// unless Gio::Application::hold() has been called without a corresponding |
| 132 |
// call to Gio::Application::release(). |
| 133 |
quit(); |
| 134 |
} |