c++-gtk-utils
application.h
Go to the documentation of this file.
1 /* Copyright (C) 2011, 2013 and 2020 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 */
24 
25 #ifndef CGU_APPLICATION_H
26 #define CGU_APPLICATION_H
27 
28 #include <list>
29 #include <exception>
30 #include <utility>
31 
33 
34 #ifdef CGU_USE_GTK
35 #include <gtk/gtk.h>
36 #endif
37 
38 #include <gio/gio.h>
39 
41 #include <c++-gtk-utils/window.h>
42 #include <c++-gtk-utils/emitter.h>
43 
44 namespace Cgu {
45 
46 #if defined(DOXYGEN_PARSING) || defined(CGU_USE_GTK)
47 #if defined(DOXYGEN_PARSING) || GTK_CHECK_VERSION(2,99,0)
48 
49 /**
50  * @class Cgu::ApplicationNameError application.h c++-gtk-utils/application.h
51  * @brief This class is thrown when the program id name passed to the
52  * constructor of Cgu::Application is invalid.
53  */
54 struct ApplicationNameError: public std::exception {
55  virtual const char* what() const throw() {return "ApplicationNameError\n";}
56 };
57 
58 /**
59  * @class Cgu::Application application.h c++-gtk-utils/application.h
60  * @brief This is a class for constructing and managing GtkApplication
61  * objects.
62  *
63  * @details It is available since version 2.0.0-rc2. It is only
64  * compiled in with a GTK-3 or GTK-4 installation, and if the library
65  * is not configured with the \--without-gtk option.
66  *
67  * In typical usage, a Cgu::Application object is created in main(),
68  * and then a callback is attached to the 'activate', 'command_line'
69  * or 'open' emitter, depending on the flag passed to the Application
70  * object's constructor. The run() method of the Application object
71  * is then called, and a window deriving from Cgu::WinBase is
72  * constructed in the callback and added to the Application object or,
73  * if the program is a single instance program with only one main
74  * window and an instance is already running, a function is called to
75  * present that window.
76  *
77  * Here is a brief explanation of how the flag options passed to the
78  * Cgu::Application object (and so to the underlying GApplication
79  * object) work:
80  *
81  * @par
82  * (a) If a Cgu::Application object is constructed with the
83  * G_APPLICATION_FLAGS_NONE flag set, then calling the
84  * Cgu::Application::run() method (which hands off to
85  * g_application_run()) will cause the 'activate' emitter to emit. No
86  * command line parameter should be passed to the run method (argc
87  * should be 0 or 1), otherwise GtkApplication will cause the start-up
88  * to abort, except that from glib >=2.40 and gtk >=3.12 recognised
89  * gtk command line options can be passed in for stripping out and
90  * consumption by gtk. g_application_run(), and so
91  * Cgu::Application::run(), can be called with argc and argv set to 0
92  * where such aborting must be avoided.
93  * @par
94  * (b) If a Cgu::Application object is constructed with the
95  * G_APPLICATION_HANDLES_OPEN flag set, then calling the
96  * Cgu::Application::run() method will cause the 'activate' emitter to
97  * emit if no command line parameters were provided when the program
98  * was started, or cause the 'open' emitter to emit if parameters are
99  * passed. Such parameters will be construed as files/uris, and will
100  * be passed to the 'open' emitter by array of GFile*'s.
101  * @par
102  * (c) If a Cgu::Application object is constructed with the
103  * G_APPLICATION_HANDLES_COMMAND_LINE flag set, then calling the
104  * Cgu::Application::run() method will cause the 'command_line'
105  * emitter to emit. All the command line parameters not handled
106  * locally in the new instance will be passed on, and they can be
107  * obtained via the GApplicationCommandLine argument of the
108  * 'command_line' emitter.
109  *
110  * Prior to glib-2.40/gtk-3.12 g_application_run() (and so
111  * Cgu::Application::run()) does not consume any recognised glib/gtk
112  * options such as \--display. Such options can be stripped out (and
113  * acted on by gtk) by calling gtk_init() before constructing the
114  * Cgu::Application object (but gtk_init() does not need to be called
115  * for any other purpose), or by using the GOptionGroup/GOptionEntry
116  * interface.
117  *
118  * There is no emitter provided for GApplication's 'shutdown' and
119  * 'handle-local-options' signals ('handle-local-options' is provided
120  * from glib-2.40 onwards). There is only very rarely a need to use
121  * the 'shutdown' signal in C++ code, which should not normally keep
122  * naked resources (the question whether clean-up on shutdown is
123  * needed is usually not just answered by whether we are in the first
124  * instance of the program to start, but whether particular state has
125  * accumulated in consequence of the program running, which is a
126  * question best answered by the object(s) managing the state, say on
127  * destruction on stack unwinding); but where the 'shutdown' signal is
128  * useful, GApplication's C level API is available via
129  * Cgu::Application::get_g_app(). If using 'handle-local-options' you
130  * will normally have hooked into local option handling using
131  * g_application_add_main_option_entries() or
132  * g_application_add_option_group(). In that case the option
133  * handling, including connecting where relevent to
134  * 'handle-local-options', is also best done at GApplication's C level
135  * by obtaining the GApplication object with
136  * Cgu::Application::get_g_app().
137  *
138  * There is little in this class that cannot also be done using the
139  * @ref prog_presenterAnchor "Cgu::prog_present" interface, which has
140  * the advantage of being more portable (@ref prog_presenterAnchor
141  * "Cgu::prog_present" does not depend on GTK-3 or GTK-4), but this
142  * class is more convenient to use where a program requires multiple
143  * main application windows which can be independently opened and any
144  * of which are to keep the program alive until the last one is
145  * closed, or if an application menu is to be provided for, say, the
146  * gnome shell.
147  *
148  * Cgu::Application objects are not singletons. It is possible to
149  * drop an Application object out of scope or destroy it in some other
150  * way after closing or removing all its windows, then construct
151  * another with a different flag and then call run() on the second one
152  * (although it would be a curious application that wanted to do so).
153  * It is also possible, but even more off-the-wall, to have two
154  * Application objects in existence in the same process at the same
155  * time provided different dbus identifiers are supplied to the
156  * constructor for each, although run() may only be called on one of
157  * them at any one time. However, this is something of a curiosity:
158  * in nearly all cases an application will only have one
159  * Cgu::Application object, since the main purpose of Cgu::Application
160  * is to facilitate single instance programs.
161  *
162  * Cgu::WinBase objects, and so Cgu::Application, can be used with
163  * widget heirarchies or top level windows created using GtkBuilder.
164  * See @ref GtkBuilder for particulars about that.
165  *
166  * Here is a compilable example, demonstrating the use of the
167  * GApplicationFlags options. It uses a GtkApplicationWindow object,
168  * as provided by GTK >= 3.4, so that it can provide an application
169  * menu for, say, the gnome shell. For earlier versions of GTK-3, the
170  * Message class can be constructed from a standard GtkWindow object,
171  * and the application and window menu code in the startup() callback
172  * can be omitted.
173  *
174  * @code
175  * #include <iostream>
176  * #include <ostream>
177  * #include <string>
178  * #include <list>
179  *
180  * #include <gtk/gtk.h>
181  *
182  * #include <c++-gtk-utils/callback.h>
183  * #include <c++-gtk-utils/application.h>
184  * #include <c++-gtk-utils/window.h>
185  * #include <c++-gtk-utils/shared_handle.h>
186  * #include <c++-gtk-utils/gobj_handle.h>
187  *
188  * // SETUP HERE: uncomment the flag to be tested:
189  *
190  * //const GApplicationFlags app_flag = G_APPLICATION_FLAGS_NONE;
191  * const GApplicationFlags app_flag = G_APPLICATION_HANDLES_OPEN;
192  * //const GApplicationFlags app_flag = G_APPLICATION_HANDLES_COMMAND_LINE;
193  *
194  * using namespace Cgu;
195  *
196  * // *** Demonstration Message class ***
197  *
198  * extern "C" void message_button_clicked(GtkWidget*, void*);
199  *
200  * class Message: public Cgu::WinBase {
201  * public:
202  * friend void message_button_clicked(GtkWidget*, void*);
203  * Message(const char* text, GApplication* app);
204  * };
205  *
206  * // *** for Message menu actions ***
207  *
208  * namespace {
209  * extern "C" {
210  * void win_beep(GSimpleAction*, GVariant*, void*) {
211  * gdk_beep();
212  * }
213  *
214  * GActionEntry win_entries[] = {
215  * { "beep", win_beep, NULL, NULL, NULL },
216  * };
217  * } // extern "C"
218  * } // unnamed namespace
219  *
220  * // *** Message callbacks ***
221  *
222  * void message_button_clicked(GtkWidget* w, void*) {
223  * std::cout << "Clicked" << std::endl;
224  * }
225  *
226  * // *** Message implementation ***
227  *
228  * Message::Message(const char* text, GApplication* app):
229  * WinBase{"Message", 0, false, 0,
230  * GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(app)))} {
231  * g_action_map_add_action_entries(G_ACTION_MAP(get_win()),
232  * win_entries,
233  * G_N_ELEMENTS(win_entries),
234  * this);
235  * GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
236  * gtk_box_set_homogeneous(GTK_BOX(box), false);
237  * gtk_container_add(GTK_CONTAINER(get_win()), box);
238  * GtkWidget* label = gtk_label_new(text);
239  * gtk_box_pack_start(GTK_BOX(box), label,
240  * true, false, 0);
241  * GtkWidget* button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
242  * gtk_box_pack_start(GTK_BOX(box), button_box,
243  * false, false, 0);
244  * GtkWidget* button = gtk_button_new_from_stock(GTK_STOCK_OK);
245  * gtk_container_add(GTK_CONTAINER(button_box), button);
246  * g_signal_connect(G_OBJECT(button), "clicked",
247  * G_CALLBACK(message_button_clicked), 0);
248  * gtk_widget_set_can_default(button, true);
249  * }
250  *
251  * namespace {
252  *
253  * // *** for application actions ***
254  *
255  * extern "C" {
256  * void app_beep(GSimpleAction*, GVariant*, void*) {
257  * gdk_beep();
258  * }
259  *
260  * void app_quit(GSimpleAction*, GVariant*, void* data) {
261  * Cgu::Application* app = static_cast<Cgu::Application*>(data);
262  * std::list<Cgu::WinBase*> wins = app->get_windows();
263  * for (auto iter = wins.begin(); iter != wins.end(); ++iter) {
264  * delete *iter; // this will also remove the Message object from
265  * // the Cgu::Application object
266  * }
267  * }
268  *
269  * GActionEntry app_entries[] = {
270  * { "beep", app_beep, NULL, NULL, NULL },
271  * { "quit", app_quit, NULL, NULL, NULL },
272  * };
273  * } // extern "C"
274  *
275  * // *** application callbacks ***
276  *
277  * void startup(Cgu::Application* app) {
278  * std::cout << "startup() called" << std::endl;
279  *
280  * g_action_map_add_action_entries(G_ACTION_MAP(app->get_g_app()),
281  * app_entries,
282  * G_N_ELEMENTS(app_entries),
283  * app);
284  * Cgu::GobjHandle<GMenu> app_menu{g_menu_new()};
285  * g_menu_append(app_menu, "Beep", "app.beep");
286  * g_menu_append(app_menu, "_Quit", "app.quit");
287  * gtk_application_set_app_menu(GTK_APPLICATION(app->get_g_app()),
288  * G_MENU_MODEL(app_menu.get()));
289  *
290  * Cgu::GobjHandle<GMenu> menubar{g_menu_new()};
291  * Cgu::GobjHandle<GMenu> win_menu{g_menu_new()};
292  * g_menu_append_submenu(menubar, "Menu1", G_MENU_MODEL(win_menu.get()));
293  * g_menu_append(win_menu, "App beep", "app.beep");
294  * g_menu_append(win_menu, "More beep", "win.beep"); // see Message::Message() for action
295  * gtk_application_set_menubar(GTK_APPLICATION(app->get_g_app()),
296  * G_MENU_MODEL(menubar.get()));
297  * }
298  *
299  * void activate(Cgu::Application* app) {
300  * std::cout << "activate() called" << std::endl;
301  *
302  * // probably if no arguments are passed, only one window is wanted,
303  * // which is now to present itself if it already exists: comment this
304  * // 'if' block out if a new window is to be added on each occasion
305  * // the program is started
306  * if (app->get_win_count() > 0) {
307  * gtk_window_present(app->get_windows().front()->get_win());
308  * return;
309  * }
310  * WinBase* dialog = new Message("This is a message", app->get_g_app());
311  * app->add(dialog);
312  * dialog->show_all();
313  * }
314  *
315  * void command_line(Cgu::Application* app, GApplicationCommandLine* cl, gint&) {
316  * std::cout << "command_line() called" << std::endl;
317  *
318  * // probably if the G_APPLICATION_HANDLES_COMMAND_LINE flag is set,
319  * // only one window is wanted, which is now to present itself if it
320  * // already exists: comment this 'if' block out if a new window is to
321  * // be added on each occasion the program is started
322  * if (app->get_win_count() > 0) {
323  * gtk_window_present(app->get_windows().front()->get_win());
324  * return;
325  * }
326  * std::string text("Command line options are:\n");
327  * int argc = 0;
328  * gchar** argv = g_application_command_line_get_arguments(cl, &argc);
329  * for (int count = 0; count < argc; ++count) {
330  * try {
331  * text += argv[count];
332  * text += '\n';
333  * }
334  * catch (...) {
335  * g_strfreev(argv);
336  * throw; // exceptions will be consumed by the callback handler and
337  * // a g_critical warning issued, but let's not leak memory
338  * }
339  * }
340  * g_strfreev(argv);
341  * WinBase* dialog = new Message(text.c_str(), app->get_g_app());
342  * app->add(dialog);
343  * dialog->show_all();
344  * }
345  *
346  * void open(Cgu::Application* app, std::pair<GFile**, gint> files, gchar*) {
347  * std::cout << "open() called" << std::endl;
348  *
349  * // probably if the G_APPLICATION_HANDLES_OPEN flag is set and an
350  * // argument is passed, the adding of a new window is wanted on each
351  * // occasion the program is started
352  * std::string text("Files are:\n");
353  * for (int count = 0; count < files.second; ++count) {
354  * GcharScopedHandle uri(g_file_get_uri(files.first[count]));
355  * text += uri;
356  * text += '\n';
357  * }
358  * WinBase* dialog = new Message(text.c_str(), app->get_g_app());
359  * app->add(dialog);
360  * dialog->show_all();
361  * }
362  *
363  * } // unnamed namespace
364  *
365  * // *** main() ***
366  *
367  * int main(int argc, char* argv[]) {
368  *
369  * // gtk_init() is only relevant for the purposes of stripping out
370  * // and acting on glib/gtk recognised options, either where
371  * // glib < 2.40 and gtk < 3.12 are used or where you want to pass
372  * // no arguments to Cgu::Application::run() when
373  * // G_APPLICATION_FLAGS_NONE is set in order to ensure that the
374  * // program does not abort with unrecognised options -
375  * // gtk_application_new() (and so the Cgu::Application constructor)
376  * // will call g_type_init() if the type system needs initialization
377  * gtk_init(&argc, &argv);
378  *
379  * Application app{"my_prog", app_flag};
380  * app.startup.connect(Callback::make(startup));
381  * app.activate.connect(Callback::make(activate));
382  * app.command_line.connect(Callback::make(command_line));
383  * app.open.connect(Callback::make(open));
384  * if (app_flag == G_APPLICATION_FLAGS_NONE)
385  * return app.run(0, 0);
386  * else
387  * return app.run(argc, argv);
388  * }
389  * @endcode
390  *
391  * One thing to note about this example is that the callbacks
392  * connected to the Cgu::Application object execute in the first
393  * instance of the program to be started (the instance in which
394  * Cgu::Application::run() blocks). If the program is then restarted,
395  * Cgu::Application::run() returns in the new program instance as soon
396  * as it has invoked the existing instance via dbus, following which
397  * the new program instance exits, so immediately disposing of the
398  * Cgu::Application object and callbacks which were constructed on the
399  * restart. This is a feature of GApplication/GtkApplication: given
400  * the overhead of starting a new process, and that restarting a
401  * single-instance program is in any event an exceptional event, any
402  * additional overhead created by constructing and then destroying the
403  * Cgu::Application object and callbacks in the new instance is
404  * trivial. (As mentioned above, from glib-2.40 the
405  * 'handle-local-options' signal can be used to hook into the
406  * GApplication object constructed in the new program instance, but
407  * this is intended to assist option parsing in the new instance.)
408  */
409 
410 class Application {
411 
412  std::list<WinBase*> win_list;
414 
415  void* reserved; // for future use
416 public:
417 
418  typedef std::list<WinBase*>::size_type size_type;
419 
420 /**
421  * This class cannot be copied. The copy constructor is deleted.
422  */
423  Application(const Application&) = delete;
424 
425 /**
426  * This class cannot be copied. The assignment operator is deleted.
427  */
428  Application& operator=(const Application&) = delete;
429 
430 /**
431  * This SafeEmitterArg object emits (and so executes any connected
432  * callback) when the underlying GApplication object emits its
433  * @a activate signal. The argument passed to the emitter's
434  * callback(s) is a pointer to the Cgu::Application object.
435  * @note When the callback executes, thread cancellation is blocked,
436  * and any exceptions are consumed with a g_critical message issued.
437  * The callback will always execute in the main GUI thread when
438  * executed in response to the run() method. Because a SafeEmitterArg
439  * object is used, the emitter object itself is thread safe.
440  *
441  * Since 2.0.0-rc2
442  */
444 
445 /**
446  * This SafeEmitterArg object emits (and so executes any connected
447  * callback) when the underlying GApplication object emits its @a
448  * startup signal (which, unless the G_APPLICATION_NON_UNIQUE flag has
449  * been set in the Cgu::Application object's constructor, it will
450  * normally do once, on the first occasion that run() is called). The
451  * argument passed to the emitter's callback(s) is a pointer to the
452  * Cgu::Application object. This signal can be used to set up a
453  * desktop application menu, or a menu bar for WinBase objects
454  * constructed from a GtkApplicationWindow object.
455  * @note When the callback executes, thread cancellation is blocked,
456  * and any exceptions are consumed with a g_critical message issued.
457  * The callback will always execute in the main GUI thread when
458  * executed in response to the run() method. Because a SafeEmitterArg
459  * object is used, the emitter object itself is thread safe.
460  *
461  * Since 2.0.0-rc2
462  */
464 
465 /**
466  * This SafeEmitterArg object emits (and so executes any connected
467  * callback) when the underlying GApplication object emits its
468  * @a command-line signal. The second argument passed to the
469  * emitter's callback(s) is the one passed by that signal, that is to
470  * say the arguments are:
471  *
472  * first: a pointer to the Cgu::Application object.
473  *
474  * second: a pointer to a GApplicationCommandLine object representing
475  * the passed command line (this is owned by gio and should not be
476  * unref'ed unless it has previously been explicitly ref'ed to keep
477  * the GApplicationCommandLine object alive even after the connected
478  * callback has returned).
479  *
480  * third: a gint& reference to which the value to be returned to the
481  * GApplication's command-line signal can be passed: if no value is
482  * assigned to it or no callback has been attached to the signal, 0
483  * will be returned, except that if an exception from a callback is
484  * consumed, -1 will be returned. If more than one callback is
485  * attached to the signal and no exception is consumed, the last one
486  * to assign a value will be have its value returned.
487  *
488  * @note When the callback executes, thread cancellation is blocked,
489  * and any exceptions are consumed with a g_critical message issued
490  * and a return value of -1 set. The callback will always execute in
491  * the main GUI thread when executed in response to the run() method.
492  * Because a SafeEmitterArg object is used, the emitter object itself
493  * is thread safe.
494  *
495  * Since 2.0.0-rc2
496  */
498 
499 /**
500  * This SafeEmitterArg object emits (and so executes any connected
501  * callback) when the underlying GApplication object emits its @a open
502  * signal. The second and third arguments passed to the emitter's
503  * callback(s) are those passed by that signal, that is to say the
504  * arguments are:
505  *
506  * first: a pointer to the Cgu::Application object.
507  *
508  * second: a std::pair object where the first member is an array of
509  * GFile*'s representing the files/uris passed as arguments, and the
510  * second member is the length of that array (the array is owned by
511  * gio and should not be freed).
512  *
513  * third: a gchar* argument comprising the text of the "hint" (this is
514  * owned by gio and should not be freed).
515  *
516  * @note When the callback executes, thread cancellation is blocked,
517  * and any exceptions are consumed with a g_critical message issued.
518  * The callback will always execute in the main GUI thread when
519  * executed in response to the run() method. Because a SafeEmitterArg
520  * object is used, the emitter object itself is thread safe.
521  *
522  * Since 2.0.0-rc2
523  */
525 
526 /**
527  * Add a Cgu::WinBase object to the Cgu::Application object, and so
528  * also add its managed GtkWindow object to the GtkApplication object.
529  * Any Cgu::WinBase object passed to this method should not normally
530  * be modal and must have been constructed on free store with the new
531  * expression. It is passed by pointer because it will be self-owning
532  * (its lifetime would normally be determined by user action, not by
533  * the program), although if it is removed from this Cgu::Application
534  * object with remove(), the delete expression can (and normally
535  * should) be called on it. If a delete event occurs on the WinBase
536  * object so that the WinBase object destroys itself (say, by the user
537  * clicking on the window's close/delete button), or it destroys
538  * itself in some other way (say, by calling the WinBase::close()
539  * method), it will automatically be removed from this Application
540  * object without further action being necessary. The WinBase::exec()
541  * method should never be called on a WinBase object which has been
542  * added to an Application object. The Cgu::Application class, and
543  * thus this method, does not employ mutexes to make it thread safe,
544  * as there should never be a reason to call Cgu::Application methods
545  * in other than the main GUI thread.
546  * @param win The Cgu::WinBase object to be added.
547  * @exception std::bad_alloc This method might throw std::bad_alloc if
548  * memory is exhausted and the system throws in that case, in which
549  * case the WinBase object passed in will not be added. If such an
550  * exception is thrown and program recovery is to be attempted (which
551  * is usually a waste of time as glib/gtk will terminate the program
552  * if unable to obtain memory from the operating system), it would be
553  * best to delete the Cgu::WinBase object passed in and start again.
554  * An alternative is to show the window to the user for the user to
555  * dispose of (windows passed to this method are self-owning); but if
556  * that is done note that if the contained GtkWindow object is a
557  * GtkApplicationWindow object, then it will already be associated
558  * with a GtkApplication object when constructed, so it would usually
559  * be best also to call gtk_application_remove_window() on it via
560  * Cgu::Application::get_g_app() and Cgu::WinBase::get_win() before or
561  * after it is shown so the Cgu::Application and GtkApplication
562  * objects remain in sync.
563  * @note As well as this method only being called in the main GUI
564  * thread, if the program by which it is called calls GTK directly in
565  * more than one thread and thus employs
566  * gdk_threads_enter()/gdk_threads_leave() (rather than, say,
567  * Cgu::Notifier or Cgu::Callback::post()), it must be surrounded by
568  * gdk_threads_enter()/gdk_threads_leave() if called otherwise than in
569  * a GTK signal handler. (The best approach however is for a program
570  * only to address GTK/GDK in the main program thread, for which
571  * purpose this library provides various functions and classes for
572  * inter-thread communication, such as Cgu::Notifier and
573  * Cgu::Callback::post(): see @ref Threading for particulars about
574  * GTK thread safety.)
575  *
576  * Since 2.0.0-rc2
577  */
578  void add(Cgu::WinBase* win);
579 
580 /**
581  * Remove a Cgu::WinBase object from the Cgu::Application object, and
582  * so also remove its managed GtkWindow object from the GtkApplication
583  * object. This method will not throw assuming that merely iterating
584  * through a list does not throw (as it would not on any sane
585  * implementation). The Cgu::Application class, and thus this method,
586  * does not employ mutexes to make it thread safe, as there should
587  * never be a reason to call Cgu::Application methods in other than
588  * the main GUI thread. Calling this method does not destroy the
589  * WinBase object.
590  * @param win The Cgu::WinBase object to be removed.
591  * @return true if the Cgu::WinBase object was found in the
592  * Cgu::Application object and so removed, otherwise false.
593  * @note As well as this method only being called in the main GUI
594  * thread, if the program by which it is called calls GTK directly in
595  * more than one thread and thus employs
596  * gdk_threads_enter()/gdk_threads_leave() (rather than, say,
597  * Cgu::Notifier or Cgu::Callback::post()), it must be surrounded by
598  * gdk_threads_enter()/gdk_threads_leave() if called otherwise than in
599  * a GTK signal handler. (The best approach however is for a program
600  * only to address GTK/GDK in the main program thread, for which
601  * purpose this library provides various functions and classes for
602  * inter-thread communication, such as Cgu::Notifier and
603  * Cgu::Callback::post(): see @ref Threading for particulars about
604  * GTK thread safety.)
605  *
606  * Since 2.0.0-rc2
607  */
608  bool remove(Cgu::WinBase* win);
609 
610 /**
611  * Calls g_application_run() in respect of the underlying
612  * GtkApplication object, so invoking some of the Cgu::Application
613  * class's emitters (normally in the first instance of the program to
614  * be started): the exact behaviour depends on the GApplication flags
615  * passed to the constructor and is explained in the introductory
616  * remarks above. This method is thread safe (although that is
617  * irrelevant to its purpose) and will not throw. In addition, if a
618  * callback connected to an emitter throws, the exception is consumed
619  * and a g_critical warning issued. This function blocks until the
620  * last WinBase object associated with this Application object is
621  * destroyed or removed.
622  * @param argc The argc from main() or 0.
623  * @param argv The argv from main() or 0.
624  * @return The exit status from g_application_run().
625  *
626  * Since 2.0.0-rc2
627  */
628  int run(int argc, char** argv) {
629  return g_application_run((GApplication*)app.get(), argc, argv);
630  }
631 
632 /**
633  * Get the underlying GApplication object (note, not the
634  * GtkApplication object, although the GApplication object can be cast
635  * to GtkApplication), so allowing any of gio's g_application_*()
636  * functions to be applied to it. In normal usage it will not be
637  * necessary to call this method. This method is thread safe and will
638  * not throw.
639  * @return The underlying GApplication object.
640  *
641  * Since 2.0.0-rc2
642  */
643  GApplication* get_g_app() const {return (GApplication*)app.get();}
644 
645 /**
646  * Get the list of Cgu::WinBase objects associated with the
647  * application. The Cgu::Application class, and thus this method,
648  * does not employ mutexes to make it thread safe, as there should
649  * never be a reason to call Cgu::Application methods in other than
650  * the main GUI thread.
651  * @return A list of the top level Cgu::WinBase objects associated
652  * with the application, which will appear in the order in which they
653  * were added. If you need to access these, you will probably want to
654  * do a dynamic_cast or static_cast to the child type.
655  * @exception std::bad_alloc This method might throw std::bad_alloc if
656  * memory is exhausted and the system throws in that case.
657  *
658  * Since 2.0.0-rc2
659  */
660  std::list<Cgu::WinBase*> get_windows() const {return win_list;}
661 
662 /**
663  * Gets the current count of Cgu::WinBase objects associated with this
664  * Cgu::Application object. When it reaches 0, the application will
665  * normally end (but this can be prevented by calling
666  * g_application_hold()/g_application_release() on the GApplication
667  * object returned by get_g_app()). This method can be used in the
668  * callback of one of this class's emitters to determine whether this
669  * is the first instance of a program to be started (assuming the
670  * first instance calls add() to bring up a window), because in that
671  * case it will return 0 until add() is called. Calling
672  * get_windows().size() will give the same result, but using this
673  * method is more efficient as it will avoid a copy of the list of
674  * windows. This method will not throw assuming that calling
675  * std::list::size() does not throw (as it would not on any sane
676  * implementation). The Cgu::Application class, and thus this method,
677  * does not employ mutexes to make it thread safe, as there should
678  * never be a reason to call Cgu::Application methods in other than
679  * the main GUI thread.
680  * @return The number of Cgu::WinBase objects currently associated
681  * with this Cgu::Application object.
682  *
683  * Since 2.0.0-rc2
684  */
685  size_type get_win_count() const {return win_list.size();}
686 
687 /**
688  * This constructor will, via gtk_application_new(), cause
689  * g_type_init() to be called. If any GTK functions are to be called
690  * before an Application object is constructed, g_type_init() (or
691  * gtk_init()) must be called explicitly.
692  * @param prog_name An identifier name. This can comprise any valid
693  * ASCII characters "[A-Z][a-z][0-9]_-", although it is usually best
694  * to pass the program name. Unlike with gtk_application_new(), it
695  * does not need to comprise a full dbus bus name: this method will
696  * construct its own valid dbus bus name from prog_name by prepending
697  * "org.cgu.applications." to prog_name.
698  * @param flags The GApplicationFlags to be passed to the
699  * Cgu::Application object. This class does not contain its own
700  * sub-class of GApplication to customize this, but adopts the
701  * behaviour of GtkApplication. That behaviour is explained in the
702  * introductory remarks.
703  * @exception Cgu::ApplicationNameError This exception will be thrown
704  * if the prog_name parameter does not meet the requirements referred
705  * to above.
706  * @exception std::bad_alloc This method might throw std::bad_alloc if
707  * memory is exhausted and the system throws in that case.
708  *
709  * Since 2.0.0-rc2
710  */
711  Application(const char* prog_name, GApplicationFlags flags);
712 
713 /**
714  * From version 2.0.0-rc3, as a safety feature the destructor removes
715  * any remaining WinBase objects associated with this Application
716  * object (this would only be relevant if the user constructs the
717  * Application object on free store, and then deletes it while the
718  * run() method is still blocking for the purpose of constructing a
719  * different Application object, but does not call the remove() method
720  * on all associated WinBase objects before doing so: constructing an
721  * Application object on free store in this way would be highly
722  * unusual however).
723  *
724  * Since 2.0.0-rc3
725  */
726  ~Application() {while (!win_list.empty()) remove(win_list.front());}
727 
728 /* Only has effect if --with-glib-memory-slices-compat or
729  * --with-glib-memory-slices-no-compat option picked */
731 };
732 
733 #endif // GTK_CHECK_VERSION
734 #endif // CGU_USE_GTK
735 
736 } // namespace Cgu
737 
738 #endif // CGU_APPLICATION_H
Cgu::Application::command_line
Cgu::SafeEmitterArg< Cgu::Application *, GApplicationCommandLine *, gint & > command_line
Definition: application.h:497
Cgu::WinBase
This is a class for managing the lifetime of top level widgets.
Definition: window.h:230
Cgu
Definition: application.h:44
Cgu::Application::Application
Application(const Application &)=delete
Cgu::Application::remove
bool remove(Cgu::WinBase *win)
Cgu::ApplicationNameError
This class is thrown when the program id name passed to the constructor of Cgu::Application is invali...
Definition: application.h:54
Cgu::Application::~Application
~Application()
Definition: application.h:726
Cgu::GobjHandle< GtkApplication >
Cgu::Application::open
Cgu::SafeEmitterArg< Cgu::Application *, std::pair< GFile **, gint >, gchar * > open
Definition: application.h:524
Cgu::Application
This is a class for constructing and managing GtkApplication objects.
Definition: application.h:410
Cgu::ApplicationNameError::what
virtual const char * what() const
Definition: application.h:55
Cgu::Application::add
void add(Cgu::WinBase *win)
Cgu::Application::startup
Cgu::SafeEmitterArg< Cgu::Application * > startup
Definition: application.h:463
gobj_handle.h
Cgu::Application::get_windows
std::list< Cgu::WinBase * > get_windows() const
Definition: application.h:660
Cgu::SafeEmitterArg< Cgu::Application * >
Cgu::Application::Application
Application(const char *prog_name, GApplicationFlags flags)
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::Application::get_win_count
size_type get_win_count() const
Definition: application.h:685
Cgu::Application::size_type
std::list< WinBase * >::size_type size_type
Definition: application.h:418
Cgu::Application::get_g_app
GApplication * get_g_app() const
Definition: application.h:643
window.h
Cgu::Application::run
int run(int argc, char **argv)
Definition: application.h:628
emitter.h
This file provides a thread-safe signal/slot mechanism, with automatic disconnection.
cgu_config.h
Cgu::Application::operator=
Application & operator=(const Application &)=delete
Cgu::Application::activate
Cgu::SafeEmitterArg< Cgu::Application * > activate
Definition: application.h:443
Cgu::GobjHandle::get
T * get() const noexcept
Definition: gobj_handle.h:292