root / src / trayicon.c @ 1195
History | View | Annotate | Download (9.9 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2006 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include <glib.h> |
| 25 | #include <glib/gi18n.h> |
| 26 | #include <gtk/gtkwindow.h> |
| 27 | #include <gtk/gtkeventbox.h> |
| 28 | #include <gtk/gtktooltips.h> |
| 29 | #include <gtk/gtkimage.h> |
| 30 | #include <gtk/gtkmenu.h> |
| 31 | #include <gtk/gtkmenuitem.h> |
| 32 | #include <gtk/gtkversion.h> |
| 33 | |
| 34 | #include "eggtrayicon.h" |
| 35 | #include "trayicon.h" |
| 36 | #include "mainwindow.h" |
| 37 | #include "utils.h" |
| 38 | #include "gtkutils.h" |
| 39 | #include "eggtrayicon.h" |
| 40 | #include "stock_pixmap.h" |
| 41 | #include "menu.h" |
| 42 | #include "main.h" |
| 43 | #include "inc.h" |
| 44 | #include "compose.h" |
| 45 | #include "gtkutils.h" |
| 46 | |
| 47 | #if GTK_CHECK_VERSION(2, 10, 0) || defined(GDK_WINDOWING_X11) |
| 48 | |
| 49 | #if GTK_CHECK_VERSION(2, 10, 0) |
| 50 | |
| 51 | #include <gtk/gtkstatusicon.h> |
| 52 | |
| 53 | static TrayIcon trayicon;
|
| 54 | static GtkWidget *trayicon_menu;
|
| 55 | static gboolean default_tooltip = FALSE;
|
| 56 | |
| 57 | static void trayicon_activated (GtkStatusIcon *status_icon, |
| 58 | gpointer data); |
| 59 | static void trayicon_popup_menu_cb (GtkStatusIcon *status_icon, |
| 60 | guint button, |
| 61 | guint activate_time, |
| 62 | gpointer data); |
| 63 | |
| 64 | #else
|
| 65 | |
| 66 | static TrayIcon trayicon;
|
| 67 | static GtkWidget *trayicon_img;
|
| 68 | static GtkWidget *eventbox;
|
| 69 | static GtkTooltips *trayicon_tip;
|
| 70 | static GtkWidget *trayicon_menu;
|
| 71 | static gboolean default_tooltip = FALSE;
|
| 72 | |
| 73 | static void trayicon_button_pressed (GtkWidget *widget, |
| 74 | GdkEventButton *event, |
| 75 | gpointer data); |
| 76 | static void trayicon_destroy_cb (GtkWidget *widget, |
| 77 | gpointer data); |
| 78 | |
| 79 | #endif
|
| 80 | |
| 81 | static void trayicon_window_present (GtkWindow *window); |
| 82 | |
| 83 | static void trayicon_present (GtkWidget *widget, |
| 84 | gpointer data); |
| 85 | static void trayicon_inc (GtkWidget *widget, |
| 86 | gpointer data); |
| 87 | static void trayicon_inc_all (GtkWidget *widget, |
| 88 | gpointer data); |
| 89 | static void trayicon_send (GtkWidget *widget, |
| 90 | gpointer data); |
| 91 | static void trayicon_compose (GtkWidget *widget, |
| 92 | gpointer data); |
| 93 | static void trayicon_app_exit (GtkWidget *widget, |
| 94 | gpointer data); |
| 95 | |
| 96 | TrayIcon *trayicon_create(MainWindow *mainwin) |
| 97 | {
|
| 98 | GtkWidget *menuitem; |
| 99 | |
| 100 | #if GTK_CHECK_VERSION(2, 10, 0) |
| 101 | GdkPixbuf *pixbuf; |
| 102 | |
| 103 | stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED, &pixbuf);
|
| 104 | trayicon.status_icon = gtk_status_icon_new_from_pixbuf(pixbuf); |
| 105 | |
| 106 | g_signal_connect(G_OBJECT(trayicon.status_icon), "activate",
|
| 107 | G_CALLBACK(trayicon_activated), mainwin); |
| 108 | g_signal_connect(G_OBJECT(trayicon.status_icon), "popup-menu",
|
| 109 | G_CALLBACK(trayicon_popup_menu_cb), mainwin); |
| 110 | #else
|
| 111 | trayicon.widget = GTK_WIDGET(egg_tray_icon_new("Sylpheed"));
|
| 112 | g_signal_connect(G_OBJECT(trayicon.widget), "destroy",
|
| 113 | G_CALLBACK(trayicon_destroy_cb), mainwin); |
| 114 | |
| 115 | eventbox = gtk_event_box_new(); |
| 116 | gtk_widget_show(eventbox); |
| 117 | gtk_container_add(GTK_CONTAINER(trayicon.widget), eventbox); |
| 118 | g_signal_connect(G_OBJECT(eventbox), "button_press_event",
|
| 119 | G_CALLBACK(trayicon_button_pressed), mainwin); |
| 120 | trayicon_img = stock_pixbuf_widget_scale(NULL, STOCK_PIXMAP_SYLPHEED,
|
| 121 | 24, 24); |
| 122 | gtk_widget_show(trayicon_img); |
| 123 | gtk_container_add(GTK_CONTAINER(eventbox), trayicon_img); |
| 124 | |
| 125 | trayicon_tip = gtk_tooltips_new(); |
| 126 | #endif
|
| 127 | default_tooltip = FALSE; |
| 128 | trayicon_set_tooltip(NULL);
|
| 129 | |
| 130 | if (!trayicon_menu) {
|
| 131 | trayicon_menu = gtk_menu_new(); |
| 132 | gtk_widget_show(trayicon_menu); |
| 133 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 134 | _("_Display Sylpheed"), 0); |
| 135 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 136 | G_CALLBACK(trayicon_present), mainwin); |
| 137 | MENUITEM_ADD(trayicon_menu, menuitem, NULL, 0); |
| 138 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 139 | _("Get from _current account"), 0); |
| 140 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 141 | G_CALLBACK(trayicon_inc), mainwin); |
| 142 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 143 | _("Get from _all accounts"), 0); |
| 144 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 145 | G_CALLBACK(trayicon_inc_all), mainwin); |
| 146 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 147 | _("_Send queued messages"), 0); |
| 148 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 149 | G_CALLBACK(trayicon_send), mainwin); |
| 150 | |
| 151 | MENUITEM_ADD(trayicon_menu, menuitem, NULL, 0); |
| 152 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 153 | _("Compose _new message"), 0); |
| 154 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 155 | G_CALLBACK(trayicon_compose), mainwin); |
| 156 | |
| 157 | MENUITEM_ADD(trayicon_menu, menuitem, NULL, 0); |
| 158 | MENUITEM_ADD_WITH_MNEMONIC(trayicon_menu, menuitem, |
| 159 | _("E_xit"), 0); |
| 160 | g_signal_connect(G_OBJECT(menuitem), "activate",
|
| 161 | G_CALLBACK(trayicon_app_exit), mainwin); |
| 162 | } |
| 163 | |
| 164 | return &trayicon;
|
| 165 | } |
| 166 | |
| 167 | #if GTK_CHECK_VERSION(2, 10, 0) |
| 168 | |
| 169 | void trayicon_show(TrayIcon *tray_icon)
|
| 170 | {
|
| 171 | gtk_status_icon_set_visible(trayicon.status_icon, TRUE); |
| 172 | }; |
| 173 | |
| 174 | void trayicon_destroy(TrayIcon *tray_icon)
|
| 175 | {
|
| 176 | #if 0
|
| 177 | g_object_unref(tray_icon->status_icon); |
| 178 | tray_icon->status_icon = NULL; |
| 179 | #endif |
| 180 | gtk_status_icon_set_visible(tray_icon->status_icon, FALSE); |
| 181 | } |
| 182 | |
| 183 | void trayicon_set_tooltip(const gchar *text) |
| 184 | {
|
| 185 | if (text) {
|
| 186 | default_tooltip = FALSE; |
| 187 | gtk_status_icon_set_tooltip(trayicon.status_icon, text); |
| 188 | } else if (!default_tooltip) { |
| 189 | default_tooltip = TRUE; |
| 190 | gtk_status_icon_set_tooltip(trayicon.status_icon, |
| 191 | _("Sylpheed"));
|
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static guint notify_tag = 0; |
| 196 | |
| 197 | gboolean notify_timeout_cb(gpointer data) |
| 198 | {
|
| 199 | gtk_status_icon_set_blinking(trayicon.status_icon, FALSE); |
| 200 | notify_tag = 0;
|
| 201 | return FALSE;
|
| 202 | } |
| 203 | |
| 204 | void trayicon_set_notify(gboolean enabled)
|
| 205 | {
|
| 206 | if (enabled && notify_tag == 0) { |
| 207 | gtk_status_icon_set_blinking(trayicon.status_icon, enabled); |
| 208 | notify_tag = g_timeout_add(5000, notify_timeout_cb, NULL); |
| 209 | } else if (!enabled && notify_tag > 0) |
| 210 | g_source_remove(notify_tag); |
| 211 | } |
| 212 | |
| 213 | void trayicon_set_stock_icon(StockPixmap icon)
|
| 214 | {
|
| 215 | GdkPixbuf *pixbuf; |
| 216 | GdkPixbuf *scaled_pixbuf; |
| 217 | |
| 218 | stock_pixbuf_gdk(NULL, icon, &pixbuf);
|
| 219 | scaled_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 24, 24, |
| 220 | GDK_INTERP_HYPER); |
| 221 | gtk_status_icon_set_from_pixbuf(trayicon.status_icon, scaled_pixbuf); |
| 222 | g_object_unref(scaled_pixbuf); |
| 223 | } |
| 224 | |
| 225 | static void trayicon_activated(GtkStatusIcon *status_icon, gpointer data) |
| 226 | {
|
| 227 | MainWindow *mainwin = (MainWindow *)data; |
| 228 | |
| 229 | trayicon_window_present(GTK_WINDOW(mainwin->window)); |
| 230 | } |
| 231 | |
| 232 | static void trayicon_popup_menu_cb(GtkStatusIcon *status_icon, guint button, |
| 233 | guint activate_time, gpointer data) |
| 234 | {
|
| 235 | gtk_menu_popup(GTK_MENU(trayicon_menu), NULL, NULL, NULL, NULL, |
| 236 | button, activate_time); |
| 237 | } |
| 238 | |
| 239 | #else
|
| 240 | |
| 241 | void trayicon_show(TrayIcon *tray_icon)
|
| 242 | {
|
| 243 | gtk_widget_show(tray_icon->widget); |
| 244 | }; |
| 245 | |
| 246 | void trayicon_destroy(TrayIcon *tray_icon)
|
| 247 | {
|
| 248 | gtk_widget_destroy(tray_icon->widget); |
| 249 | tray_icon->widget = NULL;
|
| 250 | } |
| 251 | |
| 252 | void trayicon_set_tooltip(const gchar *text) |
| 253 | {
|
| 254 | if (text) {
|
| 255 | default_tooltip = FALSE; |
| 256 | gtk_tooltips_set_tip(trayicon_tip, trayicon.widget, text, |
| 257 | NULL);
|
| 258 | } else if (!default_tooltip) { |
| 259 | default_tooltip = TRUE; |
| 260 | gtk_tooltips_set_tip(trayicon_tip, trayicon.widget, |
| 261 | _("Sylpheed"), NULL); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void trayicon_set_notify(gboolean enabled)
|
| 266 | {
|
| 267 | } |
| 268 | |
| 269 | void trayicon_set_stock_icon(StockPixmap icon)
|
| 270 | {
|
| 271 | GdkPixbuf *pixbuf; |
| 272 | GdkPixbuf *scaled_pixbuf; |
| 273 | |
| 274 | stock_pixbuf_gdk(NULL, icon, &pixbuf);
|
| 275 | scaled_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 24, 24, |
| 276 | GDK_INTERP_HYPER); |
| 277 | gtk_image_set_from_pixbuf(GTK_IMAGE(trayicon_img), scaled_pixbuf); |
| 278 | g_object_unref(scaled_pixbuf); |
| 279 | } |
| 280 | |
| 281 | static void trayicon_button_pressed(GtkWidget *widget, GdkEventButton *event, |
| 282 | gpointer data) |
| 283 | {
|
| 284 | MainWindow *mainwin = (MainWindow *)data; |
| 285 | |
| 286 | if (!event)
|
| 287 | return;
|
| 288 | |
| 289 | if (event->button == 1) |
| 290 | trayicon_window_present(GTK_WINDOW(mainwin->window)); |
| 291 | else if (event->button == 3) { |
| 292 | gtk_menu_popup(GTK_MENU(trayicon_menu), NULL, NULL, NULL, NULL, |
| 293 | event->button, event->time); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static gboolean trayicon_restore(gpointer data)
|
| 298 | {
|
| 299 | MainWindow *mainwin = (MainWindow *)data; |
| 300 | |
| 301 | mainwin->tray_icon = trayicon_create(mainwin); |
| 302 | return FALSE;
|
| 303 | } |
| 304 | |
| 305 | static void trayicon_destroy_cb(GtkWidget *widget, gpointer data) |
| 306 | {
|
| 307 | g_idle_add(trayicon_restore, data); |
| 308 | } |
| 309 | |
| 310 | #endif
|
| 311 | |
| 312 | static void trayicon_window_present(GtkWindow *window) |
| 313 | {
|
| 314 | gtk_window_set_skip_taskbar_hint(window, FALSE); |
| 315 | gtk_window_present(window); |
| 316 | } |
| 317 | |
| 318 | static void trayicon_present(GtkWidget *widget, gpointer data) |
| 319 | {
|
| 320 | MainWindow *mainwin = (MainWindow *)data; |
| 321 | |
| 322 | trayicon_window_present(GTK_WINDOW(mainwin->window)); |
| 323 | } |
| 324 | |
| 325 | static void trayicon_inc(GtkWidget *widget, gpointer data) |
| 326 | {
|
| 327 | if (!inc_is_active() && !gtkut_window_modal_exist())
|
| 328 | inc_mail((MainWindow *)data); |
| 329 | } |
| 330 | |
| 331 | static void trayicon_inc_all(GtkWidget *widget, gpointer data) |
| 332 | {
|
| 333 | if (!inc_is_active() && !gtkut_window_modal_exist())
|
| 334 | inc_all_account_mail((MainWindow *)data, FALSE); |
| 335 | } |
| 336 | |
| 337 | static void trayicon_send(GtkWidget *widget, gpointer data) |
| 338 | {
|
| 339 | if (!gtkut_window_modal_exist())
|
| 340 | main_window_send_queue((MainWindow *)data); |
| 341 | } |
| 342 | |
| 343 | static void trayicon_compose(GtkWidget *widget, gpointer data) |
| 344 | {
|
| 345 | if (!gtkut_window_modal_exist())
|
| 346 | compose_new(NULL, NULL, NULL, NULL); |
| 347 | } |
| 348 | |
| 349 | static void trayicon_app_exit(GtkWidget *widget, gpointer data) |
| 350 | {
|
| 351 | MainWindow *mainwin = (MainWindow *)data; |
| 352 | |
| 353 | if (mainwin->lock_count == 0 && !gtkut_window_modal_exist()) |
| 354 | app_will_exit(FALSE); |
| 355 | } |
| 356 | |
| 357 | #else /* GTK_CHECK_VERSION(2, 10, 0) || defined(GDK_WINDOWING_X11) */ |
| 358 | |
| 359 | TrayIcon *trayicon_create(MainWindow *mainwin) |
| 360 | {
|
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | void trayicon_show(TrayIcon *tray_icon)
|
| 365 | {
|
| 366 | } |
| 367 | |
| 368 | void trayicon_destroy(TrayIcon *tray_icon)
|
| 369 | {
|
| 370 | } |
| 371 | |
| 372 | void trayicon_set_tooltip(const gchar *text) |
| 373 | {
|
| 374 | } |
| 375 | |
| 376 | void trayicon_set_notify(gboolean enabled)
|
| 377 | {
|
| 378 | } |
| 379 | |
| 380 | void trayicon_set_stock_icon(StockPixmap icon)
|
| 381 | {
|
| 382 | } |
| 383 | |
| 384 | #endif /* GTK_CHECK_VERSION(2, 10, 0) || defined(GDK_WINDOWING_X11) */ |