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)
104 gtk_application_quit_mainloop (GApplication *application)
110 gtk_application_run_mainloop (GApplication *application)
116 gtk_application_add_platform_data (GApplication *application,
117 GVariantBuilder *builder)
119 const gchar *startup_id;
121 startup_id = getenv ("DESKTOP_STARTUP_ID");
123 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
124 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
125 g_variant_new_string (startup_id));
129 gtk_application_before_emit (GApplication *application,
130 GVariant *platform_data)
136 g_variant_iter_init (&iter, platform_data);
137 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
139 #ifdef GDK_WINDOWING_X11
140 if (strcmp (key, "desktop-startup-id") == 0)
145 display = gdk_display_get_default ();
146 id = g_variant_get_string (value, NULL);
147 gdk_x11_display_set_startup_notification_id (display, id);
154 gtk_application_after_emit (GApplication *application,
155 GVariant *platform_data)
157 gdk_notify_startup_complete ();
161 gtk_application_init (GtkApplication *application)
163 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
164 GTK_TYPE_APPLICATION,
165 GtkApplicationPrivate);
169 gtk_application_window_added (GtkApplication *application,
172 GtkApplicationPrivate *priv = application->priv;
174 priv->windows = g_list_prepend (priv->windows, window);
175 gtk_window_set_application (window, application);
176 g_application_hold (G_APPLICATION (application));
178 g_signal_connect (window, "focus-in-event",
179 G_CALLBACK (gtk_application_focus_in_event_cb),
184 gtk_application_window_removed (GtkApplication *application,
187 GtkApplicationPrivate *priv = application->priv;
189 g_signal_handlers_disconnect_by_func (window,
190 gtk_application_focus_in_event_cb,
193 g_application_release (G_APPLICATION (application));
194 priv->windows = g_list_remove (priv->windows, window);
195 gtk_window_set_application (window, NULL);
199 gtk_application_class_init (GtkApplicationClass *class)
201 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
203 application_class->add_platform_data = gtk_application_add_platform_data;
204 application_class->before_emit = gtk_application_before_emit;
205 application_class->after_emit = gtk_application_after_emit;
206 application_class->startup = gtk_application_startup;
208 application_class->quit_mainloop = gtk_application_quit_mainloop;
209 application_class->run_mainloop = gtk_application_run_mainloop;
211 class->window_added = gtk_application_window_added;
212 class->window_removed = gtk_application_window_removed;
214 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
217 * GtkApplication::window-added:
218 * @application: the #GtkApplication which emitted the signal
219 * @window: the newly-added #GtkWindow
221 * Emitted when a #GtkWindow is added to @application through
222 * gtk_application_add_wi!ndow().
226 gtk_application_signals[WINDOW_ADDED] =
227 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
228 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
230 g_cclosure_marshal_VOID__OBJECT,
231 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
234 * GtkApplication::window-removed:
235 * @application: the #GtkApplication which emitted the signal
236 * @window: the #GtkWindow that is being removed
238 * Emitted when a #GtkWindow is removed from @application,
239 * either as a side-effect of being destroyed or explicitly
240 * through gtk_application_remove_window().
244 gtk_application_signals[WINDOW_REMOVED] =
245 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
246 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
248 g_cclosure_marshal_VOID__OBJECT,
249 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
253 * gtk_application_new:
254 * @application_id: the application id
255 * @flags: the application flags
257 * Creates a new #GtkApplication instance.
259 * This function calls g_type_init() for you. gtk_init() is called
260 * as soon as the application gets registered as the primary instance.
262 * The application id must be valid. See g_application_id_is_valid().
264 * Returns: a new #GtkApplication instance
267 gtk_application_new (const gchar *application_id,
268 GApplicationFlags flags)
270 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
274 return g_object_new (GTK_TYPE_APPLICATION,
275 "application-id", application_id,
281 * gtk_application_add_window:
282 * @application: a #GtkApplication
283 * @window: a #GtkWindow
285 * Adds a window from @application.
287 * This call is equivalent to setting the #GtkWindow:application
288 * property of @window to @application.
290 * Normally, the connection between the application and the window
291 * will remain until the window is destroyed, but you can explicitly
292 * remove it with gtk_application_remove_window().
294 * GTK+ will keep the application running as long as it has
300 gtk_application_add_window (GtkApplication *application,
303 g_return_if_fail (GTK_IS_APPLICATION (application));
305 if (!g_list_find (application->priv->windows, window))
306 g_signal_emit (application,
307 gtk_application_signals[WINDOW_ADDED], 0, window);
311 * gtk_application_remove_window:
312 * @application: a #GtkApplication
313 * @window: a #GtkWindow
315 * Remove a window from @application.
317 * If @window belongs to @application then this call is equivalent to
318 * setting the #GtkWindow:application property of @window to
321 * The application may stop running as a result of a call to this
327 gtk_application_remove_window (GtkApplication *application,
330 g_return_if_fail (GTK_IS_APPLICATION (application));
332 if (g_list_find (application->priv->windows, window))
333 g_signal_emit (application,
334 gtk_application_signals[WINDOW_REMOVED], 0, window);
338 * gtk_application_get_windows:
339 * @application: a #GtkApplication
341 * Gets a list of the #GtkWindow<!-- -->s associated with @application.
343 * The list is sorted by most recently focused window, such that the first
344 * element is the currently focused window. (Useful for choosing a parent
345 * for a transient window.)
347 * The list that is returned should not be modified in any way. It will
348 * only remain valid until the next focus change or window creation or
351 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
356 gtk_application_get_windows (GtkApplication *application)
358 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
360 return application->priv->windows;