Fix a memory leak around buttons in Colors prefs page.
[claws.git] / src / prefs_msg_colors.c
index 2b1077d0de6ab4048fa34f650196ba5f9d927df3..13ba614c0538effa58b97635c8f66692420c717c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2004-2007 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 2004-2012 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
@@ -17,9 +17,9 @@
  * 
  */
 
-#undef GTK_DISABLE_DEPRECATED /*gtk_color_selection_set_color*/
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
@@ -134,7 +134,6 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        gint c;
        gchar *tooltip_btn_text = NULL;
        gchar *tooltip_entry_text = NULL;
-       CLAWS_TIP_DECL();
 
        notebook = gtk_notebook_new();
        gtk_widget_show(notebook);
@@ -159,9 +158,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        vbox_quotefg = gtk_vbox_new(FALSE, VBOX_BORDER);
        gtk_widget_show (vbox_quotefg);
        vbox_quotebg = gtk_vbox_new(FALSE, VBOX_BORDER);
-#if GTK_CHECK_VERSION(2, 8, 0)
        gtk_widget_show (vbox_quotebg);
-#endif 
        vbox3 = gtkut_get_options_frame(vbox2, &frame_quote, _("Quote"));
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, frame_quote);
        
@@ -204,7 +201,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, color_buttons.btn_quote_level1);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level1,
-                            Q_("Tooltip|Pick color for 1st level text"));
+                            C_("Tooltip", "Pick color for 1st level text"));
 
        hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
        gtk_widget_show (hbox);
@@ -230,7 +227,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, color_buttons.btn_quote_level2);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level2,
-                            Q_("Tooltip|Pick color for 2nd level text"));
+                            C_("Tooltip", "Pick color for 2nd level text"));
 
        hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
        gtk_widget_show (hbox);
@@ -256,7 +253,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, color_buttons.btn_quote_level3);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level3,
-                            Q_("Tooltip|Pick color for 3rd level text"));
+                            C_("Tooltip", "Pick color for 3rd level text"));
 
        hbox = gtk_hbox_new(FALSE, VBOX_BORDER);
        gtk_widget_show (hbox);
@@ -279,7 +276,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, color_buttons.btn_quote_level1_bg);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level1_bg,
-                            Q_("Tooltip|Pick color for 1st level text background"));
+                            C_("Tooltip", "Pick color for 1st level text background"));
 
        label_quote_bgcolor1 = gtk_label_new (_("Background"));
        gtk_widget_show(label_quote_bgcolor1);
@@ -300,7 +297,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, color_buttons.btn_quote_level2_bg);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level2_bg,
-                            Q_("Tooltip|Pick color for 2nd level text background"));
+                            C_("Tooltip", "Pick color for 2nd level text background"));
 
        label_quote_bgcolor2 = gtk_label_new (_("Background"));
        gtk_widget_show(label_quote_bgcolor2);
@@ -321,7 +318,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_bgcolors, color_buttons.btn_quote_level3_bg);
 
        CLAWS_SET_TIP(color_buttons.btn_quote_level3_bg,
-                            Q_("Tooltip|Pick color for 3rd level text background"));
+                            C_("Tooltip", "Pick color for 3rd level text background"));
 
        label_quote_bgcolor3 = gtk_label_new (_("Background"));
        gtk_widget_show(label_quote_bgcolor3);
@@ -341,7 +338,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, color_buttons.btn_uri);
 
        CLAWS_SET_TIP(color_buttons.btn_uri,
-                            Q_("Tooltip|Pick color for links"));
+                            C_("Tooltip", "Pick color for links"));
 
        lable_uri = gtk_label_new (_("URI link"));
        gtk_widget_show(lable_uri);
@@ -360,7 +357,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        SET_TOGGLE_SENSITIVITY(checkbtn_enable_colors, color_buttons.btn_signature);
 
        CLAWS_SET_TIP(color_buttons.btn_signature,
-                            Q_("Tooltip|Pick color for signatures"));
+                            C_("Tooltip", "Pick color for signatures"));
 
        label_signature = gtk_label_new (_("Signatures"));
        gtk_widget_show(label_signature);
@@ -428,7 +425,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
        for (c = 0; c < (COLORLABELS>>1)+(COLORLABELS&1); c++) {
                /* TRANSLATORS: 'color %d' refers to the filtering/processing 
                   rule name and should not be translated */
-               tooltip_btn_text = g_strdup_printf(Q_("Tooltip|Pick color for 'color %d'"), c+1);
+               tooltip_btn_text = g_strdup_printf(C_("Tooltip", "Pick color for 'color %d'"), c+1);
 
                /* TRANSLATORS: 'color %d' refers to the filtering/processing 
                   rule name and should not be translated */
@@ -447,6 +444,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
 
                CLAWS_SET_TIP(color_buttons.custom_color[c],
                                     tooltip_btn_text);
+               g_free(tooltip_btn_text);
 
                entry_custom_colorlabel[c] = gtk_entry_new();
                gtk_widget_show (entry_custom_colorlabel[c]);
@@ -454,12 +452,13 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
                                   FALSE, FALSE, 0);
                CLAWS_SET_TIP(entry_custom_colorlabel[c],
                                     tooltip_entry_text);
+               g_free(tooltip_entry_text);
        }
 
        for (c = (COLORLABELS>>1)+(COLORLABELS&1); c < COLORLABELS; c++) {
                /* TRANSLATORS: 'color %d' refers to the filtering/processing 
                   rule name and should not be translated */
-               tooltip_btn_text = g_strdup_printf(Q_("Tooltip|Pick color for 'color %d'"), c+1);
+               tooltip_btn_text = g_strdup_printf(C_("Tooltip", "Pick color for 'color %d'"), c+1);
 
                /* TRANSLATORS: 'color %d' refers to the filtering/processing 
                   rule name and should not be translated */
@@ -477,6 +476,7 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
                                   FALSE, FALSE, 0);
                CLAWS_SET_TIP(color_buttons.custom_color[c],
                                     tooltip_btn_text);
+               g_free(tooltip_btn_text);
 
                entry_custom_colorlabel[c] = gtk_entry_new();
                gtk_widget_show (entry_custom_colorlabel[c]);
@@ -484,10 +484,10 @@ static void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window,
                                   FALSE, FALSE, 0);
                CLAWS_SET_TIP(entry_custom_colorlabel[c],
                                     tooltip_entry_text);
+
+               g_free(tooltip_entry_text);
        }
 
-       g_free(tooltip_btn_text);
-       g_free(tooltip_entry_text);
 
        hbox_reset_custom_colors = gtk_hbox_new(FALSE, VBOX_BORDER);
        gtk_widget_show (hbox_reset_custom_colors);
@@ -579,9 +579,11 @@ static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
 {
        gchar *type = (gchar *)data;
        gchar *title = NULL;
-       gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
+       GdkColor color;
        gint rgbvalue = 0;
        GtkColorSelectionDialog *dialog;
+       GtkWidget *button_ok;
+       GtkWidget *button_cancel;
        gint c;
 
        /* custom colors */
@@ -590,7 +592,7 @@ static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
                if (GPOINTER_TO_INT(type) == c) {
                        /* TRANSLATORS: 'color %d' refers to the filtering/processing 
                           rule name and should not be translated */
-                       title = g_strdup_printf(Q_("Dialog title|Pick color for 'color %d'"), c+1);
+                       title = g_strdup_printf(C_("Dialog title", "Pick color for 'color %d'"), c+1);
                        rgbvalue = prefs_common.custom_colorlabel[c].color;
                        break;
                }
@@ -598,38 +600,38 @@ static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
        /* other colors */
        if (c == COLORLABELS) {
                if(g_ascii_strcasecmp(type, "LEVEL1") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 1st level text"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 1st level text"));
                        rgbvalue = prefs_common.quote_level1_col;
                } else if(g_ascii_strcasecmp(type, "LEVEL2") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 2nd level text"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 2nd level text"));
                        rgbvalue = prefs_common.quote_level2_col;
                } else if(g_ascii_strcasecmp(type, "LEVEL3") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 3rd level text"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 3rd level text"));
                        rgbvalue = prefs_common.quote_level3_col;
                } else if(g_ascii_strcasecmp(type, "LEVEL1BG") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 1st level text background"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 1st level text background"));
                        rgbvalue = prefs_common.quote_level1_bgcol;
                } else if(g_ascii_strcasecmp(type, "LEVEL2BG") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 2nd level text background"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 2nd level text background"));
                        rgbvalue = prefs_common.quote_level2_bgcol;
                } else if(g_ascii_strcasecmp(type, "LEVEL3BG") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for 3rd level text background"));
+                       title = g_strdup(C_("Dialog title", "Pick color for 3rd level text background"));
                        rgbvalue = prefs_common.quote_level3_bgcol;
                } else if(g_ascii_strcasecmp(type, "URI") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for links"));
+                       title = g_strdup(C_("Dialog title", "Pick color for links"));
                        rgbvalue = prefs_common.uri_col;
                } else if(g_ascii_strcasecmp(type, "TGTFLD") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for target folder"));
+                       title = g_strdup(C_("Dialog title", "Pick color for target folder"));
                        rgbvalue = prefs_common.tgt_folder_col;
                } else if(g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for signatures"));
+                       title = g_strdup(C_("Dialog title", "Pick color for signatures"));
                        rgbvalue = prefs_common.signature_col;
                } else if(g_ascii_strcasecmp(type, "NEW") == 0) {
-                       title = g_strdup(Q_("Dialog title|Pick color for folder"));
+                       title = g_strdup(C_("Dialog title", "Pick color for folder"));
                        rgbvalue = prefs_common.color_new;
                } else {
                        /* Should never be called */
-                       g_warning("Unrecognized datatype '%s' in quote_color_set_dialog\n", type);
+                       g_warning("Unrecognized datatype '%s' in quote_color_set_dialog", type);
                        return;
                }
        }
@@ -641,39 +643,40 @@ static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
        gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
        manage_window_set_transient(GTK_WINDOW(color_dialog));
 
-       g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
+       g_object_get(color_dialog, "ok-button", &button_ok,
+                               "cancel-button", &button_cancel,
+                               NULL);
+
+       g_signal_connect(G_OBJECT(button_cancel),
                         "clicked", G_CALLBACK(quote_colors_set_dialog_cancel), data);
-       g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
+       g_signal_connect(G_OBJECT(button_ok),
                         "clicked", G_CALLBACK(quote_colors_set_dialog_ok), data);
        g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
                         G_CALLBACK(quote_colors_set_dialog_key_pressed),data);
 
        /* preselect the previous color in the color selection dialog */
-       color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
-       color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
-       color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
+
+       gtkut_convert_int_to_gdk_color(rgbvalue, &color);
+
        dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
-       gtk_color_selection_set_color
-               (GTK_COLOR_SELECTION(dialog->colorsel), color);
+       gtk_color_selection_set_current_color
+               (GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection(dialog)), &color);
 
        gtk_widget_show(color_dialog);
 }
 
 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
 {
-       GtkColorSelection *colorsel = (GtkColorSelection *)
-                                               ((GtkColorSelectionDialog *)color_dialog)->colorsel;
-       gdouble color[4];
-       gint red, green, blue, rgbvalue;
+       GtkColorSelection *colorsel = GTK_COLOR_SELECTION(
+               gtk_color_selection_dialog_get_color_selection
+                                               ((GtkColorSelectionDialog *)color_dialog));
+       GdkColor color;
+       gint rgbvalue;
        gchar *type = (gchar *)data;
        gint c;
 
-       gtk_color_selection_get_color(colorsel, color);
-
-       red      = (gint) (color[0] * 255.0);
-       green    = (gint) (color[1] * 255.0);
-       blue     = (gint) (color[2] * 255.0);
-       rgbvalue = (gint) ((red * 0x10000) | (green * 0x100) | blue);
+       gtk_color_selection_get_current_color(colorsel, &color);
+       rgbvalue = gtkut_convert_gdk_color_to_int(&color);
 
 
        /* custom colors */
@@ -734,23 +737,27 @@ static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
                                                GdkEventKey *event,
                                                gpointer data)
 {
+       GtkColorSelectionDialog *dialog;
+       GtkWidget *button_ok;
+       GtkWidget *button_cancel;
+
+       dialog = GTK_COLOR_SELECTION_DIALOG(widget);
+       g_object_get(dialog, "ok-button", &button_ok,
+                         "cancel-button", &button_cancel,
+                          NULL);
+
        if (event) {
                switch (event->keyval) {
-                       case GDK_Escape:
-                               gtk_button_clicked(GTK_BUTTON(GTK_COLOR_SELECTION_DIALOG
-                                                       (widget)->cancel_button));
+                       case GDK_KEY_Escape:
+                               gtk_button_clicked(GTK_BUTTON(button_cancel));
                                return TRUE;
-                       case GDK_Return: 
-                       case GDK_KP_Enter:
+                       case GDK_KEY_Return: 
+                       case GDK_KEY_KP_Enter:
                                /* NOTE: changing focus makes widget accept all currently 
                                 * changed settings! */
-                               gtk_widget_grab_focus
-                                       (GTK_COLOR_SELECTION_DIALOG
-                                               (widget)->ok_button);
+                               gtk_widget_grab_focus(button_ok);
                                /* call ok handler */                                           
-                               gtk_button_clicked(GTK_BUTTON
-                                       (GTK_COLOR_SELECTION_DIALOG
-                                               (widget)->ok_button));
+                               gtk_button_clicked(GTK_BUTTON(button_ok));
                                return TRUE;
                        default:
                                break;
@@ -771,6 +778,7 @@ static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
        newstyle->bg[GTK_STATE_ACTIVE]   = color;
 
        gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
+       gtk_style_unref(newstyle);
 }
 
 static void prefs_msg_colors_save(PrefsPage *_page)