72dd2b25d07d9eb69ad41c61be22009be19caca2
[claws.git] / src / alertpanel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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[256];
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                 val = alertpanel_full(_("Error"), buf, GTK_STOCK_CLOSE,
190                                       _("_View log"), NULL, FALSE, NULL,
191                                       ALERT_ERROR, G_ALERTDEFAULT);
192                 if (val == G_ALERTALTERNATE)
193                         log_window_show(mainwin->logwin);
194         } else
195                 alertpanel_error(buf);
196 }
197
198 static void alertpanel_show(void)
199 {
200         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
201         manage_window_set_transient(GTK_WINDOW(dialog));
202         value = G_ALERTWAIT;
203
204         if (gdk_pointer_is_grabbed())
205                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
206         inc_lock();
207         while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
208                 gtk_main_iteration();
209
210         gtk_widget_destroy(dialog);
211         GTK_EVENTS_FLUSH();
212
213         alertpanel_is_open = FALSE;
214         inc_unlock();
215 }
216
217 static void alertpanel_create(const gchar *title,
218                               const gchar *message,
219                               const gchar *button1_label,
220                               const gchar *button2_label,
221                               const gchar *button3_label,
222                               gboolean     can_disable,
223                               GtkWidget   *custom_widget,
224                               gint         alert_type,
225                               AlertValue   default_value)
226 {
227         static PangoFontDescription *font_desc;
228         GtkWidget *image;
229         GtkWidget *label;
230         GtkWidget *hbox;
231         GtkWidget *vbox;
232         GtkWidget *disable_chkbtn;
233         GtkWidget *confirm_area;
234         GtkWidget *button1;
235         GtkWidget *button2;
236         GtkWidget *button3;
237         const gchar *label2;
238         const gchar *label3;
239         
240         gchar *title_full = g_strdup_printf("<span weight=\"bold\" "
241                                 "size=\"larger\">%s</span>",
242                                 title?title:"");
243
244         debug_print("Creating alert panel dialog...\n");
245
246         dialog = gtk_dialog_new();
247         gtk_window_set_title(GTK_WINDOW(dialog), title);
248         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
249
250         gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 100);
251         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
252         
253         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
254         g_signal_connect(G_OBJECT(dialog), "delete_event",
255                          G_CALLBACK(alertpanel_deleted),
256                          (gpointer)G_ALERTCANCEL);
257         g_signal_connect(G_OBJECT(dialog), "key_press_event",
258                          G_CALLBACK(alertpanel_close),
259                          (gpointer)G_ALERTCANCEL);
260
261         /* for title icon, label and message */
262         hbox = gtk_hbox_new(FALSE, 12);
263         gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
264         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
265                            hbox, FALSE, FALSE, 0);
266
267         /* title icon */
268         switch (alert_type) {
269         case ALERT_QUESTION:
270                 image = gtk_image_new_from_stock
271                         (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
272                 break;
273         case ALERT_WARNING:
274                 image = gtk_image_new_from_stock
275                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
276                 break;
277         case ALERT_ERROR:
278                 image = gtk_image_new_from_stock
279                         (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
280                 break;
281         case ALERT_NOTICE:
282         default:
283                 image = gtk_image_new_from_stock
284                         (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
285                 break;
286         }
287         gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
288         gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
289
290         vbox = gtk_vbox_new (FALSE, 12);
291         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
292         gtk_widget_show (vbox);
293         
294         label = gtk_label_new(title_full);
295         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
296         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
297         gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
298         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
299         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
300         if (!font_desc) {
301                 gint size;
302
303                 size = pango_font_description_get_size
304                         (label->style->font_desc);
305                 font_desc = pango_font_description_new();
306                 pango_font_description_set_weight
307                         (font_desc, PANGO_WEIGHT_BOLD);
308                 pango_font_description_set_size
309                         (font_desc, size * PANGO_SCALE_LARGE);
310         }
311         if (font_desc)
312                 gtk_widget_modify_font(label, font_desc);
313         g_free(title_full);
314         
315         label = gtk_label_new(message);
316         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
317         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
318         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
319         gtk_label_set_selectable(GTK_LABEL(label), TRUE);
320         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
321         GTK_WIDGET_UNSET_FLAGS(label, GTK_CAN_FOCUS);
322         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
323         gtk_widget_show(label);
324                 
325         /* Claws: custom widget */
326         if (custom_widget) {
327                 gtk_box_pack_start(GTK_BOX(vbox), custom_widget, FALSE,
328                                    FALSE, 0);
329         }
330
331         if (can_disable) {
332                 hbox = gtk_hbox_new(FALSE, 0);
333                 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
334                                    FALSE, FALSE, 0);
335
336                 disable_chkbtn = gtk_check_button_new_with_label
337                         (_("Show this message next time"));
338                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
339                                              TRUE);
340                 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
341                                    FALSE, FALSE, 12);
342                 g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
343                                  G_CALLBACK(alertpanel_button_toggled),
344                                  GUINT_TO_POINTER(G_ALERTDISABLE));
345         }
346
347         /* for button(s) */
348         if (!button1_label)
349                 button1_label = GTK_STOCK_OK;
350         label2 = button2_label;
351         label3 = button3_label;
352         if (label2 && *label2 == '+') label2++;
353         if (label3 && *label3 == '+') label3++;
354
355         gtkut_stock_button_set_create(&confirm_area,
356                                       &button1, button1_label,
357                                       button2_label ? &button2 : NULL, label2,
358                                       button3_label ? &button3 : NULL, label3);
359
360         gtk_widget_set_size_request(confirm_area, 375, -1);
361
362         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
363                          confirm_area, FALSE, FALSE, 0);
364         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
365         gtk_widget_grab_default(button1);
366         gtk_widget_grab_focus(button1);
367         if (button2_label &&
368             (default_value == G_ALERTALTERNATE || *button2_label == '+')) {
369                 gtk_widget_grab_default(button2);
370                 gtk_widget_grab_focus(button2);
371         }
372         if (button3_label &&
373             (default_value == G_ALERTOTHER || *button3_label == '+')) {
374                 gtk_widget_grab_default(button3);
375                 gtk_widget_grab_focus(button3);
376         }
377
378         g_signal_connect(G_OBJECT(button1), "clicked",
379                          G_CALLBACK(alertpanel_button_clicked),
380                          GUINT_TO_POINTER(G_ALERTDEFAULT));
381         if (button2_label)
382                 g_signal_connect(G_OBJECT(button2), "clicked",
383                                  G_CALLBACK(alertpanel_button_clicked),
384                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
385         if (button3_label)
386                 g_signal_connect(G_OBJECT(button3), "clicked",
387                                  G_CALLBACK(alertpanel_button_clicked),
388                                  GUINT_TO_POINTER(G_ALERTOTHER));
389
390         gtk_widget_show_all(dialog);
391 }
392
393 static void alertpanel_button_toggled(GtkToggleButton *button,
394                                       gpointer data)
395 {
396         if (gtk_toggle_button_get_active(button))
397                 value &= ~GPOINTER_TO_UINT(data);
398         else
399                 value |= GPOINTER_TO_UINT(data);
400 }
401
402 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
403 {
404         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
405 }
406
407 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
408                                gpointer data)
409 {
410         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
411         return TRUE;
412 }
413
414 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
415                                  gpointer data)
416 {
417         if (event->type == GDK_KEY_PRESS)
418                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
419                         return FALSE;
420
421         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
422         return FALSE;
423 }