2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gtkapplication.h"
31 #include "gtkmarshalers.h"
32 #include "gtkwindow.h"
36 #ifdef GDK_WINDOWING_X11
37 #include <gdk/x11/gdkx.h>
41 * SECTION:gtkapplication
42 * @title: GtkApplication
43 * @short_description: Application class
45 * #GtkApplication is a class that handles many important aspects
46 * of a GTK+ application in a convenient fashion, without enforcing
47 * a one-size-fits-all application model.
49 * Currently, GtkApplication handles GTK+ initialization, application
50 * uniqueness, provides some basic scriptability by exporting 'actions',
51 * and manages a list of toplevel windows whose life-cycle is automatically
52 * tied to the life-cycle of your application.
54 * <example id="gtkapplication"><title>A simple application</title>
56 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
57 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
69 static guint gtk_application_signals[LAST_SIGNAL];
71 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
73 struct _GtkApplicationPrivate
79 gtk_application_focus_in_event_cb (GtkWindow *window,
81 GtkApplication *application)
83 GtkApplicationPrivate *priv = application->priv;
86 /* Keep the window list sorted by most-recently-focused. */
87 link = g_list_find (priv->windows, window);
88 if (link != NULL && link != priv->windows)
90 priv->windows = g_list_remove_link (priv->windows, link);
91 priv->windows = g_list_concat (link, priv->windows);
98 gtk_application_startup (GApplication *application)
100 G_APPLICATION_CLASS (gtk_application_parent_class)
101 ->startup (application);
107 gtk_application_quit_mainloop (GApplication *application)
113 gtk_application_run_mainloop (GApplication *application)
119 gtk_application_add_platform_data (GApplication *application,
120 GVariantBuilder *builder)
122 const gchar *startup_id;
124 startup_id = getenv ("DESKTOP_STARTUP_ID");
126 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
127 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
128 g_variant_new_string (startup_id));
132 gtk_application_before_emit (GApplication *application,
133 GVariant *platform_data)
139 g_variant_iter_init (&iter, platform_data);
140 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
142 #ifdef GDK_WINDOWING_X11
143 if (strcmp (key, "desktop-startup-id") == 0)
148 display = gdk_display_get_default ();
149 id = g_variant_get_string (value, NULL);
150 gdk_x11_display_set_startup_notification_id (display, id);
157 gtk_application_after_emit (GApplication *application,
158 GVariant *platform_data)
160 gdk_notify_startup_complete ();
164 gtk_application_init (GtkApplication *application)
166 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
167 GTK_TYPE_APPLICATION,
168 GtkApplicationPrivate);
172 gtk_application_window_added (GtkApplication *application,
175 GtkApplicationPrivate *priv = application->priv;
177 priv->windows = g_list_prepend (priv->windows, window);
178 gtk_window_set_application (window, application);
179 g_application_hold (G_APPLICATION (application));
181 g_signal_connect (window, "focus-in-event",
182 G_CALLBACK (gtk_application_focus_in_event_cb),
187 gtk_application_window_removed (GtkApplication *application,
190 GtkApplicationPrivate *priv = application->priv;
192 g_signal_handlers_disconnect_by_func (window,
193 gtk_application_focus_in_event_cb,
196 g_application_release (G_APPLICATION (application));
197 priv->windows = g_list_remove (priv->windows, window);
198 gtk_window_set_application (window, NULL);
202 gtk_application_class_init (GtkApplicationClass *class)
204 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
206 application_class->add_platform_data = gtk_application_add_platform_data;
207 application_class->before_emit = gtk_application_before_emit;
208 application_class->after_emit = gtk_application_after_emit;
209 application_class->startup = gtk_application_startup;
211 application_class->quit_mainloop = gtk_application_quit_mainloop;
212 application_class->run_mainloop = gtk_application_run_mainloop;
214 class->window_added = gtk_application_window_added;
215 class->window_removed = gtk_application_window_removed;
217 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
220 * GtkApplication::window-added:
221 * @application: the #GtkApplication which emitted the signal
222 * @window: the newly-added #GtkWindow
224 * Emitted when a #GtkWindow is added to @application through
225 * gtk_application_add_wi!ndow().
229 gtk_application_signals[WINDOW_ADDED] =
230 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
231 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
233 g_cclosure_marshal_VOID__OBJECT,
234 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
237 * GtkApplication::window-removed:
238 * @application: the #GtkApplication which emitted the signal
239 * @window: the #GtkWindow that is being removed
241 * Emitted when a #GtkWindow is removed from @application,
242 * either as a side-effect of being destroyed or explicitly
243 * through gtk_application_remove_window().
247 gtk_application_signals[WINDOW_REMOVED] =
248 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
249 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
251 g_cclosure_marshal_VOID__OBJECT,
252 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
256 * gtk_application_new:
257 * @application_id: the application id
258 * @flags: the application flags
260 * Creates a new #GtkApplication instance.
262 * This function calls g_type_init() for you. gtk_init() is called
263 * as soon as the application gets registered as the primary instance.
265 * The application id must be valid. See g_application_id_is_valid().
267 * Returns: a new #GtkApplication instance
270 gtk_application_new (const gchar *application_id,
271 GApplicationFlags flags)
273 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
277 return g_object_new (GTK_TYPE_APPLICATION,
278 "application-id", application_id,
284 * gtk_application_add_window:
285 * @application: a #GtkApplication
286 * @window: a #GtkWindow
288 * Adds a window from @application.
290 * This call is equivalent to setting the #GtkWindow:application
291 * property of @window to @application.
293 * Normally, the connection between the application and the window
294 * will remain until the window is destroyed, but you can explicitly
295 * remove it with gtk_application_remove_window().
297 * GTK+ will keep the application running as long as it has
303 gtk_application_add_window (GtkApplication *application,
306 g_return_if_fail (GTK_IS_APPLICATION (application));
308 if (!g_list_find (application->priv->windows, window))
309 g_signal_emit (application,
310 gtk_application_signals[WINDOW_ADDED], 0, window);
314 * gtk_application_remove_window:
315 * @application: a #GtkApplication
316 * @window: a #GtkWindow
318 * Remove a window from @application.
320 * If @window belongs to @application then this call is equivalent to
321 * setting the #GtkWindow:application property of @window to
324 * The application may stop running as a result of a call to this
330 gtk_application_remove_window (GtkApplication *application,
333 g_return_if_fail (GTK_IS_APPLICATION (application));
335 if (g_list_find (application->priv->windows, window))
336 g_signal_emit (application,
337 gtk_application_signals[WINDOW_REMOVED], 0, window);
341 * gtk_application_get_windows:
342 * @application: a #GtkApplication
344 * Gets a list of the #GtkWindow<!-- -->s associated with @application.
346 * The list is sorted by most recently focused window, such that the first
347 * element is the currently focused window. (Useful for choosing a parent
348 * for a transient window.)
350 * The list that is returned should not be modified in any way. It will
351 * only remain valid until the next focus change or window creation or
354 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
359 gtk_application_get_windows (GtkApplication *application)
361 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
363 return application->priv->windows;