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