0.9.3claws23
[claws.git] / src / plugins / trayicon / trayicon.c
index 9f2eebcffb87fff33d64c6f51da4264dcf29e37d..56f185955a4f11fbbefda31b5a1b857fce369c0f 100644 (file)
 #include <gtk/gtk.h>
 
 #include "plugin.h"
+#include "utils.h"
 #include "hooks.h"
 #include "folder.h"
 #include "mainwindow.h"
+#include "gtkutils.h"
+#include "intl.h"
 
 #include "eggtrayicon.h"
-#include "gnome-mail.xpm"
-#include "gnome-nomail.xpm"
+#include "newmail.xpm"
+#include "unreadmail.xpm"
+#include "nomail.xpm"
 
-static gint hook_id;
+static guint hook_id;
 
-static GdkPixmap *mail_pixmap;
-static GdkPixmap *mail_bitmap;
+static GdkPixmap *newmail_pixmap;
+static GdkPixmap *newmail_bitmap;
+static GdkPixmap *unreadmail_pixmap;
+static GdkPixmap *unreadmail_bitmap;
 static GdkPixmap *nomail_pixmap;
 static GdkPixmap *nomail_bitmap;
 
@@ -42,6 +48,7 @@ static EggTrayIcon *trayicon;
 static GtkWidget *eventbox;
 static GtkWidget *image;
 static GtkTooltips *tooltips;
+guint destroy_signal_id;
 
 typedef enum
 {
@@ -51,25 +58,7 @@ typedef enum
        TRAYICON_NOTHING,
 } TrayIconType;
 
-static gboolean mainwin_hidden = FALSE;
-
-static gboolean click_cb(GtkWidget * widget,
-                        GdkEventButton * event, gpointer user_data)
-{
-/*
-       MainWindow *mainwin;
-
-       mainwin = mainwindow_get_mainwindow();
-       if (mainwin_hidden) {
-               gtk_widget_show(mainwin->window);
-               mainwin_hidden = FALSE;
-       } else {
-               gtk_widget_hide(mainwin->window);
-               mainwin_hidden = TRUE;
-        }
-*/
-       return TRUE;
-}
+/* static gboolean mainwin_hidden = FALSE; */
 
 static void set_trayicon_pixmap(TrayIconType icontype)
 {
@@ -78,8 +67,13 @@ static void set_trayicon_pixmap(TrayIconType icontype)
 
        switch(icontype) {
        case TRAYICON_NEW:
-               pixmap = mail_pixmap;
-               bitmap = mail_bitmap;
+               pixmap = newmail_pixmap;
+               bitmap = newmail_bitmap;
+               break;
+       case TRAYICON_UNREAD:
+       case TRAYICON_UNREADMARKED:
+               pixmap = unreadmail_pixmap;
+               bitmap = unreadmail_bitmap;
                break;
        default:
                pixmap = nomail_pixmap;
@@ -88,9 +82,10 @@ static void set_trayicon_pixmap(TrayIconType icontype)
        }
 
        gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
+       gtk_widget_shape_combine_mask(GTK_WIDGET(trayicon), bitmap, GTK_WIDGET(image)->allocation.x, GTK_WIDGET(image)->allocation.y);
 }
 
-static gboolean folder_item_update_hook(gpointer source, gpointer data)
+static void update(void)
 {
        gint new, unread, unreadmarked, total;
        gchar *buf;
@@ -101,33 +96,76 @@ static gboolean folder_item_update_hook(gpointer source, gpointer data)
         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
        g_free(buf);
 
-       set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : TRAYICON_NOTHING);
+       set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : (unread > 0 ? TRAYICON_UNREAD : TRAYICON_NOTHING));
+}
+
+static gboolean folder_item_update_hook(gpointer source, gpointer data)
+{
+       update();
 
        return FALSE;
 }
 
-int plugin_init(gchar **error)
+static gboolean click_cb(GtkWidget * widget,
+                        GdkEventButton * event, gpointer user_data)
 {
-       hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
-       if (hook_id == -1) {
-               *error = g_strdup("Failed to register folder item update hook");
-               return -1;
-       }
+       MainWindow *mainwin;
+
+       mainwin = mainwindow_get_mainwindow();
+       if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
+               main_window_hide(mainwin);
+       } else {
+               main_window_show(mainwin);
+        }
+       return TRUE;
+}
+
+static void resize_cb(GtkWidget *widget, GtkAllocation *allocation)
+{
+       update();
+}
+
+static void create_trayicon(void);
+
+static void destroy_cb(GtkWidget *widget, gpointer *data)
+{
+       debug_print("Widget destroyed\n");
+
+       create_trayicon();
+}
+
+static void create_trayicon()
+{
+       GtkPacker *packer;
 
         trayicon = egg_tray_icon_new("Sylpheed-Claws");
+//        trayicon = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       gtk_widget_realize(GTK_WIDGET(trayicon));
+       gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
 
-        nomail_pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(trayicon)->window, &nomail_bitmap, NULL, nomail_xpm);
-        mail_pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(trayicon)->window, &mail_bitmap, NULL, mail_xpm);
-    
+        PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
+        PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
+        PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
+
         eventbox = gtk_event_box_new();
-        gtk_container_set_border_width(eventbox, 0);
+        gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
 
-        image = gtk_pixmap_new (nomail_pixmap, nomail_bitmap);
-        gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(image));
+       packer = GTK_PACKER(gtk_packer_new());
+        gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(packer));
+        gtk_container_set_border_width(GTK_CONTAINER(packer), 0);
 
-        gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event", GTK_SIGNAL_FUNC(click_cb), NULL);
+        image = gtk_pixmap_new(nomail_pixmap, nomail_bitmap);
+        gtk_packer_add_defaults(GTK_PACKER(packer), GTK_WIDGET(image), GTK_SIDE_TOP, GTK_ANCHOR_CENTER, GTK_PACK_EXPAND);
+
+       destroy_signal_id =
+       gtk_signal_connect(GTK_OBJECT(trayicon), "destroy",
+                     GTK_SIGNAL_FUNC(destroy_cb), NULL);
+       gtk_signal_connect(GTK_OBJECT(trayicon), "size_allocate",
+                   GTK_SIGNAL_FUNC(resize_cb), NULL);
+       gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event",
+                   GTK_SIGNAL_FUNC(click_cb), NULL);
 
         tooltips = gtk_tooltips_new();
         gtk_tooltips_set_delay(tooltips, 1000);
@@ -135,26 +173,46 @@ int plugin_init(gchar **error)
 
         gtk_widget_show_all(GTK_WIDGET(trayicon));
 
+       update();
+}
+
+int plugin_init(gchar **error)
+{
+       hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
+       if (hook_id == -1) {
+               *error = g_strdup("Failed to register folder item update hook");
+               return -1;
+       }
+
+       create_trayicon();
+
         return 0;
 }
 
-void plugin_done()
+void plugin_done(void)
 {
+       gtk_signal_disconnect(GTK_OBJECT(trayicon), destroy_signal_id);
+
        gtk_widget_destroy(GTK_WIDGET(trayicon));
        hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
 }
 
-const gchar *plugin_name()
+const gchar *plugin_name(void)
 {
-       return "Trayicon";
+       return _("Trayicon");
 }
 
-const gchar *plugin_desc()
+const gchar *plugin_desc(void)
 {
-       return "";
+       return _("This plugin places a mailbox icon in the system tray that "
+                "indicates if you have new or unread mail.\n"
+                "\n"
+                "The mailbox is empty if you have no unread mail, otherwise "
+                "it contains a letter. A tooltip shows new, unread and total "
+                "number of messages.");
 }
 
-const gchar *plugin_type()
+const gchar *plugin_type(void)
 {
        return "GTK";
 }