Rename panel, plugin, and plugin_class to Panel, Plugin, and
[lxde/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
112
113
114 static int
115 tray_constructor(Plugin *p, char** fp)
116 {
117 line s;
118 tray *tr;
119 GdkScreen *screen;
120
121 ENTER;
122 s.len = 256;
123 if( fp )
124 {
125 while ( lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
126 g_debug("s.str = \'%s\'", s.str);
127 ERR("tray: illegal in this context %s\n", s.str);
128 RET(0);
129 }
130 }
131 tr = g_new0(tray, 1);
132 g_return_val_if_fail(tr != NULL, 0);
133 p->priv = tr;
134 tr->plug = p;
135 tr->icon_num = 0;
136
137 tr->box = p->panel->my_box_new(FALSE, 0);
138
139 screen = gtk_widget_get_screen (GTK_WIDGET (p->panel->topgwin));
140
141 if (na_tray_manager_check_running(screen)) {
142 tr->tray_manager = NULL;
143 ERR("tray: another systray already running\n");
144 RET(1);
145 }
146 tr->tray_manager = na_tray_manager_new ();
147 if (!na_tray_manager_manage_screen (tr->tray_manager, screen))
148 g_printerr ("tray: System tray didn't get the system tray manager selection\n");
149
150 g_signal_connect (tr->tray_manager, "tray_icon_added", G_CALLBACK (tray_added), tr);
151 g_signal_connect (tr->tray_manager, "tray_icon_removed", G_CALLBACK (tray_removed), tr);
152 g_signal_connect (tr->tray_manager, "message_sent", G_CALLBACK (message_sent), tr);
153 g_signal_connect (tr->tray_manager, "message_cancelled", G_CALLBACK (message_cancelled), tr);
154
155 gtk_widget_show_all(tr->box);
156 gtk_widget_set_size_request (tr->box, -1, -1);
157
158 p->pwid = gtk_event_box_new();
159 GTK_WIDGET_SET_FLAGS( p->pwid, GTK_NO_WINDOW );
160
161 gtk_container_add( (GtkContainer*)p->pwid, tr->box );
162 gtk_container_set_border_width(GTK_CONTAINER(p->pwid), 1);
163
164 RET(1);
165
166 }
167
168 static void orientation_changed( Plugin* p )
169 {
170 tray *tr = (tray *)p->priv;
171 GtkBox* newbox;
172 GList *l, *children;
173 int width = 0, height = 0;
174
175 na_tray_manager_set_orientation( tr->tray_manager, p->panel->orientation );
176
177 /* weird... na_tray_manager will re-add the icons to the tray automatically... */
178 gtk_widget_destroy( tr->box );
179 tr->box = p->panel->my_box_new( FALSE, 0 );
180 gtk_container_add(GTK_CONTAINER(p->pwid), tr->box);
181
182 force_redraw(tr);
183 }
184
185 PluginClass tray_plugin_class = {
186 fname: NULL,
187 count: 0,
188
189 type : "tray",
190 name : N_("System Tray"),
191 version: "1.0",
192 description : N_("Old KDE/GNOME Tray"),
193
194 constructor : tray_constructor,
195 destructor : tray_destructor,
196 config : NULL,
197 save : NULL,
198 orientation : orientation_changed
199 };