Debian Codegraph @

gtk/src/application.ccg

raw
1 /* Copyright (C) 2003 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <glibmm/vectorutils.h>
18
19 #include <gtk/gtk.h>
20 #include <gtkmm/window.h>
21 #include <glibmm/init.h>
22 #include <giomm/init.h>
23 #include <pangomm/wrap_init.h>
24 #ifdef GTKMM_ATKMM_ENABLED
25 #include <atkmm/wrap_init.h>
26 #endif //GTKMM_ATKMM_ENABLED
27 #include <gdkmm/wrap_init.h>
28 #include <gtkmm/wrap_init.h>
29
30 namespace
31 {
32
33 static void init_gtkmm_internals()
34 {
35 static bool init_done = false;
36
37 if(!init_done)
38 {
39 Glib::init();
40 Gio::init();
41
42 // Populate the map of GTypes to C++ wrap_new() functions.
43 Pango::wrap_init();
44 #ifdef GTKMM_ATKMM_ENABLED
45 Atk::wrap_init();
46 #endif //GTKMM_ATKMM_ENABLED
47 Gdk::wrap_init();
48 Gtk::wrap_init();
49
50 init_done = true;
51 }
52 }
53
54 } // anonymous namespace
55
56 namespace Gtk
57 {
58
59 const Glib::Class& Application::custom_class_init()
60 {
61 init_gtkmm_internals();
62 return application_class_.init();
63 }
64
65 Application::Application(const Glib::ustring& application_id, Gio::ApplicationFlags flags)
66 :
67 // Mark this class as non-derived to allow C++ vfuncs to be skipped.
68 //Note that GApplication complains about "" but allows nullptr, so we avoid passing "".
69 Glib::ObjectBase(nullptr),
70 Gio::Application(Glib::ConstructParams(custom_class_init(), "application_id", (application_id.empty() ? nullptr : application_id.c_str()), "flags", GApplicationFlags(flags), nullptr)),
71 m_argc(0),
72 m_argv(nullptr)
73 {
74 gtk_init(nullptr, nullptr);
75 }
76
77 Application::Application(int& argc, char**& argv, const Glib::ustring& application_id, Gio::ApplicationFlags flags)
78 :
79 // Mark this class as non-derived to allow C++ vfuncs to be skipped.
80 //Note that GApplication complains about "" but allows nullptr, so we avoid passing "".
81 Glib::ObjectBase(nullptr),
82 Gio::Application(Glib::ConstructParams(custom_class_init(), "application_id", (application_id.empty() ? nullptr : application_id.c_str()), "flags", GApplicationFlags(flags), nullptr)),
83 m_argc(argc),
84 m_argv(argv)
85 {
86 gtk_init(&argc, &argv);
87 }
88
89 Glib::RefPtr<Application> Application::create(const Glib::ustring& application_id, Gio::ApplicationFlags flags)
90 {
91 return Glib::RefPtr<Application>( new Application(application_id, flags) );
92 }
93
94 Glib::RefPtr<Application> Application::create(int& argc, char**& argv, const Glib::ustring& application_id, Gio::ApplicationFlags flags)
95 {
96 return Glib::RefPtr<Application>( new Application(argc, argv, application_id, flags) );
97 }
98
99 void Application::on_window_hide(Window* window)
100 {
101 //Tell GtkApplication to forget the window.
102 //This can cause run() to return, if it is the last window.
103 //Otherwise, GtkApplication waits for the window to be _destroyed_,
104 //which is just not something that it should care about:
105 //See https://bugzilla.gnome.org/show_bug.cgi?id=639931
106 if(window && window->get_application()) //We check that it's still in an application anyway.
107 remove_window(*window);
108 }
109
110 void Application::add_window(Window& window)
111 {
112 //Respond to window hiding, not destruction:
113 //See https://bugzilla.gnome.org/show_bug.cgi?id=639931
114 window.signal_hide().connect(
115 sigc::bind(
116 sigc::mem_fun(*this, &Application::on_window_hide),
117 &window));
118
119 gtk_application_add_window(gobj(), (window).gobj());
120 }
121
122 int Application::run(int argc, char** argv)
123 {
124 return Gio::Application::run(argc, argv);
125 }
126
127 int Application::run(Window& window, int argc, char** argv)
128 {
129 //We cannot add and show the window until the GApplication::activate signal
130 //has been emitted, or we will crash because the application has not been
131 //registered. (At least if window is an ApplicationWindow.)
132 signal_activate().connect(
133 sigc::bind(
134 sigc::mem_fun(*this, &Application::on_activate_add_and_show_main_window),
135 &window));
136
137 const auto result = Gio::Application::run(argc, argv);
138 return result;
139 }
140
141
142 int Application::run(Window& window)
143 {
144 //We cannot add and show the window until the GApplication::activate signal
145 //has been emitted, or we will crash because the application has not been
146 //registered. (At least if window is an ApplicationWindow.)
147 signal_activate().connect(
148 sigc::bind(
149 sigc::mem_fun(*this, &Application::on_activate_add_and_show_main_window),
150 &window));
151
152 const auto result = Gio::Application::run(m_argc, m_argv);
153 return result;
154 }
155
156 int Application::run()
157 {
158 const auto result = Gio::Application::run(m_argc, m_argv);
159 return result;
160 }
161
162 void Application::on_activate_add_and_show_main_window(Window* window)
163 {
164 if(window)
165 {
166 add_window(*window);
167 window->show();
168 }
169 }
170
171 void Application::set_accel_for_action(const Glib::ustring& detailed_action_name, const Glib::ustring& accel)
172 {
173 std::vector<Glib::ustring> vec;
174 vec.push_back(accel);
175 set_accels_for_action(detailed_action_name, vec);
176 }
177
178 void Application::unset_accels_for_action(const Glib::ustring& detailed_action_name)
179 {
180 //gtk_application_set_accels_for_action() can take an empty array, but not NULL.
181 //See https://bugzilla.gnome.org/show_bug.cgi?id=708905#c10
182 std::vector<Glib::ustring> vec;
183 set_accels_for_action(detailed_action_name, vec);
184 }
185
186 } // namespace Gtk