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