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
88 GtkActionGroup *main_actions;
90 GtkWindow *default_window;
94 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
97 gtk_application_default_quit (GApplication *application,
105 gtk_application_default_run (GApplication *application)
111 gtk_application_default_prepare_activation (GApplication *application,
113 GVariant *platform_data)
119 g_variant_iter_init (&iter, platform_data);
120 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
122 if (strcmp (key, "startup-notification-id") == 0 &&
123 strcmp (g_variant_get_type_string (value), "s") == 0)
124 gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
126 g_variant_unref (value);
129 g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
133 gtk_application_default_activated (GApplication *application,
136 GtkApplication *app = GTK_APPLICATION (application);
138 /* TODO: should we raise the last focused window instead ? */
139 if (app->priv->default_window != NULL)
140 gtk_window_present (app->priv->default_window);
144 gtk_application_default_action (GApplication *application,
148 GtkApplication *app = GTK_APPLICATION (application);
149 GList *actions, *iter;
151 actions = gtk_action_group_list_actions (app->priv->main_actions);
152 for (iter = actions; iter; iter = iter->next)
154 GtkAction *gtkaction = iter->data;
155 if (strcmp (action, gtk_action_get_name (gtkaction)) == 0)
157 /* TODO set timestamp */
158 gtk_action_activate (gtkaction);
162 g_list_free (actions);
166 gtk_application_format_activation_data (void)
168 const gchar *startup_id = NULL;
169 GdkDisplay *display = gdk_display_get_default ();
170 GVariantBuilder builder;
172 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
174 /* try and get the startup notification id from GDK, the environment
175 * or, if everything else failed, fake one.
177 #ifdef GDK_WINDOWING_X11
178 startup_id = gdk_x11_display_get_startup_notification_id (display);
179 #endif /* GDK_WINDOWING_X11 */
182 g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
183 g_variant_new ("s", startup_id));
184 return g_variant_builder_end (&builder);
188 * gtk_application_new:
189 * @argc: (allow-none) (inout): System argument count
190 * @argv: (allow-none) (inout): System argument vector
191 * @appid: System-dependent application identifier
193 * Create a new #GtkApplication, or if one has already been initialized
194 * in this process, return the existing instance. This function will as
195 * a side effect initialize the display system; see gtk_init().
197 * For the behavior if this application is running in another process,
198 * see g_application_new().
200 * Returns: (transfer full): A newly-referenced #GtkApplication
205 gtk_application_new (gint *argc,
211 gchar **argv_for_app;
212 GVariant *platform_data;
214 gtk_init (argc, argv);
217 argc_for_app = *argc;
221 argv_for_app = *argv;
225 app = g_object_new (GTK_TYPE_APPLICATION, "appid", appid, NULL);
227 platform_data = gtk_application_format_activation_data ();
228 g_application_register_with_data (G_APPLICATION (app), argc_for_app, argv_for_app,
230 g_variant_unref (platform_data);
236 on_action_sensitive (GtkAction *action,
240 g_application_set_action_enabled (G_APPLICATION (app),
241 gtk_action_get_name (action),
242 gtk_action_get_sensitive (action));
246 * gtk_application_set_action_group:
247 * @app: A #GtkApplication
248 * @group: A #GtkActionGroup
250 * Set @group as this application's global action group.
251 * This will ensure the operating system interface uses
252 * these actions as follows:
255 * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
256 * <listitem>In GNOME 3, this function populates the application menu.</listitem>
257 * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
258 * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
259 * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
262 * It is an error to call this function more than once.
267 gtk_application_set_action_group (GtkApplication *app,
268 GtkActionGroup *group)
270 GList *actions, *iter;
272 g_return_if_fail (GTK_IS_APPLICATION (app));
273 g_return_if_fail (app->priv->main_actions == NULL);
275 app->priv->main_actions = g_object_ref (group);
276 actions = gtk_action_group_list_actions (group);
277 for (iter = actions; iter; iter = iter->next)
279 GtkAction *action = iter->data;
280 g_application_add_action (G_APPLICATION (app),
281 gtk_action_get_name (action),
282 gtk_action_get_tooltip (action));
283 g_signal_connect (action, "notify::sensitive",
284 G_CALLBACK (on_action_sensitive), app);
286 g_list_free (actions);
290 gtk_application_on_window_destroy (GtkWidget *window,
293 GtkApplication *app = GTK_APPLICATION (user_data);
295 app->priv->windows = g_slist_remove (app->priv->windows, window);
297 if (app->priv->windows == NULL)
298 gtk_application_quit (app);
303 static gchar *default_title;
306 * gtk_application_add_window:
307 * @app: a #GtkApplication
308 * @window: a toplevel window to add to @app
310 * Adds a window to the #GtkApplication.
312 * If the user closes all of the windows added to @app, the default
313 * behaviour is to call gtk_application_quit().
315 * If your application uses only a single toplevel window, you can
316 * use gtk_application_get_window().
321 gtk_application_add_window (GtkApplication *app,
324 app->priv->windows = g_slist_prepend (app->priv->windows, window);
326 if (gtk_window_get_title (window) == NULL && default_title != NULL)
327 gtk_window_set_title (window, default_title);
329 g_signal_connect (window, "destroy",
330 G_CALLBACK (gtk_application_on_window_destroy), app);
334 * gtk_application_get_window:
335 * @app: a #GtkApplication
337 * A simple #GtkApplication has a "default window". This window should
338 * act as the primary user interaction point with your application.
339 * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
340 * and its properties such as "title" and "icon-name" will be initialized
341 * as appropriate for the platform.
343 * If the user closes this window, and your application hasn't created
344 * any other windows, the default action will be to call gtk_application_quit().
346 * If your application has more than one toplevel window (e.g. an
347 * single-document-interface application with multiple open documents),
348 * or if you are constructing your toplevel windows yourself (e.g. using
349 * #GtkBuilder), use gtk_application_add_window() instead.
351 * Returns: (transfer none): The default #GtkWindow for this application
356 gtk_application_get_window (GtkApplication *app)
358 if (app->priv->default_window != NULL)
359 return app->priv->default_window;
361 app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
362 g_object_ref_sink (app->priv->default_window);
364 gtk_application_add_window (app, app->priv->default_window);
366 return app->priv->default_window;
370 * gtk_application_run:
371 * @app: a #GtkApplication
373 * Runs the main loop; see g_application_run().
374 * The default implementation for #GtkApplication uses gtk_main().
379 gtk_application_run (GtkApplication *app)
381 g_application_run (G_APPLICATION (app));
385 * gtk_application_quit:
386 * @app: a #GtkApplication
388 * Request the application exit.
389 * By default, this method will exit the main loop; see gtk_main_quit().
394 gtk_application_quit (GtkApplication *app)
396 g_application_quit (G_APPLICATION (app), gtk_get_current_event_time ());
400 gtk_application_get_property (GObject *object,
405 GtkApplication *app = GTK_APPLICATION (object);
410 g_value_set_object (value, gtk_application_get_window (app));
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 gtk_application_set_property (GObject *object,
423 GtkApplication *app = GTK_APPLICATION (object);
425 g_assert (app != NULL);
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 setup_default_window_decorations (void)
438 const gchar *filename;
443 pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
444 filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
446 keyfile = g_key_file_new ();
448 if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
449 g_key_file_load_from_file (keyfile, filename, 0, NULL))
451 title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
452 icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
454 g_print ("default title: %s\n", title);
455 g_print ("default icon: %s\n", icon_name);
457 if (default_title == NULL)
458 default_title = title;
460 if (gtk_window_get_default_icon_name () == NULL)
461 gtk_window_set_default_icon_name (icon_name);
466 g_key_file_free (keyfile);
470 gtk_application_init (GtkApplication *application)
472 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
474 setup_default_window_decorations ();
479 gtk_application_constructor (GType type,
480 guint n_construct_properties,
481 GObjectConstructParam *construct_params)
485 /* Last ditch effort here */
488 object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
489 n_construct_properties,
496 gtk_application_class_init (GtkApplicationClass *klass)
498 GObjectClass *gobject_class;
499 GApplicationClass *application_class;
501 gobject_class = G_OBJECT_CLASS (klass);
502 application_class = G_APPLICATION_CLASS (klass);
504 gobject_class->constructor = gtk_application_constructor;
505 gobject_class->get_property = gtk_application_get_property;
506 gobject_class->set_property = gtk_application_set_property;
508 application_class->run = gtk_application_default_run;
509 application_class->quit = gtk_application_default_quit;
510 application_class->action = gtk_application_default_action;
511 application_class->prepare_activation = gtk_application_default_prepare_activation;
513 klass->activated = gtk_application_default_activated;
516 * GtkApplication::activated:
517 * @arguments: A #GVariant with the signature "aay"
519 * This signal is emitted when a non-primary process for a given
520 * application is invoked while your application is running; for
521 * example, when a file browser launches your program to open a
522 * file. The raw operating system arguments are passed in the
523 * variant @arguments.
526 gtk_application_signals[ACTIVATED] =
527 g_signal_new (g_intern_static_string ("activated"),
528 G_OBJECT_CLASS_TYPE (klass),
530 G_STRUCT_OFFSET (GtkApplicationClass, activated),
532 g_cclosure_marshal_VOID__BOXED,
536 g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
539 #define __GTK_APPLICATION_C__
540 #include "gtkaliasdef.c"