]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
c192d75e0b1c04f6917cfe99effdffd7e05fc9ce
[~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 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <string.h>
29
30 #include "gtkapplication.h"
31 #include "gtkmarshalers.h"
32 #include "gtkwindow.h"
33 #include "gtkmain.h"
34
35 #include <gdk/gdk.h>
36 #ifdef GDK_WINDOWING_X11
37 #include <gdk/x11/gdkx.h>
38 #endif
39
40 /**
41  * SECTION:gtkapplication
42  * @title: GtkApplication
43  * @short_description: Application class
44  *
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.
48  *
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.
53  *
54  * <example id="gtkapplication"><title>A simple application</title>
55  * <programlisting>
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>
58  * </xi:include>
59  * </programlisting>
60  * </example>
61  */
62
63 enum {
64   WINDOW_ADDED,
65   WINDOW_REMOVED,
66   LAST_SIGNAL
67 };
68
69 static guint gtk_application_signals[LAST_SIGNAL];
70
71 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
72
73 struct _GtkApplicationPrivate
74 {
75   GList *windows;
76 };
77
78 static gboolean
79 gtk_application_focus_in_event_cb (GtkWindow      *window,
80                                    GdkEventFocus  *event,
81                                    GtkApplication *application)
82 {
83   GtkApplicationPrivate *priv = application->priv;
84   GList *link;
85
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)
89     {
90       priv->windows = g_list_remove_link (priv->windows, link);
91       priv->windows = g_list_concat (link, priv->windows);
92     }
93
94   return FALSE;
95 }
96
97 static void
98 gtk_application_startup (GApplication *application)
99 {
100   G_APPLICATION_CLASS (gtk_application_parent_class)
101     ->startup (application);
102
103   gtk_init (0, 0);
104 }
105
106 static void
107 gtk_application_quit_mainloop (GApplication *application)
108 {
109   gtk_main_quit ();
110 }
111
112 static void
113 gtk_application_run_mainloop (GApplication *application)
114 {
115   gtk_main ();
116 }
117
118 static void
119 gtk_application_add_platform_data (GApplication    *application,
120                                    GVariantBuilder *builder)
121 {
122   const gchar *startup_id;
123
124   startup_id = getenv ("DESKTOP_STARTUP_ID");
125   
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));
129 }
130
131 static void
132 gtk_application_before_emit (GApplication *application,
133                              GVariant     *platform_data)
134 {
135   GVariantIter iter;
136   const gchar *key;
137   GVariant *value;
138
139   g_variant_iter_init (&iter, platform_data);
140   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
141     {
142 #ifdef GDK_WINDOWING_X11
143       if (strcmp (key, "desktop-startup-id") == 0)
144         {
145           GdkDisplay *display;
146           const gchar *id;
147
148           display = gdk_display_get_default ();
149           id = g_variant_get_string (value, NULL);
150           gdk_x11_display_set_startup_notification_id (display, id);
151        }
152 #endif
153     }
154 }
155
156 static void
157 gtk_application_after_emit (GApplication *application,
158                             GVariant     *platform_data)
159 {
160   gdk_notify_startup_complete ();
161 }
162
163 static void
164 gtk_application_init (GtkApplication *application)
165 {
166   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
167                                                    GTK_TYPE_APPLICATION,
168                                                    GtkApplicationPrivate);
169 }
170
171 static void
172 gtk_application_window_added (GtkApplication *application,
173                               GtkWindow      *window)
174 {
175   GtkApplicationPrivate *priv = application->priv;
176
177   priv->windows = g_list_prepend (priv->windows, window);
178   gtk_window_set_application (window, application);
179   g_application_hold (G_APPLICATION (application));
180
181   g_signal_connect (window, "focus-in-event",
182                     G_CALLBACK (gtk_application_focus_in_event_cb),
183                     application);
184 }
185
186 static void
187 gtk_application_window_removed (GtkApplication *application,
188                                 GtkWindow      *window)
189 {
190   GtkApplicationPrivate *priv = application->priv;
191
192   g_signal_handlers_disconnect_by_func (window,
193                                         gtk_application_focus_in_event_cb,
194                                         application);
195
196   g_application_release (G_APPLICATION (application));
197   priv->windows = g_list_remove (priv->windows, window);
198   gtk_window_set_application (window, NULL);
199 }
200
201 static void
202 gtk_application_class_init (GtkApplicationClass *class)
203 {
204   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
205
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;
210
211   application_class->quit_mainloop = gtk_application_quit_mainloop;
212   application_class->run_mainloop = gtk_application_run_mainloop;
213
214   class->window_added = gtk_application_window_added;
215   class->window_removed = gtk_application_window_removed;
216
217   g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
218
219   /**
220    * GtkApplication::window-added:
221    * @application: the #GtkApplication which emitted the signal
222    * @window: the newly-added #GtkWindow
223    *
224    * Emitted when a #GtkWindow is added to @application through
225    * gtk_application_add_wi!ndow().
226    *
227    * Since: 3.2
228    */
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),
232                   NULL, NULL,
233                   g_cclosure_marshal_VOID__OBJECT,
234                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
235
236   /**
237    * GtkApplication::window-removed:
238    * @application: the #GtkApplication which emitted the signal
239    * @window: the #GtkWindow that is being removed
240    *
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().
244    *
245    * Since: 3.2
246    */
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),
250                   NULL, NULL,
251                   g_cclosure_marshal_VOID__OBJECT,
252                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
253 }
254
255 /**
256  * gtk_application_new:
257  * @application_id: the application id
258  * @flags: the application flags
259  *
260  * Creates a new #GtkApplication instance.
261  *
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.
264  *
265  * The application id must be valid. See g_application_id_is_valid().
266  *
267  * Returns: a new #GtkApplication instance
268  */
269 GtkApplication *
270 gtk_application_new (const gchar       *application_id,
271                      GApplicationFlags  flags)
272 {
273   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
274
275   g_type_init ();
276
277   return g_object_new (GTK_TYPE_APPLICATION,
278                        "application-id", application_id,
279                        "flags", flags,
280                        NULL);
281 }
282
283 /**
284  * gtk_application_add_window:
285  * @application: a #GtkApplication
286  * @window: a #GtkWindow
287  *
288  * Adds a window from @application.
289  *
290  * This call is equivalent to setting the #GtkWindow:application
291  * property of @window to @application.
292  *
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().
296  *
297  * GTK+ will keep the application running as long as it has
298  * any windows.
299  *
300  * Since: 3.0
301  **/
302 void
303 gtk_application_add_window (GtkApplication *application,
304                             GtkWindow      *window)
305 {
306   g_return_if_fail (GTK_IS_APPLICATION (application));
307
308   if (!g_list_find (application->priv->windows, window))
309     g_signal_emit (application,
310                    gtk_application_signals[WINDOW_ADDED], 0, window);
311 }
312
313 /**
314  * gtk_application_remove_window:
315  * @application: a #GtkApplication
316  * @window: a #GtkWindow
317  *
318  * Remove a window from @application.
319  *
320  * If @window belongs to @application then this call is equivalent to
321  * setting the #GtkWindow:application property of @window to
322  * %NULL.
323  *
324  * The application may stop running as a result of a call to this
325  * function.
326  *
327  * Since: 3.0
328  **/
329 void
330 gtk_application_remove_window (GtkApplication *application,
331                                GtkWindow      *window)
332 {
333   g_return_if_fail (GTK_IS_APPLICATION (application));
334
335   if (g_list_find (application->priv->windows, window))
336     g_signal_emit (application,
337                    gtk_application_signals[WINDOW_REMOVED], 0, window);
338 }
339
340 /**
341  * gtk_application_get_windows:
342  * @application: a #GtkApplication
343  *
344  * Gets a list of the #GtkWindow<!-- -->s associated with @application.
345  *
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.)
349  *
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
352  * deletion.
353  *
354  * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
355  *
356  * Since: 3.0
357  **/
358 GList *
359 gtk_application_get_windows (GtkApplication *application)
360 {
361   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
362
363   return application->priv->windows;
364 }