1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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.
25 #include "eggtrayicon.h"
27 #include <gdkconfig.h>
28 #if defined (GDK_WINDOWING_X11)
30 #include <X11/Xatom.h>
31 #elif defined (GDK_WINDOWING_WIN32)
32 #include <gdk/gdkwin32.h>
35 #ifndef EGG_COMPILATION
37 #define _(x) dgettext (GETTEXT_PACKAGE, x)
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
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
,
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
);
77 egg_tray_icon_get_type (void)
79 static GType our_type
= 0;
83 static const GTypeInfo our_info
=
85 sizeof (EggTrayIconClass
),
87 (GBaseFinalizeFunc
) NULL
,
88 (GClassInitFunc
) egg_tray_icon_class_init
,
89 NULL
, /* class_finalize */
90 NULL
, /* class_data */
93 (GInstanceInitFunc
) egg_tray_icon_init
96 our_type
= g_type_register_static (GTK_TYPE_PLUG
, "EggTrayIcon", &our_info
, 0);
103 egg_tray_icon_init (EggTrayIcon
*icon
)
106 icon
->orientation
= GTK_ORIENTATION_HORIZONTAL
;
108 gtk_widget_add_events (GTK_WIDGET (icon
), GDK_PROPERTY_CHANGE_MASK
);
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
,
126 g_param_spec_enum ("orientation",
128 _("The orientation of the tray."),
129 GTK_TYPE_ORIENTATION
,
130 GTK_ORIENTATION_HORIZONTAL
,
133 #if defined (GDK_WINDOWING_X11)
135 #elif defined (GDK_WINDOWING_WIN32)
136 g_warning ("Port eggtrayicon to Win32");
138 g_warning ("Port eggtrayicon to this GTK+ backend");
143 egg_tray_icon_get_property (GObject
*object
,
148 EggTrayIcon
*icon
= EGG_TRAY_ICON (object
);
152 case PROP_ORIENTATION
:
153 g_value_set_enum (value
, icon
->orientation
);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
161 #ifdef GDK_WINDOWING_X11
164 egg_tray_icon_get_orientation_property (EggTrayIcon
*icon
)
177 g_assert (icon
->manager_window
!= None
);
179 xdisplay
= GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon
)));
181 gdk_error_trap_push ();
183 result
= XGetWindowProperty (xdisplay
,
184 icon
->manager_window
,
185 icon
->orientation_atom
,
188 &type
, &format
, &nitems
,
189 &bytes_after
, &(prop
.prop_ch
));
190 error
= gdk_error_trap_pop ();
192 if (error
|| result
!= Success
)
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");
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
;
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
)
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
);
270 #ifdef GDK_WINDOWING_X11
273 egg_tray_icon_send_manager_message (EggTrayIcon
*icon
,
280 XClientMessageEvent ev
;
283 ev
.type
= ClientMessage
;
285 ev
.message_type
= icon
->system_tray_opcode_atom
;
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 ();
297 icon
->manager_window
, False
, NoEventMask
, (XEvent
*)&ev
);
298 XSync (display
, False
);
299 gdk_error_trap_pop ();
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
)),
313 egg_tray_icon_update_manager_window (EggTrayIcon
*icon
,
314 gboolean dock_if_realized
)
318 if (icon
->manager_window
!= None
)
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
);
335 if (icon
->manager_window
!= None
)
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
);
352 egg_tray_icon_manager_window_destroyed (EggTrayIcon
*icon
)
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
);
371 egg_tray_icon_realize (GtkWidget
*widget
)
373 #ifdef GDK_WINDOWING_X11
374 EggTrayIcon
*icon
= EGG_TRAY_ICON (widget
);
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",
401 icon
->orientation_atom
= XInternAtom (xdisplay
,
402 "_NET_SYSTEM_TRAY_ORIENTATION",
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
);
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
);
425 egg_tray_icon_new (const gchar
*name
)
427 return g_object_new (EGG_TYPE_TRAY_ICON
, "title", name
, NULL
);
431 egg_tray_icon_send_message (EggTrayIcon
*icon
,
433 const gchar
*message
,
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
)
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 ();
462 XClientMessageEvent ev
;
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
));
470 ev
.message_type
= XInternAtom (xdisplay
,
471 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False
);
474 memcpy (&ev
.data
, message
, 20);
480 memcpy (&ev
.data
, message
, len
);
484 XSendEvent (xdisplay
,
485 icon
->manager_window
, False
, StructureNotifyMask
, (XEvent
*)&ev
);
486 XSync (xdisplay
, False
);
488 gdk_error_trap_pop ();
495 egg_tray_icon_cancel_message (EggTrayIcon
*icon
,
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
)),
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
;