]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
gdk/x11: Add gdk_x11_device_manager_lookup()
[~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   gtk_init (0, 0);
101 }
102
103 static void
104 gtk_application_quit_mainloop (GApplication *application)
105 {
106   gtk_main_quit ();
107 }
108
109 static void
110 gtk_application_run_mainloop (GApplication *application)
111 {
112   gtk_main ();
113 }
114
115 static void
116 gtk_application_add_platform_data (GApplication    *application,
117                                    GVariantBuilder *builder)
118 {
119   const gchar *startup_id;
120
121   startup_id = getenv ("DESKTOP_STARTUP_ID");
122   
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));
126 }
127
128 static void
129 gtk_application_before_emit (GApplication *application,
130                              GVariant     *platform_data)
131 {
132   GVariantIter iter;
133   const gchar *key;
134   GVariant *value;
135
136   g_variant_iter_init (&iter, platform_data);
137   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
138     {
139 #ifdef GDK_WINDOWING_X11
140       if (strcmp (key, "desktop-startup-id") == 0)
141         {
142           GdkDisplay *display;
143           const gchar *id;
144
145           display = gdk_display_get_default ();
146           id = g_variant_get_string (value, NULL);
147           gdk_x11_display_set_startup_notification_id (display, id);
148        }
149 #endif
150     }
151 }
152
153 static void
154 gtk_application_after_emit (GApplication *application,
155                             GVariant     *platform_data)
156 {
157   gdk_notify_startup_complete ();
158 }
159
160 static void
161 gtk_application_init (GtkApplication *application)
162 {
163   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
164                                                    GTK_TYPE_APPLICATION,
165                                                    GtkApplicationPrivate);
166 }
167
168 static void
169 gtk_application_window_added (GtkApplication *application,
170                               GtkWindow      *window)
171 {
172   GtkApplicationPrivate *priv = application->priv;
173
174   priv->windows = g_list_prepend (priv->windows, window);
175   gtk_window_set_application (window, application);
176   g_application_hold (G_APPLICATION (application));
177
178   g_signal_connect (window, "focus-in-event",
179                     G_CALLBACK (gtk_application_focus_in_event_cb),
180                     application);
181 }
182
183 static void
184 gtk_application_window_removed (GtkApplication *application,
185                                 GtkWindow      *window)
186 {
187   GtkApplicationPrivate *priv = application->priv;
188
189   g_signal_handlers_disconnect_by_func (window,
190                                         gtk_application_focus_in_event_cb,
191                                         application);
192
193   g_application_release (G_APPLICATION (application));
194   priv->windows = g_list_remove (priv->windows, window);
195   gtk_window_set_application (window, NULL);
196 }
197
198 static void
199 gtk_application_class_init (GtkApplicationClass *class)
200 {
201   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
202
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;
207
208   application_class->quit_mainloop = gtk_application_quit_mainloop;
209   application_class->run_mainloop = gtk_application_run_mainloop;
210
211   class->window_added = gtk_application_window_added;
212   class->window_removed = gtk_application_window_removed;
213
214   g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
215
216   /**
217    * GtkApplication::window-added:
218    * @application: the #GtkApplication which emitted the signal
219    * @window: the newly-added #GtkWindow
220    *
221    * Emitted when a #GtkWindow is added to @application through
222    * gtk_application_add_wi!ndow().
223    *
224    * Since: 3.2
225    */
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),
229                   NULL, NULL,
230                   g_cclosure_marshal_VOID__OBJECT,
231                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
232
233   /**
234    * GtkApplication::window-removed:
235    * @application: the #GtkApplication which emitted the signal
236    * @window: the #GtkWindow that is being removed
237    *
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().
241    *
242    * Since: 3.2
243    */
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),
247                   NULL, NULL,
248                   g_cclosure_marshal_VOID__OBJECT,
249                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
250 }
251
252 /**
253  * gtk_application_new:
254  * @application_id: the application id
255  * @flags: the application flags
256  *
257  * Creates a new #GtkApplication instance.
258  *
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.
261  *
262  * The application id must be valid. See g_application_id_is_valid().
263  *
264  * Returns: a new #GtkApplication instance
265  */
266 GtkApplication *
267 gtk_application_new (const gchar       *application_id,
268                      GApplicationFlags  flags)
269 {
270   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
271
272   g_type_init ();
273
274   return g_object_new (GTK_TYPE_APPLICATION,
275                        "application-id", application_id,
276                        "flags", flags,
277                        NULL);
278 }
279
280 /**
281  * gtk_application_add_window:
282  * @application: a #GtkApplication
283  * @window: a #GtkWindow
284  *
285  * Adds a window from @application.
286  *
287  * This call is equivalent to setting the #GtkWindow:application
288  * property of @window to @application.
289  *
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().
293  *
294  * GTK+ will keep the application running as long as it has
295  * any windows.
296  *
297  * Since: 3.0
298  **/
299 void
300 gtk_application_add_window (GtkApplication *application,
301                             GtkWindow      *window)
302 {
303   g_return_if_fail (GTK_IS_APPLICATION (application));
304
305   if (!g_list_find (application->priv->windows, window))
306     g_signal_emit (application,
307                    gtk_application_signals[WINDOW_ADDED], 0, window);
308 }
309
310 /**
311  * gtk_application_remove_window:
312  * @application: a #GtkApplication
313  * @window: a #GtkWindow
314  *
315  * Remove a window from @application.
316  *
317  * If @window belongs to @application then this call is equivalent to
318  * setting the #GtkWindow:application property of @window to
319  * %NULL.
320  *
321  * The application may stop running as a result of a call to this
322  * function.
323  *
324  * Since: 3.0
325  **/
326 void
327 gtk_application_remove_window (GtkApplication *application,
328                                GtkWindow      *window)
329 {
330   g_return_if_fail (GTK_IS_APPLICATION (application));
331
332   if (g_list_find (application->priv->windows, window))
333     g_signal_emit (application,
334                    gtk_application_signals[WINDOW_REMOVED], 0, window);
335 }
336
337 /**
338  * gtk_application_get_windows:
339  * @application: a #GtkApplication
340  *
341  * Gets a list of the #GtkWindow<!-- -->s associated with @application.
342  *
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.)
346  *
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
349  * deletion.
350  *
351  * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
352  *
353  * Since: 3.0
354  **/
355 GList *
356 gtk_application_get_windows (GtkApplication *application)
357 {
358   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
359
360   return application->priv->windows;
361 }