Debian Codegraph @

examples/book/buildapp/step5/exampleapplication.cc

raw
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 "exampleappprefs.h"
20 #include <iostream>
21 #include <exception>
22
23 ExampleApplication::ExampleApplication()
24 : Gtk::Application("org.gtkmm.examples.application", Gio::Application::Flags::HANDLES_OPEN)
25 {
26 // Delete a window when it is removed from the application.
27 signal_window_removed().connect([](Gtk::Window* window) { delete window; });
28 }
29
30 Glib::RefPtr<ExampleApplication> ExampleApplication::create()
31 {
32 return Glib::make_refptr_for_instance<ExampleApplication>(new ExampleApplication());
33 }
34
35 ExampleAppWindow* ExampleApplication::create_appwindow()
36 {
37 auto appwindow = ExampleAppWindow::create();
38
39 // Make sure that the application runs for as long this window is still open.
40 add_window(*appwindow);
41
42 // A window can be added to an application with Gtk::Application::add_window()
43 // or Gtk::Window::set_application(). When all added windows have been removed,
44 // the application stops running (Gtk::Application::run() returns),
45 // unless Gio::Application::hold() has been called.
46
47 return appwindow;
48 }
49
50 void ExampleApplication::on_startup()
51 {
52 // Call the base class's implementation.
53 Gtk::Application::on_startup();
54
55 // Add actions and keyboard accelerators for the menu.
56 add_action("preferences", sigc::mem_fun(*this, &ExampleApplication::on_action_preferences));
57 add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_action_quit));
58 set_accel_for_action("app.quit", "<Ctrl>Q");
59 }
60
61 void ExampleApplication::on_activate()
62 {
63 try
64 {
65 // The application has been started, so let's show a window.
66 auto appwindow = create_appwindow();
67 appwindow->present();
68 }
69 // If create_appwindow() throws an exception (perhaps from Gtk::Builder),
70 // no window has been created, no window has been added to the application,
71 // and therefore the application will stop running.
72 catch (const Glib::Error& ex)
73 {
74 std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl;
75 }
76 catch (const std::exception& ex)
77 {
78 std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl;
79 }
80 }
81
82 void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
83 const Glib::ustring& /* hint */)
84 {
85 // The application has been asked to open some files,
86 // so let's open a new view for each one.
87 ExampleAppWindow* appwindow = nullptr;
88 auto windows = get_windows();
89 if (windows.size() > 0)
90 appwindow = dynamic_cast<ExampleAppWindow*>(windows[0]);
91
92 try
93 {
94 if (!appwindow)
95 appwindow = create_appwindow();
96
97 for (const auto& file : files)
98 appwindow->open_file_view(file);
99
100 appwindow->present();
101 }
102 catch (const Glib::Error& ex)
103 {
104 std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl;
105 }
106 catch (const std::exception& ex)
107 {
108 std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl;
109 }
110 }
111
112 void ExampleApplication::on_action_preferences()
113 {
114 try
115 {
116 auto prefs_dialog = ExampleAppPrefs::create(*get_active_window());
117 prefs_dialog->present();
118
119 // Delete the dialog when it is hidden.
120 prefs_dialog->signal_hide().connect([prefs_dialog](){ delete prefs_dialog; });
121 }
122 catch (const Glib::Error& ex)
123 {
124 std::cerr << "ExampleApplication::on_action_preferences(): " << ex.what() << std::endl;
125 }
126 catch (const std::exception& ex)
127 {
128 std::cerr << "ExampleApplication::on_action_preferences(): " << ex.what() << std::endl;
129 }
130 }
131
132 void ExampleApplication::on_action_quit()
133 {
134 // Gio::Application::quit() will make Gio::Application::run() return,
135 // but it's a crude way of ending the program. The window is not removed
136 // from the application. Neither the window's nor the application's
137 // destructors will be called, because there will be remaining reference
138 // counts in both of them. If we want the destructors to be called, we
139 // must remove the window from the application. See ExampleApplication's constructor.
140 // A window can be removed from an application with Gtk::Application::remove_window()
141 // or Gtk::Window::unset_application().
142 auto windows = get_windows();
143 for (auto window : windows)
144 remove_window(*window);
145
146 // Not really necessary, when Gtk::Application::remove_window() is called,
147 // unless Gio::Application::hold() has been called without a corresponding
148 // call to Gio::Application::release().
149 quit();
150 }