]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
Use gtk_action_group_get_action when looking up actions
[~andy/gtk] / gtk / gtkapplication.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Colin Walters <walters@verbum.org>
21  */
22
23 /*
24  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
25  * file for a list of people on the GTK+ Team.  See the ChangeLog
26  * files for a list of changes.  These files are distributed with
27  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28  */
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "gtkapplication.h"
36 #include "gtkmain.h"
37 #include "gtkintl.h"
38 #include "gtkprivate.h"
39
40 #include "gtkalias.h"
41
42 #include <gdk/gdk.h>
43 #ifdef GDK_WINDOWING_X11
44 #include <gdk/x11/gdkx.h>
45 #endif
46
47 /**
48  * SECTION:gtkapplication
49  * @title: GtkApplication
50  * @short_description: Application class
51  *
52  * #GtkApplication is a class that handles many important aspects
53  * of a GTK+ application in a convenient fashion, without enforcing
54  * a one-size-fits-all application model.
55  *
56  * Currently, GtkApplication handles application uniqueness, provides
57  * some basic scriptability by exporting 'actions', implements some
58  * standard actions itself (such as 'Quit') and provides a main window
59  * whose life-cycle is automatically tied to the life-cycle of your
60  * application.
61  *
62  * <example id="gtkapplication"><title>A simple application</title>
63  * <programlisting>
64  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
65  *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
66  * </xi:include>
67  * </programlisting>
68  * </example>
69  */
70 enum
71 {
72   PROP_0,
73   PROP_WINDOW
74 };
75
76 enum
77 {
78   ACTIVATED,
79
80   LAST_SIGNAL
81 };
82
83 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
84
85 struct _GtkApplicationPrivate
86 {
87   GtkActionGroup *main_actions;
88
89   GtkWindow *default_window;
90   GSList *windows;
91 };
92
93 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
94
95 static gboolean
96 gtk_application_default_quit (GApplication *application,
97                               guint         timestamp)
98 {
99   gtk_main_quit ();
100   return TRUE;
101 }
102
103 static void
104 gtk_application_default_run (GApplication *application)
105 {
106   gtk_main ();
107 }
108
109 static void
110 gtk_application_default_prepare_activation (GApplication *application,
111                                             GVariant     *arguments,
112                                             GVariant     *platform_data)
113 {
114   GVariantIter iter;
115   gchar *key;
116   GVariant *value;
117
118   g_variant_iter_init (&iter, platform_data);
119   while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
120     {
121       if (strcmp (key, "startup-notification-id") == 0 &&
122           g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
123         gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
124       g_variant_unref (value);
125     }
126   
127   g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
128 }
129
130 static void
131 gtk_application_default_activated (GApplication *application,
132                                    GVariant     *arguments)
133 {
134   GtkApplication *app = GTK_APPLICATION (application);
135
136   /* TODO: should we raise the last focused window instead ? */
137   if (app->priv->default_window != NULL)
138     gtk_window_present (app->priv->default_window);
139 }
140
141 static void
142 gtk_application_default_action (GApplication *application,
143                                 const gchar  *action_name,
144                                 guint         timestamp)
145 {
146   GtkApplication *app = GTK_APPLICATION (application);
147   GtkAction *action;
148
149   action = gtk_action_group_get_action (app->priv->main_actions, action_name);
150   if (action)
151     {
152       /* TODO set timestamp */
153       gtk_action_activate (action);
154     }
155 }
156
157 static GVariant *
158 gtk_application_format_activation_data (void)
159 {
160   const gchar *startup_id = NULL;
161   GdkDisplay *display = gdk_display_get_default ();
162   GVariantBuilder builder;
163
164   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
165
166   /* try and get the startup notification id from GDK, the environment
167    * or, if everything else failed, fake one.
168    */
169 #ifdef GDK_WINDOWING_X11
170   startup_id = gdk_x11_display_get_startup_notification_id (display);
171 #endif /* GDK_WINDOWING_X11 */
172
173   if (startup_id)
174     g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
175                            g_variant_new ("s", startup_id));
176   return g_variant_builder_end (&builder);
177 }
178
179 /**
180  * gtk_application_new:
181  * @argc: (allow-none) (inout): System argument count
182  * @argv: (allow-none) (inout): System argument vector
183  * @appid: System-dependent application identifier
184  *
185  * Create a new #GtkApplication, or if one has already been initialized
186  * in this process, return the existing instance. This function will as
187  * a side effect initialize the display system; see gtk_init().
188  *
189  * For the behavior if this application is running in another process,
190  * see g_application_new().
191  *
192  * Returns: (transfer full): A newly-referenced #GtkApplication
193  *
194  * Since: 3.0
195  */
196 GtkApplication*
197 gtk_application_new (gint          *argc,
198                      gchar       ***argv,
199                      const gchar   *appid)
200 {
201   GtkApplication *app;
202   gint argc_for_app;
203   gchar **argv_for_app;
204   GVariant *platform_data;
205
206   gtk_init (argc, argv);
207
208   if (argc)
209     argc_for_app = *argc;
210   else
211     argc_for_app = 0;
212   if (argv)
213     argv_for_app = *argv;
214   else
215     argv_for_app = NULL;
216
217   app = g_object_new (GTK_TYPE_APPLICATION, "application-id", appid, NULL);
218
219   platform_data = gtk_application_format_activation_data ();
220   g_application_register_with_data (G_APPLICATION (app), argc_for_app, argv_for_app,
221                                     platform_data);
222   g_variant_unref (platform_data);
223
224   return app;
225 }
226
227 static void
228 on_action_sensitive (GtkAction      *action,
229                      GParamSpec     *pspec,
230                      GtkApplication *app)
231 {
232   g_application_set_action_enabled (G_APPLICATION (app),
233                                     gtk_action_get_name (action),
234                                     gtk_action_get_sensitive (action));
235 }
236
237 /**
238  * gtk_application_set_action_group:
239  * @app: A #GtkApplication
240  * @group: A #GtkActionGroup
241  *
242  * Set @group as this application's global action group.
243  * This will ensure the operating system interface uses
244  * these actions as follows:
245  *
246  * <itemizedlist>
247  *   <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
248  *   <listitem>In GNOME 3, this function populates the application menu.</listitem>
249  *   <listitem>In Windows prior to version 7, this function does nothing.</listitem>
250  *   <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
251  *   <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
252  * </itemizedlist>
253  *
254  * It is an error to call this function more than once.
255  *
256  * Since: 3.0
257  */
258 void
259 gtk_application_set_action_group (GtkApplication *app,
260                                   GtkActionGroup *group)
261 {
262   GList *actions, *iter;
263
264   g_return_if_fail (GTK_IS_APPLICATION (app));
265   g_return_if_fail (app->priv->main_actions == NULL);
266
267   app->priv->main_actions = g_object_ref (group);
268   actions = gtk_action_group_list_actions (group);
269   for (iter = actions; iter; iter = iter->next)
270     {
271       GtkAction *action = iter->data;
272       g_application_add_action (G_APPLICATION (app),
273                                 gtk_action_get_name (action),
274                                 gtk_action_get_tooltip (action));
275       g_signal_connect (action, "notify::sensitive",
276                         G_CALLBACK (on_action_sensitive), app);
277     }
278   g_list_free (actions);
279 }
280
281 static gboolean
282 gtk_application_on_window_destroy (GtkWidget *window,
283                                    gpointer   user_data)
284 {
285   GtkApplication *app = GTK_APPLICATION (user_data);
286
287   app->priv->windows = g_slist_remove (app->priv->windows, window);
288
289   if (app->priv->windows == NULL)
290     gtk_application_quit (app);
291
292   return FALSE;
293 }
294
295 static gchar *default_title;
296
297 /**
298  * gtk_application_add_window:
299  * @app: a #GtkApplication
300  * @window: a toplevel window to add to @app
301  *
302  * Adds a window to the #GtkApplication.
303  *
304  * If the user closes all of the windows added to @app, the default
305  * behaviour is to call gtk_application_quit().
306  *
307  * If your application uses only a single toplevel window, you can
308  * use gtk_application_get_window().
309  *
310  * Since: 3.0
311  */
312 void
313 gtk_application_add_window (GtkApplication *app,
314                             GtkWindow      *window)
315 {
316   app->priv->windows = g_slist_prepend (app->priv->windows, window);
317
318   if (gtk_window_get_title (window) == NULL && default_title != NULL)
319     gtk_window_set_title (window, default_title);
320
321   g_signal_connect (window, "destroy",
322                     G_CALLBACK (gtk_application_on_window_destroy), app);
323 }
324
325 /**
326  * gtk_application_get_window:
327  * @app: a #GtkApplication
328  *
329  * A simple #GtkApplication has a "default window". This window should
330  * act as the primary user interaction point with your application.
331  * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
332  * and its properties such as "title" and "icon-name" will be initialized
333  * as appropriate for the platform.
334  *
335  * If the user closes this window, and your application hasn't created
336  * any other windows, the default action will be to call gtk_application_quit().
337  *
338  * If your application has more than one toplevel window (e.g. an
339  * single-document-interface application with multiple open documents),
340  * or if you are constructing your toplevel windows yourself (e.g. using
341  * #GtkBuilder), use gtk_application_add_window() instead.
342  *
343  * Returns: (transfer none): The default #GtkWindow for this application
344  *
345  * Since: 3.0
346  */
347 GtkWindow *
348 gtk_application_get_window (GtkApplication *app)
349 {
350   if (app->priv->default_window != NULL)
351     return app->priv->default_window;
352
353   app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
354   g_object_ref_sink (app->priv->default_window);
355
356   gtk_application_add_window (app, app->priv->default_window);
357
358   return app->priv->default_window;
359 }
360
361 /**
362  * gtk_application_run:
363  * @app: a #GtkApplication
364  *
365  * Runs the main loop; see g_application_run().
366  * The default implementation for #GtkApplication uses gtk_main().
367  *
368  * Since: 3.0
369  */
370 void
371 gtk_application_run (GtkApplication *app)
372 {
373   g_application_run (G_APPLICATION (app));
374 }
375
376 /**
377  * gtk_application_quit:
378  * @app: a #GtkApplication
379  *
380  * Request the application exit.
381  * By default, this method will exit the main loop; see gtk_main_quit().
382  *
383  * Since: 3.0
384  */
385 void
386 gtk_application_quit (GtkApplication *app)
387 {
388   g_application_quit (G_APPLICATION (app), gtk_get_current_event_time ());
389 }
390
391 static void
392 gtk_application_get_property (GObject    *object,
393                               guint       prop_id,
394                               GValue     *value,
395                               GParamSpec *pspec)
396 {
397   GtkApplication *app = GTK_APPLICATION (object);
398
399   switch (prop_id)
400     {
401       case PROP_WINDOW:
402         g_value_set_object (value, gtk_application_get_window (app));
403         break;
404       default:
405         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
406     }
407 }
408
409 static void
410 gtk_application_set_property (GObject      *object,
411                               guint         prop_id,
412                               const GValue *value,
413                               GParamSpec   *pspec)
414 {
415   GtkApplication *app = GTK_APPLICATION (object);
416
417   g_assert (app != NULL);
418
419   switch (prop_id)
420     {
421       default:
422         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
423     }
424 }
425
426 static void
427 setup_default_window_decorations (void)
428 {
429   const gchar *pid;
430   const gchar *filename;
431   GKeyFile *keyfile;
432   gchar *title;
433   gchar *icon_name;
434
435   pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
436   filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
437
438   keyfile = g_key_file_new ();
439
440   if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
441       g_key_file_load_from_file (keyfile, filename, 0, NULL))
442     {
443       title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
444       icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
445
446       if (default_title == NULL)
447         default_title = title;
448
449       if (gtk_window_get_default_icon_name () == NULL)
450         gtk_window_set_default_icon_name (icon_name);
451
452       g_free (icon_name);
453     }
454
455   g_key_file_free (keyfile);
456 }
457
458 static void
459 gtk_application_init (GtkApplication *application)
460 {
461   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
462
463   setup_default_window_decorations ();
464 }
465
466
467 static GObject*
468 gtk_application_constructor (GType                  type,
469                              guint                  n_construct_properties,
470                              GObjectConstructParam *construct_params)
471 {
472   GObject *object;
473
474   /* Last ditch effort here */
475   gtk_init (0, NULL);
476
477   object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
478                                                                            n_construct_properties,
479                                                                            construct_params);
480
481   return object;
482 }
483
484 static void
485 gtk_application_class_init (GtkApplicationClass *klass)
486 {
487   GObjectClass *gobject_class;
488   GApplicationClass *application_class;
489
490   gobject_class = G_OBJECT_CLASS (klass);
491   application_class = G_APPLICATION_CLASS (klass);
492
493   gobject_class->constructor = gtk_application_constructor;
494   gobject_class->get_property = gtk_application_get_property;
495   gobject_class->set_property = gtk_application_set_property;
496
497   application_class->run = gtk_application_default_run;
498   application_class->quit = gtk_application_default_quit;
499   application_class->action = gtk_application_default_action;
500   application_class->prepare_activation = gtk_application_default_prepare_activation;
501
502   klass->activated = gtk_application_default_activated;
503
504   /**
505    * GtkApplication::activated:
506    * @arguments: A #GVariant with the signature "aay"
507    *
508    * This signal is emitted when a non-primary process for a given
509    * application is invoked while your application is running; for
510    * example, when a file browser launches your program to open a
511    * file.  The raw operating system arguments are passed in the
512    * variant @arguments.
513    */
514
515   gtk_application_signals[ACTIVATED] =
516     g_signal_new (g_intern_static_string ("activated"),
517                   G_OBJECT_CLASS_TYPE (klass),
518                   G_SIGNAL_RUN_LAST,
519                   G_STRUCT_OFFSET (GtkApplicationClass, activated),
520                   NULL, NULL,
521                   g_cclosure_marshal_VOID__BOXED,
522                   G_TYPE_NONE, 1,
523                   G_TYPE_VARIANT);
524
525   g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
526 }
527
528 #define __GTK_APPLICATION_C__
529 #include "gtkaliasdef.c"