2007-02-09 [paul] 2.7.2cvs35
[claws.git] / src / alertpanel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stddef.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30
31 #include "mainwindow.h"
32 #include "alertpanel.h"
33 #include "manage_window.h"
34 #include "utils.h"
35 #include "gtkutils.h"
36 #include "inc.h"
37 #include "logwindow.h"
38 #include "prefs_common.h"
39
40 #define ALERT_PANEL_WIDTH       380
41 #define TITLE_HEIGHT            72
42 #define MESSAGE_HEIGHT          62
43
44 gboolean alertpanel_is_open = FALSE;
45 static AlertValue value;
46
47 static GtkWidget *dialog;
48
49 static void alertpanel_show             (void);
50 static void alertpanel_create           (const gchar    *title,
51                                          const gchar    *message,
52                                          const gchar    *button1_label,
53                                          const gchar    *button2_label,
54                                          const gchar    *button3_label,
55                                          gboolean        can_disable,
56                                          GtkWidget      *custom_widget,
57                                          gint            alert_type,
58                                          AlertValue      default_value);
59
60 static void alertpanel_button_toggled   (GtkToggleButton        *button,
61                                          gpointer                data);
62 static void alertpanel_button_clicked   (GtkWidget              *widget,
63                                          gpointer                data);
64 static gint alertpanel_deleted          (GtkWidget              *widget,
65                                          GdkEventAny            *event,
66                                          gpointer                data);
67 static gboolean alertpanel_close        (GtkWidget              *widget,
68                                          GdkEventAny            *event,
69                                          gpointer                data);
70
71 AlertValue alertpanel_with_widget(const gchar *title,
72                                   const gchar *message,
73                                   const gchar *button1_label,
74                                   const gchar *button2_label,
75                                   const gchar *button3_label,
76                                   gboolean     can_disable,
77                                   GtkWidget *widget)
78 {
79         return alertpanel_full(title, message, button1_label,
80                                     button2_label, button3_label,
81                                     can_disable, widget, ALERT_QUESTION,
82                                     G_ALERTDEFAULT);
83 }
84
85 AlertValue alertpanel_full(const gchar *title, const gchar *message,
86                            const gchar *button1_label,
87                            const gchar *button2_label,
88                            const gchar *button3_label,
89                            gboolean     can_disable,
90                            GtkWidget   *widget,
91                            AlertType    alert_type,
92                            AlertValue   default_value)
93 {
94         if (alertpanel_is_open)
95                 return -1;
96         else
97                 alertpanel_is_open = TRUE;
98         
99         alertpanel_create(title, message, button1_label, button2_label,
100                           button3_label, can_disable, widget, alert_type,
101                           default_value);
102         alertpanel_show();
103
104         debug_print("return value = %d\n", value);
105         return value;
106 }
107
108 AlertValue alertpanel(const gchar *title,
109                       const gchar *message,
110                       const gchar *button1_label,
111                       const gchar *button2_label,
112                       const gchar *button3_label)
113 {
114         return alertpanel_full(title, message, button1_label, button2_label,
115                                button3_label, FALSE, NULL, ALERT_QUESTION,
116                                G_ALERTDEFAULT);
117 }
118
119 static void alertpanel_message(const gchar *title, const gchar *message, gint type)
120 {
121         if (alertpanel_is_open)
122                 return;
123         else
124                 alertpanel_is_open = TRUE;
125
126         alertpanel_create(title, message, GTK_STOCK_CLOSE, NULL, NULL,
127                           FALSE, NULL, type, G_ALERTDEFAULT);
128         alertpanel_show();
129 }
130
131 void alertpanel_notice(const gchar *format, ...)
132 {
133         va_list args;
134         gchar buf[256];
135
136         va_start(args, format);
137         g_vsnprintf(buf, sizeof(buf), format, args);
138         va_end(args);
139         strretchomp(buf);
140
141         alertpanel_message(_("Notice"), buf, ALERT_NOTICE);
142 }
143
144 void alertpanel_warning(const gchar *format, ...)
145 {
146         va_list args;
147         gchar buf[256];
148
149         va_start(args, format);
150         g_vsnprintf(buf, sizeof(buf), format, args);
151         va_end(args);
152         strretchomp(buf);
153
154         alertpanel_message(_("Warning"), buf, ALERT_WARNING);
155 }
156
157 void alertpanel_error(const gchar *format, ...)
158 {
159         va_list args;
160         gchar buf[512];
161
162         va_start(args, format);
163         g_vsnprintf(buf, sizeof(buf), format, args);
164         va_end(args);
165         strretchomp(buf);
166
167         alertpanel_message(_("Error"), buf, ALERT_ERROR);
168 }
169
170 /*!
171  *\brief        display an error with a View Log button
172  *
173  */
174 void alertpanel_error_log(const gchar *format, ...)
175 {
176         va_list args;
177         int val;
178         MainWindow *mainwin;
179         gchar buf[256];
180
181         va_start(args, format);
182         g_vsnprintf(buf, sizeof(buf), format, args);
183         va_end(args);
184         strretchomp(buf);
185
186         mainwin = mainwindow_get_mainwindow();
187         
188         if (mainwin && mainwin->logwin) {
189                 mainwindow_clear_error(mainwin);
190                 val = alertpanel_full(_("Error"), buf, GTK_STOCK_CLOSE,
191                                       _("_View log"), NULL, FALSE, NULL,
192                                       ALERT_ERROR, G_ALERTDEFAULT);
193                 if (val == G_ALERTALTERNATE)
194                         log_window_show(mainwin->logwin);
195         } else
196                 alertpanel_error(buf);
197 }
198
199 static void alertpanel_show(void)
200 {
201         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
202         manage_window_set_transient(GTK_WINDOW(dialog));
203         value = G_ALERTWAIT;
204
205         if (gdk_pointer_is_grabbed())
206                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
207         inc_lock();
208         while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
209                 gtk_main_iteration();
210
211         gtk_widget_destroy(dialog);
212         GTK_EVENTS_FLUSH();
213
214         alertpanel_is_open = FALSE;
215         inc_unlock();
216 }
217
218 static void alertpanel_create(const gchar *title,
219                               const gchar *message,
220                               const gchar *button1_label,
221                               const gchar *button2_label,
222                               const gchar *button3_label,
223                               gboolean     can_disable,
224                               GtkWidget   *custom_widget,
225                               gint         alert_type,
226                               AlertValue   default_value)
227 {
228         static PangoFontDescription *font_desc;
229         GtkWidget *image;
230         GtkWidget *label;
231         GtkWidget *hbox;
232         GtkWidget *vbox;
233         GtkWidget *disable_checkbtn;
234         GtkWidget *confirm_area;
235         GtkWidget *button1;
236         GtkWidget *button2;
237         GtkWidget *button3;
238         const gchar *label2;
239         const gchar *label3;
240         
241         gchar *title_full = g_strdup_printf("<span weight=\"bold\" "
242                                 "size=\"larger\">%s</span>",
243                                 title?title:"");
244
245         debug_print("Creating alert panel dialog...\n");
246
247         dialog = gtk_dialog_new();
248         gtk_window_set_title(GTK_WINDOW(dialog), title);
249         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
250
251         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
252         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
253         
254         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
255         g_signal_connect(G_OBJECT(dialog), "delete_event",
256                          G_CALLBACK(alertpanel_deleted),
257                          (gpointer)G_ALERTCANCEL);
258         g_signal_connect(G_OBJECT(dialog), "key_press_event",
259                          G_CALLBACK(alertpanel_close),
260                          (gpointer)G_ALERTCANCEL);
261
262         /* for title icon, label and message */
263         hbox = gtk_hbox_new(FALSE, 12);
264         gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
265         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
266                            hbox, FALSE, FALSE, 0);
267
268         /* title icon */
269         switch (alert_type) {
270         case ALERT_QUESTION:
271                 image = gtk_image_new_from_stock
272                         (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
273                 break;
274         case ALERT_WARNING:
275                 image = gtk_image_new_from_stock
276                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
277                 break;
278         case ALERT_ERROR:
279                 image = gtk_image_new_from_stock
280                         (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
281                 break;
282         case ALERT_NOTICE:
283         default:
284                 image = gtk_image_new_from_stock
285                         (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
286                 break;
287         }
288         gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
289         gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
290
291         vbox = gtk_vbox_new (FALSE, 12);
292         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
293         gtk_widget_show (vbox);
294         
295         label = gtk_label_new(title_full);
296         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
297         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
298         gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
299         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
300         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
301         if (!font_desc) {
302                 gint size;
303
304                 size = pango_font_description_get_size
305                         (label->style->font_desc);
306                 font_desc = pango_font_description_new();
307                 pango_font_description_set_weight
308                         (font_desc, PANGO_WEIGHT_BOLD);
309                 pango_font_description_set_size
310                         (font_desc, size * PANGO_SCALE_LARGE);
311         }
312         if (font_desc)
313                 gtk_widget_modify_font(label, font_desc);
314         g_free(title_full);
315         
316         label = gtk_label_new(message);
317         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
318         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
319         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
320         gtk_label_set_selectable(GTK_LABEL(label), TRUE);
321         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
322         GTK_WIDGET_UNSET_FLAGS(label, GTK_CAN_FOCUS);
323         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
324         gtk_widget_show(label);
325                 
326         /* Claws: custom widget */
327         if (custom_widget) {
328                 gtk_box_pack_start(GTK_BOX(vbox), custom_widget, FALSE,
329                                    FALSE, 0);
330         }
331
332         if (can_disable) {
333                 hbox = gtk_hbox_new(FALSE, 0);
334                 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
335                                    FALSE, FALSE, 0);
336
337                 disable_checkbtn = gtk_check_button_new_with_label
338                         (_("Show this message next time"));
339                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_checkbtn),
340                                              TRUE);
341                 gtk_box_pack_start(GTK_BOX(hbox), disable_checkbtn,
342                                    FALSE, FALSE, 12);
343                 g_signal_connect(G_OBJECT(disable_checkbtn), "toggled",
344                                  G_CALLBACK(alertpanel_button_toggled),
345                                  GUINT_TO_POINTER(G_ALERTDISABLE));
346         }
347
348         /* for button(s) */
349         if (!button1_label)
350                 button1_label = GTK_STOCK_OK;
351         label2 = button2_label;
352         label3 = button3_label;
353         if (label2 && *label2 == '+') label2++;
354         if (label3 && *label3 == '+') label3++;
355
356         gtkut_stock_button_set_create(&confirm_area,
357                                       &button1, button1_label,
358                                       button2_label ? &button2 : NULL, label2,
359                                       button3_label ? &button3 : NULL, label3);
360
361         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
362                          confirm_area, FALSE, FALSE, 0);
363         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
364         gtk_widget_grab_default(button1);
365         gtk_widget_grab_focus(button1);
366         if (button2_label &&
367             (default_value == G_ALERTALTERNATE || *button2_label == '+')) {
368                 gtk_widget_grab_default(button2);
369                 gtk_widget_grab_focus(button2);
370         }
371         if (button3_label &&
372             (default_value == G_ALERTOTHER || *button3_label == '+')) {
373                 gtk_widget_grab_default(button3);
374                 gtk_widget_grab_focus(button3);
375         }
376
377         g_signal_connect(G_OBJECT(button1), "clicked",
378                          G_CALLBACK(alertpanel_button_clicked),
379                          GUINT_TO_POINTER(G_ALERTDEFAULT));
380         if (button2_label)
381                 g_signal_connect(G_OBJECT(button2), "clicked",
382                                  G_CALLBACK(alertpanel_button_clicked),
383                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
384         if (button3_label)
385                 g_signal_connect(G_OBJECT(button3), "clicked",
386                                  G_CALLBACK(alertpanel_button_clicked),
387                                  GUINT_TO_POINTER(G_ALERTOTHER));
388
389         gtk_widget_show_all(dialog);
390 }
391
392 static void alertpanel_button_toggled(GtkToggleButton *button,
393                                       gpointer data)
394 {
395         if (gtk_toggle_button_get_active(button))
396                 value &= ~GPOINTER_TO_UINT(data);
397         else
398                 value |= GPOINTER_TO_UINT(data);
399 }
400
401 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
402 {
403         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
404 }
405
406 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
407                                gpointer data)
408 {
409         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
410         return TRUE;
411 }
412
413 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
414                                  gpointer data)
415 {
416         if (event->type == GDK_KEY_PRESS)
417                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
418                         return FALSE;
419
420         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
421         return FALSE;
422 }