| 1 |
/* Copyright (C) 2003-2014 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 |
_CONFIGINCLUDE(gtkmmconfig.h) |
| 18 |
|
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#ifndef GTKMM_DISABLE_DEPRECATED |
| 22 |
#include <gtkmm/actiongroup.h> |
| 23 |
#endif |
| 24 |
#include <giomm/application.h> |
| 25 |
#include <giomm/menumodel.h> |
| 26 |
#include <giomm/menu.h> |
| 27 |
#include <vector> |
| 28 |
#include <glibmm/arrayhandle.h> |
| 29 |
|
| 30 |
|
| 31 |
_DEFS(gtkmm,gtk) |
| 32 |
_PINCLUDE(giomm/private/application_p.h) |
| 33 |
|
| 34 |
#m4 _PUSH(SECTION_CC_PRE_INCLUDES) |
| 35 |
// Needed before gtkmm/private/application_p.h is included. |
| 36 |
typedef struct _GtkWindow GtkWindow; |
| 37 |
#m4 _POP() |
| 38 |
|
| 39 |
namespace Gtk |
| 40 |
{ |
| 41 |
|
| 42 |
_WRAP_ENUM(ApplicationInhibitFlags, GtkApplicationInhibitFlags, decl_prefix GTKMM_API) |
| 43 |
|
| 44 |
class GTKMM_API Window; |
| 45 |
|
| 46 |
//TODO: Add overview documentation discussing the techniques show in the examples in gtkmm-docmentation. |
| 47 |
//TODO: Add the screenshots from the C API? |
| 48 |
|
| 49 |
/** The Gtk::Application class handles many important aspects |
| 50 |
* of a GTK+ application in a convenient fashion, without enforcing |
| 51 |
* a one-size-fits-all application model. |
| 52 |
* |
| 53 |
* Currently, Gtk::Application handles GTK+ and gtkmm initialization, application |
| 54 |
* uniqueness, session management, provides some basic scriptability and |
| 55 |
* desktop shell integration by exporting actions and menus, and manages a |
| 56 |
* list of toplevel windows whose life-cycle is automatically tied to the |
| 57 |
* life-cycle of your application. |
| 58 |
* |
| 59 |
* While Gtk::Application works fine with plain Gtk::Window%s, it is recommended |
| 60 |
* to use it together with Gtk::ApplicationWindow. |
| 61 |
* |
| 62 |
* When GDK threads are enabled, Gtk::Application will acquire the GDK |
| 63 |
* lock when invoking actions that arrive from other processes. The GDK |
| 64 |
* lock is not touched for local action invocations. In order to have |
| 65 |
* actions invoked in a predictable context it is therefore recommended |
| 66 |
* that the GDK lock be held while invoking actions locally with |
| 67 |
* Gio::ActionGroup::activate_action(). The same applies to actions |
| 68 |
* associated with Gtk::ApplicationWindow and to the Gio::Application::activate() and |
| 69 |
* Gio::Application::open() methods. |
| 70 |
* |
| 71 |
* To set an application menu for a Gtk::Application, use |
| 72 |
* set_app_menu(). The Gio::MenuModel that this method |
| 73 |
* expects is usually constructed using Gtk::Builder. |
| 74 |
* To specify a menubar that will be shown by |
| 75 |
* Gtk::ApplicationWindow%s, use set_menubar(). Use the base |
| 76 |
* Gio::ActionMap interface to add actions, to respond to the user |
| 77 |
* selecting these menu items. |
| 78 |
* |
| 79 |
* GTK+ displays these menus as expected, depending on the platform |
| 80 |
* the application is running on. |
| 81 |
* |
| 82 |
* Gtk::Application optionally registers with a session manager |
| 83 |
* of the users session (if you set the register-session |
| 84 |
* property) and offers various functionality related to the session |
| 85 |
* life-cycle. |
| 86 |
* |
| 87 |
* An application can block various ways to end the session with |
| 88 |
* the inhibit() method. Typical use cases for |
| 89 |
* this kind of inhibiting are long-running, uninterruptible operations, |
| 90 |
* such as burning a CD or performing a disk backup. The session |
| 91 |
* manager may not honor the inhibitor, but it can be expected to |
| 92 |
* inform the user about the negative consequences of ending the |
| 93 |
* session while inhibitors are present. |
| 94 |
* |
| 95 |
* @newin{3,4} |
| 96 |
*/ |
| 97 |
class GTKMM_API Application |
| 98 |
: public Gio::Application |
| 99 |
{ |
| 100 |
protected: |
| 101 |
_CLASS_GOBJECT(Application, GtkApplication, GTK_APPLICATION, Gio::Application, GApplication, , , GTKMM_API) |
| 102 |
|
| 103 |
protected: |
| 104 |
|
| 105 |
/** Creates a new Application instance. |
| 106 |
* |
| 107 |
* This initializes gtkmm (and the GObject type system) for you. |
| 108 |
* |
| 109 |
* If non-empty, the application ID must be valid. See |
| 110 |
* g_application_id_is_valid(). |
| 111 |
* |
| 112 |
* If no application ID is given then some features (most notably application |
| 113 |
* uniqueness) will be disabled. A null application ID is only allowed with |
| 114 |
* gtkmm 3.6 or later. |
| 115 |
* |
| 116 |
* @param application_id A valid application ID. |
| 117 |
* @param flags The application flags. |
| 118 |
* |
| 119 |
* @newin{3,4} |
| 120 |
*/ |
| 121 |
explicit Application(const Glib::ustring& application_id = Glib::ustring(), Gio::ApplicationFlags flags = Gio::APPLICATION_FLAGS_NONE); |
| 122 |
_IGNORE(gtk_application_new) |
| 123 |
_IGNORE(gtk_application_window_new) |
| 124 |
|
| 125 |
|
| 126 |
//This constructor does not correspond to anything in the C API. |
| 127 |
//We added it so we can choose to always initialize gtkmm as early as possible. |
| 128 |
//See https://bugzilla.gnome.org/show_bug.cgi?id=639925 |
| 129 |
/** Creates a new Application instance. |
| 130 |
* |
| 131 |
* This initializes gtkmm (and the GObject type system) for you, parsing the |
| 132 |
* standard GTK+ command line arguments. |
| 133 |
* |
| 134 |
* You could instead use the constructor that takes no command line arguments |
| 135 |
* and later supply the command line arguments to run(). |
| 136 |
* |
| 137 |
* Note that all GTK+ functionality that is available via command line arguments |
| 138 |
* can instead be achieved by setting suitable environment variables |
| 139 |
* such as G_DEBUG. |
| 140 |
* |
| 141 |
* If non-empty, the application ID must be valid. See |
| 142 |
* g_application_id_is_valid(). |
| 143 |
* |
| 144 |
* If no application ID is given then some features (most notably application |
| 145 |
* uniqueness) will be disabled. A null application ID is only allowed with |
| 146 |
* gtkmm 3.6 or later. |
| 147 |
* |
| 148 |
* @param argc The parameter received by your main() function. |
| 149 |
* @param argv The parameter received by your main() function. |
| 150 |
* @param application_id A valid application ID. |
| 151 |
* @param flags The application flags. |
| 152 |
* |
| 153 |
* @newin{3,4} |
| 154 |
*/ |
| 155 |
explicit Application(int& argc, char**& argv, const Glib::ustring& application_id = Glib::ustring(), Gio::ApplicationFlags flags = Gio::APPLICATION_FLAGS_NONE); |
| 156 |
|
| 157 |
public: |
| 158 |
|
| 159 |
/** Creates a new Application instance. |
| 160 |
* |
| 161 |
* This initializes gtkmm (and the GObject type system) for you. |
| 162 |
* |
| 163 |
* If non-empty, the application ID must be valid. See |
| 164 |
* g_application_id_is_valid(). |
| 165 |
* |
| 166 |
* If no application ID is given then some features (most notably application |
| 167 |
* uniqueness) will be disabled. A null application ID is only allowed with |
| 168 |
* gtkmm 3.6 or later. |
| 169 |
* |
| 170 |
* @param application_id A valid application ID. |
| 171 |
* @param flags The application flags. |
| 172 |
* |
| 173 |
* @newin{3,4} |
| 174 |
*/ |
| 175 |
static Glib::RefPtr<Application> create(const Glib::ustring& application_id = Glib::ustring(), Gio::ApplicationFlags flags = Gio::APPLICATION_FLAGS_NONE); |
| 176 |
|
| 177 |
/** Creates a new Application instance. |
| 178 |
* |
| 179 |
* This initializes gtkmm (and the GObject type system) for you, parsing the |
| 180 |
* standard GTK+ command line arguments. |
| 181 |
* |
| 182 |
* You could instead use the constructor that takes no command line arguments |
| 183 |
* and later supply the command line arguments to run(). |
| 184 |
* |
| 185 |
* Note that all GTK+ functionality that is available via command line arguments |
| 186 |
* can instead be achieved by setting suitable environment variables |
| 187 |
* such as G_DEBUG. |
| 188 |
* |
| 189 |
* If non-empty, the application ID must be valid. See |
| 190 |
* g_application_id_is_valid(). |
| 191 |
* |
| 192 |
* If no application ID is given then some features (most notably application |
| 193 |
* uniqueness) will be disabled. A null application ID is only allowed with |
| 194 |
* gtkmm 3.6 or later. |
| 195 |
* |
| 196 |
* @param argc The parameter received by your main() function. |
| 197 |
* @param argv The parameter received by your main() function. |
| 198 |
* @param application_id A valid application ID. |
| 199 |
* |
| 200 |
* @param flags The application flags. |
| 201 |
* @newin{3,4} |
| 202 |
*/ |
| 203 |
static Glib::RefPtr<Application> create(int& argc, char**& argv, const Glib::ustring& application_id = Glib::ustring(), Gio::ApplicationFlags flags = Gio::APPLICATION_FLAGS_NONE); |
| 204 |
|
| 205 |
#m4 _CONVERSION(`GList*',`std::vector<Window*>',`Glib::ListHandler<Window*>::list_to_vector($3, Glib::OWNERSHIP_NONE)') |
| 206 |
_WRAP_METHOD(std::vector<Window*> get_windows(), gtk_application_get_windows) |
| 207 |
|
| 208 |
#m4 _CONVERSION(`GList*',`std::vector<const Window*>',`Glib::ListHandler<const Window*>::list_to_vector($3, Glib::OWNERSHIP_NONE)') |
| 209 |
_WRAP_METHOD(std::vector<const Window*> get_windows() const, gtk_application_get_windows) |
| 210 |
|
| 211 |
// Concerning the note on quit(), see https://bugzilla.gnome.org/show_bug.cgi?id=731126 |
| 212 |
// Concerning the return from run() on hidden windows, see |
| 213 |
// https://bugzilla.gnome.org/show_bug.cgi?id=639931 |
| 214 |
/** Adds a window to the Gtk::Application. |
| 215 |
* |
| 216 |
* If all the windows managed by Gtk::Application are closed (hidden) or |
| 217 |
* removed from the application then the call to run() will return, |
| 218 |
* unless Gio::Application::hold() has been called. |
| 219 |
* |
| 220 |
* This call is equivalent to calling Gtk::Window::set_application(), except |
| 221 |
* that run() does not return when a window added with Gtk::Window::set_application() |
| 222 |
* is closed (hidden). |
| 223 |
* |
| 224 |
* Normally, the connection between the application and the window |
| 225 |
* will remain until the window is closed (hidden) or destroyed, but you can explicitly |
| 226 |
* remove it with remove_window(). |
| 227 |
* |
| 228 |
* @note If you call Gio::Application::quit() while a window is connected to |
| 229 |
* the application, and then return from main() without removing the window |
| 230 |
* from the application, the application's destructor will not be called. |
| 231 |
* |
| 232 |
* @newin{3,4} |
| 233 |
* |
| 234 |
* @param window A toplevel window to add to the application. |
| 235 |
*/ |
| 236 |
void add_window(Window& window); |
| 237 |
_IGNORE(gtk_application_add_window) |
| 238 |
|
| 239 |
_WRAP_METHOD(void remove_window(Window& window), gtk_application_remove_window) |
| 240 |
|
| 241 |
/** Starts the application. |
| 242 |
* |
| 243 |
* The default implementation of this virtual function will simply run |
| 244 |
* a main loop. |
| 245 |
* |
| 246 |
* It is an error to call this function if @a application is a proxy for |
| 247 |
* a remote application. |
| 248 |
* |
| 249 |
* @param argc The argc from main() (or 0 if @a argv is <tt>0</tt>). |
| 250 |
* @param argv The argv from main(), or <tt>0</tt>. |
| 251 |
* @return The exit status. |
| 252 |
* |
| 253 |
* @newin{3,4} |
| 254 |
*/ |
| 255 |
int run(int argc, char** argv); |
| 256 |
|
| 257 |
/** Starts the application. |
| 258 |
* |
| 259 |
* The default implementation of this virtual function will simply run |
| 260 |
* a main loop. |
| 261 |
* |
| 262 |
* It is an error to call this function if @a application is a proxy for |
| 263 |
* a remote application. |
| 264 |
* |
| 265 |
* @note If you call Gio::Application::quit() while a window is connected to |
| 266 |
* the application, and then return from main() without removing the window |
| 267 |
* from the application, the application's destructor will not be called. |
| 268 |
* |
| 269 |
* @param window The window to show. This method will return when the window is hidden. |
| 270 |
* @param argc The argc from main() (or 0 if @a argv is <tt>0</tt>). |
| 271 |
* @param argv The argv from main(), or <tt>0</tt>. |
| 272 |
* @return The exit status. |
| 273 |
* |
| 274 |
* @newin{3,4} |
| 275 |
*/ |
| 276 |
int run(Window& window, int argc, char** argv); |
| 277 |
|
| 278 |
//TODO: If argc and argv are necessary at all, document the need to call that constructor first. |
| 279 |
//See https://bugzilla.gnome.org/show_bug.cgi?id=639925#c4 |
| 280 |
/** Starts the application. |
| 281 |
* |
| 282 |
* The default implementation of this virtual function will simply run |
| 283 |
* a main loop. |
| 284 |
* |
| 285 |
* It is an error to call this function if @a application is a proxy for |
| 286 |
* a remote application. |
| 287 |
* |
| 288 |
* @note If you call Gio::Application::quit() while a window is connected to |
| 289 |
* the application, and then return from main() without removing the window |
| 290 |
* from the application, the application's destructor will not be called. |
| 291 |
* |
| 292 |
* @param window The window to show. This method will return when the window is hidden. |
| 293 |
* @return The exit status. |
| 294 |
* |
| 295 |
* @newin{3,4} |
| 296 |
*/ |
| 297 |
int run(Window& window); |
| 298 |
|
| 299 |
//TODO: If argc and argv are necessary at all, document the need to call that constructor first. |
| 300 |
//See https://bugzilla.gnome.org/show_bug.cgi?id=639925#c4 |
| 301 |
/** Starts the application. |
| 302 |
* |
| 303 |
* The default implementation of this virtual function will simply run |
| 304 |
* a main loop. |
| 305 |
* |
| 306 |
* It is an error to call this function if @a application is a proxy for |
| 307 |
* a remote application. |
| 308 |
* |
| 309 |
* @return The exit status. |
| 310 |
* |
| 311 |
* @newin{3,4} |
| 312 |
*/ |
| 313 |
int run(); |
| 314 |
|
| 315 |
_WRAP_METHOD(Glib::RefPtr<Gio::MenuModel> get_app_menu(), gtk_application_get_app_menu, refreturn) |
| 316 |
_WRAP_METHOD(Glib::RefPtr<const Gio::MenuModel> get_app_menu() const, gtk_application_get_app_menu, refreturn, constversion) |
| 317 |
|
| 318 |
_WRAP_METHOD(void set_app_menu(const Glib::RefPtr<Gio::MenuModel>& app_menu), gtk_application_set_app_menu) |
| 319 |
|
| 320 |
_WRAP_METHOD(Glib::RefPtr<Gio::MenuModel> get_menubar(), gtk_application_get_menubar, refreturn) |
| 321 |
_WRAP_METHOD(Glib::RefPtr<const Gio::MenuModel> get_menubar() const, gtk_application_get_menubar, refreturn, constversion) |
| 322 |
|
| 323 |
_WRAP_METHOD(void set_menubar(const Glib::RefPtr<Gio::MenuModel>& menubar), gtk_application_set_menubar) |
| 324 |
|
| 325 |
_WRAP_METHOD(void add_accelerator(const Glib::ustring& accelerator, const Glib::ustring& action_name, GVariant* parameter{?}), gtk_application_add_accelerator, deprecated "Use set_accels_for_action() instead.") |
| 326 |
|
| 327 |
_WRAP_METHOD(void remove_accelerator(const Glib::ustring& action_name, GVariant* parameter{?}), gtk_application_remove_accelerator, deprecated "Use set_accels_for_action() or unset_accels_for_action() instead.") |
| 328 |
|
| 329 |
_WRAP_METHOD(guint inhibit(Window& window, ApplicationInhibitFlags flags, const Glib::ustring& reason), gtk_application_inhibit) |
| 330 |
_WRAP_METHOD(void uninhibit(guint cookie), gtk_application_uninhibit) |
| 331 |
_WRAP_METHOD(bool is_inhibited(ApplicationInhibitFlags flags) const, gtk_application_is_inhibited) |
| 332 |
|
| 333 |
_WRAP_METHOD(Window* get_window_by_id(guint id), gtk_application_get_window_by_id, refreturn) |
| 334 |
_WRAP_METHOD(const Window* get_window_by_id(guint id) const, gtk_application_get_window_by_id, refreturn, constversion) |
| 335 |
|
| 336 |
_WRAP_METHOD(Window* get_active_window(), gtk_application_get_active_window, refreturn) |
| 337 |
_WRAP_METHOD(const Window* get_active_window() const, gtk_application_get_active_window, refreturn, constversion) |
| 338 |
|
| 339 |
#m4 _CONVERSION(`gchar**',`std::vector<Glib::ustring>',`Glib::ArrayHandler<Glib::ustring>::array_to_vector($3, Glib::OWNERSHIP_DEEP)') |
| 340 |
_WRAP_METHOD(std::vector<Glib::ustring> list_action_descriptions() const, gtk_application_list_action_descriptions) |
| 341 |
|
| 342 |
#m4 _CONVERSION(`gchar**',`std::vector<Glib::ustring>',`Glib::ArrayHandler<Glib::ustring>::array_to_vector($3, Glib::OWNERSHIP_DEEP)') |
| 343 |
_WRAP_METHOD(std::vector<Glib::ustring> get_accels_for_action(const Glib::ustring& detailed_action_name) const, gtk_application_get_accels_for_action) |
| 344 |
|
| 345 |
_WRAP_METHOD(std::vector<Glib::ustring> get_actions_for_accel(const Glib::ustring& accel) const, gtk_application_get_actions_for_accel) |
| 346 |
|
| 347 |
#m4 _CONVERSION(`const std::vector<Glib::ustring>&',`const gchar*-const*',`Glib::ArrayHandler<Glib::ustring>::vector_to_array($3).data ()') |
| 348 |
_WRAP_METHOD(void set_accels_for_action(const Glib::ustring& detailed_action_name, const std::vector<Glib::ustring>& accels), gtk_application_set_accels_for_action) |
| 349 |
|
| 350 |
/** Sets a keyboard accelerator that will trigger the |
| 351 |
* given action. It will be the primary |
| 352 |
* accelerator, which may be displayed in the UI. |
| 353 |
* |
| 354 |
* See unset_accels_for_action(). |
| 355 |
* |
| 356 |
* @param detailed_action_name A detailed action name, specifying an action |
| 357 |
* and target to associate accelerators with. |
| 358 |
* @param accel An accelerator in the format understood by gtk_accelerator_parse(). |
| 359 |
* |
| 360 |
* @newin{3,12} |
| 361 |
*/ |
| 362 |
void set_accel_for_action(const Glib::ustring& detailed_action_name, const Glib::ustring& accel); |
| 363 |
|
| 364 |
/** Unsets a keyboard accelerator that will trigger the |
| 365 |
* given action. See set_accels_for_action() and set_accel_for_action(). |
| 366 |
* |
| 367 |
* @param detailed_action_name A detailed action name, specifying an action |
| 368 |
* and target to associate accelerators with. |
| 369 |
* |
| 370 |
* @newin{3,12} |
| 371 |
*/ |
| 372 |
void unset_accels_for_action(const Glib::ustring& detailed_action_name); |
| 373 |
|
| 374 |
_WRAP_METHOD(bool prefers_app_menu() const, gtk_application_prefers_app_menu) |
| 375 |
|
| 376 |
_WRAP_METHOD(Glib::RefPtr<Gio::Menu> get_menu_by_id(const Glib::ustring& id), gtk_application_get_menu_by_id, refreturn) |
| 377 |
_WRAP_METHOD(Glib::RefPtr<const Gio::Menu> get_menu_by_id(const Glib::ustring& id) const, gtk_application_get_menu_by_id, refreturn, constversion) |
| 378 |
|
| 379 |
_WRAP_PROPERTY("app-menu", Glib::RefPtr<Gio::MenuModel>) |
| 380 |
_WRAP_PROPERTY("menubar", Glib::RefPtr<Gio::MenuModel>) |
| 381 |
_WRAP_PROPERTY("register-session", bool) |
| 382 |
_WRAP_PROPERTY("active-window", Window*) |
| 383 |
|
| 384 |
_WRAP_SIGNAL(void window_added(Window* window), "window-added") |
| 385 |
_WRAP_SIGNAL(void window_removed(Window* window), "window-removed") |
| 386 |
|
| 387 |
private: |
| 388 |
/** This is just a way to call Glib::init() (which calls g_type_init()) before |
| 389 |
* calling application_class_.init(), so that |
| 390 |
* gtk_application_get_type() will always succeed. |
| 391 |
* See https://bugzilla.gnome.org/show_bug.cgi?id=639925 |
| 392 |
*/ |
| 393 |
const Glib::Class& custom_class_init(); |
| 394 |
|
| 395 |
void on_activate_add_and_show_main_window(Window* window); |
| 396 |
void on_window_hide(Window* window); |
| 397 |
|
| 398 |
//We need these to call g_application_run(), |
| 399 |
//even if we have already called gtk_init(). |
| 400 |
int m_argc; |
| 401 |
char** m_argv; |
| 402 |
}; |
| 403 |
|
| 404 |
} // namespace Gtk |