1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Colin Walters <walters@verbum.org>
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include "gtkapplication.h"
38 #include "gtkprivate.h"
43 #ifdef GDK_WINDOWING_X11
44 #include <gdk/x11/gdkx.h>
48 * SECTION:gtkapplication
49 * @title: GtkApplication
50 * @short_description: Application class
52 * #GtkApplication is a class that handles many important aspects
53 * of a GTK+ application in a convenient fashion, without enforcing
54 * a one-size-fits-all application model.
56 * Currently, GtkApplication handles application uniqueness, provides
57 * some basic scriptability by exporting 'actions', implements some
58 * standard actions itself (such as 'Quit') and provides a main window
59 * whose life-cycle is automatically tied to the life-cycle of your
62 * <example id="gtkapplication"><title>A simple application</title>
64 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
65 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
83 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
85 struct _GtkApplicationPrivate
87 GtkActionGroup *main_actions;
89 GtkWindow *default_window;
93 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
96 gtk_application_default_quit (GApplication *application,
104 gtk_application_default_run (GApplication *application)
110 gtk_application_default_prepare_activation (GApplication *application,
112 GVariant *platform_data)
118 g_variant_iter_init (&iter, platform_data);
119 while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
121 if (strcmp (key, "startup-notification-id") == 0 &&
122 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
123 gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
124 g_variant_unref (value);
127 g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
131 gtk_application_default_activated (GApplication *application,
134 GtkApplication *app = GTK_APPLICATION (application);
136 /* TODO: should we raise the last focused window instead ? */
137 if (app->priv->default_window != NULL)
138 gtk_window_present (app->priv->default_window);
142 gtk_application_default_action (GApplication *application,
143 const gchar *action_name,
146 GtkApplication *app = GTK_APPLICATION (application);
149 action = gtk_action_group_get_action (app->priv->main_actions, action_name);
152 /* TODO set timestamp */
153 gtk_action_activate (action);
158 gtk_application_format_activation_data (void)
160 const gchar *startup_id = NULL;
161 GdkDisplay *display = gdk_display_get_default ();
162 GVariantBuilder builder;
164 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
166 /* try and get the startup notification id from GDK, the environment
167 * or, if everything else failed, fake one.
169 #ifdef GDK_WINDOWING_X11
170 startup_id = gdk_x11_display_get_startup_notification_id (display);
171 #endif /* GDK_WINDOWING_X11 */
174 g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
175 g_variant_new ("s", startup_id));
176 return g_variant_builder_end (&builder);
180 * gtk_application_new:
181 * @argc: (allow-none) (inout): System argument count
182 * @argv: (allow-none) (inout): System argument vector
183 * @appid: System-dependent application identifier
185 * Create a new #GtkApplication, or if one has already been initialized
186 * in this process, return the existing instance. This function will as
187 * a side effect initialize the display system; see gtk_init().
189 * For the behavior if this application is running in another process,
190 * see g_application_new().
192 * Returns: (transfer full): A newly-referenced #GtkApplication
197 gtk_application_new (gint *argc,
203 gchar **argv_for_app;
204 GVariant *platform_data;
206 gtk_init (argc, argv);
209 argc_for_app = *argc;
213 argv_for_app = *argv;
217 app = g_object_new (GTK_TYPE_APPLICATION, "application-id", appid, NULL);
219 platform_data = gtk_application_format_activation_data ();
220 g_application_register_with_data (G_APPLICATION (app), argc_for_app, argv_for_app,
222 g_variant_unref (platform_data);
228 on_action_sensitive (GtkAction *action,
232 g_application_set_action_enabled (G_APPLICATION (app),
233 gtk_action_get_name (action),
234 gtk_action_get_sensitive (action));
238 * gtk_application_set_action_group:
239 * @app: A #GtkApplication
240 * @group: A #GtkActionGroup
242 * Set @group as this application's global action group.
243 * This will ensure the operating system interface uses
244 * these actions as follows:
247 * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
248 * <listitem>In GNOME 3, this function populates the application menu.</listitem>
249 * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
250 * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
251 * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
254 * It is an error to call this function more than once.
259 gtk_application_set_action_group (GtkApplication *app,
260 GtkActionGroup *group)
262 GList *actions, *iter;
264 g_return_if_fail (GTK_IS_APPLICATION (app));
265 g_return_if_fail (app->priv->main_actions == NULL);
267 app->priv->main_actions = g_object_ref (group);
268 actions = gtk_action_group_list_actions (group);
269 for (iter = actions; iter; iter = iter->next)
271 GtkAction *action = iter->data;
272 g_application_add_action (G_APPLICATION (app),
273 gtk_action_get_name (action),
274 gtk_action_get_tooltip (action));
275 g_signal_connect (action, "notify::sensitive",
276 G_CALLBACK (on_action_sensitive), app);
278 g_list_free (actions);
282 gtk_application_on_window_destroy (GtkWidget *window,
285 GtkApplication *app = GTK_APPLICATION (user_data);
287 app->priv->windows = g_slist_remove (app->priv->windows, window);
289 if (app->priv->windows == NULL)
290 gtk_application_quit (app);
295 static gchar *default_title;
298 * gtk_application_add_window:
299 * @app: a #GtkApplication
300 * @window: a toplevel window to add to @app
302 * Adds a window to the #GtkApplication.
304 * If the user closes all of the windows added to @app, the default
305 * behaviour is to call gtk_application_quit().
307 * If your application uses only a single toplevel window, you can
308 * use gtk_application_get_window().
313 gtk_application_add_window (GtkApplication *app,
316 app->priv->windows = g_slist_prepend (app->priv->windows, window);
318 if (gtk_window_get_title (window) == NULL && default_title != NULL)
319 gtk_window_set_title (window, default_title);
321 g_signal_connect (window, "destroy",
322 G_CALLBACK (gtk_application_on_window_destroy), app);
326 * gtk_application_get_window:
327 * @app: a #GtkApplication
329 * A simple #GtkApplication has a "default window". This window should
330 * act as the primary user interaction point with your application.
331 * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
332 * and its properties such as "title" and "icon-name" will be initialized
333 * as appropriate for the platform.
335 * If the user closes this window, and your application hasn't created
336 * any other windows, the default action will be to call gtk_application_quit().
338 * If your application has more than one toplevel window (e.g. an
339 * single-document-interface application with multiple open documents),
340 * or if you are constructing your toplevel windows yourself (e.g. using
341 * #GtkBuilder), use gtk_application_add_window() instead.
343 * Returns: (transfer none): The default #GtkWindow for this application
348 gtk_application_get_window (GtkApplication *app)
350 if (app->priv->default_window != NULL)
351 return app->priv->default_window;
353 app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
354 g_object_ref_sink (app->priv->default_window);
356 gtk_application_add_window (app, app->priv->default_window);
358 return app->priv->default_window;
362 * gtk_application_run:
363 * @app: a #GtkApplication
365 * Runs the main loop; see g_application_run().
366 * The default implementation for #GtkApplication uses gtk_main().
371 gtk_application_run (GtkApplication *app)
373 g_application_run (G_APPLICATION (app));
377 * gtk_application_quit:
378 * @app: a #GtkApplication
380 * Request the application exit.
381 * By default, this method will exit the main loop; see gtk_main_quit().
386 gtk_application_quit (GtkApplication *app)
388 g_application_quit (G_APPLICATION (app), gtk_get_current_event_time ());
392 gtk_application_get_property (GObject *object,
397 GtkApplication *app = GTK_APPLICATION (object);
402 g_value_set_object (value, gtk_application_get_window (app));
405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410 gtk_application_set_property (GObject *object,
415 GtkApplication *app = GTK_APPLICATION (object);
417 g_assert (app != NULL);
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
427 setup_default_window_decorations (void)
430 const gchar *filename;
435 pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
436 filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
438 keyfile = g_key_file_new ();
440 if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
441 g_key_file_load_from_file (keyfile, filename, 0, NULL))
443 title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
444 icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
446 if (default_title == NULL)
447 default_title = title;
449 if (gtk_window_get_default_icon_name () == NULL)
450 gtk_window_set_default_icon_name (icon_name);
455 g_key_file_free (keyfile);
459 gtk_application_init (GtkApplication *application)
461 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
463 setup_default_window_decorations ();
468 gtk_application_constructor (GType type,
469 guint n_construct_properties,
470 GObjectConstructParam *construct_params)
474 /* Last ditch effort here */
477 object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
478 n_construct_properties,
485 gtk_application_class_init (GtkApplicationClass *klass)
487 GObjectClass *gobject_class;
488 GApplicationClass *application_class;
490 gobject_class = G_OBJECT_CLASS (klass);
491 application_class = G_APPLICATION_CLASS (klass);
493 gobject_class->constructor = gtk_application_constructor;
494 gobject_class->get_property = gtk_application_get_property;
495 gobject_class->set_property = gtk_application_set_property;
497 application_class->run = gtk_application_default_run;
498 application_class->quit = gtk_application_default_quit;
499 application_class->action = gtk_application_default_action;
500 application_class->prepare_activation = gtk_application_default_prepare_activation;
502 klass->activated = gtk_application_default_activated;
505 * GtkApplication::activated:
506 * @arguments: A #GVariant with the signature "aay"
508 * This signal is emitted when a non-primary process for a given
509 * application is invoked while your application is running; for
510 * example, when a file browser launches your program to open a
511 * file. The raw operating system arguments are passed in the
512 * variant @arguments.
515 gtk_application_signals[ACTIVATED] =
516 g_signal_new (g_intern_static_string ("activated"),
517 G_OBJECT_CLASS_TYPE (klass),
519 G_STRUCT_OFFSET (GtkApplicationClass, activated),
521 g_cclosure_marshal_VOID__BOXED,
525 g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
528 #define __GTK_APPLICATION_C__
529 #include "gtkaliasdef.c"