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"
37 #include "gtkmarshalers.h"
39 #include "gtkprivate.h"
44 #ifdef GDK_WINDOWING_X11
45 #include <gdk/x11/gdkx.h>
49 * SECTION:gtkapplication
50 * @title: GtkApplication
51 * @short_description: Application class
53 * #GtkApplication is a class that handles many important aspects
54 * of a GTK+ application in a convenient fashion, without enforcing
55 * a one-size-fits-all application model.
57 * Currently, GtkApplication handles application uniqueness, provides
58 * some basic scriptability by exporting 'actions', implements some
59 * standard actions itself (such as 'Quit') and provides a main window
60 * whose life-cycle is automatically tied to the life-cycle of your
63 * <example id="gtkapplication"><title>A simple application</title>
65 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
66 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
86 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
88 struct _GtkApplicationPrivate
90 GtkActionGroup *main_actions;
92 GtkWindow *default_window;
96 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
99 process_timestamp_from_platform_data (GVariant *platform_data)
101 /* TODO - extract timestamp from here, update GDK time */
105 gtk_application_default_quit (GtkApplication *application)
112 gtk_application_default_quit_with_data (GApplication *application,
113 GVariant *platform_data)
117 process_timestamp_from_platform_data (platform_data);
119 g_signal_emit (application, gtk_application_signals[QUIT], 0, &result);
125 gtk_application_default_run (GApplication *application)
131 gtk_application_default_prepare_activation (GApplication *application,
133 GVariant *platform_data)
139 g_variant_iter_init (&iter, platform_data);
140 while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
142 if (strcmp (key, "startup-notification-id") == 0 &&
143 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
144 gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
145 g_variant_unref (value);
148 g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
152 gtk_application_default_activated (GtkApplication *application,
155 GtkApplication *app = GTK_APPLICATION (application);
157 /* TODO: should we raise the last focused window instead ? */
158 if (app->priv->default_window != NULL)
159 gtk_window_present (app->priv->default_window);
163 gtk_application_default_action (GtkApplication *application,
164 const gchar *action_name)
168 action = gtk_action_group_get_action (application->priv->main_actions, action_name);
171 gtk_action_activate (action);
176 gtk_application_default_action_with_data (GApplication *application,
177 const gchar *action_name,
178 GVariant *platform_data)
180 process_timestamp_from_platform_data (platform_data);
182 g_signal_emit (application, gtk_application_signals[ACTION], g_quark_from_string (action_name));
186 gtk_application_format_activation_data (void)
188 const gchar *startup_id = NULL;
189 GdkDisplay *display = gdk_display_get_default ();
190 GVariantBuilder builder;
192 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
194 /* try and get the startup notification id from GDK, the environment
195 * or, if everything else failed, fake one.
197 #ifdef GDK_WINDOWING_X11
198 startup_id = gdk_x11_display_get_startup_notification_id (display);
199 #endif /* GDK_WINDOWING_X11 */
202 g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
203 g_variant_new ("s", startup_id));
204 return g_variant_builder_end (&builder);
208 * gtk_application_new:
209 * @argc: (allow-none) (inout): System argument count
210 * @argv: (allow-none) (inout): System argument vector
211 * @appid: System-dependent application identifier
213 * Create a new #GtkApplication, or if one has already been initialized
214 * in this process, return the existing instance. This function will as
215 * a side effect initialize the display system; see gtk_init().
217 * For the behavior if this application is running in another process,
218 * see g_application_new().
220 * Returns: (transfer full): A newly-referenced #GtkApplication
225 gtk_application_new (gint *argc,
231 gchar **argv_for_app;
232 GVariant *platform_data;
234 gtk_init (argc, argv);
237 argc_for_app = *argc;
241 argv_for_app = *argv;
245 app = g_object_new (GTK_TYPE_APPLICATION, "application-id", appid, NULL);
247 platform_data = gtk_application_format_activation_data ();
248 g_application_register_with_data (G_APPLICATION (app), argc_for_app, argv_for_app,
250 g_variant_unref (platform_data);
256 on_action_sensitive (GtkAction *action,
260 g_application_set_action_enabled (G_APPLICATION (app),
261 gtk_action_get_name (action),
262 gtk_action_get_sensitive (action));
266 * gtk_application_set_action_group:
267 * @app: A #GtkApplication
268 * @group: A #GtkActionGroup
270 * Set @group as this application's global action group.
271 * This will ensure the operating system interface uses
272 * these actions as follows:
275 * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
276 * <listitem>In GNOME 3, this function populates the application menu.</listitem>
277 * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
278 * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
279 * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
282 * It is an error to call this function more than once.
287 gtk_application_set_action_group (GtkApplication *app,
288 GtkActionGroup *group)
290 GList *actions, *iter;
292 g_return_if_fail (GTK_IS_APPLICATION (app));
293 g_return_if_fail (app->priv->main_actions == NULL);
295 app->priv->main_actions = g_object_ref (group);
296 actions = gtk_action_group_list_actions (group);
297 for (iter = actions; iter; iter = iter->next)
299 GtkAction *action = iter->data;
300 g_application_add_action (G_APPLICATION (app),
301 gtk_action_get_name (action),
302 gtk_action_get_tooltip (action));
303 g_signal_connect (action, "notify::sensitive",
304 G_CALLBACK (on_action_sensitive), app);
306 g_list_free (actions);
310 gtk_application_on_window_destroy (GtkWidget *window,
313 GtkApplication *app = GTK_APPLICATION (user_data);
315 app->priv->windows = g_slist_remove (app->priv->windows, window);
317 if (app->priv->windows == NULL)
318 gtk_application_quit (app);
323 static gchar *default_title;
326 * gtk_application_add_window:
327 * @app: a #GtkApplication
328 * @window: a toplevel window to add to @app
330 * Adds a window to the #GtkApplication.
332 * If the user closes all of the windows added to @app, the default
333 * behaviour is to call gtk_application_quit().
335 * If your application uses only a single toplevel window, you can
336 * use gtk_application_get_window().
341 gtk_application_add_window (GtkApplication *app,
344 app->priv->windows = g_slist_prepend (app->priv->windows, window);
346 if (gtk_window_get_title (window) == NULL && default_title != NULL)
347 gtk_window_set_title (window, default_title);
349 g_signal_connect (window, "destroy",
350 G_CALLBACK (gtk_application_on_window_destroy), app);
354 * gtk_application_get_window:
355 * @app: a #GtkApplication
357 * A simple #GtkApplication has a "default window". This window should
358 * act as the primary user interaction point with your application.
359 * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
360 * and its properties such as "title" and "icon-name" will be initialized
361 * as appropriate for the platform.
363 * If the user closes this window, and your application hasn't created
364 * any other windows, the default action will be to call gtk_application_quit().
366 * If your application has more than one toplevel window (e.g. an
367 * single-document-interface application with multiple open documents),
368 * or if you are constructing your toplevel windows yourself (e.g. using
369 * #GtkBuilder), use gtk_application_add_window() instead.
371 * Returns: (transfer none): The default #GtkWindow for this application
376 gtk_application_get_window (GtkApplication *app)
378 if (app->priv->default_window != NULL)
379 return app->priv->default_window;
381 app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
382 g_object_ref_sink (app->priv->default_window);
384 gtk_application_add_window (app, app->priv->default_window);
386 return app->priv->default_window;
390 * gtk_application_run:
391 * @app: a #GtkApplication
393 * Runs the main loop; see g_application_run().
394 * The default implementation for #GtkApplication uses gtk_main().
399 gtk_application_run (GtkApplication *app)
401 g_application_run (G_APPLICATION (app));
405 * gtk_application_quit:
406 * @app: a #GtkApplication
408 * Request the application exit. This function invokes
409 * g_application_quit_with_data(), which normally will
410 * in turn cause @app to emit #GtkApplication::quit.
412 * To control an application's quit behavior (for example, to ask for
413 * files to be saved), connect to the #GtkApplication::quit signal
419 gtk_application_quit (GtkApplication *app)
421 GVariantBuilder builder;
422 GVariant *platform_data;
424 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
425 g_variant_builder_add (&builder, "{sv}",
428 gtk_get_current_event_time ()));
429 platform_data = g_variant_builder_end (&builder);
431 g_application_quit_with_data (G_APPLICATION (app), platform_data);
433 g_variant_unref (platform_data);
437 gtk_application_get_property (GObject *object,
442 GtkApplication *app = GTK_APPLICATION (object);
447 g_value_set_object (value, gtk_application_get_window (app));
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
455 gtk_application_set_property (GObject *object,
460 GtkApplication *app = GTK_APPLICATION (object);
462 g_assert (app != NULL);
467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
472 setup_default_window_decorations (void)
475 const gchar *filename;
480 pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
481 filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
483 keyfile = g_key_file_new ();
485 if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
486 g_key_file_load_from_file (keyfile, filename, 0, NULL))
488 title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
489 icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
491 if (default_title == NULL)
492 default_title = title;
494 if (gtk_window_get_default_icon_name () == NULL)
495 gtk_window_set_default_icon_name (icon_name);
500 g_key_file_free (keyfile);
504 gtk_application_init (GtkApplication *application)
506 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
508 setup_default_window_decorations ();
513 gtk_application_constructor (GType type,
514 guint n_construct_properties,
515 GObjectConstructParam *construct_params)
519 /* Last ditch effort here */
522 object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
523 n_construct_properties,
530 gtk_application_class_init (GtkApplicationClass *klass)
532 GObjectClass *gobject_class;
533 GApplicationClass *application_class;
535 gobject_class = G_OBJECT_CLASS (klass);
536 application_class = G_APPLICATION_CLASS (klass);
538 gobject_class->constructor = gtk_application_constructor;
539 gobject_class->get_property = gtk_application_get_property;
540 gobject_class->set_property = gtk_application_set_property;
542 application_class->run = gtk_application_default_run;
543 application_class->quit_with_data = gtk_application_default_quit_with_data;
544 application_class->action_with_data = gtk_application_default_action_with_data;
545 application_class->prepare_activation = gtk_application_default_prepare_activation;
547 klass->quit = gtk_application_default_quit;
548 klass->action = gtk_application_default_action;
550 klass->activated = gtk_application_default_activated;
553 * GtkApplication::activated:
554 * @arguments: A #GVariant with the signature "aay"
556 * This signal is emitted when a non-primary process for a given
557 * application is invoked while your application is running; for
558 * example, when a file browser launches your program to open a
559 * file. The raw operating system arguments are passed in the
560 * variant @arguments.
563 gtk_application_signals[ACTIVATED] =
564 g_signal_new (g_intern_static_string ("activated"),
565 G_OBJECT_CLASS_TYPE (klass),
567 G_STRUCT_OFFSET (GtkApplicationClass, activated),
569 g_cclosure_marshal_VOID__BOXED,
574 * GtkApplication::quit:
575 * @application: the object on which the signal is emitted
577 * This signal is emitted when a quit is initiated. See also
578 * the #GApplication::quit-with-data signal which may in
579 * turn trigger this signal.
581 * The default handler for this signal exits the mainloop of the
584 * Returns: %TRUE if the signal has been handled, %FALSE to continue
587 gtk_application_signals[QUIT] =
588 g_signal_new (g_intern_static_string ("quit"),
589 G_OBJECT_CLASS_TYPE (klass),
591 G_STRUCT_OFFSET (GtkApplicationClass, quit),
592 g_signal_accumulator_true_handled, NULL,
593 _gtk_marshal_BOOLEAN__VOID,
597 * GtkApplication::action:
598 * @application: the object on which the signal is emitted
599 * @name: The name of the activated action
601 * This signal is emitted when an action is activated. The action name
602 * is passed as the first argument, but also as signal detail, so it
603 * is possible to connect to this signal for individual actions.
605 * See also the #GApplication::action-with-data signal which may in
606 * turn trigger this signal.
608 * The signal is never emitted for disabled actions.
610 gtk_application_signals[ACTION] =
611 g_signal_new (g_intern_static_string ("action"),
612 G_OBJECT_CLASS_TYPE (klass),
613 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
614 G_STRUCT_OFFSET (GtkApplicationClass, action),
616 g_cclosure_marshal_VOID__STRING,
620 g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
623 #define __GTK_APPLICATION_C__
624 #include "gtkaliasdef.c"