]> Pileus Git - ~andy/gtk/blob - gdk/gdkapplaunchcontext.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gdk / gdkapplaunchcontext.c
1 /* gdkapplaunchcontext.c - Gtk+ implementation for GAppLaunchContext
2
3    Copyright (C) 2007 Red Hat, Inc.
4
5    The Gnome Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The Gnome 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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with this library. If not, see <http://www.gnu.org/licenses/>.
17
18    Author: Alexander Larsson <alexl@redhat.com>
19 */
20
21 #include "config.h"
22
23 #include "gdkapplaunchcontextprivate.h"
24 #include "gdkscreen.h"
25 #include "gdkintl.h"
26
27
28 /**
29  * SECTION:gdkapplaunchcontext
30  * @Short_description: Startup notification for applications
31  * @Title: Application launching
32  *
33  * GdkAppLaunchContext is an implementation of #GAppLaunchContext that
34  * handles launching an application in a graphical context. It provides
35  * startup notification and allows to launch applications on a specific
36  * screen or workspace.
37  * <example>
38  * <title>Launching an application</title>
39  * <informalexample>
40  * <programlisting>
41  * GdkAppLaunchContext *context;
42  *
43  * context = gdk_display_get_app_launch_context (display);
44  *
45  * gdk_app_launch_context_set_screen (screen);
46  * gdk_app_launch_context_set_timestamp (event->time);
47  *
48  * if (!g_app_info_launch_default_for_uri ("http://www.gtk.org", context, &error))
49  *   g_warning ("Launching failed: %s\n", error->message);
50  *
51  * g_object_unref (context);
52  * </programlisting>
53  * </informalexample>
54  * </example>
55  */
56
57
58 static void    gdk_app_launch_context_finalize    (GObject           *object);
59 static gchar * gdk_app_launch_context_get_display (GAppLaunchContext *context,
60                                                    GAppInfo          *info,
61                                                    GList             *files);
62 static gchar * gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
63                                                              GAppInfo          *info,
64                                                              GList             *files);
65 static void    gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
66                                                      const gchar       *startup_notify_id);
67
68
69 enum
70 {
71   PROP_0,
72   PROP_DISPLAY
73 };
74
75 G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
76
77 static void
78 gdk_app_launch_context_get_property (GObject    *object,
79                                      guint       prop_id,
80                                      GValue     *value,
81                                      GParamSpec *pspec)
82 {
83   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
84
85   switch (prop_id)
86     {
87     case PROP_DISPLAY:
88       g_value_set_object (value, context->display);
89       break;
90     default:
91       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92     }
93 }
94
95 static void
96 gdk_app_launch_context_set_property (GObject      *object,
97                                      guint         prop_id,
98                                      const GValue *value,
99                                      GParamSpec   *pspec)
100 {
101   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
102
103   switch (prop_id)
104     {
105     case PROP_DISPLAY:
106       context->display = g_value_dup_object (value);
107       break;
108     default:
109       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110     }
111 }
112
113 static void
114 gdk_app_launch_context_class_init (GdkAppLaunchContextClass *klass)
115 {
116   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117   GAppLaunchContextClass *context_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);
118
119   gobject_class->set_property = gdk_app_launch_context_set_property,
120   gobject_class->get_property = gdk_app_launch_context_get_property;
121
122   gobject_class->finalize = gdk_app_launch_context_finalize;
123
124   context_class->get_display = gdk_app_launch_context_get_display;
125   context_class->get_startup_notify_id = gdk_app_launch_context_get_startup_notify_id;
126   context_class->launch_failed = gdk_app_launch_context_launch_failed;
127
128   g_object_class_install_property (gobject_class, PROP_DISPLAY,
129     g_param_spec_object ("display", P_("Display"), P_("Display"),
130                          GDK_TYPE_DISPLAY,
131                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
132 }
133
134 static void
135 gdk_app_launch_context_init (GdkAppLaunchContext *context)
136 {
137   context->workspace = -1;
138 }
139
140 static void
141 gdk_app_launch_context_finalize (GObject *object)
142 {
143   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
144
145   if (context->display)
146     g_object_unref (context->display);
147
148   if (context->screen)
149     g_object_unref (context->screen);
150
151   if (context->icon)
152     g_object_unref (context->icon);
153
154   g_free (context->icon_name);
155
156   G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
157 }
158
159 static gchar *
160 gdk_app_launch_context_get_display (GAppLaunchContext *context,
161                                     GAppInfo          *info,
162                                     GList             *files)
163 {
164   GdkAppLaunchContext *ctx = GDK_APP_LAUNCH_CONTEXT (context);
165   GdkDisplay *display;
166
167   if (ctx->screen)
168     return gdk_screen_make_display_name (ctx->screen);
169
170   if (ctx->display)
171     display = ctx->display;
172   else
173     display = gdk_display_get_default ();
174
175   return g_strdup (gdk_display_get_name (display));
176 }
177
178 /**
179  * gdk_app_launch_context_set_display:
180  * @context: a #GdkAppLaunchContext
181  * @display: a #GdkDisplay
182  *
183  * Sets the display on which applications will be launched when
184  * using this context. See also gdk_app_launch_context_set_screen().
185  *
186  * Since: 2.14
187  *
188  * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
189  */
190 void
191 gdk_app_launch_context_set_display (GdkAppLaunchContext *context,
192                                     GdkDisplay          *display)
193 {
194   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
195   g_return_if_fail (display == NULL || GDK_IS_DISPLAY (display));
196
197   g_warn_if_fail (display == NULL || display == context->display);
198 }
199
200 /**
201  * gdk_app_launch_context_set_screen:
202  * @context: a #GdkAppLaunchContext
203  * @screen: a #GdkScreen
204  *
205  * Sets the screen on which applications will be launched when
206  * using this context. See also gdk_app_launch_context_set_display().
207  *
208  * If both @screen and @display are set, the @screen takes priority.
209  * If neither @screen or @display are set, the default screen and
210  * display are used.
211  *
212  * Since: 2.14
213  */
214 void
215 gdk_app_launch_context_set_screen (GdkAppLaunchContext *context,
216                                    GdkScreen           *screen)
217 {
218   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
219   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
220
221   g_return_if_fail (screen == NULL || gdk_screen_get_display (screen) == context->display);
222
223   if (context->screen)
224     {
225       g_object_unref (context->screen);
226       context->screen = NULL;
227     }
228
229   if (screen)
230     context->screen = g_object_ref (screen);
231 }
232
233 /**
234  * gdk_app_launch_context_set_desktop:
235  * @context: a #GdkAppLaunchContext
236  * @desktop: the number of a workspace, or -1
237  *
238  * Sets the workspace on which applications will be launched when
239  * using this context when running under a window manager that
240  * supports multiple workspaces, as described in the
241  * <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
242  * Window Manager Hints</ulink>.
243  *
244  * When the workspace is not specified or @desktop is set to -1,
245  * it is up to the window manager to pick one, typically it will
246  * be the current workspace.
247  *
248  * Since: 2.14
249  */
250 void
251 gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
252                                     gint                 desktop)
253 {
254   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
255
256   context->workspace = desktop;
257 }
258
259 /**
260  * gdk_app_launch_context_set_timestamp:
261  * @context: a #GdkAppLaunchContext
262  * @timestamp: a timestamp
263  *
264  * Sets the timestamp of @context. The timestamp should ideally
265  * be taken from the event that triggered the launch.
266  *
267  * Window managers can use this information to avoid moving the
268  * focus to the newly launched application when the user is busy
269  * typing in another window. This is also known as 'focus stealing
270  * prevention'.
271  *
272  * Since: 2.14
273  */
274 void
275 gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
276                                       guint32              timestamp)
277 {
278   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
279
280   context->timestamp = timestamp;
281 }
282
283 /**
284  * gdk_app_launch_context_set_icon:
285  * @context: a #GdkAppLaunchContext
286  * @icon: (allow-none): a #GIcon, or %NULL
287  *
288  * Sets the icon for applications that are launched with this
289  * context.
290  *
291  * Window Managers can use this information when displaying startup
292  * notification.
293  *
294  * See also gdk_app_launch_context_set_icon_name().
295  *
296  * Since: 2.14
297  */
298 void
299 gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
300                                  GIcon               *icon)
301 {
302   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
303   g_return_if_fail (icon == NULL || G_IS_ICON (icon));
304
305   if (context->icon)
306     {
307       g_object_unref (context->icon);
308       context->icon = NULL;
309     }
310
311   if (icon)
312     context->icon = g_object_ref (icon);
313 }
314
315 /**
316  * gdk_app_launch_context_set_icon_name:
317  * @context: a #GdkAppLaunchContext
318  * @icon_name: (allow-none): an icon name, or %NULL
319  *
320  * Sets the icon for applications that are launched with this context.
321  * The @icon_name will be interpreted in the same way as the Icon field
322  * in desktop files. See also gdk_app_launch_context_set_icon().
323  *
324  * If both @icon and @icon_name are set, the @icon_name takes priority.
325  * If neither @icon or @icon_name is set, the icon is taken from either
326  * the file that is passed to launched application or from the #GAppInfo
327  * for the launched application itself.
328  *
329  * Since: 2.14
330  */
331 void
332 gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
333                                       const char          *icon_name)
334 {
335   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
336
337   g_free (context->icon_name);
338   context->icon_name = g_strdup (icon_name);
339 }
340
341 /**
342  * gdk_app_launch_context_new:
343  *
344  * Creates a new #GdkAppLaunchContext.
345  *
346  * Returns: a new #GdkAppLaunchContext
347  *
348  * Since: 2.14
349  *
350  * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
351  */
352 GdkAppLaunchContext *
353 gdk_app_launch_context_new (void)
354 {
355   return gdk_display_get_app_launch_context (gdk_display_get_default ());
356 }
357
358 static char *
359 gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
360                                               GAppInfo          *info,
361                                               GList             *files)
362 {
363  return NULL;
364 }
365
366 static void
367 gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
368                                       const gchar       *startup_notify_id)
369 {
370 }