todo
[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 <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "intl.h"
28 #include "mainwindow.h"
29 #include "alertpanel.h"
30 #include "manage_window.h"
31 #include "utils.h"
32 #include "gtkutils.h"
33 #include "inc.h"
34 #include "log.h"
35 #include "logwindow.h"
36
37 #define TITLE_FONT      "-*-helvetica-medium-r-normal--17-*-*-*-*-*-*-*," \
38                         "-*-*-medium-r-normal--16-*-*-*-*-*-*-*,*"
39 #define MESSAGE_FONT    "-*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*," \
40                         "-*-*-medium-r-normal--14-*-*-*-*-*-*-*,*"
41 #define ALERT_PANEL_WIDTH       380
42 #define TITLE_HEIGHT            72
43 #define MESSAGE_HEIGHT          62
44
45 static gboolean alertpanel_is_open = FALSE;
46 static AlertValue value;
47
48 static GtkWidget *dialog;
49
50 static void alertpanel_show             (void);
51 static void alertpanel_create           (const gchar    *title,
52                                          const gchar    *message,
53                                          const gchar    *button1_label,
54                                          const gchar    *button2_label,
55                                          const gchar    *button3_label,
56                                          gboolean        can_disable,
57                                          GtkWidget      *custom_widget);
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 void 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);
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);
102         alertpanel_show();
103
104         debug_print("return value = %d\n", value);
105         return value;
106 }
107 void alertpanel_message(const gchar *title, const gchar *message)
108 {
109         if (alertpanel_is_open)
110                 return;
111         else
112                 alertpanel_is_open = TRUE;
113
114         alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL);
115         alertpanel_show();
116 }
117
118 AlertValue alertpanel_message_with_disable(const gchar *title,
119                                            const gchar *message)
120 {
121         if (alertpanel_is_open)
122                 return 0;
123         else
124                 alertpanel_is_open = TRUE;
125
126         alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL);
127         alertpanel_show();
128
129         return value;
130 }
131
132 void alertpanel_notice(const gchar *format, ...)
133 {
134         va_list args;
135         gchar buf[256];
136
137         va_start(args, format);
138         g_vsnprintf(buf, sizeof(buf), format, args);
139         va_end(args);
140         strretchomp(buf);
141
142         alertpanel_message(_("Notice"), buf);
143 }
144
145 void alertpanel_warning(const gchar *format, ...)
146 {
147         va_list args;
148         gchar buf[256];
149
150         va_start(args, format);
151         g_vsnprintf(buf, sizeof(buf), format, args);
152         va_end(args);
153         strretchomp(buf);
154
155         alertpanel_message(_("Warning"), buf);
156 }
157
158 void alertpanel_error(const gchar *format, ...)
159 {
160         va_list args;
161         gchar buf[256];
162
163         va_start(args, format);
164         g_vsnprintf(buf, sizeof(buf), format, args);
165         va_end(args);
166         strretchomp(buf);
167
168         alertpanel_message(_("Error"), buf);
169 }
170
171 /*!
172  *\brief        display an error with a View Log button
173  *
174  */
175 void alertpanel_error_log(const gchar *format, ...)
176 {
177         va_list args;
178         int val;
179         MainWindow *mainwin;
180         gchar buf[256];
181
182         va_start(args, format);
183         g_vsnprintf(buf, sizeof(buf), format, args);
184         va_end(args);
185         strretchomp(buf);
186
187         mainwin = mainwindow_get_mainwindow();
188         
189         if (mainwin && mainwin->logwin) {
190                 val = alertpanel(_("Error"), buf, _("OK"), _("View log"), NULL);
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 {
224         static GdkFont *titlefont;
225         GtkStyle *style;
226         GtkWidget *label;
227         GtkWidget *hbox;
228         GtkWidget *vbox;
229         GtkWidget *spc_vbox;
230         GtkWidget *msg_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         debug_print("Creating alert panel dialog...\n");
240
241         dialog = gtk_dialog_new();
242         gtk_window_set_title(GTK_WINDOW(dialog), title);
243         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
244         gtk_container_set_border_width
245                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
246         gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
247         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
248                            GTK_SIGNAL_FUNC(alertpanel_deleted),
249                            (gpointer)G_ALERTOTHER);
250         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
251                            GTK_SIGNAL_FUNC(alertpanel_close),
252                            (gpointer)G_ALERTOTHER);
253         gtk_widget_realize(dialog);
254
255         /* for title label */
256         hbox = gtk_hbox_new(FALSE, 0);
257         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
258                            hbox, TRUE, TRUE, 16);
259
260         /* title label */
261         /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
262         /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
263         label = gtk_label_new(title);
264         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
265         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
266         style = gtk_style_copy(gtk_widget_get_style(label));
267         if (!titlefont)
268                 titlefont = gtkut_font_load(TITLE_FONT);
269         if (titlefont)
270                 style->font = titlefont;
271         gtk_widget_set_style(label, style);
272
273         /* for message and button(s) */
274         vbox = gtk_vbox_new(FALSE, 0);
275         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
276                           vbox);
277
278         spc_vbox = gtk_vbox_new(FALSE, 0);
279         gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
280         gtk_widget_set_usize(spc_vbox, -1, 16);
281
282         msg_vbox = gtk_vbox_new(FALSE, 0);
283         gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
284
285         /* for message label */
286         hbox = gtk_hbox_new(FALSE, 0);
287         gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
288
289         /* message label */
290         label = gtk_label_new(message);
291         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
292         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
293
294         /* Claws: custom widget */
295         if (custom_widget) {
296                 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
297                 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
298                                    FALSE, 0);
299                 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
300                                    FALSE, 24);
301         }
302         if (can_disable) {
303                 hbox = gtk_hbox_new(FALSE, 0);
304                 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
305                 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
306
307                 disable_chkbtn = gtk_check_button_new_with_label
308                         (_("Show this message next time"));
309                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
310                                              TRUE);
311                 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
312                                    FALSE, FALSE, 0);
313                 gtk_signal_connect(GTK_OBJECT(disable_chkbtn), "toggled",
314                                    GTK_SIGNAL_FUNC(alertpanel_button_toggled),
315                                    GUINT_TO_POINTER(G_ALERTDISABLE));
316         } else {
317                 spc_vbox = gtk_vbox_new(FALSE, 0);
318                 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
319                 gtk_widget_set_usize(spc_vbox, -1, 20);
320         }
321
322         /* for button(s) */
323         if (!button1_label)
324                 button1_label = _("OK");
325         label2 = button2_label;
326         label3 = button3_label;
327         if (label2 && *label2 == '+') label2++;
328         if (label3 && *label3 == '+') label3++;
329
330         gtkut_button_set_create(&confirm_area,
331                                 &button1, button1_label,
332                                 button2_label ? &button2 : NULL, label2,
333                                 button3_label ? &button3 : NULL, label3);
334
335         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
336         gtk_widget_grab_default(button1);
337         gtk_widget_grab_focus(button1);
338         if (button2_label && *button2_label == '+') {
339                 gtk_widget_grab_default(button2);
340                 gtk_widget_grab_focus(button2);
341         }
342         if (button3_label && *button3_label == '+') {
343                 gtk_widget_grab_default(button3);
344                 gtk_widget_grab_focus(button3);
345         }
346
347         gtk_signal_connect(GTK_OBJECT(button1), "clicked",
348                            GTK_SIGNAL_FUNC(alertpanel_button_clicked),
349                            GUINT_TO_POINTER(G_ALERTDEFAULT));
350         if (button2_label)
351                 gtk_signal_connect(GTK_OBJECT(button2), "clicked",
352                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
353                                    GUINT_TO_POINTER(G_ALERTALTERNATE));
354         if (button3_label)
355                 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
356                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
357                                    GUINT_TO_POINTER(G_ALERTOTHER));
358
359         gtk_widget_show_all(dialog);
360 }
361
362 static void alertpanel_button_toggled(GtkToggleButton *button,
363                                       gpointer data)
364 {
365         if (gtk_toggle_button_get_active(button))
366                 value &= ~GPOINTER_TO_UINT(data);
367         else
368                 value |= GPOINTER_TO_UINT(data);
369 }
370
371 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
372 {
373         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
374 }
375
376 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
377                                gpointer data)
378 {
379         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
380         return TRUE;
381 }
382
383 static void alertpanel_close(GtkWidget *widget, GdkEventAny *event,
384                              gpointer data)
385 {
386         if (event->type == GDK_KEY_PRESS)
387                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
388                         return;
389
390         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
391 }