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