]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/eggtrayicon.c
don't do anything before including the PCH header
[wxWidgets.git] / src / gtk / eggtrayicon.c
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>
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
21 /*
22
23 Permission to use this file under wxWindows license given by
24 copyright holder:
25 --------
26 From andersca@gnu.org Tue Dec 9 13:01:56 2003
27 Return-path: <andersca@gnu.org>
28 Envelope-to: vasek@localhost
29 Delivery-date: Tue, 09 Dec 2003 13:04:35 +0100
30 Received: from localhost
31 ([127.0.0.1] helo=amavis ident=amavis)
32 by armitage with esmtp (Exim 3.35 #1)
33 id 1ATgbS-0001Gs-00
34 for vasek@localhost; Tue, 09 Dec 2003 13:04:35 +0100
35 Received: from armitage ([127.0.0.1])
36 by amavis (armitage [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
37 id 04227-09 for <vasek@localhost>;
38 Tue, 9 Dec 2003 13:04:11 +0100 (CET)
39 Received: from localhost ([127.0.0.1] ident=fetchmail)
40 by armitage with esmtp (Exim 3.35 #1)
41 id 1ATgb5-0001GY-00
42 for vasek@localhost; Tue, 09 Dec 2003 13:04:11 +0100
43 Delivered-To: alias-email-slavikvaclav@seznam.cz
44 Received: from pop3.seznam.cz [212.80.76.45]
45 by localhost with POP3 (fetchmail-5.9.11)
46 for vasek@localhost (single-drop); Tue, 09 Dec 2003 13:04:11 +0100 (CET)
47 Received: (qmail 9861 invoked from network); 9 Dec 2003 12:02:17 -0000
48 Received: from unknown (HELO maxipes.logix.cz) (81.0.234.97)
49 by buster.go.seznam.cz with SMTP; 9 Dec 2003 12:02:17 -0000
50 Received: by maxipes.logix.cz (Postfix, from userid 604)
51 id 37E6D29A51; Tue, 9 Dec 2003 13:02:16 +0100 (CET)
52 X-Original-To: vaclav.slavik@matfyz.cz
53 Received: from mail.csbnet.se (glutus.csbnet.se [193.11.248.2])
54 by maxipes.logix.cz (Postfix) with ESMTP id 90D6A29A51
55 for <vaclav.slavik@matfyz.cz>; Tue, 9 Dec 2003 13:02:15 +0100 (CET)
56 Received: by mail.csbnet.se (Postfix, from userid 8)
57 id 7AA7F10A6D7; Tue, 9 Dec 2003 13:02:14 +0100 (CET)
58 Received: from carbon.csbnet.se (carbon.csbnet.se [193.11.248.180])
59 by mail.csbnet.se (Postfix) with ESMTP id A190F10A71D
60 for <vaclav.slavik@matfyz.cz>; Tue, 9 Dec 2003 13:01:56 +0100 (CET)
61 Subject: Re: eggtrayicon.{c,h} licensing
62 From: Anders Carlsson <andersca@gnu.org>
63 To: Vaclav Slavik <vaclav.slavik@matfyz.cz>
64 In-Reply-To: <200312091142.54542.vaclav.slavik@matfyz.cz>
65 References: <200312091142.54542.vaclav.slavik@matfyz.cz>
66 Content-Type: text/plain
67 Message-Id: <1070971316.30989.0.camel@carbon.csbnet.se>
68 Mime-Version: 1.0
69 X-Mailer: Ximian Evolution 1.5
70 Date: Tue, 09 Dec 2003 13:01:56 +0100
71 Content-Transfer-Encoding: 7bit
72 X-Scanned-By: CLAM (openantivirus DB) antivirus scanner at mail.csbnet.se
73 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at armitage
74 X-Spam-Status: No, hits=-4.9 tagged_above=-999.0 required=6.3 tests=BAYES_00
75 X-Spam-Level:
76 Status: R
77 X-Status: N
78 X-KMail-EncryptionState:
79 X-KMail-SignatureState:
80
81 On tis, 2003-12-09 at 11:42 +0100, Vaclav Slavik wrote:
82 > Hi,
83 >
84 > I'm working on the wxWindows cross-platform GUI toolkit
85 > (http://www.wxwindows.org) which uses GTK+ and it would save me a lot
86 > of time if I could use your eggtrayicon code to implement tray icons
87 > on X11. Unfortunately I can't use it right now because it is not part
88 > of any library we could depend on (as we do depend on GTK+) and would
89 > have to be included in our sources and it is under the LGPL license.
90 > The problem is that wxWindows' license is more permissive (see
91 > http://www.opensource.org/licenses/wxwindows.php for details) and so
92 > I can't take your code and put it under wxWindows License. And I
93 > can't put code that can't be used under the terms of wxWindows
94 > License into wxWindows either. Do you think it would be possible to
95 > get permission to include eggtrayicon under wxWindows license?
96 >
97 > Thanks,
98 > Vaclav
99 >
100
101 Sure, that's fine by me.
102
103 Anders
104 --------
105 */
106
107 #include "wx/platform.h"
108
109 #if wxUSE_TASKBARICON
110
111 #include <gtk/gtkversion.h>
112 #if GTK_CHECK_VERSION(2, 1, 0)
113
114 #include <string.h>
115 #include "eggtrayicon.h"
116
117 #include <gdkconfig.h>
118 #if defined (GDK_WINDOWING_X11)
119 #include <gdk/gdkx.h>
120 #include <X11/Xatom.h>
121 #elif defined (GDK_WINDOWING_WIN32)
122 #include <gdk/gdkwin32.h>
123 #endif
124
125
126 #define SYSTEM_TRAY_REQUEST_DOCK 0
127 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
128 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
129
130 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
131 #define SYSTEM_TRAY_ORIENTATION_VERT 1
132
133 enum {
134 PROP_0,
135 PROP_ORIENTATION
136 };
137
138 static GtkPlugClass *parent_class = NULL;
139
140 static void egg_tray_icon_init (EggTrayIcon *icon);
141 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
142
143 static void egg_tray_icon_get_property (GObject *object,
144 guint prop_id,
145 GValue *value,
146 GParamSpec *pspec);
147
148 static void egg_tray_icon_realize (GtkWidget *widget);
149 static void egg_tray_icon_unrealize (GtkWidget *widget);
150
151 static void egg_tray_icon_add (GtkContainer *container,
152 GtkWidget *widget);
153
154 #ifdef GDK_WINDOWING_X11
155 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon,
156 gboolean dock_if_realized);
157 static void egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon);
158 #endif
159
160 GType
161 egg_tray_icon_get_type (void)
162 {
163 static GType our_type = 0;
164
165 if (our_type == 0)
166 {
167 static const GTypeInfo our_info =
168 {
169 sizeof (EggTrayIconClass),
170 (GBaseInitFunc) NULL,
171 (GBaseFinalizeFunc) NULL,
172 (GClassInitFunc) egg_tray_icon_class_init,
173 NULL, /* class_finalize */
174 NULL, /* class_data */
175 sizeof (EggTrayIcon),
176 0, /* n_preallocs */
177 (GInstanceInitFunc) egg_tray_icon_init
178 };
179
180 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
181 }
182
183 return our_type;
184 }
185
186 static void
187 egg_tray_icon_init (EggTrayIcon *icon)
188 {
189 icon->stamp = 1;
190 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
191
192 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
193 }
194
195 static void
196 egg_tray_icon_class_init (EggTrayIconClass *klass)
197 {
198 GObjectClass *gobject_class = (GObjectClass *)klass;
199 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
200 GtkContainerClass *container_class = (GtkContainerClass *)klass;
201
202 parent_class = g_type_class_peek_parent (klass);
203
204 gobject_class->get_property = egg_tray_icon_get_property;
205
206 widget_class->realize = egg_tray_icon_realize;
207 widget_class->unrealize = egg_tray_icon_unrealize;
208
209 container_class->add = egg_tray_icon_add;
210
211 g_object_class_install_property (gobject_class,
212 PROP_ORIENTATION,
213 g_param_spec_enum ("orientation",
214 "Orientation",
215 "The orientation of the tray.",
216 GTK_TYPE_ORIENTATION,
217 GTK_ORIENTATION_HORIZONTAL,
218 G_PARAM_READABLE));
219 }
220
221 static void
222 egg_tray_icon_get_property (GObject *object,
223 guint prop_id,
224 GValue *value,
225 GParamSpec *pspec)
226 {
227 EggTrayIcon *icon = EGG_TRAY_ICON (object);
228
229 switch (prop_id)
230 {
231 case PROP_ORIENTATION:
232 g_value_set_enum (value, icon->orientation);
233 break;
234 default:
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 break;
237 }
238 }
239
240 #ifdef GDK_WINDOWING_X11
241
242 static void
243 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
244 {
245 Display *xdisplay;
246 Atom type;
247 int format;
248 union {
249 gulong *prop;
250 guchar *prop_ch;
251 } prop = { NULL };
252 gulong nitems;
253 gulong bytes_after;
254 int error, result;
255
256 g_assert (icon->manager_window != None);
257
258 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
259
260 gdk_error_trap_push ();
261 type = None;
262 result = XGetWindowProperty (xdisplay,
263 icon->manager_window,
264 icon->orientation_atom,
265 0, G_MAXLONG, FALSE,
266 XA_CARDINAL,
267 &type, &format, &nitems,
268 &bytes_after, &(prop.prop_ch));
269 error = gdk_error_trap_pop ();
270
271 if (error || result != Success)
272 return;
273
274 if (type == XA_CARDINAL)
275 {
276 GtkOrientation orientation;
277
278 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
279 GTK_ORIENTATION_HORIZONTAL :
280 GTK_ORIENTATION_VERTICAL;
281
282 if (icon->orientation != orientation)
283 {
284 icon->orientation = orientation;
285
286 g_object_notify (G_OBJECT (icon), "orientation");
287 }
288 }
289
290 if (prop.prop)
291 XFree (prop.prop);
292 }
293
294 static GdkFilterReturn
295 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
296 {
297 EggTrayIcon *icon = user_data;
298 XEvent *xev = (XEvent *)xevent;
299
300 if (xev->xany.type == ClientMessage &&
301 xev->xclient.message_type == icon->manager_atom &&
302 xev->xclient.data.l[1] == icon->selection_atom)
303 {
304 egg_tray_icon_update_manager_window (icon, TRUE);
305 }
306 else if (xev->xany.window == icon->manager_window)
307 {
308 if (xev->xany.type == PropertyNotify &&
309 xev->xproperty.atom == icon->orientation_atom)
310 {
311 egg_tray_icon_get_orientation_property (icon);
312 }
313 if (xev->xany.type == DestroyNotify)
314 {
315 egg_tray_icon_manager_window_destroyed (icon);
316 }
317 }
318 return GDK_FILTER_CONTINUE;
319 }
320
321 #endif
322
323 static void
324 egg_tray_icon_unrealize (GtkWidget *widget)
325 {
326 #ifdef GDK_WINDOWING_X11
327 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
328 GdkWindow *root_window;
329
330 if (icon->manager_window != None)
331 {
332 GdkWindow *gdkwin;
333
334 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
335 icon->manager_window);
336
337 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
338 }
339
340 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
341
342 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
343
344 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
345 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
346 #endif
347 }
348
349 #ifdef GDK_WINDOWING_X11
350
351 static void
352 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
353 long message,
354 Window window,
355 long data1,
356 long data2,
357 long data3)
358 {
359 XClientMessageEvent ev;
360 Display *display;
361
362 ev.type = ClientMessage;
363 ev.window = window;
364 ev.message_type = icon->system_tray_opcode_atom;
365 ev.format = 32;
366 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
367 ev.data.l[1] = message;
368 ev.data.l[2] = data1;
369 ev.data.l[3] = data2;
370 ev.data.l[4] = data3;
371
372 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
373
374 gdk_error_trap_push ();
375 XSendEvent (display,
376 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
377 XSync (display, False);
378 gdk_error_trap_pop ();
379 }
380
381 static void
382 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
383 {
384 egg_tray_icon_send_manager_message (icon,
385 SYSTEM_TRAY_REQUEST_DOCK,
386 icon->manager_window,
387 gtk_plug_get_id (GTK_PLUG (icon)),
388 0, 0);
389 }
390
391 static void
392 egg_tray_icon_update_manager_window (EggTrayIcon *icon,
393 gboolean dock_if_realized)
394 {
395 Display *xdisplay;
396
397 if (icon->manager_window != None)
398 return;
399
400 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
401
402 XGrabServer (xdisplay);
403
404 icon->manager_window = XGetSelectionOwner (xdisplay,
405 icon->selection_atom);
406
407 if (icon->manager_window != None)
408 XSelectInput (xdisplay,
409 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
410
411 XUngrabServer (xdisplay);
412 XFlush (xdisplay);
413
414 if (icon->manager_window != None)
415 {
416 GdkWindow *gdkwin;
417
418 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
419 icon->manager_window);
420
421 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
422
423 if (dock_if_realized && GTK_WIDGET_REALIZED (icon))
424 egg_tray_icon_send_dock_request (icon);
425
426 egg_tray_icon_get_orientation_property (icon);
427 }
428 }
429
430 static void
431 egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon)
432 {
433 GdkWindow *gdkwin;
434
435 g_return_if_fail (icon->manager_window != None);
436
437 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
438 icon->manager_window);
439
440 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
441
442 icon->manager_window = None;
443
444 egg_tray_icon_update_manager_window (icon, TRUE);
445 }
446
447 #endif
448
449 static gboolean
450 transparent_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
451 {
452 gdk_window_clear_area (widget->window, event->area.x, event->area.y,
453 event->area.width, event->area.height);
454 return FALSE;
455 }
456
457 static void
458 make_transparent_again (GtkWidget *widget, GtkStyle *previous_style,
459 gpointer user_data)
460 {
461 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
462 }
463
464 static void
465 make_transparent (GtkWidget *widget, gpointer user_data)
466 {
467 if (GTK_WIDGET_NO_WINDOW (widget) || GTK_WIDGET_APP_PAINTABLE (widget))
468 return;
469
470 gtk_widget_set_app_paintable (widget, TRUE);
471 gtk_widget_set_double_buffered (widget, FALSE);
472 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
473 g_signal_connect (widget, "expose_event",
474 G_CALLBACK (transparent_expose_event), NULL);
475 g_signal_connect_after (widget, "style_set",
476 G_CALLBACK (make_transparent_again), NULL);
477 }
478
479 static void
480 egg_tray_icon_realize (GtkWidget *widget)
481 {
482 #ifdef GDK_WINDOWING_X11
483 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
484 GdkScreen *screen;
485 GdkDisplay *display;
486 Display *xdisplay;
487 char buffer[256];
488 GdkWindow *root_window;
489
490 if (GTK_WIDGET_CLASS (parent_class)->realize)
491 GTK_WIDGET_CLASS (parent_class)->realize (widget);
492
493 make_transparent (widget, NULL);
494
495 screen = gtk_widget_get_screen (widget);
496 display = gdk_screen_get_display (screen);
497 xdisplay = gdk_x11_display_get_xdisplay (display);
498
499 /* Now see if there's a manager window around */
500 g_snprintf (buffer, sizeof (buffer),
501 "_NET_SYSTEM_TRAY_S%d",
502 gdk_screen_get_number (screen));
503
504 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
505
506 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
507
508 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
509 "_NET_SYSTEM_TRAY_OPCODE",
510 False);
511
512 icon->orientation_atom = XInternAtom (xdisplay,
513 "_NET_SYSTEM_TRAY_ORIENTATION",
514 False);
515
516 egg_tray_icon_update_manager_window (icon, FALSE);
517 egg_tray_icon_send_dock_request (icon);
518
519 root_window = gdk_screen_get_root_window (screen);
520
521 /* Add a root window filter so that we get changes on MANAGER */
522 gdk_window_add_filter (root_window,
523 egg_tray_icon_manager_filter, icon);
524 #endif
525 }
526
527 static void
528 egg_tray_icon_add (GtkContainer *container, GtkWidget *widget)
529 {
530 g_signal_connect (widget, "realize",
531 G_CALLBACK (make_transparent), NULL);
532 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
533 }
534
535 EggTrayIcon *
536 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
537 {
538 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
539
540 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
541 }
542
543 EggTrayIcon*
544 egg_tray_icon_new (const gchar *name)
545 {
546 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
547 }
548
549 guint
550 egg_tray_icon_send_message (EggTrayIcon *icon,
551 gint timeout,
552 const gchar *message,
553 gint len)
554 {
555 guint stamp;
556
557 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
558 g_return_val_if_fail (timeout >= 0, 0);
559 g_return_val_if_fail (message != NULL, 0);
560
561 #ifdef GDK_WINDOWING_X11
562 if (icon->manager_window == None)
563 return 0;
564 #endif
565
566 if (len < 0)
567 len = strlen (message);
568
569 stamp = icon->stamp++;
570
571 #ifdef GDK_WINDOWING_X11
572 /* Get ready to send the message */
573 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
574 icon->manager_window,
575 timeout, len, stamp);
576
577 /* Now to send the actual message */
578 gdk_error_trap_push ();
579 while (len > 0)
580 {
581 XClientMessageEvent ev;
582 Display *xdisplay;
583
584 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
585
586 ev.type = ClientMessage;
587 ev.window = icon->manager_window;
588 ev.format = 8;
589 ev.message_type = XInternAtom (xdisplay,
590 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
591 if (len > 20)
592 {
593 memcpy (&ev.data, message, 20);
594 len -= 20;
595 message += 20;
596 }
597 else
598 {
599 memcpy (&ev.data, message, len);
600 len = 0;
601 }
602
603 XSendEvent (xdisplay,
604 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
605 XSync (xdisplay, False);
606 }
607 gdk_error_trap_pop ();
608 #endif
609
610 return stamp;
611 }
612
613 void
614 egg_tray_icon_cancel_message (EggTrayIcon *icon,
615 guint id)
616 {
617 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
618 g_return_if_fail (id > 0);
619 #ifdef GDK_WINDOWING_X11
620 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
621 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
622 id, 0, 0);
623 #endif
624 }
625
626 GtkOrientation
627 egg_tray_icon_get_orientation (EggTrayIcon *icon)
628 {
629 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
630
631 return icon->orientation;
632 }
633
634
635
636
637
638 #endif /* GTK_CHECK_VERSION(2, 1, 0) */
639 #endif /* wxUSE_TASKBARICON */