2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / gtk / colorlabel.c
index d6ae64f4b68ef47ff4cf6708d5e2cee12236fffd..c5014bfe65fe29e3aedde1001fc6efaf252e8529 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
+ * Copyright (C) 2001-2011 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /* (alfons) - based on a contribution by Satoshi Nagayasu; revised for colorful 
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkimage.h>
-#include <gtk/gtkmenu.h>
-#include <gtk/gtkcheckmenuitem.h>
-#include <gtk/gtklabel.h>
-#include <gtk/gtkmenuitem.h>
-#include <gtk/gtkalignment.h>
-#include <gtk/gtkhbox.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtkdrawingarea.h>
+#include <gdk/gdkkeysyms.h>
 
 #include "colorlabel.h"
 #include "utils.h"
@@ -51,18 +41,35 @@ static gchar *labels[COLORLABELS] = {
        N_("Sky blue"),
        N_("Blue"),
        N_("Green"),
-       N_("Brown")
+       N_("Brown"),
+       N_("Grey"),
+       N_("Light brown"),
+       N_("Dark red"),
+       N_("Dark pink"),
+       N_("Steel blue"),
+       N_("Gold"),
+       N_("Bright green"),
+       N_("Magenta")
 };
 
 static GdkColor default_colors[COLORLABELS] = {
        { 0, 0xffff, (0x99 << 8), 0x0 },
-       { 0, 0xffff, 00 },
+       { 0, 0xffff, 0x0, 0x0 },
        { 0, 0xffff, (0x66 << 8), 0xffff },
        { 0, 0x0, (0xcc << 8), 0xffff },
        { 0, 0x0, 0x0, 0xffff },
        { 0, 0x0, (0x99 << 8), 0x0 },
-       { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 }
+       { 0, (0x66 << 8), (0x33 << 8), (0x33 << 8) },
+       { 0, (0xaa << 8), (0xaa << 8), (0xaa << 8) },
+       { 0, (0xc0 << 8), (0x72 << 8), (0x54 << 8) },
+       { 0, (0xc0 << 8), 0x0, 0x0 },
+       { 0, (0xcc << 8), (0x10 << 8), (0x74 << 8) },
+       { 0, (0x50 << 8), (0x94 << 8), (0xcd << 8) },
+       { 0, 0xffff, (0xd5 << 8), 0x0 },
+       { 0, 0x0, (0xd8 << 8), 0x0 },
+       { 0, (0xc0 << 8), (0x60 << 8), (0xc0 << 8) }
 };
+
        
 typedef enum LabelColorChangeFlags_ {
        LCCF_COLOR = 1 << 0,
@@ -79,11 +86,19 @@ static struct
        GdkColor                color;
 
        /* XXX: note that the label member is supposed to be dynamically 
-        * allocated and fffreed */
+        * allocated and freed */
        gchar                   *label;
        GtkWidget               *widget;
 } label_colors[NUM_MENUS][COLORLABELS] = {
     {
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
@@ -92,6 +107,14 @@ static struct
        { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL }},
     {
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
+       { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
        { LCCF_ALL, { 0 }, NULL, NULL },
@@ -107,7 +130,9 @@ static struct
 #define LABEL_COLORS_ELEMS (sizeof label_colors[0] / sizeof label_colors[0][0])
 
 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
-       g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
+       do if ((color) < 0 || (color) >= LABEL_COLORS_ELEMS) {  \
+               return val;                                     \
+       } while(0)
 
 #define INTCOLOR_TO_GDKCOLOR(intcolor, gdkcolor) \
        gdkcolor.red   = ((intcolor >> 16UL) & 0xFFUL) << 8UL; \
@@ -156,14 +181,6 @@ GdkColor colorlabel_get_default_color(gint color_index)
        return default_colors[color_index];
 }
                
-gchar *colorlabel_get_color_text(gint color_index)
-{
-       G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
-
-       colorlabel_recreate_label(color_index);
-       return label_colors[0][color_index].label;
-}
-
 gchar *colorlabel_get_color_default_text(gint color_index)
 {
        G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
@@ -172,28 +189,41 @@ gchar *colorlabel_get_color_default_text(gint color_index)
 }
 
 static gboolean colorlabel_drawing_area_expose_event_cb
+#if !GTK_CHECK_VERSION(3, 0, 0)
        (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
+#else
+       (GtkWidget *widget, cairo_t *cr, gpointer data)
+#endif
 {
-       GdkDrawable *drawable = widget->window;
+#if !GTK_CHECK_VERSION(3, 0, 0)
+       cairo_t *cr;
+       GdkWindow *drawable = gtk_widget_get_window(widget);
+#endif
+       GtkAllocation allocation;
        gulong c = (gulong) GPOINTER_TO_INT(data);
        GdkColor color;
-       GdkGC *gc;
 
        INTCOLOR_TO_GDKCOLOR(c, color)
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
-
-       gc = gdk_gc_new(drawable);
-
-       gdk_gc_set_foreground(gc, &color);
-       gdk_draw_rectangle(drawable, widget->style->black_gc,
-                          FALSE, 0, 0, widget->allocation.width - 1,
-                          widget->allocation.height - 1);
-       gdk_draw_rectangle(drawable, gc,
-                          TRUE, 1, 1, widget->allocation.width - 2,
-                          widget->allocation.height - 2);
-
-       gdk_gc_unref(gc);                          
+       cr = gdk_cairo_create(drawable);
+#endif
+       gtk_widget_get_allocation(widget, &allocation);
+
+       cairo_set_source_rgb(cr, 0., 0., 0.);
+       cairo_rectangle(cr, 0, 0,
+           allocation.width - 1,
+           allocation.height - 1);
+       cairo_stroke(cr);
+       gdk_cairo_set_source_color(cr, &color);
+       cairo_rectangle(cr, 1, 1,
+           allocation.width - 2,
+           allocation.height - 2);
+       cairo_fill(cr);
+#if !GTK_CHECK_VERSION(3, 0, 0)
+       cairo_destroy(cr);
+#endif
        
        return FALSE;
 }
@@ -211,29 +241,23 @@ static GtkWidget *colorlabel_create_color_widget(GdkColor color)
                         (CL(g) << (gulong)  8) | \
                         (CL(b)))
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        g_signal_connect(G_OBJECT(widget), "expose_event", 
                         G_CALLBACK
                                (colorlabel_drawing_area_expose_event_cb),
                         GINT_TO_POINTER
                                ((gint)CR(color.red, color.green, color.blue)));
+#else
+       g_signal_connect(G_OBJECT(widget), "draw", 
+                        G_CALLBACK
+                               (colorlabel_drawing_area_expose_event_cb),
+                        GINT_TO_POINTER
+                               ((gint)CR(color.red, color.green, color.blue)));
+#endif
 
        return widget;
 }
 
-/* XXX: this function to check if menus with colors and labels should
- * be recreated */
-gboolean colorlabel_changed(void)
-{
-       gint n;
-
-       for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
-               if (label_colors[0][n].changed) 
-                       return TRUE;
-       }
-
-       return FALSE;
-}
-
 /* XXX: colorlabel_recreate_XXX are there to make sure everything
  * is initialized ok, without having to call a global _xxx_init_
  * function */
@@ -247,7 +271,7 @@ static void colorlabel_recreate_color(gint color)
                        continue;
 
                widget = colorlabel_create_color_widget(label_colors[i][color].color);
-               g_return_if_fail(widget);
+               cm_return_if_fail(widget);
 
                if (label_colors[i][color].widget) 
                        gtk_widget_destroy(label_colors[i][color].widget);
@@ -262,7 +286,7 @@ static void colorlabel_recreate_label(gint color)
        int i;
        
        for (i = 0; i < NUM_MENUS; i++) {
-               if (!label_colors[i][color].changed & LCCF_LABEL)
+               if (!(label_colors[i][color].changed & LCCF_LABEL))
                        continue;
 
                if (label_colors[i][color].label == NULL) 
@@ -327,12 +351,21 @@ GtkWidget *colorlabel_create_check_color_menu_item(gint color_index, gboolean fo
 
        gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
-       accel = g_strdup_printf("Ctrl+%c", '1'+color_index);
-       label = gtk_label_new(accel);
-       gtk_widget_show(label);
-       gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
-       g_free(accel);
-       gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
+       if (color_index < 9) {
+               accel = gtk_accelerator_get_label(GDK_1+color_index, GDK_CONTROL_MASK);
+               label = gtk_label_new(accel);
+               gtk_widget_show(label);
+               gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
+               g_free(accel);
+               gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
+               g_object_set_data(G_OBJECT(item), "accel_label", label);
+       } else {
+               label = gtk_label_new("");
+               gtk_widget_show(label);
+               gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
+               gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
+               g_object_set_data(G_OBJECT(item), "accel_label", label);
+       }
        return item;
 }
 
@@ -369,6 +402,10 @@ GtkWidget *colorlabel_create_color_menu(void)
        g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
        gtk_widget_show(item);
 
+       item = gtk_separator_menu_item_new();
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+       gtk_widget_show(item);
+
        /* and the color items */
        for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
                GtkWidget *hbox;