Application package:gi-gtk4

A high-level API for writing applications. GtkApplication supports many aspects of writing a GTK application in a convenient fashion, without enforcing a one-size-fits-all model. Currently, it handles GTK initialization, application uniqueness, session management, provides some basic scriptability and desktop shell integration by exporting actions and menus and manages a list of toplevel windows whose life-cycle is automatically tied to the life-cycle of your application. While GtkApplication works fine with plain Windows, it is recommended to use it together with ApplicationWindow.

Automatic resources

GtkApplication will automatically load menus from the GtkBuilder resource located at "gtk/menus.ui", relative to the application's resource base path (see applicationSetResourceBasePath). The menu with the ID "menubar" is taken as the application's menubar. Additional menus (most interesting submenus) can be named and accessed via applicationGetMenuById which allows for dynamic population of a part of the menu structure. Note that automatic resource loading uses the resource base path that is set at construction time and will not work if the resource base path is changed at a later time. It is also possible to provide the menubar manually using applicationSetMenubar. GtkApplication will also automatically setup an icon search path for the default icon theme by appending "icons" to the resource base path. This allows your application to easily store its icons as resources. See iconThemeAddResourcePath for more information. If there is a resource located at gtk/help-overlay.ui which defines a ShortcutsWindow with ID help_overlay then GtkApplication associates an instance of this shortcuts window with each ApplicationWindow and sets up the keyboard accelerator <kbd>Control</kbd>+<kbd>?</kbd> to open it. To create a menu item that displays the shortcuts window, associate the item with the action win.show-help-overlay. GtkApplication will also automatically set the application id as the default window icon. Use windowSetDefaultIconName or Window:iconName to override that behavior.

A simple application

A simple example is available in the GTK source code repository GtkApplication optionally registers with a session manager of the users session (if you set the Application:registerSession property) and offers various functionality related to the session life-cycle. An application can block various ways to end the session with the applicationInhibit function. Typical use cases for this kind of inhibiting are long-running, uninterruptible operations, such as burning a CD or performing a disk backup. The session manager may not honor the inhibitor, but it can be expected to inform the user about the negative consequences of ending the session while inhibitors are present.

See Also

Memory-managed wrapper type.
Types of user actions that may be blocked by GtkApplication. See applicationInhibit.
Inhibit the session being marked as idle (and possibly locked)
Inhibit ending the user session by logging out or by shutting down the computer
Inhibit suspending the session or computer
Inhibit user switching
Emitted when the session manager is about to end the session. This signal is only emitted if Application:registerSession is true. Applications can connect to this signal and call applicationInhibit with [flagsgtk.ApplicationInhibitFlags.logout] to delay the end of the session until state has been saved.
Emitted when a window is added to an application. See applicationAddWindow.
Emitted when a window is removed from an application. This can happen as a side-effect of the window being destroyed or explicitly through applicationRemoveWindow.
A GtkWindow subclass that integrates with GtkApplication. Notably, GtkApplicationWindow can handle an application menubar. This class implements the ActionGroup and ActionMap interfaces, to let you add window-specific actions that will be exported by the associated Application, together with its application-wide actions. Window-specific actions are prefixed with the “win.” prefix and application-wide actions are prefixed with the “app.” prefix. Actions must be addressed with the prefixed name when referring to them from a menu model. Note that widgets that are placed inside a GtkApplicationWindow can also activate these actions, if they implement the Actionable interface. The settings Settings:gtkShellShowsAppMenu and Settings:gtkShellShowsMenubar tell GTK whether the desktop environment is showing the application menu and menubar models outside the application as part of the desktop shell. For instance, on OS X, both menus will be displayed remotely; on Windows neither will be. If the desktop environment does not display the menubar, it can be shown in the GtkApplicationWindow by setting the ApplicationWindow:showMenubar property to true. If the desktop environment does not display the application menu, then it will automatically be included in the menubar or in the window’s client-side decorations. See PopoverMenu for information about the XML language used by GtkBuilder for menu models. See also: applicationSetMenubar.

A GtkApplicationWindow with a menubar

The code sample below shows how to set up a GtkApplicationWindow with a menu bar defined on the Application:

c code

GtkApplication *app = gtk_application_new ("org.gtk.test", 0);

GtkBuilder *builder = gtk_builder_new_from_string (
"<interface>"
"  <menu id='menubar'>"
"    <submenu>"
"      <attribute name='label' translatable='yes'>_Edit</attribute>"
"      <item>"
"        <attribute name='label' translatable='yes'>_Copy</attribute>"
"        <attribute name='action'>win.copy</attribute>"
"      </item>"
"      <item>"
"        <attribute name='label' translatable='yes'>_Paste</attribute>"
"        <attribute name='action'>win.paste</attribute>"
"      </item>"
"    </submenu>"
"  </menu>"
"</interface>",
-1);

GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"));
gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
g_object_unref (builder);

// ...

GtkWidget *window = gtk_application_window_new (app);
Memory-managed wrapper type.
Adds a window to the application. This call can only happen after the application has started; typically, you should add new application windows in response to the emission of the GIO.Application::activate signal. This call is equivalent to setting the Window:application property of the window to application. Normally, the connection between the application and the window will remain until the window is destroyed, but you can explicitly remove it with applicationRemoveWindow. GTK will keep the application running as long as it has any windows.
Gets the accelerators that are currently associated with the given action.
Returns the list of actions (possibly empty) that the accelerator maps to. Each item in the list is a detailed action name in the usual form. This might be useful to discover if an accel already exists in order to prevent installation of a conflicting accelerator (from an accelerator editor or a plugin system, for example). Note that having more than one action per accelerator may not be a bad thing and might make sense in cases where the actions never appear in the same context. In case there are no actions for a given accelerator, an empty array is returned. NULL is never returned. It is a programmer error to pass an invalid accelerator string. If you are unsure, check it with acceleratorParse first.
Gets the “active” window for the application. The active window is the one that was most recently focused (within the application). This window may not have the focus at the moment if another application has it — this is just the most recently-focused window within this application.
Gets a menu from automatically loaded resources. See the section on Automatic resources for more information.
Returns the menu model for the menu bar of the application.
Returns the window with the given ID. The ID of a GtkApplicationWindow can be retrieved with applicationWindowGetId.
Gets a list of the window associated with the application. The list is sorted by most recently focused window, such that the first element is the currently focused window. (Useful for choosing a parent for a transient window.) The list that is returned should not be modified in any way. It will only remain valid until the next focus change or window creation or deletion.
Informs the session manager that certain types of actions should be inhibited. This is not guaranteed to work on all platforms and for all types of actions. Applications should invoke this method when they begin an operation that should not be interrupted, such as creating a CD or DVD. The types of actions that may be blocked are specified by the flags parameter. When the application completes the operation it should call applicationUninhibit to remove the inhibitor. Note that an application can have multiple inhibitors, and all of them must be individually removed. Inhibitors are also cleared when the application exits. Applications should not expect that they will always be able to block the action. In most cases, users will be given the option to force the action to take place. The reason message should be short and to the point. If a window is given, the session manager may point the user to this window to find out more about why the action is inhibited. The cookie that is returned by this function should be used as an argument to applicationUninhibit in order to remove the request.
Lists the detailed action names which have associated accelerators. See applicationSetAccelsForAction.
Creates a new application instance. When using GtkApplication, it is not necessary to call init manually. It is called as soon as the application gets registered as the primary instance. Concretely, init is called in the default handler for the GApplication::startup signal. Therefore, GtkApplication subclasses should always chain up in their GIO.Application.startup() handler before using any GTK API. Note that commandline arguments are not passed to init. If application_id is not NULL, then it must be valid. See applicationIdIsValid. If no application ID is given then some features (most notably application uniqueness) will be disabled.