]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
GtkApplication: drop Quit from the docs
[~andy/gtk] / gtk / gtkapplication.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 #include "gtkapplication.h"
29 #include "gtkmarshalers.h"
30 #include "gtkwindow.h"
31 #include "gtkmain.h"
32
33 #include <gdk/gdk.h>
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
36 #endif
37
38 /**
39  * SECTION:gtkapplication
40  * @title: GtkApplication
41  * @short_description: Application class
42  *
43  * #GtkApplication is a class that handles many important aspects
44  * of a GTK+ application in a convenient fashion, without enforcing
45  * a one-size-fits-all application model.
46  *
47  * Currently, GtkApplication handles GTK+ initialization, application
48  * uniqueness, provides some basic scriptability by exporting 'actions',
49  * and manages a list of toplevel windows whose life-cycle is automatically
50  * tied to the life-cycle of your application.
51  *
52  * <example id="gtkapplication"><title>A simple application</title>
53  * <programlisting>
54  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
55  *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
56  * </xi:include>
57  * </programlisting>
58  * </example>
59  */
60
61 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
62
63 struct _GtkApplicationPrivate
64 {
65   GList *windows;
66 };
67
68 static void
69 gtk_application_startup (GApplication *application)
70 {
71   gtk_init (0, 0);
72 }
73
74 static void
75 gtk_application_quit_mainloop (GApplication *application)
76 {
77   gtk_main_quit ();
78 }
79
80 static void
81 gtk_application_run_mainloop (GApplication *application)
82 {
83   gtk_main ();
84 }
85
86 static void
87 gtk_application_add_platform_data (GApplication    *application,
88                                    GVariantBuilder *builder)
89 {
90   const gchar *startup_id;
91
92   startup_id = getenv ("DESKTOP_STARTUP_ID");
93   
94   if (startup_id && g_utf8_validate (startup_id, -1, NULL))
95     g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
96                            g_variant_new_string (startup_id));
97 }
98
99 static void
100 gtk_application_before_emit (GApplication *application,
101                              GVariant     *platform_data)
102 {
103   GVariantIter iter;
104   const gchar *key;
105   GVariant *value;
106
107   g_variant_iter_init (&iter, platform_data);
108   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
109     {
110 #ifdef GDK_WINDOWING_X11
111       if (strcmp (key, "desktop-startup-id") == 0)
112         {
113           GdkDisplay *display;
114           const gchar *id;
115
116           display = gdk_display_get_default ();
117           id = g_variant_get_string (value, NULL);
118           gdk_x11_display_set_startup_notification_id (display, id);
119        }
120 #endif
121     }
122 }
123
124 static void
125 gtk_application_after_emit (GApplication *application,
126                             GVariant     *platform_data)
127 {
128   gdk_notify_startup_complete ();
129 }
130
131 static void
132 gtk_application_init (GtkApplication *application)
133 {
134   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
135                                                    GTK_TYPE_APPLICATION,
136                                                    GtkApplicationPrivate);
137 }
138
139 static void
140 gtk_application_class_init (GtkApplicationClass *class)
141 {
142   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
143
144   application_class->add_platform_data = gtk_application_add_platform_data;
145   application_class->before_emit = gtk_application_before_emit;
146   application_class->after_emit = gtk_application_after_emit;
147   application_class->startup = gtk_application_startup;
148
149   application_class->quit_mainloop = gtk_application_quit_mainloop;
150   application_class->run_mainloop = gtk_application_run_mainloop;
151
152   g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
153 }
154
155 /**
156  * gtk_application_new:
157  * @application_id: the application id
158  * @flags: the application flags
159  *
160  * Creates a new #GtkApplication instance.
161  *
162  * This function calls g_type_init() for you. gtk_init() is called
163  * as soon as the application gets registered as the primary instance.
164  *
165  * The application id must be valid. See g_application_id_is_valid().
166  *
167  * Returns: a new #GtkApplication instance
168  */
169 GtkApplication *
170 gtk_application_new (const gchar       *application_id,
171                      GApplicationFlags  flags)
172 {
173   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
174
175   g_type_init ();
176
177   return g_object_new (GTK_TYPE_APPLICATION,
178                        "application-id", application_id,
179                        "flags", flags,
180                        NULL);
181 }
182
183 /**
184  * gtk_application_add_window:
185  * @application: a #GtkApplication
186  * @window: a #GtkWindow
187  *
188  * Adds a window from @application.
189  *
190  * This call is equivalent to setting the #GtkWindow:application
191  * property of @window to @application.
192  *
193  * Since: 3.0
194  **/
195 void
196 gtk_application_add_window (GtkApplication *application,
197                             GtkWindow      *window)
198 {
199   GtkApplicationPrivate *priv;
200
201   g_return_if_fail (GTK_IS_APPLICATION (application));
202
203   priv = application->priv;
204
205   if (!g_list_find (priv->windows, window))
206     {
207       priv->windows = g_list_prepend (priv->windows, window);
208       gtk_window_set_application (window, application);
209       g_application_hold (G_APPLICATION (application));
210     }
211 }
212
213 /**
214  * gtk_application_remove_window:
215  * @application: a #GtkApplication
216  * @window: a #GtkWindow
217  *
218  * Remove a window from @application.
219  *
220  * If @window belongs to @application then this call is equivalent to
221  * setting the #GtkWindow:application property of @window to
222  * %NULL.
223  *
224  * The application may stop running as a result of a call to this
225  * function.
226  *
227  * Since: 3.0
228  **/
229 void
230 gtk_application_remove_window (GtkApplication *application,
231                                GtkWindow      *window)
232 {
233   GtkApplicationPrivate *priv;
234
235   g_return_if_fail (GTK_IS_APPLICATION (application));
236
237   priv = application->priv;
238   if (g_list_find (priv->windows, window))
239     {
240       priv->windows = g_list_remove (priv->windows, window);
241       g_application_release (G_APPLICATION (application));
242       gtk_window_set_application (window, NULL);
243     }
244 }
245
246 /**
247  * gtk_application_get_windows:
248  * @application: a #GtkApplication
249  *
250  * Gets a list of the #GtkWindow<!-- -->s associated with @application.
251  *
252  * The list that is returned should not be modified in any way.
253  *
254  * Returns: a #GList of #GtkWindow
255  *
256  * Since: 3.0
257  **/
258 GList *
259 gtk_application_get_windows (GtkApplication *application)
260 {
261   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
262
263   return application->priv->windows;
264 }