comparison gtk/docklet-x11.c @ 14681:2c1781ea074c

[gaim-migrate @ 17433] Depluginize the docklet. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Thu, 05 Oct 2006 23:24:00 +0000
parents
children
comparison
equal deleted inserted replaced
14680:fa285d018c71 14681:2c1781ea074c
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 "docklet.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 {
59 gaim_debug(GAIM_DEBUG_INFO, "tray icon", "embedded\n");
60
61 g_source_remove(embed_timeout);
62 embed_timeout = 0;
63 docklet_embedded();
64 }
65
66 static void
67 docklet_x11_destroyed_cb(GtkWidget *widget, void *data)
68 {
69 gaim_debug(GAIM_DEBUG_INFO, "tray icon", "destroyed\n");
70
71 docklet_remove();
72
73 g_object_unref(G_OBJECT(docklet));
74 docklet = NULL;
75
76 g_idle_add(docklet_x11_create_cb, &handle);
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 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
211 docklet_remove();
212
213 g_signal_handlers_disconnect_by_func(G_OBJECT(docklet), G_CALLBACK(docklet_x11_destroyed_cb), NULL);
214 gtk_widget_destroy(GTK_WIDGET(docklet));
215
216 g_object_unref(G_OBJECT(docklet));
217 docklet = NULL;
218
219 if (blank_icon)
220 g_object_unref(G_OBJECT(blank_icon));
221 blank_icon = NULL;
222
223 image = NULL;
224
225 gaim_debug(GAIM_DEBUG_INFO, "tray icon", "destroyed\n");
226 }
227
228 static gboolean
229 docklet_x11_embed_timeout_cb()
230 {
231 /* The docklet was not embedded within the timeout.
232 * Remove it as a visibility manager, but leave the plugin
233 * loaded so that it can embed automatically if/when a notification
234 * area becomes available.
235 */
236 gaim_debug_info("tray icon", "failed to embed within timeout\n");
237 docklet_remove();
238
239 return FALSE;
240 }
241
242 static void
243 docklet_x11_create()
244 {
245 GtkWidget *box;
246
247 if (docklet) {
248 /* if this is being called when a tray icon exists, it's because
249 something messed up. try destroying it before we proceed,
250 although docklet_refcount may be all hosed. hopefully won't happen. */
251 gaim_debug(GAIM_DEBUG_WARNING, "tray icon", "trying to create icon but it already exists?\n");
252 docklet_x11_destroy();
253 }
254
255 docklet = egg_tray_icon_new("Gaim");
256 box = gtk_event_box_new();
257 image = gtk_image_new();
258
259 g_signal_connect(G_OBJECT(docklet), "embedded", G_CALLBACK(docklet_x11_embedded_cb), NULL);
260 g_signal_connect(G_OBJECT(docklet), "destroy", G_CALLBACK(docklet_x11_destroyed_cb), NULL);
261 g_signal_connect(G_OBJECT(box), "button-press-event", G_CALLBACK(docklet_x11_clicked_cb), NULL);
262
263 gtk_container_add(GTK_CONTAINER(box), image);
264 gtk_container_add(GTK_CONTAINER(docklet), box);
265
266 if (!gtk_check_version(2,4,0))
267 g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL);
268
269 gtk_widget_show_all(GTK_WIDGET(docklet));
270
271 /* ref the docklet before we bandy it about the place */
272 g_object_ref(G_OBJECT(docklet));
273
274 /* This is a hack to avoid a race condition between the docklet getting
275 * embedded in the notification area and the gtkblist restoring its
276 * previous visibility state. If the docklet does not get embedded within
277 * the timeout, it will be removed as a visibility manager until it does
278 * get embedded. Ideally, we would only call docklet_embedded() when the
279 * icon was actually embedded.
280 */
281 docklet_embedded();
282 embed_timeout = g_timeout_add(EMBED_TIMEOUT, docklet_x11_embed_timeout_cb, NULL);
283
284 gaim_debug(GAIM_DEBUG_INFO, "tray icon", "created\n");
285 }
286
287 static struct docklet_ui_ops ui_ops =
288 {
289 docklet_x11_create,
290 docklet_x11_destroy,
291 docklet_x11_update_icon,
292 docklet_x11_blank_icon,
293 docklet_x11_set_tooltip,
294 #if GTK_CHECK_VERSION(2,2,0)
295 docklet_x11_position_menu
296 #else
297 NULL
298 #endif
299 };
300
301 void
302 docklet_ui_init()
303 {
304 docklet_set_ui_ops(&ui_ops);
305 }