]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/eggtrayicon.c
corrected some of 200 compile errors introduced by the latest global search and replace
[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 #ifdef __WXGTK20__
110 #include <gtk/gtkversion.h>
111 #if GTK_CHECK_VERSION(2, 1, 0)
112
113 #include <string.h>
114 #include "eggtrayicon.h"
115
116 #include <gdkconfig.h>
117 #if defined (GDK_WINDOWING_X11)
118 #include <gdk/gdkx.h>
119 #include <X11/Xatom.h>
120 #elif defined (GDK_WINDOWING_WIN32)
121 #include <gdk/gdkwin32.h>
122 #endif
123
124
125 #define SYSTEM_TRAY_REQUEST_DOCK 0
126 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
127 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
128
129 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
130 #define SYSTEM_TRAY_ORIENTATION_VERT 1
131
132 enum {
133 PROP_0,
134 PROP_ORIENTATION
135 };
136
137 static GtkPlugClass *parent_class = NULL;
138
139 static void egg_tray_icon_init (EggTrayIcon *icon);
140 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
141
142 static void egg_tray_icon_get_property (GObject *object,
143 guint prop_id,
144 GValue *value,
145 GParamSpec *pspec);
146
147 static void egg_tray_icon_realize (GtkWidget *widget);
148 static void egg_tray_icon_unrealize (GtkWidget *widget);
149
150 static void egg_tray_icon_add (GtkContainer *container,
151 GtkWidget *widget);
152
153 #ifdef GDK_WINDOWING_X11
154 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon,
155 gboolean dock_if_realized);
156 static void egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon);
157 #endif
158
159 GType
160 egg_tray_icon_get_type (void)
161 {
162 static GType our_type = 0;
163
164 if (our_type == 0)
165 {
166 static const GTypeInfo our_info =
167 {
168 sizeof (EggTrayIconClass),
169 (GBaseInitFunc) NULL,
170 (GBaseFinalizeFunc) NULL,
171 (GClassInitFunc) egg_tray_icon_class_init,
172 NULL, /* class_finalize */
173 NULL, /* class_data */
174 sizeof (EggTrayIcon),
175 0, /* n_preallocs */
176 (GInstanceInitFunc) egg_tray_icon_init
177 };
178
179 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
180 }
181
182 return our_type;
183 }
184
185 static void
186 egg_tray_icon_init (EggTrayIcon *icon)
187 {
188 icon->stamp = 1;
189 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
190
191 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
192 }
193
194 static void
195 egg_tray_icon_class_init (EggTrayIconClass *klass)
196 {
197 GObjectClass *gobject_class = (GObjectClass *)klass;
198 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
199 GtkContainerClass *container_class = (GtkContainerClass *)klass;
200
201 parent_class = g_type_class_peek_parent (klass);
202
203 gobject_class->get_property = egg_tray_icon_get_property;
204
205 widget_class->realize = egg_tray_icon_realize;
206 widget_class->unrealize = egg_tray_icon_unrealize;
207
208 container_class->add = egg_tray_icon_add;
209
210 g_object_class_install_property (gobject_class,
211 PROP_ORIENTATION,
212 g_param_spec_enum ("orientation",
213 "Orientation",
214 "The orientation of the tray.",
215 GTK_TYPE_ORIENTATION,
216 GTK_ORIENTATION_HORIZONTAL,
217 G_PARAM_READABLE));
218 }
219
220 static void
221 egg_tray_icon_get_property (GObject *object,
222 guint prop_id,
223 GValue *value,
224 GParamSpec *pspec)
225 {
226 EggTrayIcon *icon = EGG_TRAY_ICON (object);
227
228 switch (prop_id)
229 {
230 case PROP_ORIENTATION:
231 g_value_set_enum (value, icon->orientation);
232 break;
233 default:
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 break;
236 }
237 }
238
239 #ifdef GDK_WINDOWING_X11
240
241 static void
242 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
243 {
244 Display *xdisplay;
245 Atom type;
246 int format;
247 union {
248 gulong *prop;
249 guchar *prop_ch;
250 } prop = { NULL };
251 gulong nitems;
252 gulong bytes_after;
253 int error, result;
254
255 g_assert (icon->manager_window != None);
256
257 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
258
259 gdk_error_trap_push ();
260 type = None;
261 result = XGetWindowProperty (xdisplay,
262 icon->manager_window,
263 icon->orientation_atom,
264 0, G_MAXLONG, FALSE,
265 XA_CARDINAL,
266 &type, &format, &nitems,
267 &bytes_after, &(prop.prop_ch));
268 error = gdk_error_trap_pop ();
269
270 if (error || result != Success)
271 return;
272
273 if (type == XA_CARDINAL)
274 {
275 GtkOrientation orientation;
276
277 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
278 GTK_ORIENTATION_HORIZONTAL :
279 GTK_ORIENTATION_VERTICAL;
280
281 if (icon->orientation != orientation)
282 {
283 icon->orientation = orientation;
284
285 g_object_notify (G_OBJECT (icon), "orientation");
286 }
287 }
288
289 if (prop.prop)
290 XFree (prop.prop);
291 }
292
293 static GdkFilterReturn
294 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
295 {
296 EggTrayIcon *icon = user_data;
297 XEvent *xev = (XEvent *)xevent;
298
299 if (xev->xany.type == ClientMessage &&
300 xev->xclient.message_type == icon->manager_atom &&
301 xev->xclient.data.l[1] == icon->selection_atom)
302 {
303 egg_tray_icon_update_manager_window (icon, TRUE);
304 }
305 else if (xev->xany.window == icon->manager_window)
306 {
307 if (xev->xany.type == PropertyNotify &&
308 xev->xproperty.atom == icon->orientation_atom)
309 {
310 egg_tray_icon_get_orientation_property (icon);
311 }
312 if (xev->xany.type == DestroyNotify)
313 {
314 egg_tray_icon_manager_window_destroyed (icon);
315 }
316 }
317 return GDK_FILTER_CONTINUE;
318 }
319
320 #endif
321
322 static void
323 egg_tray_icon_unrealize (GtkWidget *widget)
324 {
325 #ifdef GDK_WINDOWING_X11
326 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
327 GdkWindow *root_window;
328
329 if (icon->manager_window != None)
330 {
331 GdkWindow *gdkwin;
332
333 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
334 icon->manager_window);
335
336 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
337 }
338
339 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
340
341 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
342
343 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
344 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
345 #endif
346 }
347
348 #ifdef GDK_WINDOWING_X11
349
350 static void
351 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
352 long message,
353 Window window,
354 long data1,
355 long data2,
356 long data3)
357 {
358 XClientMessageEvent ev;
359 Display *display;
360
361 ev.type = ClientMessage;
362 ev.window = window;
363 ev.message_type = icon->system_tray_opcode_atom;
364 ev.format = 32;
365 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
366 ev.data.l[1] = message;
367 ev.data.l[2] = data1;
368 ev.data.l[3] = data2;
369 ev.data.l[4] = data3;
370
371 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
372
373 gdk_error_trap_push ();
374 XSendEvent (display,
375 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
376 XSync (display, False);
377 gdk_error_trap_pop ();
378 }
379
380 static void
381 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
382 {
383 egg_tray_icon_send_manager_message (icon,
384 SYSTEM_TRAY_REQUEST_DOCK,
385 icon->manager_window,
386 gtk_plug_get_id (GTK_PLUG (icon)),
387 0, 0);
388 }
389
390 static void
391 egg_tray_icon_update_manager_window (EggTrayIcon *icon,
392 gboolean dock_if_realized)
393 {
394 Display *xdisplay;
395
396 if (icon->manager_window != None)
397 return;
398
399 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
400
401 XGrabServer (xdisplay);
402
403 icon->manager_window = XGetSelectionOwner (xdisplay,
404 icon->selection_atom);
405
406 if (icon->manager_window != None)
407 XSelectInput (xdisplay,
408 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
409
410 XUngrabServer (xdisplay);
411 XFlush (xdisplay);
412
413 if (icon->manager_window != None)
414 {
415 GdkWindow *gdkwin;
416
417 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
418 icon->manager_window);
419
420 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
421
422 if (dock_if_realized && GTK_WIDGET_REALIZED (icon))
423 egg_tray_icon_send_dock_request (icon);
424
425 egg_tray_icon_get_orientation_property (icon);
426 }
427 }
428
429 static void
430 egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon)
431 {
432 GdkWindow *gdkwin;
433
434 g_return_if_fail (icon->manager_window != None);
435
436 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
437 icon->manager_window);
438
439 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
440
441 icon->manager_window = None;
442
443 egg_tray_icon_update_manager_window (icon, TRUE);
444 }
445
446 #endif
447
448 static gboolean
449 transparent_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
450 {
451 gdk_window_clear_area (widget->window, event->area.x, event->area.y,
452 event->area.width, event->area.height);
453 return FALSE;
454 }
455
456 static void
457 make_transparent_again (GtkWidget *widget, GtkStyle *previous_style,
458 gpointer user_data)
459 {
460 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
461 }
462
463 static void
464 make_transparent (GtkWidget *widget, gpointer user_data)
465 {
466 if (GTK_WIDGET_NO_WINDOW (widget) || GTK_WIDGET_APP_PAINTABLE (widget))
467 return;
468
469 gtk_widget_set_app_paintable (widget, TRUE);
470 gtk_widget_set_double_buffered (widget, FALSE);
471 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
472 g_signal_connect (widget, "expose_event",
473 G_CALLBACK (transparent_expose_event), NULL);
474 g_signal_connect_after (widget, "style_set",
475 G_CALLBACK (make_transparent_again), NULL);
476 }
477
478 static void
479 egg_tray_icon_realize (GtkWidget *widget)
480 {
481 #ifdef GDK_WINDOWING_X11
482 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
483 GdkScreen *screen;
484 GdkDisplay *display;
485 Display *xdisplay;
486 char buffer[256];
487 GdkWindow *root_window;
488
489 if (GTK_WIDGET_CLASS (parent_class)->realize)
490 GTK_WIDGET_CLASS (parent_class)->realize (widget);
491
492 make_transparent (widget, NULL);
493
494 screen = gtk_widget_get_screen (widget);
495 display = gdk_screen_get_display (screen);
496 xdisplay = gdk_x11_display_get_xdisplay (display);
497
498 /* Now see if there's a manager window around */
499 g_snprintf (buffer, sizeof (buffer),
500 "_NET_SYSTEM_TRAY_S%d",
501 gdk_screen_get_number (screen));
502
503 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
504
505 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
506
507 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
508 "_NET_SYSTEM_TRAY_OPCODE",
509 False);
510
511 icon->orientation_atom = XInternAtom (xdisplay,
512 "_NET_SYSTEM_TRAY_ORIENTATION",
513 False);
514
515 egg_tray_icon_update_manager_window (icon, FALSE);
516 egg_tray_icon_send_dock_request (icon);
517
518 root_window = gdk_screen_get_root_window (screen);
519
520 /* Add a root window filter so that we get changes on MANAGER */
521 gdk_window_add_filter (root_window,
522 egg_tray_icon_manager_filter, icon);
523 #endif
524 }
525
526 static void
527 egg_tray_icon_add (GtkContainer *container, GtkWidget *widget)
528 {
529 g_signal_connect (widget, "realize",
530 G_CALLBACK (make_transparent), NULL);
531 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
532 }
533
534 EggTrayIcon *
535 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
536 {
537 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
538
539 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
540 }
541
542 EggTrayIcon*
543 egg_tray_icon_new (const gchar *name)
544 {
545 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
546 }
547
548 guint
549 egg_tray_icon_send_message (EggTrayIcon *icon,
550 gint timeout,
551 const gchar *message,
552 gint len)
553 {
554 guint stamp;
555
556 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
557 g_return_val_if_fail (timeout >= 0, 0);
558 g_return_val_if_fail (message != NULL, 0);
559
560 #ifdef GDK_WINDOWING_X11
561 if (icon->manager_window == None)
562 return 0;
563 #endif
564
565 if (len < 0)
566 len = strlen (message);
567
568 stamp = icon->stamp++;
569
570 #ifdef GDK_WINDOWING_X11
571 /* Get ready to send the message */
572 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
573 icon->manager_window,
574 timeout, len, stamp);
575
576 /* Now to send the actual message */
577 gdk_error_trap_push ();
578 while (len > 0)
579 {
580 XClientMessageEvent ev;
581 Display *xdisplay;
582
583 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
584
585 ev.type = ClientMessage;
586 ev.window = icon->manager_window;
587 ev.format = 8;
588 ev.message_type = XInternAtom (xdisplay,
589 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
590 if (len > 20)
591 {
592 memcpy (&ev.data, message, 20);
593 len -= 20;
594 message += 20;
595 }
596 else
597 {
598 memcpy (&ev.data, message, len);
599 len = 0;
600 }
601
602 XSendEvent (xdisplay,
603 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
604 XSync (xdisplay, False);
605 }
606 gdk_error_trap_pop ();
607 #endif
608
609 return stamp;
610 }
611
612 void
613 egg_tray_icon_cancel_message (EggTrayIcon *icon,
614 guint id)
615 {
616 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
617 g_return_if_fail (id > 0);
618 #ifdef GDK_WINDOWING_X11
619 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
620 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
621 id, 0, 0);
622 #endif
623 }
624
625 GtkOrientation
626 egg_tray_icon_get_orientation (EggTrayIcon *icon)
627 {
628 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
629
630 return icon->orientation;
631 }
632
633
634
635
636
637 #endif /* __WXGTK20__ */
638 #endif /* GTK_CHECK_VERSION(2, 1, 0) */