2005-02-24 [colin] 1.0.1cvs15.11
[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                                            gint alert_type)
128 {
129         if (alertpanel_is_open)
130                 return 0;
131         else
132                 alertpanel_is_open = TRUE;
133
134         alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL,
135                           alert_type);
136         alertpanel_show();
137
138         return value;
139 }
140
141 void alertpanel_notice(const gchar *format, ...)
142 {
143         va_list args;
144         gchar buf[256];
145
146         va_start(args, format);
147         g_vsnprintf(buf, sizeof(buf), format, args);
148         va_end(args);
149         strretchomp(buf);
150
151         alertpanel_message(_("Notice"), buf, ALERT_NOTICE);
152 }
153
154 void alertpanel_warning(const gchar *format, ...)
155 {
156         va_list args;
157         gchar buf[256];
158
159         va_start(args, format);
160         g_vsnprintf(buf, sizeof(buf), format, args);
161         va_end(args);
162         strretchomp(buf);
163
164         alertpanel_message(_("Warning"), buf, ALERT_WARNING);
165 }
166
167 void alertpanel_error(const gchar *format, ...)
168 {
169         va_list args;
170         gchar buf[256];
171
172         va_start(args, format);
173         g_vsnprintf(buf, sizeof(buf), format, args);
174         va_end(args);
175         strretchomp(buf);
176
177         alertpanel_message(_("Error"), buf, ALERT_ERROR);
178 }
179
180 /*!
181  *\brief        display an error with a View Log button
182  *
183  */
184 void alertpanel_error_log(const gchar *format, ...)
185 {
186         va_list args;
187         int val;
188         MainWindow *mainwin;
189         gchar buf[256];
190
191         va_start(args, format);
192         g_vsnprintf(buf, sizeof(buf), format, args);
193         va_end(args);
194         strretchomp(buf);
195
196         mainwin = mainwindow_get_mainwindow();
197         
198         if (mainwin && mainwin->logwin) {
199                 val = alertpanel_with_type(_("Error"), buf, _("OK"), _("View log"), NULL, NULL, ALERT_ERROR);
200                 if (val == G_ALERTALTERNATE)
201                         log_window_show(mainwin->logwin);
202         } else
203                 alertpanel_error(buf);
204 }
205
206 static void alertpanel_show(void)
207 {
208         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
209         manage_window_set_transient(GTK_WINDOW(dialog));
210         value = G_ALERTWAIT;
211
212         if (gdk_pointer_is_grabbed())
213                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
214         inc_lock();
215         while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
216                 gtk_main_iteration();
217
218         gtk_widget_destroy(dialog);
219         GTK_EVENTS_FLUSH();
220
221         alertpanel_is_open = FALSE;
222         inc_unlock();
223 }
224
225 static void alertpanel_create(const gchar *title,
226                               const gchar *message,
227                               const gchar *button1_label,
228                               const gchar *button2_label,
229                               const gchar *button3_label,
230                               gboolean     can_disable,
231                               GtkWidget   *custom_widget,
232                               gint         alert_type)
233 {
234         static PangoFontDescription *font_desc;
235         GtkWidget *label;
236         GtkWidget *w_hbox;
237         GtkWidget *hbox;
238         GtkWidget *vbox;
239         GtkWidget *disable_chkbtn;
240         GtkWidget *confirm_area;
241         GtkWidget *button1;
242         GtkWidget *button2;
243         GtkWidget *button3;
244         GtkWidget *icon;
245         const gchar *label2;
246         const gchar *label3;
247         gchar *title_full = g_strdup_printf("<span weight=\"bold\" "
248                                 "size=\"larger\">%s</span>",
249                                 title?title:"");
250
251         gchar *icon_desc[] = {  GTK_STOCK_DIALOG_INFO,
252                                 GTK_STOCK_DIALOG_QUESTION,
253                                 GTK_STOCK_DIALOG_WARNING,
254                                 GTK_STOCK_DIALOG_ERROR };
255
256         debug_print("Creating alert panel dialog...\n");
257
258         dialog = gtk_dialog_new();
259         gtk_window_set_title(GTK_WINDOW(dialog), title);
260         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
261         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
262         gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
263         
264         gtk_container_set_border_width
265                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
266         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
267         g_signal_connect(G_OBJECT(dialog), "delete_event",
268                          G_CALLBACK(alertpanel_deleted),
269                          (gpointer)G_ALERTOTHER);
270         g_signal_connect(G_OBJECT(dialog), "key_press_event",
271                          G_CALLBACK(alertpanel_close),
272                          (gpointer)G_ALERTOTHER);
273         gtk_widget_realize(dialog);
274
275         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
276         hbox = gtk_hbox_new (FALSE, 12);
277         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
278         gtk_widget_show (hbox);
279         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
280                             FALSE, FALSE, 0);
281
282         /* for title label */
283         w_hbox = gtk_hbox_new(FALSE, 0);
284         
285         if (alert_type < 0 || alert_type > 3)
286                 alert_type = 0;
287         
288         icon = gtk_image_new_from_stock(icon_desc[alert_type],
289                                         GTK_ICON_SIZE_DIALOG); 
290         gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
291         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
292         
293         vbox = gtk_vbox_new (FALSE, 12);
294         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
295         gtk_widget_show (vbox);
296         
297         label = gtk_label_new(title_full);
298         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
299         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
300         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
301         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
302         if (!font_desc) {
303                 gint size;
304
305                 size = pango_font_description_get_size(label->style->font_desc);
306                 font_desc = pango_font_description_new();
307                 pango_font_description_set_weight(font_desc, PANGO_WEIGHT_BOLD);
308                 pango_font_description_set_size
309                         (font_desc, size * PANGO_SCALE_X_LARGE);
310         }
311         if (font_desc)
312                 gtk_widget_modify_font(label, font_desc);
313         gtk_widget_show(label);
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_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
320         gtk_widget_show(label);
321                 
322         /* Claws: custom widget */
323         if (custom_widget) {
324                 gtk_box_pack_start(GTK_BOX(vbox), custom_widget, FALSE,
325                                    FALSE, 0);
326         }
327         
328         if (can_disable) {
329                 disable_chkbtn = gtk_check_button_new_with_label
330                         (_("Show this message next time"));
331                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
332                                              TRUE);
333                 gtk_box_pack_start(GTK_BOX(vbox), disable_chkbtn,
334                                    FALSE, FALSE, 0);
335                 g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
336                                  G_CALLBACK(alertpanel_button_toggled),
337                                  GUINT_TO_POINTER(G_ALERTDISABLE));
338         } 
339
340         /* for button(s) */
341         if (!button1_label)
342                 button1_label = GTK_STOCK_OK;
343         label2 = button2_label;
344         label3 = button3_label;
345         if (label2 && *label2 == '+') label2++;
346         if (label3 && *label3 == '+') label3++;
347
348         gtkut_stock_button_set_create(&confirm_area,
349                                       &button1, button1_label,
350                                       button2_label ? &button2 : NULL, label2,
351                                       button3_label ? &button3 : NULL, label3);
352
353         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), confirm_area, FALSE, FALSE, 12);
354         gtk_widget_grab_default(button1);
355         gtk_widget_grab_focus(button1);
356         if (button2_label && *button2_label == '+') {
357                 gtk_widget_grab_default(button2);
358                 gtk_widget_grab_focus(button2);
359         }
360         if (button3_label && *button3_label == '+') {
361                 gtk_widget_grab_default(button3);
362                 gtk_widget_grab_focus(button3);
363         }
364
365         g_signal_connect(G_OBJECT(button1), "clicked",
366                          G_CALLBACK(alertpanel_button_clicked),
367                          GUINT_TO_POINTER(G_ALERTDEFAULT));
368         if (button2_label)
369                 g_signal_connect(G_OBJECT(button2), "clicked",
370                                  G_CALLBACK(alertpanel_button_clicked),
371                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
372         if (button3_label)
373                 g_signal_connect(G_OBJECT(button3), "clicked",
374                                  G_CALLBACK(alertpanel_button_clicked),
375                                  GUINT_TO_POINTER(G_ALERTOTHER));
376
377         gtk_widget_show_all(dialog);
378 }
379
380 static void alertpanel_button_toggled(GtkToggleButton *button,
381                                       gpointer data)
382 {
383         if (gtk_toggle_button_get_active(button))
384                 value &= ~GPOINTER_TO_UINT(data);
385         else
386                 value |= GPOINTER_TO_UINT(data);
387 }
388
389 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
390 {
391         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
392 }
393
394 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
395                                gpointer data)
396 {
397         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
398         return TRUE;
399 }
400
401 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
402                              gpointer data)
403 {
404         if (event->type == GDK_KEY_PRESS)
405                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
406                         return FALSE;
407
408         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
409         return FALSE;
410 }