| |
1 /* |
| |
2 * System tray icon (aka docklet) plugin for Gaim |
| |
3 * |
| |
4 * Copyright (C) 2002-3 Robert McQueen <robot101@debian.org> |
| |
5 * Copyright (C) 2003 Herman Bloggs <hermanator12002@yahoo.com> |
| |
6 * Inspired by a similar plugin by: |
| |
7 * John (J5) Palmieri <johnp@martianrock.com> |
| |
8 * |
| |
9 * This program is free software; you can redistribute it and/or |
| |
10 * modify it under the terms of the GNU General Public License as |
| |
11 * published by the Free Software Foundation; either version 2 of the |
| |
12 * License, or (at your option) any later version. |
| |
13 * |
| |
14 * This program is distributed in the hope that it will be useful, but |
| |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| |
17 * General Public License for more details. |
| |
18 * |
| |
19 * You should have received a copy of the GNU General Public License |
| |
20 * along with this program; if not, write to the Free Software |
| |
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| |
22 * 02111-1307, USA. |
| |
23 */ |
| |
24 |
| |
25 #include "internal.h" |
| |
26 #include "gtkgaim.h" |
| |
27 #include "debug.h" |
| |
28 #include "gaimstock.h" |
| |
29 |
| |
30 #include "gtkdialogs.h" |
| |
31 |
| |
32 #include "eggtrayicon.h" |
| |
33 #include "gtkdocklet.h" |
| |
34 |
| |
35 #define EMBED_TIMEOUT 5000 |
| |
36 |
| |
37 /* globals */ |
| |
38 static EggTrayIcon *docklet = NULL; |
| |
39 static GtkWidget *image = NULL; |
| |
40 static GtkTooltips *tooltips = NULL; |
| |
41 static GdkPixbuf *blank_icon = NULL; |
| |
42 static int embed_timeout = 0; |
| |
43 |
| |
44 /* protos */ |
| |
45 static void docklet_x11_create(void); |
| |
46 |
| |
47 static gboolean |
| |
48 docklet_x11_create_cb() |
| |
49 { |
| |
50 docklet_x11_create(); |
| |
51 |
| |
52 return FALSE; /* for when we're called by the glib idle handler */ |
| |
53 } |
| |
54 |
| |
55 static void |
| |
56 docklet_x11_embedded_cb(GtkWidget *widget, void *data) |
| |
57 { |
| |
58 gaim_debug(GAIM_DEBUG_INFO, "docklet", "embedded\n"); |
| |
59 |
| |
60 g_source_remove(embed_timeout); |
| |
61 embed_timeout = 0; |
| |
62 gaim_gtk_docklet_embedded(); |
| |
63 } |
| |
64 |
| |
65 static void |
| |
66 docklet_x11_destroyed_cb(GtkWidget *widget, void *data) |
| |
67 { |
| |
68 gaim_debug(GAIM_DEBUG_INFO, "docklet", "destroyed\n"); |
| |
69 |
| |
70 gaim_gtk_docklet_remove(); |
| |
71 |
| |
72 g_object_unref(G_OBJECT(docklet)); |
| |
73 docklet = NULL; |
| |
74 |
| |
75 g_idle_add(docklet_x11_create_cb, NULL); |
| |
76 } |
| |
77 |
| |
78 static void |
| |
79 docklet_x11_clicked_cb(GtkWidget *button, GdkEventButton *event, void *data) |
| |
80 { |
| |
81 if (event->type != GDK_BUTTON_RELEASE) |
| |
82 return; |
| |
83 |
| |
84 gaim_gtk_docklet_clicked(event->button); |
| |
85 } |
| |
86 |
| |
87 static void |
| |
88 docklet_x11_update_icon(DockletStatus icon) |
| |
89 { |
| |
90 const gchar *icon_name = NULL; |
| |
91 |
| |
92 g_return_if_fail(image != NULL); |
| |
93 |
| |
94 switch (icon) { |
| |
95 case DOCKLET_STATUS_OFFLINE: |
| |
96 icon_name = GAIM_STOCK_ICON_OFFLINE; |
| |
97 break; |
| |
98 case DOCKLET_STATUS_CONNECTING: |
| |
99 icon_name = GAIM_STOCK_ICON_CONNECT; |
| |
100 break; |
| |
101 case DOCKLET_STATUS_ONLINE: |
| |
102 icon_name = GAIM_STOCK_ICON_ONLINE; |
| |
103 break; |
| |
104 case DOCKLET_STATUS_ONLINE_PENDING: |
| |
105 icon_name = GAIM_STOCK_ICON_ONLINE_MSG; |
| |
106 break; |
| |
107 case DOCKLET_STATUS_AWAY: |
| |
108 icon_name = GAIM_STOCK_ICON_AWAY; |
| |
109 break; |
| |
110 case DOCKLET_STATUS_AWAY_PENDING: |
| |
111 icon_name = GAIM_STOCK_ICON_AWAY_MSG; |
| |
112 break; |
| |
113 } |
| |
114 |
| |
115 if(icon_name) |
| |
116 gtk_image_set_from_stock(GTK_IMAGE(image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); |
| |
117 |
| |
118 #if 0 |
| |
119 GdkPixbuf *p; |
| |
120 GdkBitmap *mask = NULL; |
| |
121 |
| |
122 p = gtk_widget_render_icon(GTK_WIDGET(image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR, NULL); |
| |
123 |
| |
124 if (p && (gdk_pixbuf_get_colorspace(p) == GDK_COLORSPACE_RGB) && (gdk_pixbuf_get_bits_per_sample(p) == 8) |
| |
125 && (gdk_pixbuf_get_has_alpha(p)) && (gdk_pixbuf_get_n_channels(p) == 4)) { |
| |
126 int len = gdk_pixbuf_get_width(p) * gdk_pixbuf_get_height(p); |
| |
127 guchar *data = gdk_pixbuf_get_pixels(p); |
| |
128 guchar *bitmap = g_malloc((len / 8) + 1); |
| |
129 int i; |
| |
130 |
| |
131 for (i = 0; i < len; i++) |
| |
132 if (data[i*4 + 3] > 55) |
| |
133 bitmap[i/8] |= 1 << i % 8; |
| |
134 else |
| |
135 bitmap[i/8] &= ~(1 << i % 8); |
| |
136 |
| |
137 mask = gdk_bitmap_create_from_data(GDK_DRAWABLE(GTK_WIDGET(image)->window), bitmap, gdk_pixbuf_get_width(p), gdk_pixbuf_get_height(p)); |
| |
138 g_free(bitmap); |
| |
139 } |
| |
140 |
| |
141 if (mask) |
| |
142 gdk_window_shape_combine_mask(image->window, mask, 0, 0); |
| |
143 |
| |
144 g_object_unref(G_OBJECT(p)); |
| |
145 #endif |
| |
146 } |
| |
147 |
| |
148 static void |
| |
149 docklet_x11_blank_icon() |
| |
150 { |
| |
151 if (!blank_icon) { |
| |
152 gint width, height; |
| |
153 |
| |
154 gtk_icon_size_lookup(GTK_ICON_SIZE_LARGE_TOOLBAR, &width, &height); |
| |
155 blank_icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); |
| |
156 gdk_pixbuf_fill(blank_icon, 0); |
| |
157 } |
| |
158 |
| |
159 gtk_image_set_from_pixbuf(GTK_IMAGE(image), blank_icon); |
| |
160 } |
| |
161 |
| |
162 static void |
| |
163 docklet_x11_set_tooltip(gchar *tooltip) |
| |
164 { |
| |
165 if (!tooltips) |
| |
166 tooltips = gtk_tooltips_new(); |
| |
167 |
| |
168 /* image->parent is a GtkEventBox */ |
| |
169 if (tooltip) { |
| |
170 gtk_tooltips_enable(tooltips); |
| |
171 gtk_tooltips_set_tip(tooltips, image->parent, tooltip, NULL); |
| |
172 } else { |
| |
173 gtk_tooltips_set_tip(tooltips, image->parent, "", NULL); |
| |
174 gtk_tooltips_disable(tooltips); |
| |
175 } |
| |
176 } |
| |
177 |
| |
178 #if GTK_CHECK_VERSION(2,2,0) |
| |
179 static void |
| |
180 docklet_x11_position_menu(GtkMenu *menu, int *x, int *y, gboolean *push_in, |
| |
181 gpointer user_data) |
| |
182 { |
| |
183 GtkWidget *widget = GTK_WIDGET(docklet); |
| |
184 GtkRequisition req; |
| |
185 gint menu_xpos, menu_ypos; |
| |
186 |
| |
187 gtk_widget_size_request(GTK_WIDGET(menu), &req); |
| |
188 gdk_window_get_origin(widget->window, &menu_xpos, &menu_ypos); |
| |
189 |
| |
190 menu_xpos += widget->allocation.x; |
| |
191 menu_ypos += widget->allocation.y; |
| |
192 |
| |
193 if (menu_ypos > gdk_screen_get_height(gtk_widget_get_screen(widget)) / 2) |
| |
194 menu_ypos -= req.height; |
| |
195 else |
| |
196 menu_ypos += widget->allocation.height; |
| |
197 |
| |
198 *x = menu_xpos; |
| |
199 *y = menu_ypos; |
| |
200 |
| |
201 *push_in = TRUE; |
| |
202 } |
| |
203 #endif |
| |
204 |
| |
205 static void |
| |
206 docklet_x11_destroy() |
| |
207 { |
| |
208 g_return_if_fail(docklet != NULL); |
| |
209 |
| |
210 if (embed_timeout) |
| |
211 g_source_remove(embed_timeout); |
| |
212 |
| |
213 gaim_gtk_docklet_remove(); |
| |
214 |
| |
215 g_signal_handlers_disconnect_by_func(G_OBJECT(docklet), G_CALLBACK(docklet_x11_destroyed_cb), NULL); |
| |
216 gtk_widget_destroy(GTK_WIDGET(docklet)); |
| |
217 |
| |
218 g_object_unref(G_OBJECT(docklet)); |
| |
219 docklet = NULL; |
| |
220 |
| |
221 if (blank_icon) |
| |
222 g_object_unref(G_OBJECT(blank_icon)); |
| |
223 blank_icon = NULL; |
| |
224 |
| |
225 image = NULL; |
| |
226 |
| |
227 gaim_debug(GAIM_DEBUG_INFO, "docklet", "destroyed\n"); |
| |
228 } |
| |
229 |
| |
230 static gboolean |
| |
231 docklet_x11_embed_timeout_cb() |
| |
232 { |
| |
233 /* The docklet was not embedded within the timeout. |
| |
234 * Remove it as a visibility manager, but leave the plugin |
| |
235 * loaded so that it can embed automatically if/when a notification |
| |
236 * area becomes available. |
| |
237 */ |
| |
238 gaim_debug_info("docklet", "failed to embed within timeout\n"); |
| |
239 gaim_gtk_docklet_remove(); |
| |
240 |
| |
241 return FALSE; |
| |
242 } |
| |
243 |
| |
244 static void |
| |
245 docklet_x11_create() |
| |
246 { |
| |
247 GtkWidget *box; |
| |
248 |
| |
249 if (docklet) { |
| |
250 /* if this is being called when a tray icon exists, it's because |
| |
251 something messed up. try destroying it before we proceed, |
| |
252 although docklet_refcount may be all hosed. hopefully won't happen. */ |
| |
253 gaim_debug(GAIM_DEBUG_WARNING, "docklet", "trying to create icon but it already exists?\n"); |
| |
254 docklet_x11_destroy(); |
| |
255 } |
| |
256 |
| |
257 docklet = egg_tray_icon_new("Gaim"); |
| |
258 box = gtk_event_box_new(); |
| |
259 image = gtk_image_new(); |
| |
260 |
| |
261 g_signal_connect(G_OBJECT(docklet), "embedded", G_CALLBACK(docklet_x11_embedded_cb), NULL); |
| |
262 g_signal_connect(G_OBJECT(docklet), "destroy", G_CALLBACK(docklet_x11_destroyed_cb), NULL); |
| |
263 g_signal_connect(G_OBJECT(box), "button-release-event", G_CALLBACK(docklet_x11_clicked_cb), NULL); |
| |
264 |
| |
265 gtk_container_add(GTK_CONTAINER(box), image); |
| |
266 gtk_container_add(GTK_CONTAINER(docklet), box); |
| |
267 |
| |
268 if (!gtk_check_version(2,4,0)) |
| |
269 g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL); |
| |
270 |
| |
271 gtk_widget_show_all(GTK_WIDGET(docklet)); |
| |
272 |
| |
273 /* ref the docklet before we bandy it about the place */ |
| |
274 g_object_ref(G_OBJECT(docklet)); |
| |
275 |
| |
276 /* This is a hack to avoid a race condition between the docklet getting |
| |
277 * embedded in the notification area and the gtkblist restoring its |
| |
278 * previous visibility state. If the docklet does not get embedded within |
| |
279 * the timeout, it will be removed as a visibility manager until it does |
| |
280 * get embedded. Ideally, we would only call docklet_embedded() when the |
| |
281 * icon was actually embedded. |
| |
282 */ |
| |
283 gaim_gtk_docklet_embedded(); |
| |
284 embed_timeout = g_timeout_add(EMBED_TIMEOUT, docklet_x11_embed_timeout_cb, NULL); |
| |
285 |
| |
286 gaim_debug(GAIM_DEBUG_INFO, "docklet", "created\n"); |
| |
287 } |
| |
288 |
| |
289 static struct docklet_ui_ops ui_ops = |
| |
290 { |
| |
291 docklet_x11_create, |
| |
292 docklet_x11_destroy, |
| |
293 docklet_x11_update_icon, |
| |
294 docklet_x11_blank_icon, |
| |
295 docklet_x11_set_tooltip, |
| |
296 #if GTK_CHECK_VERSION(2,2,0) |
| |
297 docklet_x11_position_menu |
| |
298 #else |
| |
299 NULL |
| |
300 #endif |
| |
301 }; |
| |
302 |
| |
303 void |
| |
304 docklet_ui_init() |
| |
305 { |
| |
306 gaim_gtk_docklet_set_ui_ops(&ui_ops); |
| |
307 } |