fb9b57bafa78c0c89afbf8a3c4abbe3360a5ab6a
[debian/lxpanel.git] / src / systray / tray.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4
5 #include <gdk-pixbuf/gdk-pixbuf.h>
6 #include <glib/gi18n.h>
7
8 #include "panel.h"
9 #include "misc.h"
10 #include "plugin.h"
11 #include "bg.h"
12
13 #include "eggtraymanager.h"
14 #include "fixedtip.h"
15
16
17 //#define DEBUG
18 #include "dbg.h"
19
20
21 typedef struct {
22 Plugin *plug;
23 GtkWidget *box;
24 NaTrayManager *tray_manager;
25 int icon_num;
26 } tray;
27
28 //static void run_gtktray(tray *tr);
29
30 static void
31 force_redraw (tray *tr)
32 {
33 /* Force the icons to redraw their backgrounds.
34 * gtk_widget_queue_draw() doesn't work across process boundaries,
35 * so we do this instead.
36 */
37 GtkWidget* w = tr->plug->pwid;
38 gtk_widget_set_size_request( w, w->allocation.width, w->allocation.height );
39 gtk_widget_hide (tr->box);
40 if( gtk_events_pending() )
41 gtk_main_iteration();
42 gtk_widget_show (tr->box);
43 gtk_widget_set_size_request( w, -1, -1 );
44 }
45
46
47 static void
48 tray_added (NaTrayManager *manager, GtkWidget *icon, tray *tr)
49 {
50 gtk_box_pack_end (GTK_BOX (tr->box), icon, FALSE, FALSE, 0);
51 gtk_widget_show (icon);
52 /* g_debug( "add icon %p", icon ); */
53 plugin_widget_set_background( icon, tr->plug->panel );
54
55 if (!tr->icon_num) {
56 DBG("first icon\n");
57 gtk_widget_show_all(tr->box);
58 }
59 force_redraw(tr);
60 tr->icon_num++;
61 DBG("add icon\n");
62 }
63
64 static void
65 tray_removed (NaTrayManager *manager, GtkWidget *icon, tray *tr)
66 {
67 tr->icon_num--;
68 DBG("del icon\n");
69 if (!tr->icon_num) {
70 gtk_widget_hide(tr->box);
71 DBG("last icon\n");
72 }
73
74 force_redraw(tr);
75 }
76
77 static void
78 message_sent (NaTrayManager *manager, GtkWidget *icon, const char *text, glong id, glong timeout,
79 void *data)
80 {
81 /* FIXME multihead */
82 int x, y;
83
84 gdk_window_get_origin (icon->window, &x, &y);
85 fixed_tip_show (0, x, y, FALSE, gdk_screen_height () - 50, text);
86 }
87
88 static void
89 message_cancelled (NaTrayManager *manager, GtkWidget *icon, glong id,
90 void *data)
91 {
92
93 }
94
95
96
97 static void
98 tray_destructor(Plugin *p)
99 {
100 tray *tr = (tray *)p->priv;
101
102 ENTER;
103 /* Make sure we drop the manager selection */
104 if (tr->tray_manager)
105 g_object_unref (G_OBJECT (tr->tray_manager));
106 fixed_tip_hide ();
107 g_free(tr);
108 RET();
109 }
110
111 /* Dirty hacks used to handle background of systray */
112 static gboolean delay_update_bg( Plugin* p )
113 {
114 plugin_widget_set_background( p->pwid, p->panel );
115 return FALSE;
116 }
117
118 static void tray_realized( GtkWidget* widget, Plugin* p )
119 {
120 g_idle_add( delay_update_bg, p );
121 }
122
123 static int
124 tray_constructor(Plugin *p, char** fp)
125 {
126 line s;
127 tray *tr;
128 GdkScreen *screen;
129
130 ENTER;
131 s.len = 256;
132 if( fp )
133 {
134 while ( lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
135 /* g_debug("s.str = \'%s\'", s.str); */
136 ERR("tray: illegal in this context %s\n", s.str);
137 RET(0);
138 }
139 }
140 tr = g_new0(tray, 1);
141 g_return_val_if_fail(tr != NULL, 0);
142 p->priv = tr;
143 tr->plug = p;
144 tr->icon_num = 0;
145
146 tr->box = p->panel->my_box_new(FALSE, 0);
147
148 screen = gtk_widget_get_screen (GTK_WIDGET (p->panel->topgwin));
149
150 if (na_tray_manager_check_running(screen)) {
151 tr->tray_manager = NULL;
152 ERR("tray: another systray already running\n");
153 RET(1);
154 }
155 tr->tray_manager = na_tray_manager_new ();
156 if (!na_tray_manager_manage_screen (tr->tray_manager, screen))
157 g_printerr ("tray: System tray didn't get the system tray manager selection\n");
158
159 g_signal_connect (tr->tray_manager, "tray_icon_added", G_CALLBACK (tray_added), tr);
160 g_signal_connect (tr->tray_manager, "tray_icon_removed", G_CALLBACK (tray_removed), tr);
161 g_signal_connect (tr->tray_manager, "message_sent", G_CALLBACK (message_sent), tr);
162 g_signal_connect (tr->tray_manager, "message_cancelled", G_CALLBACK (message_cancelled), tr);
163
164 gtk_widget_show_all(tr->box);
165 gtk_widget_set_size_request (tr->box, -1, -1);
166
167 p->pwid = gtk_event_box_new();
168 GTK_WIDGET_SET_FLAGS( p->pwid, GTK_NO_WINDOW );
169 gtk_widget_set_name( p->pwid, "tray" ); /* this hack is requierd for plugin_widget_set_background() */
170
171 gtk_container_add( (GtkContainer*)p->pwid, tr->box );
172 gtk_container_set_border_width(GTK_CONTAINER(p->pwid), 1);
173
174 g_signal_connect( p->pwid, "realize", G_CALLBACK( tray_realized ), p );
175
176 RET(1);
177
178 }
179
180 static void orientation_changed( Plugin* p )
181 {
182 tray *tr = (tray *)p->priv;
183 GtkBox* newbox;
184 GList *l, *children;
185 int width = 0, height = 0;
186
187 /* another systray already running */
188 if (!tr->tray_manager)
189 return;
190
191 na_tray_manager_set_orientation( tr->tray_manager, p->panel->orientation );
192
193 /* weird... na_tray_manager will re-add the icons to the tray automatically... */
194 gtk_widget_destroy( tr->box );
195 tr->box = p->panel->my_box_new( FALSE, 0 );
196 gtk_container_add(GTK_CONTAINER(p->pwid), tr->box);
197
198 force_redraw(tr);
199 }
200
201 PluginClass tray_plugin_class = {
202 fname: NULL,
203 count: 0,
204
205 type : "tray",
206 name : N_("System Tray"),
207 version: "1.0",
208 description : N_("Old KDE/GNOME Tray"),
209
210 constructor : tray_constructor,
211 destructor : tray_destructor,
212 config : NULL,
213 save : NULL,
214 orientation : orientation_changed
215 };