QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / glue / eggtrayicon.c
blob07ce5dfce04c138be7496f8edf493381359719b0
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* eggtrayicon.c
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
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.
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.
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.
21 #include <config.h>
22 #include <string.h>
23 #include <libintl.h>
25 #include "eggtrayicon.h"
27 #include <gdkconfig.h>
28 #if defined (GDK_WINDOWING_X11)
29 #include <gdk/gdkx.h>
30 #include <X11/Xatom.h>
31 #elif defined (GDK_WINDOWING_WIN32)
32 #include <gdk/gdkwin32.h>
33 #endif
35 #ifndef EGG_COMPILATION
36 #ifndef _
37 #define _(x) dgettext (GETTEXT_PACKAGE, x)
38 #define N_(x) x
39 #endif
40 #else
41 #define _(x) x
42 #define N_(x) x
43 #endif
45 #define SYSTEM_TRAY_REQUEST_DOCK 0
46 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
47 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
49 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
50 #define SYSTEM_TRAY_ORIENTATION_VERT 1
52 enum {
53 PROP_0,
54 PROP_ORIENTATION
57 static GtkPlugClass *parent_class = NULL;
59 static void egg_tray_icon_init (EggTrayIcon *icon);
60 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
62 static void egg_tray_icon_get_property (GObject *object,
63 guint prop_id,
64 GValue *value,
65 GParamSpec *pspec);
67 static void egg_tray_icon_realize (GtkWidget *widget);
68 static void egg_tray_icon_unrealize (GtkWidget *widget);
70 #ifdef GDK_WINDOWING_X11
71 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon,
72 gboolean dock_if_realized);
73 static void egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon);
74 #endif
76 GType
77 egg_tray_icon_get_type (void)
79 static GType our_type = 0;
81 if (our_type == 0)
83 static const GTypeInfo our_info =
85 sizeof (EggTrayIconClass),
86 (GBaseInitFunc) NULL,
87 (GBaseFinalizeFunc) NULL,
88 (GClassInitFunc) egg_tray_icon_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
91 sizeof (EggTrayIcon),
92 0, /* n_preallocs */
93 (GInstanceInitFunc) egg_tray_icon_init
96 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
99 return our_type;
102 static void
103 egg_tray_icon_init (EggTrayIcon *icon)
105 icon->stamp = 1;
106 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
108 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
111 static void
112 egg_tray_icon_class_init (EggTrayIconClass *klass)
114 GObjectClass *gobject_class = (GObjectClass *)klass;
115 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
117 parent_class = g_type_class_peek_parent (klass);
119 gobject_class->get_property = egg_tray_icon_get_property;
121 widget_class->realize = egg_tray_icon_realize;
122 widget_class->unrealize = egg_tray_icon_unrealize;
124 g_object_class_install_property (gobject_class,
125 PROP_ORIENTATION,
126 g_param_spec_enum ("orientation",
127 _("Orientation"),
128 _("The orientation of the tray."),
129 GTK_TYPE_ORIENTATION,
130 GTK_ORIENTATION_HORIZONTAL,
131 G_PARAM_READABLE));
133 #if defined (GDK_WINDOWING_X11)
134 /* Nothing */
135 #elif defined (GDK_WINDOWING_WIN32)
136 g_warning ("Port eggtrayicon to Win32");
137 #else
138 g_warning ("Port eggtrayicon to this GTK+ backend");
139 #endif
142 static void
143 egg_tray_icon_get_property (GObject *object,
144 guint prop_id,
145 GValue *value,
146 GParamSpec *pspec)
148 EggTrayIcon *icon = EGG_TRAY_ICON (object);
150 switch (prop_id)
152 case PROP_ORIENTATION:
153 g_value_set_enum (value, icon->orientation);
154 break;
155 default:
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 break;
161 #ifdef GDK_WINDOWING_X11
163 static void
164 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
166 Display *xdisplay;
167 Atom type;
168 int format;
169 union {
170 gulong *prop;
171 guchar *prop_ch;
172 } prop = { NULL };
173 gulong nitems;
174 gulong bytes_after;
175 int error, result;
177 g_assert (icon->manager_window != None);
179 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
181 gdk_error_trap_push ();
182 type = None;
183 result = XGetWindowProperty (xdisplay,
184 icon->manager_window,
185 icon->orientation_atom,
186 0, G_MAXLONG, FALSE,
187 XA_CARDINAL,
188 &type, &format, &nitems,
189 &bytes_after, &(prop.prop_ch));
190 error = gdk_error_trap_pop ();
192 if (error || result != Success)
193 return;
195 if (type == XA_CARDINAL)
197 GtkOrientation orientation;
199 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
200 GTK_ORIENTATION_HORIZONTAL :
201 GTK_ORIENTATION_VERTICAL;
203 if (icon->orientation != orientation)
205 icon->orientation = orientation;
207 g_object_notify (G_OBJECT (icon), "orientation");
211 if (prop.prop)
212 XFree (prop.prop);
215 static GdkFilterReturn
216 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
218 EggTrayIcon *icon = user_data;
219 XEvent *xev = (XEvent *)xevent;
221 if (xev->xany.type == ClientMessage &&
222 xev->xclient.message_type == icon->manager_atom &&
223 xev->xclient.data.l[1] == icon->selection_atom)
225 egg_tray_icon_update_manager_window (icon, TRUE);
227 else if (xev->xany.window == icon->manager_window)
229 if (xev->xany.type == PropertyNotify &&
230 xev->xproperty.atom == icon->orientation_atom)
232 egg_tray_icon_get_orientation_property (icon);
234 if (xev->xany.type == DestroyNotify)
236 egg_tray_icon_manager_window_destroyed (icon);
239 return GDK_FILTER_CONTINUE;
242 #endif
244 static void
245 egg_tray_icon_unrealize (GtkWidget *widget)
247 #ifdef GDK_WINDOWING_X11
248 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
249 GdkWindow *root_window;
251 if (icon->manager_window != None)
253 GdkWindow *gdkwin;
255 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
256 icon->manager_window);
258 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
261 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
263 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
265 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
266 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
267 #endif
270 #ifdef GDK_WINDOWING_X11
272 static void
273 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
274 long message,
275 Window window,
276 long data1,
277 long data2,
278 long data3)
280 XClientMessageEvent ev;
281 Display *display;
283 ev.type = ClientMessage;
284 ev.window = window;
285 ev.message_type = icon->system_tray_opcode_atom;
286 ev.format = 32;
287 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
288 ev.data.l[1] = message;
289 ev.data.l[2] = data1;
290 ev.data.l[3] = data2;
291 ev.data.l[4] = data3;
293 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
295 gdk_error_trap_push ();
296 XSendEvent (display,
297 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
298 XSync (display, False);
299 gdk_error_trap_pop ();
302 static void
303 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
305 egg_tray_icon_send_manager_message (icon,
306 SYSTEM_TRAY_REQUEST_DOCK,
307 icon->manager_window,
308 gtk_plug_get_id (GTK_PLUG (icon)),
309 0, 0);
312 static void
313 egg_tray_icon_update_manager_window (EggTrayIcon *icon,
314 gboolean dock_if_realized)
316 Display *xdisplay;
318 if (icon->manager_window != None)
319 return;
321 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
323 XGrabServer (xdisplay);
325 icon->manager_window = XGetSelectionOwner (xdisplay,
326 icon->selection_atom);
328 if (icon->manager_window != None)
329 XSelectInput (xdisplay,
330 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
332 XUngrabServer (xdisplay);
333 XFlush (xdisplay);
335 if (icon->manager_window != None)
337 GdkWindow *gdkwin;
339 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
340 icon->manager_window);
342 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
344 if (dock_if_realized && GTK_WIDGET_REALIZED (icon))
345 egg_tray_icon_send_dock_request (icon);
347 egg_tray_icon_get_orientation_property (icon);
351 static void
352 egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon)
354 GdkWindow *gdkwin;
356 g_return_if_fail (icon->manager_window != None);
358 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
359 icon->manager_window);
361 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
363 icon->manager_window = None;
365 egg_tray_icon_update_manager_window (icon, TRUE);
368 #endif
370 static void
371 egg_tray_icon_realize (GtkWidget *widget)
373 #ifdef GDK_WINDOWING_X11
374 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
375 GdkScreen *screen;
376 GdkDisplay *display;
377 Display *xdisplay;
378 char buffer[256];
379 GdkWindow *root_window;
381 if (GTK_WIDGET_CLASS (parent_class)->realize)
382 GTK_WIDGET_CLASS (parent_class)->realize (widget);
384 screen = gtk_widget_get_screen (widget);
385 display = gdk_screen_get_display (screen);
386 xdisplay = gdk_x11_display_get_xdisplay (display);
388 /* Now see if there's a manager window around */
389 g_snprintf (buffer, sizeof (buffer),
390 "_NET_SYSTEM_TRAY_S%d",
391 gdk_screen_get_number (screen));
393 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
395 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
397 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
398 "_NET_SYSTEM_TRAY_OPCODE",
399 False);
401 icon->orientation_atom = XInternAtom (xdisplay,
402 "_NET_SYSTEM_TRAY_ORIENTATION",
403 False);
405 egg_tray_icon_update_manager_window (icon, FALSE);
406 egg_tray_icon_send_dock_request (icon);
408 root_window = gdk_screen_get_root_window (screen);
410 /* Add a root window filter so that we get changes on MANAGER */
411 gdk_window_add_filter (root_window,
412 egg_tray_icon_manager_filter, icon);
413 #endif
416 EggTrayIcon *
417 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
419 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
421 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
424 EggTrayIcon*
425 egg_tray_icon_new (const gchar *name)
427 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
430 guint
431 egg_tray_icon_send_message (EggTrayIcon *icon,
432 gint timeout,
433 const gchar *message,
434 gint len)
436 guint stamp;
438 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
439 g_return_val_if_fail (timeout >= 0, 0);
440 g_return_val_if_fail (message != NULL, 0);
442 #ifdef GDK_WINDOWING_X11
443 if (icon->manager_window == None)
444 return 0;
445 #endif
447 if (len < 0)
448 len = strlen (message);
450 stamp = icon->stamp++;
452 #ifdef GDK_WINDOWING_X11
453 /* Get ready to send the message */
454 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
455 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
456 timeout, len, stamp);
458 /* Now to send the actual message */
459 gdk_error_trap_push ();
460 while (len > 0)
462 XClientMessageEvent ev;
463 Display *xdisplay;
465 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
467 ev.type = ClientMessage;
468 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
469 ev.format = 8;
470 ev.message_type = XInternAtom (xdisplay,
471 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
472 if (len > 20)
474 memcpy (&ev.data, message, 20);
475 len -= 20;
476 message += 20;
478 else
480 memcpy (&ev.data, message, len);
481 len = 0;
484 XSendEvent (xdisplay,
485 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
486 XSync (xdisplay, False);
488 gdk_error_trap_pop ();
489 #endif
491 return stamp;
494 void
495 egg_tray_icon_cancel_message (EggTrayIcon *icon,
496 guint id)
498 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
499 g_return_if_fail (id > 0);
500 #ifdef GDK_WINDOWING_X11
501 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
502 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
503 id, 0, 0);
504 #endif
507 GtkOrientation
508 egg_tray_icon_get_orientation (EggTrayIcon *icon)
510 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
512 return icon->orientation;