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