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_create_window (GtkApplication *application)
178 return GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
182 gtk_application_default_action_with_data (GApplication *application,
183 const gchar *action_name,
184 GVariant *platform_data)
186 process_timestamp_from_platform_data (platform_data);
188 g_signal_emit (application, gtk_application_signals[ACTION], g_quark_from_string (action_name));
192 gtk_application_format_activation_data (void)
194 const gchar *startup_id = NULL;
195 GdkDisplay *display = gdk_display_get_default ();
196 GVariantBuilder builder;
198 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
200 /* try and get the startup notification id from GDK, the environment
201 * or, if everything else failed, fake one.
203 #ifdef GDK_WINDOWING_X11
204 startup_id = gdk_x11_display_get_startup_notification_id (display);
205 #endif /* GDK_WINDOWING_X11 */
208 g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
209 g_variant_new ("s", startup_id));
210 return g_variant_builder_end (&builder);
214 variant_from_argv (int argc,
217 GVariantBuilder builder;
220 g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
222 for (i = 1; i < argc; i++)
226 argv_bytes = (guint8*) argv[i];
227 g_variant_builder_add_value (&builder,
228 g_variant_new_byte_array (argv_bytes, -1));
231 return g_variant_builder_end (&builder);
235 * gtk_application_new:
236 * @appid: System-dependent application identifier
237 * @argc: (allow-none) (inout): System argument count
238 * @argv: (allow-none) (inout): System argument vector
240 * Create a new #GtkApplication, or if one has already been initialized
241 * in this process, return the existing instance. This function will as
242 * a side effect initialize the display system; see gtk_init().
244 * For the behavior if this application is running in another process,
245 * see g_application_new().
247 * Returns: (transfer full): A newly-referenced #GtkApplication
252 gtk_application_new (const gchar *appid,
258 gchar **argv_for_app;
259 GVariant *argv_variant;
260 GError *error = NULL;
262 gtk_init (argc, argv);
265 argc_for_app = *argc;
269 argv_for_app = *argv;
273 argv_variant = variant_from_argv (argc_for_app, argv_for_app);
275 app = g_initable_new (GTK_TYPE_APPLICATION,
278 "application-id", appid,
279 "argv", argv_variant,
283 g_error ("%s", error->message);
284 g_clear_error (&error);
292 on_action_sensitive (GtkAction *action,
296 g_application_set_action_enabled (G_APPLICATION (app),
297 gtk_action_get_name (action),
298 gtk_action_get_sensitive (action));
302 * gtk_application_set_action_group:
303 * @app: A #GtkApplication
304 * @group: A #GtkActionGroup
306 * Set @group as this application's global action group.
307 * This will ensure the operating system interface uses
308 * these actions as follows:
311 * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
312 * <listitem>In GNOME 3, this function populates the application menu.</listitem>
313 * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
314 * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
315 * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
318 * It is an error to call this function more than once.
323 gtk_application_set_action_group (GtkApplication *app,
324 GtkActionGroup *group)
326 GList *actions, *iter;
328 g_return_if_fail (GTK_IS_APPLICATION (app));
329 g_return_if_fail (app->priv->main_actions == NULL);
331 app->priv->main_actions = g_object_ref (group);
332 actions = gtk_action_group_list_actions (group);
333 for (iter = actions; iter; iter = iter->next)
335 GtkAction *action = iter->data;
336 g_application_add_action (G_APPLICATION (app),
337 gtk_action_get_name (action),
338 gtk_action_get_tooltip (action));
339 g_signal_connect (action, "notify::sensitive",
340 G_CALLBACK (on_action_sensitive), app);
342 g_list_free (actions);
346 gtk_application_on_window_destroy (GtkWidget *window,
349 GtkApplication *app = GTK_APPLICATION (user_data);
351 app->priv->windows = g_slist_remove (app->priv->windows, window);
353 if (app->priv->windows == NULL)
354 gtk_application_quit (app);
359 static gchar *default_title;
362 * gtk_application_add_window:
363 * @app: a #GtkApplication
364 * @window: a toplevel window to add to @app
366 * Adds a window to the #GtkApplication.
368 * If the user closes all of the windows added to @app, the default
369 * behaviour is to call gtk_application_quit().
371 * If your application uses only a single toplevel window, you can
372 * use gtk_application_get_window(). If you are using a sub-class
373 * of #GtkApplication you should call gtk_application_create_window()
374 * to let the #GtkApplication instance create a #GtkWindow and add
375 * it to the list of toplevels of the application. You should call
376 * this function only to add #GtkWindow<!-- -->s that you created
377 * directly using gtk_window_new().
382 gtk_application_add_window (GtkApplication *app,
385 GtkApplicationPrivate *priv;
387 g_return_if_fail (GTK_IS_APPLICATION (app));
388 g_return_if_fail (GTK_IS_WINDOW (window));
392 if (g_slist_find (priv->windows, window) != NULL)
395 priv->windows = g_slist_prepend (priv->windows, window);
397 if (priv->default_window == NULL)
398 priv->default_window = window;
400 if (gtk_window_get_title (window) == NULL && default_title != NULL)
401 gtk_window_set_title (window, default_title);
403 g_signal_connect (window, "destroy",
404 G_CALLBACK (gtk_application_on_window_destroy),
409 * gtk_application_get_window:
410 * @app: a #GtkApplication
412 * A simple #GtkApplication has a "default window". This window should
413 * act as the primary user interaction point with your application.
414 * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
415 * and its properties such as "title" and "icon-name" will be initialized
416 * as appropriate for the platform.
418 * If the user closes this window, and your application hasn't created
419 * any other windows, the default action will be to call gtk_application_quit().
421 * If your application has more than one toplevel window (e.g. an
422 * single-document-interface application with multiple open documents),
423 * or if you are constructing your toplevel windows yourself (e.g. using
424 * #GtkBuilder), use gtk_application_create_window() or
425 * gtk_application_add_window() instead.
427 * Returns: (transfer none): The default #GtkWindow for this application
432 gtk_application_get_window (GtkApplication *app)
434 GtkApplicationPrivate *priv;
437 g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
441 if (priv->default_window != NULL)
442 return priv->default_window;
444 return gtk_application_create_window (app);
448 * gtk_application_create_window:
449 * @app: a #GtkApplication
451 * Creates a new #GtkWindow for the application.
453 * This function calls the #GtkApplication::create_window() virtual function,
454 * which can be overridden by sub-classes, for instance to use #GtkBuilder to
455 * create the user interface. After creating a new #GtkWindow instance, it will
456 * be added to the list of toplevels associated to the application.
458 * Return value: (transfer none): the newly created application #GtkWindow
463 gtk_application_create_window (GtkApplication *app)
467 g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
469 window = GTK_APPLICATION_GET_CLASS (app)->create_window (app);
470 gtk_application_add_window (app, window);
476 * gtk_application_run:
477 * @app: a #GtkApplication
479 * Runs the main loop; see g_application_run().
480 * The default implementation for #GtkApplication uses gtk_main().
485 gtk_application_run (GtkApplication *app)
487 g_application_run (G_APPLICATION (app));
491 * gtk_application_quit:
492 * @app: a #GtkApplication
494 * Request the application exit. This function invokes
495 * g_application_quit_with_data(), which normally will
496 * in turn cause @app to emit #GtkApplication::quit.
498 * To control an application's quit behavior (for example, to ask for
499 * files to be saved), connect to the #GtkApplication::quit signal
505 gtk_application_quit (GtkApplication *app)
507 GVariantBuilder builder;
508 GVariant *platform_data;
510 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
511 g_variant_builder_add (&builder, "{sv}",
514 gtk_get_current_event_time ()));
515 platform_data = g_variant_builder_end (&builder);
517 g_application_quit_with_data (G_APPLICATION (app), platform_data);
519 g_variant_unref (platform_data);
523 gtk_application_get_property (GObject *object,
528 GtkApplication *app = GTK_APPLICATION (object);
533 g_value_set_object (value, gtk_application_get_window (app));
536 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gtk_application_set_property (GObject *object,
546 GtkApplication *app = GTK_APPLICATION (object);
548 g_assert (app != NULL);
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 setup_default_window_decorations (void)
561 const gchar *filename;
566 pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
567 filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
569 keyfile = g_key_file_new ();
571 if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
572 g_key_file_load_from_file (keyfile, filename, 0, NULL))
574 title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
575 icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
577 if (default_title == NULL)
578 default_title = title;
580 if (gtk_window_get_default_icon_name () == NULL)
581 gtk_window_set_default_icon_name (icon_name);
586 g_key_file_free (keyfile);
590 gtk_application_init (GtkApplication *application)
592 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
594 g_object_set (application, "platform-data", gtk_application_format_activation_data (), NULL);
596 setup_default_window_decorations ();
601 gtk_application_constructor (GType type,
602 guint n_construct_properties,
603 GObjectConstructParam *construct_params)
607 /* Last ditch effort here */
610 object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
611 n_construct_properties,
618 gtk_application_class_init (GtkApplicationClass *klass)
620 GObjectClass *gobject_class;
621 GApplicationClass *application_class;
623 gobject_class = G_OBJECT_CLASS (klass);
624 application_class = G_APPLICATION_CLASS (klass);
626 gobject_class->constructor = gtk_application_constructor;
627 gobject_class->get_property = gtk_application_get_property;
628 gobject_class->set_property = gtk_application_set_property;
630 application_class->run = gtk_application_default_run;
631 application_class->quit_with_data = gtk_application_default_quit_with_data;
632 application_class->action_with_data = gtk_application_default_action_with_data;
633 application_class->prepare_activation = gtk_application_default_prepare_activation;
635 klass->quit = gtk_application_default_quit;
636 klass->action = gtk_application_default_action;
637 klass->create_window = gtk_application_default_create_window;
639 klass->activated = gtk_application_default_activated;
642 * GtkApplication::activated:
643 * @arguments: A #GVariant with the signature "aay"
645 * This signal is emitted when a non-primary process for a given
646 * application is invoked while your application is running; for
647 * example, when a file browser launches your program to open a
648 * file. The raw operating system arguments are passed in the
649 * variant @arguments.
652 gtk_application_signals[ACTIVATED] =
653 g_signal_new (g_intern_static_string ("activated"),
654 G_OBJECT_CLASS_TYPE (klass),
656 G_STRUCT_OFFSET (GtkApplicationClass, activated),
658 g_cclosure_marshal_VOID__BOXED,
663 * GtkApplication::quit:
664 * @application: the object on which the signal is emitted
666 * This signal is emitted when a quit is initiated. See also
667 * the #GApplication::quit-with-data signal which may in
668 * turn trigger this signal.
670 * The default handler for this signal exits the mainloop of the
673 * Returns: %TRUE if the signal has been handled, %FALSE to continue
676 gtk_application_signals[QUIT] =
677 g_signal_new (g_intern_static_string ("quit"),
678 G_OBJECT_CLASS_TYPE (klass),
680 G_STRUCT_OFFSET (GtkApplicationClass, quit),
681 g_signal_accumulator_true_handled, NULL,
682 _gtk_marshal_BOOLEAN__VOID,
686 * GtkApplication::action:
687 * @application: the object on which the signal is emitted
688 * @name: The name of the activated action
690 * This signal is emitted when an action is activated. The action name
691 * is passed as the first argument, but also as signal detail, so it
692 * is possible to connect to this signal for individual actions.
694 * See also the #GApplication::action-with-data signal which may in
695 * turn trigger this signal.
697 * The signal is never emitted for disabled actions.
699 gtk_application_signals[ACTION] =
700 g_signal_new (g_intern_static_string ("action"),
701 G_OBJECT_CLASS_TYPE (klass),
702 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
703 G_STRUCT_OFFSET (GtkApplicationClass, action),
705 g_cclosure_marshal_VOID__STRING,
709 g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
712 #define __GTK_APPLICATION_C__
713 #include "gtkaliasdef.c"