-/* GTK - The GIMP Toolkit
- *
- * Copyright (C) 2010 Red Hat, Inc.
+/*
+ * Copyright © 2010 Codethink Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2 of the licence, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
- * Author: Colin Walters <walters@verbum.org>
- */
-
-/*
- * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
- * file for a list of people on the GTK+ Team. See the ChangeLog
- * files for a list of changes. These files are distributed with
- * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ * Author: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
+#include <string.h>
#include "gtkapplication.h"
+#include "gtkmarshalers.h"
#include "gtkmain.h"
-#include "gtkintl.h"
-#include "gtkprivate.h"
-
-#include "gtkalias.h"
+#include "gtkwindow.h"
#include <gdk/gdk.h>
#ifdef GDK_WINDOWING_X11
* of a GTK+ application in a convenient fashion, without enforcing
* a one-size-fits-all application model.
*
- * Currently, GtkApplication handles application uniqueness, provides
- * some basic scriptability by exporting 'actions', implements some
- * standard actions itself (such as 'Quit') and provides a main window
- * whose life-cycle is automatically tied to the life-cycle of your
- * application.
+ * Currently, GtkApplication handles GTK+ initialization, application
+ * uniqueness, provides some basic scriptability by exporting 'actions',
+ * and manages a list of toplevel windows whose life-cycle is automatically
+ * tied to the life-cycle of your application.
*
* <example id="gtkapplication"><title>A simple application</title>
* <programlisting>
- * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
+ * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
* <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
* </xi:include>
* </programlisting>
* </example>
*/
-enum
-{
- PROP_0,
- PROP_WINDOW
-};
-
-enum
-{
- ACTIVATED,
+enum {
+ WINDOW_ADDED,
+ WINDOW_REMOVED,
LAST_SIGNAL
};
-static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
+static guint gtk_application_signals[LAST_SIGNAL];
+
+G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
struct _GtkApplicationPrivate
{
- GtkActionGroup *main_actions;
-
- GtkWindow *default_window;
- GSList *windows;
+ GList *windows;
};
-G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
-
static gboolean
-gtk_application_default_quit (GApplication *application,
- guint timestamp)
+gtk_application_focus_in_event_cb (GtkWindow *window,
+ GdkEventFocus *event,
+ GtkApplication *application)
{
- gtk_main_quit ();
- return TRUE;
+ GtkApplicationPrivate *priv = application->priv;
+ GList *link;
+
+ /* Keep the window list sorted by most-recently-focused. */
+ link = g_list_find (priv->windows, window);
+ if (link != NULL && link != priv->windows)
+ {
+ priv->windows = g_list_remove_link (priv->windows, link);
+ priv->windows = g_list_concat (link, priv->windows);
+ }
+
+ return FALSE;
+}
+
+static void
+gtk_application_startup (GApplication *application)
+{
+ G_APPLICATION_CLASS (gtk_application_parent_class)
+ ->startup (application);
+
+ gtk_init (0, 0);
}
static void
-gtk_application_default_run (GApplication *application)
+gtk_application_add_platform_data (GApplication *application,
+ GVariantBuilder *builder)
{
- gtk_main ();
+ const gchar *startup_id;
+
+ startup_id = getenv ("DESKTOP_STARTUP_ID");
+
+ if (startup_id && g_utf8_validate (startup_id, -1, NULL))
+ g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
+ g_variant_new_string (startup_id));
}
static void
-gtk_application_default_prepare_activation (GApplication *application,
- GVariant *arguments,
- GVariant *platform_data)
+gtk_application_before_emit (GApplication *application,
+ GVariant *platform_data)
{
GVariantIter iter;
- gchar *key;
+ const gchar *key;
GVariant *value;
g_variant_iter_init (&iter, platform_data);
- while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
+ while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
{
- if (strcmp (key, "startup-notification-id") == 0 &&
- g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
- gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
- g_variant_unref (value);
+#ifdef GDK_WINDOWING_X11
+ if (strcmp (key, "desktop-startup-id") == 0)
+ {
+ GdkDisplay *display;
+ const gchar *id;
+
+ display = gdk_display_get_default ();
+ id = g_variant_get_string (value, NULL);
+ gdk_x11_display_set_startup_notification_id (display, id);
+ }
+#endif
}
-
- g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
}
static void
-gtk_application_default_activated (GApplication *application,
- GVariant *arguments)
+gtk_application_after_emit (GApplication *application,
+ GVariant *platform_data)
{
- GtkApplication *app = GTK_APPLICATION (application);
-
- /* TODO: should we raise the last focused window instead ? */
- if (app->priv->default_window != NULL)
- gtk_window_present (app->priv->default_window);
+ gdk_notify_startup_complete ();
}
static void
-gtk_application_default_action (GApplication *application,
- const gchar *action_name,
- guint timestamp)
+gtk_application_init (GtkApplication *application)
{
- GtkApplication *app = GTK_APPLICATION (application);
- GtkAction *action;
-
- action = gtk_action_group_get_action (app->priv->main_actions, action_name);
- if (action)
- {
- /* TODO set timestamp */
- gtk_action_activate (action);
- }
+ application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
+ GTK_TYPE_APPLICATION,
+ GtkApplicationPrivate);
}
-static GVariant *
-gtk_application_format_activation_data (void)
+static void
+gtk_application_window_added (GtkApplication *application,
+ GtkWindow *window)
{
- const gchar *startup_id = NULL;
- GdkDisplay *display = gdk_display_get_default ();
- GVariantBuilder builder;
+ GtkApplicationPrivate *priv = application->priv;
- g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
+ priv->windows = g_list_prepend (priv->windows, window);
+ gtk_window_set_application (window, application);
+ g_application_hold (G_APPLICATION (application));
- /* try and get the startup notification id from GDK, the environment
- * or, if everything else failed, fake one.
- */
-#ifdef GDK_WINDOWING_X11
- startup_id = gdk_x11_display_get_startup_notification_id (display);
-#endif /* GDK_WINDOWING_X11 */
-
- if (startup_id)
- g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
- g_variant_new ("s", startup_id));
- return g_variant_builder_end (&builder);
+ g_signal_connect (window, "focus-in-event",
+ G_CALLBACK (gtk_application_focus_in_event_cb),
+ application);
}
-/**
- * gtk_application_new:
- * @argc: (allow-none) (inout): System argument count
- * @argv: (allow-none) (inout): System argument vector
- * @appid: System-dependent application identifier
- *
- * Create a new #GtkApplication, or if one has already been initialized
- * in this process, return the existing instance. This function will as
- * a side effect initialize the display system; see gtk_init().
- *
- * For the behavior if this application is running in another process,
- * see g_application_new().
- *
- * Returns: (transfer full): A newly-referenced #GtkApplication
- *
- * Since: 3.0
- */
-GtkApplication*
-gtk_application_new (gint *argc,
- gchar ***argv,
- const gchar *appid)
+static void
+gtk_application_window_removed (GtkApplication *application,
+ GtkWindow *window)
{
- GtkApplication *app;
- gint argc_for_app;
- gchar **argv_for_app;
- GVariant *platform_data;
-
- gtk_init (argc, argv);
-
- if (argc)
- argc_for_app = *argc;
- else
- argc_for_app = 0;
- if (argv)
- argv_for_app = *argv;
- else
- argv_for_app = NULL;
-
- app = g_object_new (GTK_TYPE_APPLICATION, "application-id", appid, NULL);
-
- platform_data = gtk_application_format_activation_data ();
- g_application_register_with_data (G_APPLICATION (app), argc_for_app, argv_for_app,
- platform_data);
- g_variant_unref (platform_data);
-
- return app;
+ GtkApplicationPrivate *priv = application->priv;
+
+ g_signal_handlers_disconnect_by_func (window,
+ gtk_application_focus_in_event_cb,
+ application);
+
+ g_application_release (G_APPLICATION (application));
+ priv->windows = g_list_remove (priv->windows, window);
+ gtk_window_set_application (window, NULL);
}
static void
-on_action_sensitive (GtkAction *action,
- GParamSpec *pspec,
- GtkApplication *app)
+gtk_application_class_init (GtkApplicationClass *class)
{
- g_application_set_action_enabled (G_APPLICATION (app),
- gtk_action_get_name (action),
- gtk_action_get_sensitive (action));
+ GApplicationClass *application_class = G_APPLICATION_CLASS (class);
+
+ application_class->add_platform_data = gtk_application_add_platform_data;
+ application_class->before_emit = gtk_application_before_emit;
+ application_class->after_emit = gtk_application_after_emit;
+ application_class->startup = gtk_application_startup;
+
+ class->window_added = gtk_application_window_added;
+ class->window_removed = gtk_application_window_removed;
+
+ g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
+
+ /**
+ * GtkApplication::window-added:
+ * @application: the #GtkApplication which emitted the signal
+ * @window: the newly-added #GtkWindow
+ *
+ * Emitted when a #GtkWindow is added to @application through
+ * gtk_application_add_wi!ndow().
+ *
+ * Since: 3.2
+ */
+ gtk_application_signals[WINDOW_ADDED] =
+ g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GtkApplicationClass, window_added),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
+
+ /**
+ * GtkApplication::window-removed:
+ * @application: the #GtkApplication which emitted the signal
+ * @window: the #GtkWindow that is being removed
+ *
+ * Emitted when a #GtkWindow is removed from @application,
+ * either as a side-effect of being destroyed or explicitly
+ * through gtk_application_remove_window().
+ *
+ * Since: 3.2
+ */
+ gtk_application_signals[WINDOW_REMOVED] =
+ g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
}
/**
- * gtk_application_set_action_group:
- * @app: A #GtkApplication
- * @group: A #GtkActionGroup
+ * gtk_application_new:
+ * @application_id: the application id
+ * @flags: the application flags
*
- * Set @group as this application's global action group.
- * This will ensure the operating system interface uses
- * these actions as follows:
+ * Creates a new #GtkApplication instance.
*
- * <itemizedlist>
- * <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
- * <listitem>In GNOME 3, this function populates the application menu.</listitem>
- * <listitem>In Windows prior to version 7, this function does nothing.</listitem>
- * <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
- * <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
- * </itemizedlist>
+ * This function calls g_type_init() for you. gtk_init() is called
+ * as soon as the application gets registered as the primary instance.
*
- * It is an error to call this function more than once.
+ * The application id must be valid. See g_application_id_is_valid().
*
- * Since: 3.0
+ * Returns: a new #GtkApplication instance
*/
-void
-gtk_application_set_action_group (GtkApplication *app,
- GtkActionGroup *group)
-{
- GList *actions, *iter;
-
- g_return_if_fail (GTK_IS_APPLICATION (app));
- g_return_if_fail (app->priv->main_actions == NULL);
-
- app->priv->main_actions = g_object_ref (group);
- actions = gtk_action_group_list_actions (group);
- for (iter = actions; iter; iter = iter->next)
- {
- GtkAction *action = iter->data;
- g_application_add_action (G_APPLICATION (app),
- gtk_action_get_name (action),
- gtk_action_get_tooltip (action));
- g_signal_connect (action, "notify::sensitive",
- G_CALLBACK (on_action_sensitive), app);
- }
- g_list_free (actions);
-}
-
-static gboolean
-gtk_application_on_window_destroy (GtkWidget *window,
- gpointer user_data)
+GtkApplication *
+gtk_application_new (const gchar *application_id,
+ GApplicationFlags flags)
{
- GtkApplication *app = GTK_APPLICATION (user_data);
+ g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
- app->priv->windows = g_slist_remove (app->priv->windows, window);
+ g_type_init ();
- if (app->priv->windows == NULL)
- gtk_application_quit (app);
-
- return FALSE;
+ return g_object_new (GTK_TYPE_APPLICATION,
+ "application-id", application_id,
+ "flags", flags,
+ NULL);
}
-static gchar *default_title;
-
/**
* gtk_application_add_window:
- * @app: a #GtkApplication
- * @window: a toplevel window to add to @app
+ * @application: a #GtkApplication
+ * @window: a #GtkWindow
*
- * Adds a window to the #GtkApplication.
+ * Adds a window from @application.
*
- * If the user closes all of the windows added to @app, the default
- * behaviour is to call gtk_application_quit().
+ * This call is equivalent to setting the #GtkWindow:application
+ * property of @window to @application.
*
- * If your application uses only a single toplevel window, you can
- * use gtk_application_get_window().
+ * Normally, the connection between the application and the window
+ * will remain until the window is destroyed, but you can explicitly
+ * remove it with gtk_application_remove_window().
+ *
+ * GTK+ will keep the application running as long as it has
+ * any windows.
*
* Since: 3.0
- */
+ **/
void
-gtk_application_add_window (GtkApplication *app,
+gtk_application_add_window (GtkApplication *application,
GtkWindow *window)
{
- app->priv->windows = g_slist_prepend (app->priv->windows, window);
+ g_return_if_fail (GTK_IS_APPLICATION (application));
- if (gtk_window_get_title (window) == NULL && default_title != NULL)
- gtk_window_set_title (window, default_title);
-
- g_signal_connect (window, "destroy",
- G_CALLBACK (gtk_application_on_window_destroy), app);
+ if (!g_list_find (application->priv->windows, window))
+ g_signal_emit (application,
+ gtk_application_signals[WINDOW_ADDED], 0, window);
}
/**
- * gtk_application_get_window:
- * @app: a #GtkApplication
- *
- * A simple #GtkApplication has a "default window". This window should
- * act as the primary user interaction point with your application.
- * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
- * and its properties such as "title" and "icon-name" will be initialized
- * as appropriate for the platform.
+ * gtk_application_remove_window:
+ * @application: a #GtkApplication
+ * @window: a #GtkWindow
*
- * If the user closes this window, and your application hasn't created
- * any other windows, the default action will be to call gtk_application_quit().
+ * Remove a window from @application.
*
- * If your application has more than one toplevel window (e.g. an
- * single-document-interface application with multiple open documents),
- * or if you are constructing your toplevel windows yourself (e.g. using
- * #GtkBuilder), use gtk_application_add_window() instead.
+ * If @window belongs to @application then this call is equivalent to
+ * setting the #GtkWindow:application property of @window to
+ * %NULL.
*
- * Returns: (transfer none): The default #GtkWindow for this application
+ * The application may stop running as a result of a call to this
+ * function.
*
* Since: 3.0
- */
-GtkWindow *
-gtk_application_get_window (GtkApplication *app)
+ **/
+void
+gtk_application_remove_window (GtkApplication *application,
+ GtkWindow *window)
{
- if (app->priv->default_window != NULL)
- return app->priv->default_window;
-
- app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
- g_object_ref_sink (app->priv->default_window);
+ g_return_if_fail (GTK_IS_APPLICATION (application));
- gtk_application_add_window (app, app->priv->default_window);
-
- return app->priv->default_window;
+ if (g_list_find (application->priv->windows, window))
+ g_signal_emit (application,
+ gtk_application_signals[WINDOW_REMOVED], 0, window);
}
/**
- * gtk_application_run:
- * @app: a #GtkApplication
+ * gtk_application_get_windows:
+ * @application: a #GtkApplication
*
- * Runs the main loop; see g_application_run().
- * The default implementation for #GtkApplication uses gtk_main().
+ * Gets a list of the #GtkWindow<!-- -->s associated with @application.
*
- * Since: 3.0
- */
-void
-gtk_application_run (GtkApplication *app)
-{
- g_application_run (G_APPLICATION (app));
-}
-
-/**
- * gtk_application_quit:
- * @app: a #GtkApplication
+ * The list is sorted by most recently focused window, such that the first
+ * element is the currently focused window. (Useful for choosing a parent
+ * for a transient window.)
*
- * Request the application exit.
- * By default, this method will exit the main loop; see gtk_main_quit().
+ * The list that is returned should not be modified in any way. It will
+ * only remain valid until the next focus change or window creation or
+ * deletion.
+ *
+ * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
*
* Since: 3.0
- */
-void
-gtk_application_quit (GtkApplication *app)
-{
- g_application_quit (G_APPLICATION (app), gtk_get_current_event_time ());
-}
-
-static void
-gtk_application_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec)
+ **/
+GList *
+gtk_application_get_windows (GtkApplication *application)
{
- GtkApplication *app = GTK_APPLICATION (object);
+ g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
- switch (prop_id)
- {
- case PROP_WINDOW:
- g_value_set_object (value, gtk_application_get_window (app));
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- }
+ return application->priv->windows;
}
-
-static void
-gtk_application_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec)
-{
- GtkApplication *app = GTK_APPLICATION (object);
-
- g_assert (app != NULL);
-
- switch (prop_id)
- {
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- }
-}
-
-static void
-setup_default_window_decorations (void)
-{
- const gchar *pid;
- const gchar *filename;
- GKeyFile *keyfile;
- gchar *title;
- gchar *icon_name;
-
- pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
- filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
-
- keyfile = g_key_file_new ();
-
- if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
- g_key_file_load_from_file (keyfile, filename, 0, NULL))
- {
- title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
- icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
-
- if (default_title == NULL)
- default_title = title;
-
- if (gtk_window_get_default_icon_name () == NULL)
- gtk_window_set_default_icon_name (icon_name);
-
- g_free (icon_name);
- }
-
- g_key_file_free (keyfile);
-}
-
-static void
-gtk_application_init (GtkApplication *application)
-{
- application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
-
- setup_default_window_decorations ();
-}
-
-
-static GObject*
-gtk_application_constructor (GType type,
- guint n_construct_properties,
- GObjectConstructParam *construct_params)
-{
- GObject *object;
-
- /* Last ditch effort here */
- gtk_init (0, NULL);
-
- object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
- n_construct_properties,
- construct_params);
-
- return object;
-}
-
-static void
-gtk_application_class_init (GtkApplicationClass *klass)
-{
- GObjectClass *gobject_class;
- GApplicationClass *application_class;
-
- gobject_class = G_OBJECT_CLASS (klass);
- application_class = G_APPLICATION_CLASS (klass);
-
- gobject_class->constructor = gtk_application_constructor;
- gobject_class->get_property = gtk_application_get_property;
- gobject_class->set_property = gtk_application_set_property;
-
- application_class->run = gtk_application_default_run;
- application_class->quit = gtk_application_default_quit;
- application_class->action = gtk_application_default_action;
- application_class->prepare_activation = gtk_application_default_prepare_activation;
-
- klass->activated = gtk_application_default_activated;
-
- /**
- * GtkApplication::activated:
- * @arguments: A #GVariant with the signature "aay"
- *
- * This signal is emitted when a non-primary process for a given
- * application is invoked while your application is running; for
- * example, when a file browser launches your program to open a
- * file. The raw operating system arguments are passed in the
- * variant @arguments.
- */
-
- gtk_application_signals[ACTIVATED] =
- g_signal_new (g_intern_static_string ("activated"),
- G_OBJECT_CLASS_TYPE (klass),
- G_SIGNAL_RUN_LAST,
- G_STRUCT_OFFSET (GtkApplicationClass, activated),
- NULL, NULL,
- g_cclosure_marshal_VOID__BOXED,
- G_TYPE_NONE, 1,
- G_TYPE_VARIANT);
-
- g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
-}
-
-#define __GTK_APPLICATION_C__
-#include "gtkaliasdef.c"