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>
87 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
89 struct _GtkApplicationPrivate
91 GtkActionGroup *main_actions;
93 GtkWindow *default_window;
97 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
100 process_timestamp_from_platform_data (GVariant *platform_data)
102 /* TODO - extract timestamp from here, update GDK time */
106 gtk_application_default_quit (GtkApplication *application)
113 gtk_application_default_quit_with_data (GApplication *application,
114 GVariant *platform_data)
118 process_timestamp_from_platform_data (platform_data);
120 g_signal_emit (application, gtk_application_signals[QUIT], 0, &result);
126 gtk_application_default_run (GApplication *application)
132 gtk_application_default_prepare_activation (GApplication *application,
134 GVariant *platform_data)
140 g_variant_iter_init (&iter, platform_data);
141 while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
143 if (strcmp (key, "startup-notification-id") == 0 &&
144 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
145 gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
148 g_variant_unref (value);
151 g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
155 gtk_application_default_activated (GtkApplication *application,
158 GtkApplicationPrivate *priv = application->priv;
160 /* TODO: should we raise the last focused window instead ? */
161 if (priv->default_window != NULL)
162 gtk_window_present (priv->default_window);
166 gtk_application_default_action (GtkApplication *application,
167 const gchar *action_name)
169 GtkApplicationPrivate *priv = application->priv;
172 action = gtk_action_group_get_action (priv->main_actions, action_name);
174 gtk_action_activate (action);
178 gtk_application_default_create_window (GtkApplication *application)
180 return GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
184 gtk_application_default_action_with_data (GApplication *application,
185 const gchar *action_name,
186 GVariant *platform_data)
188 process_timestamp_from_platform_data (platform_data);
190 g_signal_emit (application, gtk_application_signals[ACTION], g_quark_from_string (action_name));
194 gtk_application_format_activation_data (void)
196 const gchar *startup_id = NULL;
197 GdkDisplay *display = gdk_display_get_default ();
198 GVariantBuilder builder;
200 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
202 /* try and get the startup notification id from GDK, the environment
203 * or, if everything else failed, fake one.
205 #ifdef GDK_WINDOWING_X11
206 startup_id = gdk_x11_display_get_startup_notification_id (display);
207 #endif /* GDK_WINDOWING_X11 */
210 g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
211 g_variant_new ("s", startup_id));
212 return g_variant_builder_end (&builder);
216 variant_from_argv (int argc,
219 GVariantBuilder builder;
222 g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
224 for (i = 1; i < argc; i++)
228 argv_bytes = (guint8*) argv[i];
229 g_variant_builder_add_value (&builder,
230 g_variant_new_byte_array (argv_bytes, -1));
233 return g_variant_builder_end (&builder);
237 * gtk_application_new:
238 * @appid: System-dependent application identifier
239 * @argc: (allow-none) (inout): System argument count
240 * @argv: (allow-none) (inout): System argument vector
242 * Create a new #GtkApplication, or if one has already been initialized
243 * in this process, return the existing instance. This function will as
244 * a side effect initialize the display system; see gtk_init().
246 * For the behavior if this application is running in another process,
247 * see g_application_new().
249 * Returns: (transfer full): A newly-referenced #GtkApplication
254 gtk_application_new (const gchar *appid,
260 gchar **argv_for_app;
261 GVariant *argv_variant;
262 GError *error = NULL;
264 gtk_init (argc, argv);
267 argc_for_app = *argc;
272 argv_for_app = *argv;
276 argv_variant = variant_from_argv (argc_for_app, argv_for_app);
278 app = g_initable_new (GTK_TYPE_APPLICATION,
281 "application-id", appid,
282 "argv", argv_variant,
286 g_error ("%s", error->message);
287 g_clear_error (&error);
295 on_action_sensitive (GtkAction *action,
299 g_application_set_action_enabled (G_APPLICATION (app),
300 gtk_action_get_name (action),
301 gtk_action_get_sensitive (action));
305 * gtk_application_set_action_group:
306 * @app: A #GtkApplication
307 * @group: A #GtkActionGroup
309 * Set @group as this application's global action group.
310 * This will ensure the operating system interface uses
311 * these actions as follows:
314 * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
315 * <listitem>In GNOME 3, this function populates the application menu.</listitem>
316 * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
317 * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
318 * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
321 * It is an error to call this function more than once.
326 gtk_application_set_action_group (GtkApplication *app,
327 GtkActionGroup *group)
329 GList *actions, *iter;
331 g_return_if_fail (GTK_IS_APPLICATION (app));
332 g_return_if_fail (GTK_IS_ACTION_GROUP (group));
333 g_return_if_fail (app->priv->main_actions == NULL);
335 app->priv->main_actions = g_object_ref (group);
336 actions = gtk_action_group_list_actions (group);
337 for (iter = actions; iter; iter = iter->next)
339 GtkAction *action = iter->data;
340 g_application_add_action (G_APPLICATION (app),
341 gtk_action_get_name (action),
342 gtk_action_get_tooltip (action));
343 g_signal_connect (action, "notify::sensitive",
344 G_CALLBACK (on_action_sensitive), app);
346 g_list_free (actions);
350 gtk_application_on_window_destroy (GtkWidget *window,
353 GtkApplication *app = GTK_APPLICATION (user_data);
355 app->priv->windows = g_slist_remove (app->priv->windows, window);
357 if (app->priv->windows == NULL)
358 gtk_application_quit (app);
363 static gchar *default_title;
366 * gtk_application_add_window:
367 * @app: a #GtkApplication
368 * @window: a toplevel window to add to @app
370 * Adds a window to the #GtkApplication.
372 * If the user closes all of the windows added to @app, the default
373 * behaviour is to call gtk_application_quit().
375 * If your application uses only a single toplevel window, you can
376 * use gtk_application_get_window(). If you are using a sub-class
377 * of #GtkApplication you should call gtk_application_create_window()
378 * to let the #GtkApplication instance create a #GtkWindow and add
379 * it to the list of toplevels of the application. You should call
380 * this function only to add #GtkWindow<!-- -->s that you created
381 * directly using gtk_window_new().
386 gtk_application_add_window (GtkApplication *app,
389 GtkApplicationPrivate *priv;
391 g_return_if_fail (GTK_IS_APPLICATION (app));
392 g_return_if_fail (GTK_IS_WINDOW (window));
396 if (g_slist_find (priv->windows, window) != NULL)
399 priv->windows = g_slist_prepend (priv->windows, window);
401 if (priv->default_window == NULL)
402 priv->default_window = window;
404 if (gtk_window_get_title (window) == NULL && default_title != NULL)
405 gtk_window_set_title (window, default_title);
407 g_signal_connect (window, "destroy",
408 G_CALLBACK (gtk_application_on_window_destroy),
413 * gtk_application_get_window:
414 * @app: a #GtkApplication
416 * A simple #GtkApplication has a "default window". This window should
417 * act as the primary user interaction point with your application.
418 * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
419 * and its properties such as "title" and "icon-name" will be initialized
420 * as appropriate for the platform.
422 * If the user closes this window, and your application hasn't created
423 * any other windows, the default action will be to call gtk_application_quit().
425 * If your application has more than one toplevel window (e.g. an
426 * single-document-interface application with multiple open documents),
427 * or if you are constructing your toplevel windows yourself (e.g. using
428 * #GtkBuilder), use gtk_application_create_window() or
429 * gtk_application_add_window() instead.
431 * Returns: (transfer none): The default #GtkWindow for this application
436 gtk_application_get_window (GtkApplication *app)
438 GtkApplicationPrivate *priv;
441 g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
445 if (priv->default_window != NULL)
446 return priv->default_window;
448 return gtk_application_create_window (app);
452 * gtk_application_create_window:
453 * @app: a #GtkApplication
455 * Creates a new #GtkWindow for the application.
457 * This function calls the #GtkApplication::create_window() virtual function,
458 * which can be overridden by sub-classes, for instance to use #GtkBuilder to
459 * create the user interface. After creating a new #GtkWindow instance, it will
460 * be added to the list of toplevels associated to the application.
462 * Return value: (transfer none): the newly created application #GtkWindow
467 gtk_application_create_window (GtkApplication *app)
471 g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
473 window = GTK_APPLICATION_GET_CLASS (app)->create_window (app);
474 gtk_application_add_window (app, window);
480 * gtk_application_run:
481 * @app: a #GtkApplication
483 * Runs the main loop; see g_application_run().
484 * The default implementation for #GtkApplication uses gtk_main().
489 gtk_application_run (GtkApplication *app)
491 g_application_run (G_APPLICATION (app));
495 * gtk_application_quit:
496 * @app: a #GtkApplication
498 * Request the application exit. This function invokes
499 * g_application_quit_with_data(), which normally will
500 * in turn cause @app to emit #GtkApplication::quit.
502 * To control an application's quit behavior (for example, to ask for
503 * files to be saved), connect to the #GtkApplication::quit signal
509 gtk_application_quit (GtkApplication *app)
511 GVariantBuilder builder;
512 GVariant *platform_data;
514 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
515 g_variant_builder_add (&builder, "{sv}",
517 g_variant_new ("u", gtk_get_current_event_time ()));
518 platform_data = g_variant_builder_end (&builder);
520 g_application_quit_with_data (G_APPLICATION (app), platform_data);
522 g_variant_unref (platform_data);
526 gtk_application_get_property (GObject *object,
531 GtkApplication *app = GTK_APPLICATION (object);
536 g_value_set_object (value, gtk_application_get_window (app));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 gtk_application_set_property (GObject *object,
549 GtkApplication *app = GTK_APPLICATION (object);
551 g_assert (app != NULL);
556 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 setup_default_window_decorations (void)
564 const gchar *filename;
569 pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
570 filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
572 keyfile = g_key_file_new ();
574 if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
575 g_key_file_load_from_file (keyfile, filename, 0, NULL))
577 title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
578 icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
580 if (default_title == NULL)
581 default_title = title;
583 if (gtk_window_get_default_icon_name () == NULL)
584 gtk_window_set_default_icon_name (icon_name);
589 g_key_file_free (keyfile);
593 gtk_application_init (GtkApplication *application)
595 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
597 g_object_set (application, "platform-data", gtk_application_format_activation_data (), NULL);
599 setup_default_window_decorations ();
604 gtk_application_constructor (GType type,
605 guint n_construct_properties,
606 GObjectConstructParam *construct_params)
610 /* Last ditch effort here */
613 object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
614 n_construct_properties,
621 gtk_application_class_init (GtkApplicationClass *klass)
623 GObjectClass *gobject_class;
624 GApplicationClass *application_class;
626 gobject_class = G_OBJECT_CLASS (klass);
627 application_class = G_APPLICATION_CLASS (klass);
629 gobject_class->constructor = gtk_application_constructor;
630 gobject_class->get_property = gtk_application_get_property;
631 gobject_class->set_property = gtk_application_set_property;
633 application_class->run = gtk_application_default_run;
634 application_class->quit_with_data = gtk_application_default_quit_with_data;
635 application_class->action_with_data = gtk_application_default_action_with_data;
636 application_class->prepare_activation = gtk_application_default_prepare_activation;
638 klass->quit = gtk_application_default_quit;
639 klass->action = gtk_application_default_action;
640 klass->create_window = gtk_application_default_create_window;
642 klass->activated = gtk_application_default_activated;
645 * GtkApplication::activated:
646 * @arguments: A #GVariant with the signature "aay"
648 * This signal is emitted when a non-primary process for a given
649 * application is invoked while your application is running; for
650 * example, when a file browser launches your program to open a
651 * file. The raw operating system arguments are passed in the
652 * variant @arguments.
655 gtk_application_signals[ACTIVATED] =
656 g_signal_new (g_intern_static_string ("activated"),
657 G_OBJECT_CLASS_TYPE (klass),
659 G_STRUCT_OFFSET (GtkApplicationClass, activated),
661 g_cclosure_marshal_VOID__VARIANT,
666 * GtkApplication::quit:
667 * @application: the object on which the signal is emitted
669 * This signal is emitted when a quit is initiated. See also
670 * the #GApplication::quit-with-data signal which may in
671 * turn trigger this signal.
673 * The default handler for this signal exits the mainloop of the
676 * Returns: %TRUE if the signal has been handled, %FALSE to continue
679 gtk_application_signals[QUIT] =
680 g_signal_new (g_intern_static_string ("quit"),
681 G_OBJECT_CLASS_TYPE (klass),
683 G_STRUCT_OFFSET (GtkApplicationClass, quit),
684 g_signal_accumulator_true_handled, NULL,
685 _gtk_marshal_BOOLEAN__VOID,
689 * GtkApplication::action:
690 * @application: the object on which the signal is emitted
691 * @name: The name of the activated action
693 * This signal is emitted when an action is activated. The action name
694 * is passed as the first argument, but also as signal detail, so it
695 * is possible to connect to this signal for individual actions.
697 * See also the #GApplication::action-with-data signal which may in
698 * turn trigger this signal.
700 * The signal is never emitted for disabled actions.
702 gtk_application_signals[ACTION] =
703 g_signal_new (g_intern_static_string ("action"),
704 G_OBJECT_CLASS_TYPE (klass),
705 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
706 G_STRUCT_OFFSET (GtkApplicationClass, action),
708 g_cclosure_marshal_VOID__STRING,
712 g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
715 #define __GTK_APPLICATION_C__
716 #include "gtkaliasdef.c"