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