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>
63 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
65 struct _GtkApplicationPrivate
71 gtk_application_startup (GApplication *application)
77 gtk_application_quit_mainloop (GApplication *application)
83 gtk_application_run_mainloop (GApplication *application)
89 gtk_application_add_platform_data (GApplication *application,
90 GVariantBuilder *builder)
92 const gchar *startup_id;
94 startup_id = getenv ("DESKTOP_STARTUP_ID");
96 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
97 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
98 g_variant_new_string (startup_id));
102 gtk_application_before_emit (GApplication *application,
103 GVariant *platform_data)
109 g_variant_iter_init (&iter, platform_data);
110 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
112 #ifdef GDK_WINDOWING_X11
113 if (strcmp (key, "desktop-startup-id") == 0)
118 display = gdk_display_get_default ();
119 id = g_variant_get_string (value, NULL);
120 gdk_x11_display_set_startup_notification_id (display, id);
127 gtk_application_after_emit (GApplication *application,
128 GVariant *platform_data)
130 gdk_notify_startup_complete ();
134 gtk_application_init (GtkApplication *application)
136 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
137 GTK_TYPE_APPLICATION,
138 GtkApplicationPrivate);
142 gtk_application_class_init (GtkApplicationClass *class)
144 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
146 application_class->add_platform_data = gtk_application_add_platform_data;
147 application_class->before_emit = gtk_application_before_emit;
148 application_class->after_emit = gtk_application_after_emit;
149 application_class->startup = gtk_application_startup;
151 application_class->quit_mainloop = gtk_application_quit_mainloop;
152 application_class->run_mainloop = gtk_application_run_mainloop;
154 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
158 * gtk_application_new:
159 * @application_id: the application id
160 * @flags: the application flags
162 * Creates a new #GtkApplication instance.
164 * This function calls g_type_init() for you. gtk_init() is called
165 * as soon as the application gets registered as the primary instance.
167 * The application id must be valid. See g_application_id_is_valid().
169 * Returns: a new #GtkApplication instance
172 gtk_application_new (const gchar *application_id,
173 GApplicationFlags flags)
175 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
179 return g_object_new (GTK_TYPE_APPLICATION,
180 "application-id", application_id,
186 * gtk_application_add_window:
187 * @application: a #GtkApplication
188 * @window: a #GtkWindow
190 * Adds a window from @application.
192 * This call is equivalent to setting the #GtkWindow:application
193 * property of @window to @application.
198 gtk_application_add_window (GtkApplication *application,
201 GtkApplicationPrivate *priv;
203 g_return_if_fail (GTK_IS_APPLICATION (application));
205 priv = application->priv;
207 if (!g_list_find (priv->windows, window))
209 priv->windows = g_list_prepend (priv->windows, window);
210 gtk_window_set_application (window, application);
211 g_application_hold (G_APPLICATION (application));
216 * gtk_application_remove_window:
217 * @application: a #GtkApplication
218 * @window: a #GtkWindow
220 * Remove a window from @application.
222 * If @window belongs to @application then this call is equivalent to
223 * setting the #GtkWindow:application property of @window to
226 * The application may stop running as a result of a call to this
232 gtk_application_remove_window (GtkApplication *application,
235 GtkApplicationPrivate *priv;
237 g_return_if_fail (GTK_IS_APPLICATION (application));
239 priv = application->priv;
240 if (g_list_find (priv->windows, window))
242 priv->windows = g_list_remove (priv->windows, window);
243 g_application_release (G_APPLICATION (application));
244 gtk_window_set_application (window, NULL);
249 * gtk_application_get_windows:
250 * @application: a #GtkApplication
252 * Gets a list of the #GtkWindow<!-- -->s associated with @application.
254 * The list that is returned should not be modified in any way.
256 * Returns: a #GList of #GtkWindow
261 gtk_application_get_windows (GtkApplication *application)
263 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
265 return application->priv->windows;