1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.Application;
26 
27 private import gio.ActionGroupIF;
28 private import gio.ActionGroupT;
29 private import gio.ActionMapIF;
30 private import gio.ActionMapT;
31 private import gio.Application : DGioApplication = Application;
32 private import gio.Menu;
33 private import gio.MenuModel;
34 private import glib.ConstructionException;
35 private import glib.ListG;
36 private import glib.Str;
37 private import glib.c.functions;
38 private import gobject.ObjectG;
39 private import gobject.Signals;
40 private import gtk.Window;
41 private import gtk.c.functions;
42 public  import gtk.c.types;
43 private import std.algorithm;
44 
45 
46 /**
47  * `GtkApplication` is a high-level API for writing applications.
48  * 
49  * It supports many aspects of writing a GTK application in a convenient
50  * fashion, without enforcing a one-size-fits-all model.
51  * 
52  * Currently, `GtkApplication` handles GTK initialization, application
53  * uniqueness, session management, provides some basic scriptability and
54  * desktop shell integration by exporting actions and menus and manages a
55  * list of toplevel windows whose life-cycle is automatically tied to the
56  * life-cycle of your application.
57  * 
58  * While `GtkApplication` works fine with plain [class@Gtk.Window]s, it is
59  * recommended to use it together with [class@Gtk.ApplicationWindow].
60  * 
61  * ## Automatic resources
62  * 
63  * `GtkApplication` will automatically load menus from the `GtkBuilder`
64  * resource located at "gtk/menus.ui", relative to the application's
65  * resource base path (see `g_application_set_resource_base_path()`).
66  * The menu with the ID "menubar" is taken as the application's
67  * menubar. Additional menus (most interesting submenus) can be named
68  * and accessed via [method@Gtk.Application.get_menu_by_id] which allows for
69  * dynamic population of a part of the menu structure.
70  * 
71  * It is also possible to provide the menubar manually using
72  * [method@Gtk.Application.set_menubar].
73  * 
74  * `GtkApplication` will also automatically setup an icon search path for
75  * the default icon theme by appending "icons" to the resource base
76  * path. This allows your application to easily store its icons as
77  * resources. See [method@Gtk.IconTheme.add_resource_path] for more
78  * information.
79  * 
80  * If there is a resource located at "gtk/help-overlay.ui" which
81  * defines a [class@Gtk.ShortcutsWindow] with ID "help_overlay" then
82  * `GtkApplication` associates an instance of this shortcuts window with
83  * each [class@Gtk.ApplicationWindow] and sets up the keyboard accelerator
84  * <kbd>Control</kbd>+<kbd>?</kbd> to open it. To create a menu item that
85  * displays the shortcuts window, associate the item with the action
86  * `win.show-help-overlay`.
87  * 
88  * ## A simple application
89  * 
90  * [A simple example](https://gitlab.gnome.org/GNOME/gtk/tree/master/examples/bp/bloatpad.c)
91  * is available in the GTK source code repository
92  * 
93  * `GtkApplication` optionally registers with a session manager of the
94  * users session (if you set the [property@Gtk.Application:register-session]
95  * property) and offers various functionality related to the session
96  * life-cycle.
97  * 
98  * An application can block various ways to end the session with
99  * the [method@Gtk.Application.inhibit] function. Typical use cases for
100  * this kind of inhibiting are long-running, uninterruptible operations,
101  * such as burning a CD or performing a disk backup. The session
102  * manager may not honor the inhibitor, but it can be expected to
103  * inform the user about the negative consequences of ending the
104  * session while inhibitors are present.
105  * 
106  * ## See Also
107  * 
108  * [HowDoI: Using GtkApplication](https://wiki.gnome.org/HowDoI/GtkApplication),
109  * [Getting Started with GTK: Basics](getting_started.html#basics)
110  */
111 public class Application : DGioApplication
112 {
113 	/** the main Gtk struct */
114 	protected GtkApplication* gtkApplication;
115 
116 	/** Get the main Gtk struct */
117 	public GtkApplication* getGtkApplicationStruct(bool transferOwnership = false)
118 	{
119 		if (transferOwnership)
120 			ownedRef = false;
121 		return gtkApplication;
122 	}
123 
124 	/** the main Gtk struct as a void* */
125 	protected override void* getStruct()
126 	{
127 		return cast(void*)gtkApplication;
128 	}
129 
130 	/**
131 	 * Sets our main struct and passes it to the parent class.
132 	 */
133 	public this (GtkApplication* gtkApplication, bool ownedRef = false)
134 	{
135 		this.gtkApplication = gtkApplication;
136 		super(cast(GApplication*)gtkApplication, ownedRef);
137 	}
138 
139 
140 	/** */
141 	public static GType getType()
142 	{
143 		return gtk_application_get_type();
144 	}
145 
146 	/**
147 	 * Creates a new `GtkApplication` instance.
148 	 *
149 	 * When using `GtkApplication`, it is not necessary to call [func@Gtk.init]
150 	 * manually. It is called as soon as the application gets registered as
151 	 * the primary instance.
152 	 *
153 	 * Concretely, [func@Gtk.init] is called in the default handler for the
154 	 * `GApplication::startup` signal. Therefore, `GtkApplication` subclasses should
155 	 * always chain up in their `GApplication::startup` handler before using any GTK
156 	 * API.
157 	 *
158 	 * Note that commandline arguments are not passed to [func@Gtk.init].
159 	 *
160 	 * If `application_id` is not %NULL, then it must be valid. See
161 	 * `g_application_id_is_valid()`.
162 	 *
163 	 * If no application ID is given then some features (most notably application
164 	 * uniqueness) will be disabled.
165 	 *
166 	 * Params:
167 	 *     applicationId = The application ID
168 	 *     flags = the application flags
169 	 *
170 	 * Returns: a new `GtkApplication` instance
171 	 *
172 	 * Throws: ConstructionException GTK+ fails to create the object.
173 	 */
174 	public this(string applicationId, GApplicationFlags flags)
175 	{
176 		auto __p = gtk_application_new(Str.toStringz(applicationId), flags);
177 
178 		if(__p is null)
179 		{
180 			throw new ConstructionException("null returned by new");
181 		}
182 
183 		this(cast(GtkApplication*) __p, true);
184 	}
185 
186 	/**
187 	 * Adds a window to `application`.
188 	 *
189 	 * This call can only happen after the `application` has started;
190 	 * typically, you should add new application windows in response
191 	 * to the emission of the `GApplication::activate` signal.
192 	 *
193 	 * This call is equivalent to setting the [property@Gtk.Window:application]
194 	 * property of `window` to `application`.
195 	 *
196 	 * Normally, the connection between the application and the window
197 	 * will remain until the window is destroyed, but you can explicitly
198 	 * remove it with [method@Gtk.Application.remove_window].
199 	 *
200 	 * GTK will keep the `application` running as long as it has
201 	 * any windows.
202 	 *
203 	 * Params:
204 	 *     window = a `GtkWindow`
205 	 */
206 	public void addWindow(Window window)
207 	{
208 		gtk_application_add_window(gtkApplication, (window is null) ? null : window.getWindowStruct());
209 	}
210 
211 	/**
212 	 * Gets the accelerators that are currently associated with
213 	 * the given action.
214 	 *
215 	 * Params:
216 	 *     detailedActionName = a detailed action name, specifying an action
217 	 *         and target to obtain accelerators for
218 	 *
219 	 * Returns: accelerators for `detailed_action_name`
220 	 */
221 	public string[] getAccelsForAction(string detailedActionName)
222 	{
223 		auto retStr = gtk_application_get_accels_for_action(gtkApplication, Str.toStringz(detailedActionName));
224 
225 		scope(exit) Str.freeStringArray(retStr);
226 		return Str.toStringArray(retStr);
227 	}
228 
229 	/**
230 	 * Returns the list of actions (possibly empty) that `accel` maps to.
231 	 *
232 	 * Each item in the list is a detailed action name in the usual form.
233 	 *
234 	 * This might be useful to discover if an accel already exists in
235 	 * order to prevent installation of a conflicting accelerator (from
236 	 * an accelerator editor or a plugin system, for example). Note that
237 	 * having more than one action per accelerator may not be a bad thing
238 	 * and might make sense in cases where the actions never appear in the
239 	 * same context.
240 	 *
241 	 * In case there are no actions for a given accelerator, an empty array
242 	 * is returned. `NULL` is never returned.
243 	 *
244 	 * It is a programmer error to pass an invalid accelerator string.
245 	 *
246 	 * If you are unsure, check it with [func@Gtk.accelerator_parse] first.
247 	 *
248 	 * Params:
249 	 *     accel = an accelerator that can be parsed by [func@Gtk.accelerator_parse]
250 	 *
251 	 * Returns: a %NULL-terminated array of actions for `accel`
252 	 */
253 	public string[] getActionsForAccel(string accel)
254 	{
255 		auto retStr = gtk_application_get_actions_for_accel(gtkApplication, Str.toStringz(accel));
256 
257 		scope(exit) Str.freeStringArray(retStr);
258 		return Str.toStringArray(retStr);
259 	}
260 
261 	/**
262 	 * Gets the “active” window for the application.
263 	 *
264 	 * The active window is the one that was most recently focused (within
265 	 * the application).  This window may not have the focus at the moment
266 	 * if another application has it — this is just the most
267 	 * recently-focused window within this application.
268 	 *
269 	 * Returns: the active window
270 	 */
271 	public Window getActiveWindow()
272 	{
273 		auto __p = gtk_application_get_active_window(gtkApplication);
274 
275 		if(__p is null)
276 		{
277 			return null;
278 		}
279 
280 		return ObjectG.getDObject!(Window)(cast(GtkWindow*) __p);
281 	}
282 
283 	/**
284 	 * Gets a menu from automatically loaded resources.
285 	 *
286 	 * See [the section on Automatic resources](class.Application.html#automatic-resources)
287 	 * for more information.
288 	 *
289 	 * Params:
290 	 *     id = the id of the menu to look up
291 	 *
292 	 * Returns: Gets the menu with the
293 	 *     given id from the automatically loaded resources
294 	 */
295 	public Menu getMenuById(string id)
296 	{
297 		auto __p = gtk_application_get_menu_by_id(gtkApplication, Str.toStringz(id));
298 
299 		if(__p is null)
300 		{
301 			return null;
302 		}
303 
304 		return ObjectG.getDObject!(Menu)(cast(GMenu*) __p);
305 	}
306 
307 	/**
308 	 * Returns the menu model that has been set with
309 	 * [method@Gtk.Application.set_menubar].
310 	 *
311 	 * Returns: the menubar for windows of `application`
312 	 */
313 	public MenuModel getMenubar()
314 	{
315 		auto __p = gtk_application_get_menubar(gtkApplication);
316 
317 		if(__p is null)
318 		{
319 			return null;
320 		}
321 
322 		return ObjectG.getDObject!(MenuModel)(cast(GMenuModel*) __p);
323 	}
324 
325 	/**
326 	 * Returns the [class@Gtk.ApplicationWindow] with the given ID.
327 	 *
328 	 * The ID of a `GtkApplicationWindow` can be retrieved with
329 	 * [method@Gtk.ApplicationWindow.get_id].
330 	 *
331 	 * Params:
332 	 *     id = an identifier number
333 	 *
334 	 * Returns: the window for the given `id`
335 	 */
336 	public Window getWindowById(uint id)
337 	{
338 		auto __p = gtk_application_get_window_by_id(gtkApplication, id);
339 
340 		if(__p is null)
341 		{
342 			return null;
343 		}
344 
345 		return ObjectG.getDObject!(Window)(cast(GtkWindow*) __p);
346 	}
347 
348 	/**
349 	 * Gets a list of the [class@Gtk.Window] instances associated with `application`.
350 	 *
351 	 * The list is sorted by most recently focused window, such that the first
352 	 * element is the currently focused window. (Useful for choosing a parent
353 	 * for a transient window.)
354 	 *
355 	 * The list that is returned should not be modified in any way. It will
356 	 * only remain valid until the next focus change or window creation or
357 	 * deletion.
358 	 *
359 	 * Returns: a `GList` of `GtkWindow`
360 	 *     instances
361 	 */
362 	public ListG getWindows()
363 	{
364 		auto __p = gtk_application_get_windows(gtkApplication);
365 
366 		if(__p is null)
367 		{
368 			return null;
369 		}
370 
371 		return new ListG(cast(GList*) __p);
372 	}
373 
374 	/**
375 	 * Inform the session manager that certain types of actions should be
376 	 * inhibited.
377 	 *
378 	 * This is not guaranteed to work on all platforms and for all types of
379 	 * actions.
380 	 *
381 	 * Applications should invoke this method when they begin an operation
382 	 * that should not be interrupted, such as creating a CD or DVD. The
383 	 * types of actions that may be blocked are specified by the `flags`
384 	 * parameter. When the application completes the operation it should
385 	 * call [method@Gtk.Application.uninhibit] to remove the inhibitor. Note
386 	 * that an application can have multiple inhibitors, and all of them must
387 	 * be individually removed. Inhibitors are also cleared when the
388 	 * application exits.
389 	 *
390 	 * Applications should not expect that they will always be able to block
391 	 * the action. In most cases, users will be given the option to force
392 	 * the action to take place.
393 	 *
394 	 * The `reason` message should be short and to the point.
395 	 *
396 	 * If `window` is given, the session manager may point the user to
397 	 * this window to find out more about why the action is inhibited.
398 	 *
399 	 * Params:
400 	 *     window = a `GtkWindow`
401 	 *     flags = what types of actions should be inhibited
402 	 *     reason = a short, human-readable string that explains
403 	 *         why these operations are inhibited
404 	 *
405 	 * Returns: A non-zero cookie that is used to uniquely identify this
406 	 *     request. It should be used as an argument to [method@Gtk.Application.uninhibit]
407 	 *     in order to remove the request. If the platform does not support
408 	 *     inhibiting or the request failed for some reason, 0 is returned.
409 	 */
410 	public uint inhibit(Window window, GtkApplicationInhibitFlags flags, string reason)
411 	{
412 		return gtk_application_inhibit(gtkApplication, (window is null) ? null : window.getWindowStruct(), flags, Str.toStringz(reason));
413 	}
414 
415 	/**
416 	 * Lists the detailed action names which have associated accelerators.
417 	 *
418 	 * See [method@Gtk.Application.set_accels_for_action].
419 	 *
420 	 * Returns: the detailed action names
421 	 */
422 	public string[] listActionDescriptions()
423 	{
424 		auto retStr = gtk_application_list_action_descriptions(gtkApplication);
425 
426 		scope(exit) Str.freeStringArray(retStr);
427 		return Str.toStringArray(retStr);
428 	}
429 
430 	/**
431 	 * Remove a window from `application`.
432 	 *
433 	 * If `window` belongs to `application` then this call is equivalent to
434 	 * setting the [property@Gtk.Window:application] property of `window` to
435 	 * `NULL`.
436 	 *
437 	 * The application may stop running as a result of a call to this
438 	 * function, if `window` was the last window of the `application`.
439 	 *
440 	 * Params:
441 	 *     window = a `GtkWindow`
442 	 */
443 	public void removeWindow(Window window)
444 	{
445 		gtk_application_remove_window(gtkApplication, (window is null) ? null : window.getWindowStruct());
446 	}
447 
448 	/**
449 	 * Sets zero or more keyboard accelerators that will trigger the
450 	 * given action.
451 	 *
452 	 * The first item in `accels` will be the primary accelerator, which may be
453 	 * displayed in the UI.
454 	 *
455 	 * To remove all accelerators for an action, use an empty, zero-terminated
456 	 * array for `accels`.
457 	 *
458 	 * For the `detailed_action_name`, see `g_action_parse_detailed_name()` and
459 	 * `g_action_print_detailed_name()`.
460 	 *
461 	 * Params:
462 	 *     detailedActionName = a detailed action name, specifying an action
463 	 *         and target to associate accelerators with
464 	 *     accels = a list of accelerators in the format
465 	 *         understood by [func@Gtk.accelerator_parse]
466 	 */
467 	public void setAccelsForAction(string detailedActionName, string[] accels)
468 	{
469 		gtk_application_set_accels_for_action(gtkApplication, Str.toStringz(detailedActionName), Str.toStringzArray(accels));
470 	}
471 
472 	/**
473 	 * Sets or unsets the menubar for windows of `application`.
474 	 *
475 	 * This is a menubar in the traditional sense.
476 	 *
477 	 * This can only be done in the primary instance of the application,
478 	 * after it has been registered. `GApplication::startup` is a good place
479 	 * to call this.
480 	 *
481 	 * Depending on the desktop environment, this may appear at the top of
482 	 * each window, or at the top of the screen.  In some environments, if
483 	 * both the application menu and the menubar are set, the application
484 	 * menu will be presented as if it were the first item of the menubar.
485 	 * Other environments treat the two as completely separate — for example,
486 	 * the application menu may be rendered by the desktop shell while the
487 	 * menubar (if set) remains in each individual window.
488 	 *
489 	 * Use the base `GActionMap` interface to add actions, to respond to the
490 	 * user selecting these menu items.
491 	 *
492 	 * Params:
493 	 *     menubar = a `GMenuModel`
494 	 */
495 	public void setMenubar(MenuModel menubar)
496 	{
497 		gtk_application_set_menubar(gtkApplication, (menubar is null) ? null : menubar.getMenuModelStruct());
498 	}
499 
500 	/**
501 	 * Removes an inhibitor that has been previously established.
502 	 *
503 	 * See [method@Gtk.Application.inhibit].
504 	 *
505 	 * Inhibitors are also cleared when the application exits.
506 	 *
507 	 * Params:
508 	 *     cookie = a cookie that was returned by [method@Gtk.Application.inhibit]
509 	 */
510 	public void uninhibit(uint cookie)
511 	{
512 		gtk_application_uninhibit(gtkApplication, cookie);
513 	}
514 
515 	/**
516 	 * Emitted when the session manager is about to end the session.
517 	 *
518 	 * This signal is only emitted if [property@Gtk.Application:register-session]
519 	 * is `TRUE`. Applications can connect to this signal and call
520 	 * [method@Gtk.Application.inhibit] with `GTK_APPLICATION_INHIBIT_LOGOUT`
521 	 * to delay the end of the session until state has been saved.
522 	 */
523 	gulong addOnQueryEnd(void delegate(Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
524 	{
525 		return Signals.connect(this, "query-end", dlg, connectFlags ^ ConnectFlags.SWAPPED);
526 	}
527 
528 	/**
529 	 * Emitted when a [class@Gtk.Window] is added to `application` through
530 	 * [method@Gtk.Application.add_window].
531 	 *
532 	 * Params:
533 	 *     window = the newly-added [class@Gtk.Window]
534 	 */
535 	gulong addOnWindowAdded(void delegate(Window, Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
536 	{
537 		return Signals.connect(this, "window-added", dlg, connectFlags ^ ConnectFlags.SWAPPED);
538 	}
539 
540 	/**
541 	 * Emitted when a [class@Gtk.Window] is removed from `application`.
542 	 *
543 	 * This can happen as a side-effect of the window being destroyed
544 	 * or explicitly through [method@Gtk.Application.remove_window].
545 	 *
546 	 * Params:
547 	 *     window = the [class@Gtk.Window] that is being removed
548 	 */
549 	gulong addOnWindowRemoved(void delegate(Window, Application) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
550 	{
551 		return Signals.connect(this, "window-removed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
552 	}
553 }