]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
cc5de346d1a405041f0a07b3697af34695b06946
[~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 "gtkmarshalers.h"
38 #include "gtkintl.h"
39 #include "gtkprivate.h"
40
41 #include "gtkalias.h"
42
43 #include <gdk/gdk.h>
44 #ifdef GDK_WINDOWING_X11
45 #include <gdk/x11/gdkx.h>
46 #endif
47
48 /**
49  * SECTION:gtkapplication
50  * @title: GtkApplication
51  * @short_description: Application class
52  *
53  * #GtkApplication is a class that handles many important aspects
54  * of a GTK+ application in a convenient fashion, without enforcing
55  * a one-size-fits-all application model.
56  *
57  * Currently, GtkApplication handles application uniqueness, provides
58  * some basic scriptability by exporting 'actions', implements some
59  * standard actions itself (such as 'Quit') and provides a main window
60  * whose life-cycle is automatically tied to the life-cycle of your
61  * application.
62  *
63  * <example id="gtkapplication"><title>A simple application</title>
64  * <programlisting>
65  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
66  *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
67  * </xi:include>
68  * </programlisting>
69  * </example>
70  */
71 enum
72 {
73   PROP_0,
74
75   PROP_WINDOW
76 };
77
78 enum
79 {
80   ACTIVATED,
81   QUIT,
82   ACTION,
83
84   LAST_SIGNAL
85 };
86
87 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
88
89 struct _GtkApplicationPrivate
90 {
91   GtkActionGroup *main_actions;
92
93   GtkWindow *default_window;
94   GSList *windows;
95 };
96
97 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
98
99 static void
100 process_timestamp_from_platform_data (GVariant *platform_data)
101 {
102   /* TODO - extract timestamp from here, update GDK time */
103 }
104
105 static gboolean
106 gtk_application_default_quit (GtkApplication *application)
107 {
108   gtk_main_quit ();
109   return TRUE;
110 }
111
112 static gboolean
113 gtk_application_default_quit_with_data (GApplication *application,
114                                         GVariant     *platform_data)
115 {
116   gboolean result;
117
118   process_timestamp_from_platform_data (platform_data);
119   
120   g_signal_emit (application, gtk_application_signals[QUIT], 0, &result);
121
122   return result;
123 }
124
125 static void
126 gtk_application_default_run (GApplication *application)
127 {
128   gtk_main ();
129 }
130
131 static void
132 gtk_application_default_prepare_activation (GApplication *application,
133                                             GVariant     *arguments,
134                                             GVariant     *platform_data)
135 {
136   GVariantIter iter;
137   const gchar *key;
138   GVariant *value;
139
140   g_variant_iter_init (&iter, platform_data);
141   while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
142     {
143       if (strcmp (key, "startup-notification-id") == 0 &&
144           g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
145         gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
146       g_variant_unref (value);
147     }
148   
149   g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
150 }
151
152 static void
153 gtk_application_default_activated (GtkApplication *application,
154                                    GVariant       *arguments)
155 {
156   GtkApplicationPrivate *priv = application->priv;
157
158   /* TODO: should we raise the last focused window instead ? */
159   if (priv->default_window != NULL)
160     gtk_window_present (priv->default_window);
161 }
162
163 static void
164 gtk_application_default_action (GtkApplication *application,
165                                 const gchar    *action_name)
166 {
167   GtkApplicationPrivate *priv = application->priv;
168   GtkAction *action;
169
170   action = gtk_action_group_get_action (priv->main_actions, action_name);
171   if (action)
172     gtk_action_activate (action);
173 }
174
175 static GtkWindow *
176 gtk_application_default_create_window (GtkApplication *application)
177 {
178   return GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
179 }
180
181 static void
182 gtk_application_default_action_with_data (GApplication *application,
183                                           const gchar  *action_name,
184                                           GVariant     *platform_data)
185 {
186   process_timestamp_from_platform_data (platform_data);
187
188   g_signal_emit (application, gtk_application_signals[ACTION], g_quark_from_string (action_name));
189 }
190
191 static GVariant *
192 gtk_application_format_activation_data (void)
193 {
194   const gchar *startup_id = NULL;
195   GdkDisplay *display = gdk_display_get_default ();
196   GVariantBuilder builder;
197
198   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
199
200   /* try and get the startup notification id from GDK, the environment
201    * or, if everything else failed, fake one.
202    */
203 #ifdef GDK_WINDOWING_X11
204   startup_id = gdk_x11_display_get_startup_notification_id (display);
205 #endif /* GDK_WINDOWING_X11 */
206
207   if (startup_id)
208     g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
209                            g_variant_new ("s", startup_id));
210   return g_variant_builder_end (&builder);
211 }
212
213 /**
214  * gtk_application_new:
215  * @appid: System-dependent application identifier
216  * @argc: (allow-none) (inout): System argument count
217  * @argv: (allow-none) (inout): System argument vector
218  *
219  * Create a new #GtkApplication, or if one has already been initialized
220  * in this process, return the existing instance. This function will as
221  * a side effect initialize the display system; see gtk_init().
222  *
223  * For the behavior if this application is running in another process,
224  * see g_application_new().
225  *
226  * Returns: (transfer full): A newly-referenced #GtkApplication
227  *
228  * Since: 3.0
229  */
230 GtkApplication*
231 gtk_application_new (const gchar   *appid,
232                      gint          *argc,
233                      gchar       ***argv)
234 {
235   GtkApplication *app;
236   gint argc_for_app;
237   const gchar **argv_for_app;
238   GVariant *argv_variant;
239   GError *error = NULL;
240
241   gtk_init (argc, argv);
242
243   if (argc)
244     argc_for_app = *argc;
245   else
246     argc_for_app = 0;
247
248   if (argv)
249     argv_for_app = (const gchar **) *argv;
250   else
251     argv_for_app = NULL;
252
253   argv_variant = g_variant_new_bytestring_array (argv_for_app, argc_for_app);
254
255   app = g_initable_new (GTK_TYPE_APPLICATION, 
256                         NULL,
257                         &error,
258                         "application-id", appid, 
259                         "argv", argv_variant, 
260                         NULL);
261   if (app == NULL)
262     {
263       g_error ("%s", error->message);
264       g_clear_error (&error);
265       return NULL;
266     }
267
268   return app;
269 }
270
271 static void
272 on_action_sensitive (GtkAction      *action,
273                      GParamSpec     *pspec,
274                      GtkApplication *app)
275 {
276   g_application_set_action_enabled (G_APPLICATION (app),
277                                     gtk_action_get_name (action),
278                                     gtk_action_get_sensitive (action));
279 }
280
281 /**
282  * gtk_application_set_action_group:
283  * @app: A #GtkApplication
284  * @group: A #GtkActionGroup
285  *
286  * Set @group as this application's global action group.
287  * This will ensure the operating system interface uses
288  * these actions as follows:
289  *
290  * <itemizedlist>
291  *   <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
292  *   <listitem>In GNOME 3, this function populates the application menu.</listitem>
293  *   <listitem>In Windows prior to version 7, this function does nothing.</listitem>
294  *   <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
295  *   <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
296  * </itemizedlist>
297  *
298  * It is an error to call this function more than once.
299  *
300  * Since: 3.0
301  */
302 void
303 gtk_application_set_action_group (GtkApplication *app,
304                                   GtkActionGroup *group)
305 {
306   GList *actions, *iter;
307
308   g_return_if_fail (GTK_IS_APPLICATION (app));
309   g_return_if_fail (GTK_IS_ACTION_GROUP (group));
310   g_return_if_fail (app->priv->main_actions == NULL);
311
312   app->priv->main_actions = g_object_ref (group);
313   actions = gtk_action_group_list_actions (group);
314   for (iter = actions; iter; iter = iter->next)
315     {
316       GtkAction *action = iter->data;
317       g_application_add_action (G_APPLICATION (app),
318                                 gtk_action_get_name (action),
319                                 gtk_action_get_tooltip (action));
320       g_signal_connect (action, "notify::sensitive",
321                         G_CALLBACK (on_action_sensitive), app);
322     }
323   g_list_free (actions);
324 }
325
326 static gboolean
327 gtk_application_on_window_destroy (GtkWidget *window,
328                                    gpointer   user_data)
329 {
330   GtkApplication *app = GTK_APPLICATION (user_data);
331
332   app->priv->windows = g_slist_remove (app->priv->windows, window);
333
334   if (app->priv->windows == NULL)
335     gtk_application_quit (app);
336
337   return FALSE;
338 }
339
340 static gchar *default_title;
341
342 /**
343  * gtk_application_add_window:
344  * @app: a #GtkApplication
345  * @window: a toplevel window to add to @app
346  *
347  * Adds a window to the #GtkApplication.
348  *
349  * If the user closes all of the windows added to @app, the default
350  * behaviour is to call gtk_application_quit().
351  *
352  * If your application uses only a single toplevel window, you can
353  * use gtk_application_get_window(). If you are using a sub-class
354  * of #GtkApplication you should call gtk_application_create_window()
355  * to let the #GtkApplication instance create a #GtkWindow and add
356  * it to the list of toplevels of the application. You should call
357  * this function only to add #GtkWindow<!-- -->s that you created
358  * directly using gtk_window_new().
359  *
360  * Since: 3.0
361  */
362 void
363 gtk_application_add_window (GtkApplication *app,
364                             GtkWindow      *window)
365 {
366   GtkApplicationPrivate *priv;
367
368   g_return_if_fail (GTK_IS_APPLICATION (app));
369   g_return_if_fail (GTK_IS_WINDOW (window));
370
371   priv = app->priv;
372
373   if (g_slist_find (priv->windows, window) != NULL)
374     return;
375
376   priv->windows = g_slist_prepend (priv->windows, window);
377
378   if (priv->default_window == NULL)
379     priv->default_window = window;
380
381   if (gtk_window_get_title (window) == NULL && default_title != NULL)
382     gtk_window_set_title (window, default_title);
383
384   g_signal_connect (window, "destroy",
385                     G_CALLBACK (gtk_application_on_window_destroy),
386                     app);
387 }
388
389 /**
390  * gtk_application_get_window:
391  * @app: a #GtkApplication
392  *
393  * A simple #GtkApplication has a "default window". This window should
394  * act as the primary user interaction point with your application.
395  * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
396  * and its properties such as "title" and "icon-name" will be initialized
397  * as appropriate for the platform.
398  *
399  * If the user closes this window, and your application hasn't created
400  * any other windows, the default action will be to call gtk_application_quit().
401  *
402  * If your application has more than one toplevel window (e.g. an
403  * single-document-interface application with multiple open documents),
404  * or if you are constructing your toplevel windows yourself (e.g. using
405  * #GtkBuilder), use gtk_application_create_window() or
406  * gtk_application_add_window() instead.
407  *
408  * Returns: (transfer none): The default #GtkWindow for this application
409  *
410  * Since: 3.0
411  */
412 GtkWindow *
413 gtk_application_get_window (GtkApplication *app)
414 {
415   GtkApplicationPrivate *priv;
416
417   g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
418
419   priv = app->priv;
420
421   if (priv->default_window != NULL)
422     return priv->default_window;
423
424   return gtk_application_create_window (app);
425 }
426
427 /**
428  * gtk_application_create_window:
429  * @app: a #GtkApplication
430  *
431  * Creates a new #GtkWindow for the application.
432  *
433  * This function calls the #GtkApplication::create_window() virtual function,
434  * which can be overridden by sub-classes, for instance to use #GtkBuilder to
435  * create the user interface. After creating a new #GtkWindow instance, it will
436  * be added to the list of toplevels associated to the application.
437  *
438  * Return value: (transfer none): the newly created application #GtkWindow
439  *
440  * Since: 3.0
441  */
442 GtkWindow *
443 gtk_application_create_window (GtkApplication *app)
444 {
445   GtkWindow *window;
446
447   g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
448
449   window = GTK_APPLICATION_GET_CLASS (app)->create_window (app);
450   gtk_application_add_window (app, window);
451
452   return window;
453 }
454
455 /**
456  * gtk_application_run:
457  * @app: a #GtkApplication
458  *
459  * Runs the main loop; see g_application_run().
460  * The default implementation for #GtkApplication uses gtk_main().
461  *
462  * Since: 3.0
463  */
464 void
465 gtk_application_run (GtkApplication *app)
466 {
467   g_application_run (G_APPLICATION (app));
468 }
469
470 /**
471  * gtk_application_quit:
472  * @app: a #GtkApplication
473  *
474  * Request the application exit.  This function invokes
475  * g_application_quit_with_data(), which normally will
476  * in turn cause @app to emit #GtkApplication::quit.
477  *
478  * To control an application's quit behavior (for example, to ask for
479  * files to be saved), connect to the #GtkApplication::quit signal
480  * handler.
481  *
482  * Since: 3.0
483  */
484 void
485 gtk_application_quit (GtkApplication *app)
486 {
487   GVariantBuilder builder;
488   GVariant *platform_data;
489
490   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
491   g_variant_builder_add (&builder, "{sv}",
492                          "timestamp",
493                          g_variant_new ("u", gtk_get_current_event_time ()));
494   platform_data = g_variant_builder_end (&builder);
495
496   g_application_quit_with_data (G_APPLICATION (app), platform_data);
497
498   g_variant_unref (platform_data);
499 }
500
501 static void
502 gtk_application_get_property (GObject    *object,
503                               guint       prop_id,
504                               GValue     *value,
505                               GParamSpec *pspec)
506 {
507   GtkApplication *app = GTK_APPLICATION (object);
508
509   switch (prop_id)
510     {
511       case PROP_WINDOW:
512         g_value_set_object (value, gtk_application_get_window (app));
513         break;
514       default:
515         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516     }
517 }
518
519 static void
520 gtk_application_set_property (GObject      *object,
521                               guint         prop_id,
522                               const GValue *value,
523                               GParamSpec   *pspec)
524 {
525   GtkApplication *app = GTK_APPLICATION (object);
526
527   g_assert (app != NULL);
528
529   switch (prop_id)
530     {
531       default:
532         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
533     }
534 }
535
536 static void
537 setup_default_window_decorations (void)
538 {
539   const gchar *pid;
540   const gchar *filename;
541   GKeyFile *keyfile;
542   gchar *title;
543   gchar *icon_name;
544
545   pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
546   filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
547
548   keyfile = g_key_file_new ();
549
550   if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
551       g_key_file_load_from_file (keyfile, filename, 0, NULL))
552     {
553       title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
554       icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
555
556       if (default_title == NULL)
557         default_title = title;
558
559       if (gtk_window_get_default_icon_name () == NULL)
560         gtk_window_set_default_icon_name (icon_name);
561
562       g_free (icon_name);
563     }
564
565   g_key_file_free (keyfile);
566 }
567
568 static void
569 gtk_application_init (GtkApplication *application)
570 {
571   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
572
573   g_object_set (application, "platform-data", gtk_application_format_activation_data (), NULL);
574
575   setup_default_window_decorations ();
576 }
577
578
579 static GObject*
580 gtk_application_constructor (GType                  type,
581                              guint                  n_construct_properties,
582                              GObjectConstructParam *construct_params)
583 {
584   GObject *object;
585
586   /* Last ditch effort here */
587   gtk_init (0, NULL);
588
589   object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
590                                                                            n_construct_properties,
591                                                                            construct_params);
592
593   return object;
594 }
595
596 static void
597 gtk_application_class_init (GtkApplicationClass *klass)
598 {
599   GObjectClass *gobject_class;
600   GApplicationClass *application_class;
601
602   gobject_class = G_OBJECT_CLASS (klass);
603   application_class = G_APPLICATION_CLASS (klass);
604
605   gobject_class->constructor = gtk_application_constructor;
606   gobject_class->get_property = gtk_application_get_property;
607   gobject_class->set_property = gtk_application_set_property;
608
609   application_class->run = gtk_application_default_run;
610   application_class->quit_with_data = gtk_application_default_quit_with_data;
611   application_class->action_with_data = gtk_application_default_action_with_data;
612   application_class->prepare_activation = gtk_application_default_prepare_activation;
613
614   klass->quit = gtk_application_default_quit;
615   klass->action = gtk_application_default_action;
616   klass->create_window = gtk_application_default_create_window;
617
618   klass->activated = gtk_application_default_activated;
619
620   /**
621    * GtkApplication::activated:
622    * @arguments: A #GVariant with the signature "aay"
623    *
624    * This signal is emitted when a non-primary process for a given
625    * application is invoked while your application is running; for
626    * example, when a file browser launches your program to open a
627    * file.  The raw operating system arguments are passed in the
628    * variant @arguments.
629    */
630
631   gtk_application_signals[ACTIVATED] =
632     g_signal_new (g_intern_static_string ("activated"),
633                   G_OBJECT_CLASS_TYPE (klass),
634                   G_SIGNAL_RUN_LAST,
635                   G_STRUCT_OFFSET (GtkApplicationClass, activated),
636                   NULL, NULL,
637                   g_cclosure_marshal_VOID__VARIANT,
638                   G_TYPE_NONE, 1,
639                   G_TYPE_VARIANT);
640
641   /**
642    * GtkApplication::quit:
643    * @application: the object on which the signal is emitted
644    *
645    * This signal is emitted when a quit is initiated.  See also
646    * the #GApplication::quit-with-data signal which may in
647    * turn trigger this signal.
648    *
649    * The default handler for this signal exits the mainloop of the
650    * application.
651    *
652    * Returns: %TRUE if the signal has been handled, %FALSE to continue
653    *   signal emission
654    */
655   gtk_application_signals[QUIT] =
656     g_signal_new (g_intern_static_string ("quit"),
657                   G_OBJECT_CLASS_TYPE (klass),
658                   G_SIGNAL_RUN_LAST,
659                   G_STRUCT_OFFSET (GtkApplicationClass, quit),
660                   g_signal_accumulator_true_handled, NULL,
661                   _gtk_marshal_BOOLEAN__VOID,
662                   G_TYPE_BOOLEAN, 0);
663
664   /**
665    * GtkApplication::action:
666    * @application: the object on which the signal is emitted
667    * @name: The name of the activated action
668    *
669    * This signal is emitted when an action is activated. The action name
670    * is passed as the first argument, but also as signal detail, so it
671    * is possible to connect to this signal for individual actions.
672    *
673    * See also the #GApplication::action-with-data signal which may in
674    * turn trigger this signal.
675    *
676    * The signal is never emitted for disabled actions.
677    */
678   gtk_application_signals[ACTION] =
679     g_signal_new (g_intern_static_string ("action"),
680                   G_OBJECT_CLASS_TYPE (klass),
681                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
682                   G_STRUCT_OFFSET (GtkApplicationClass, action),
683                   NULL, NULL,
684                   g_cclosure_marshal_VOID__STRING,
685                   G_TYPE_NONE, 1,
686                   G_TYPE_STRING);
687                  
688   g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
689 }
690
691 #define __GTK_APPLICATION_C__
692 #include "gtkaliasdef.c"