]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
GtkApplication rewrite
[~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 application uniqueness, provides
48  * some basic scriptability by exporting 'actions', implements some
49  * standard actions itself (such as 'Quit') and provides a main window
50  * whose life-cycle is automatically tied to the life-cycle of your
51  * 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 GtkApplication *
65 gtk_application_new (const gchar       *application_id,
66                      GApplicationFlags  flags)
67 {
68   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
69
70   g_type_init ();
71
72   return g_object_new (GTK_TYPE_APPLICATION,
73                        "application-id", application_id,
74                        "flags", flags,
75                        NULL);
76 }
77
78 static void
79 gtk_application_startup (GApplication *application)
80 {
81   gtk_init (0, 0);
82 }
83
84 static void
85 gtk_application_quit_mainloop (GApplication *application)
86 {
87   gtk_main_quit ();
88 }
89
90 static void
91 gtk_application_run_mainloop (GApplication *application)
92 {
93   gtk_main ();
94 }
95
96 static void
97 gtk_application_add_platform_data (GApplication    *application,
98                                    GVariantBuilder *builder)
99 {
100   const gchar *startup_id;
101
102   startup_id = getenv ("DESKTOP_STARTUP_ID");
103   
104   if (startup_id && g_utf8_validate (startup_id, -1, NULL))
105     g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
106                            g_variant_new_string (startup_id));
107 }
108
109 static void
110 gtk_application_before_emit (GApplication *application,
111                              GVariant     *platform_data)
112 {
113   GVariantIter iter;
114   const gchar *key;
115   GVariant *value;
116
117   g_variant_iter_init (&iter, platform_data);
118   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
119     {
120       if (strcmp (key, "desktop-startup-id") == 0)
121         {
122           GdkDisplay *display;
123           const gchar *id;
124
125           display = gdk_display_get_default ();
126           id = g_variant_get_string (value, NULL);
127           gdk_x11_display_set_startup_notification_id (display, id);
128        }
129     }
130 }
131
132 static void
133 gtk_application_after_emit (GApplication *application,
134                             GVariant     *platform_data)
135 {
136   gdk_notify_startup_complete ();
137 }
138
139 static void
140 gtk_application_init (GtkApplication *application)
141 {
142 }
143
144 static void
145 gtk_application_class_init (GtkApplicationClass *class)
146 {
147   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
148
149   application_class->add_platform_data = gtk_application_add_platform_data;
150   application_class->before_emit = gtk_application_before_emit;
151   application_class->after_emit = gtk_application_after_emit;
152   application_class->startup = gtk_application_startup;
153
154   application_class->quit_mainloop = gtk_application_quit_mainloop;
155   application_class->run_mainloop = gtk_application_run_mainloop;
156 }