0.9.6claws24
[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 "logwindow.h"
35
36 #define TITLE_FONT      "-*-helvetica-medium-r-normal--17-*-*-*-*-*-*-*," \
37                         "-*-*-medium-r-normal--16-*-*-*-*-*-*-*,*"
38 #define MESSAGE_FONT    "-*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*," \
39                         "-*-*-medium-r-normal--14-*-*-*-*-*-*-*,*"
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
58 static void alertpanel_button_toggled   (GtkToggleButton        *button,
59                                          gpointer                data);
60 static void alertpanel_button_clicked   (GtkWidget              *widget,
61                                          gpointer                data);
62 static gint alertpanel_deleted          (GtkWidget              *widget,
63                                          GdkEventAny            *event,
64                                          gpointer                data);
65 static void alertpanel_close            (GtkWidget              *widget,
66                                          GdkEventAny            *event,
67                                          gpointer                data);
68
69 AlertValue alertpanel(const gchar *title,
70                       const gchar *message,
71                       const gchar *button1_label,
72                       const gchar *button2_label,
73                       const gchar *button3_label)
74 {
75         if (alertpanel_is_open)
76                 return -1;
77         else
78                 alertpanel_is_open = TRUE;
79
80         alertpanel_create(title, message, button1_label, button2_label,
81                           button3_label, FALSE, NULL);
82         alertpanel_show();
83
84         debug_print("return value = %d\n", value);
85         return value;
86 }
87
88 AlertValue alertpanel_with_widget(const gchar *title,
89                                   const gchar *message,
90                                   const gchar *button1_label,
91                                   const gchar *button2_label,
92                                   const gchar *button3_label,
93                                   GtkWidget *widget)
94 {
95         if (alertpanel_is_open)
96                 return -1;
97         else
98                 alertpanel_is_open = TRUE;
99         alertpanel_create(title, message, button1_label, button2_label, 
100                           button3_label, FALSE, widget);
101         alertpanel_show();
102
103         debug_print("return value = %d\n", value);
104         return value;
105 }
106 void alertpanel_message(const gchar *title, const gchar *message)
107 {
108         if (alertpanel_is_open)
109                 return;
110         else
111                 alertpanel_is_open = TRUE;
112
113         alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL);
114         alertpanel_show();
115 }
116
117 AlertValue alertpanel_message_with_disable(const gchar *title,
118                                            const gchar *message)
119 {
120         if (alertpanel_is_open)
121                 return 0;
122         else
123                 alertpanel_is_open = TRUE;
124
125         alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL);
126         alertpanel_show();
127
128         return value;
129 }
130
131 void alertpanel_notice(const gchar *format, ...)
132 {
133         va_list args;
134         gchar buf[256];
135
136         va_start(args, format);
137         g_vsnprintf(buf, sizeof(buf), format, args);
138         va_end(args);
139         strretchomp(buf);
140
141         alertpanel_message(_("Notice"), buf);
142 }
143
144 void alertpanel_warning(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(_("Warning"), buf);
155 }
156
157 void alertpanel_error(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(_("Error"), buf);
168 }
169
170 /*!
171  *\brief        display an error with a View Log button
172  *
173  */
174 void alertpanel_error_log(const gchar *format, ...)
175 {
176         va_list args;
177         int val;
178         MainWindow *mainwin;
179         gchar buf[256];
180
181         va_start(args, format);
182         g_vsnprintf(buf, sizeof(buf), format, args);
183         va_end(args);
184         strretchomp(buf);
185
186         mainwin = mainwindow_get_mainwindow();
187         
188         if (mainwin && mainwin->logwin) {
189                 val = alertpanel(_("Error"), buf, _("OK"), _("View log"), NULL);
190                 if (val == G_ALERTALTERNATE)
191                         log_window_show(mainwin->logwin);
192         } else
193                 alertpanel_error(buf);
194 }
195
196 static void alertpanel_show(void)
197 {
198         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
199         manage_window_set_transient(GTK_WINDOW(dialog));
200         value = G_ALERTWAIT;
201
202         if (gdk_pointer_is_grabbed())
203                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
204         inc_lock();
205         while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
206                 gtk_main_iteration();
207
208         gtk_widget_destroy(dialog);
209         GTK_EVENTS_FLUSH();
210
211         alertpanel_is_open = FALSE;
212         inc_unlock();
213 }
214
215 static void alertpanel_create(const gchar *title,
216                               const gchar *message,
217                               const gchar *button1_label,
218                               const gchar *button2_label,
219                               const gchar *button3_label,
220                               gboolean     can_disable,
221                               GtkWidget *custom_widget)
222 {
223         static GdkFont *titlefont;
224         GtkStyle *style;
225         GtkWidget *label;
226         GtkWidget *hbox;
227         GtkWidget *vbox;
228         GtkWidget *spc_vbox;
229         GtkWidget *msg_vbox;
230         GtkWidget *disable_chkbtn;
231         GtkWidget *confirm_area;
232         GtkWidget *button1;
233         GtkWidget *button2;
234         GtkWidget *button3;
235         const gchar *label2;
236         const gchar *label3;
237
238         debug_print("Creating alert panel dialog...\n");
239
240         dialog = gtk_dialog_new();
241         gtk_window_set_title(GTK_WINDOW(dialog), title);
242         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
243         gtk_container_set_border_width
244                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
245         gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
246         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
247                            GTK_SIGNAL_FUNC(alertpanel_deleted),
248                            (gpointer)G_ALERTOTHER);
249         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
250                            GTK_SIGNAL_FUNC(alertpanel_close),
251                            (gpointer)G_ALERTOTHER);
252         gtk_widget_realize(dialog);
253
254         /* for title label */
255         hbox = gtk_hbox_new(FALSE, 0);
256         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
257                            hbox, TRUE, TRUE, 16);
258
259         /* title label */
260         /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
261         /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
262         label = gtk_label_new(title);
263         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
264         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
265         style = gtk_style_copy(gtk_widget_get_style(label));
266         if (!titlefont)
267                 titlefont = gtkut_font_load(TITLE_FONT);
268         if (titlefont)
269                 style->font = titlefont;
270         gtk_widget_set_style(label, style);
271
272         /* for message and button(s) */
273         vbox = gtk_vbox_new(FALSE, 0);
274         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
275                           vbox);
276
277         spc_vbox = gtk_vbox_new(FALSE, 0);
278         gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
279         gtk_widget_set_usize(spc_vbox, -1, 16);
280
281         msg_vbox = gtk_vbox_new(FALSE, 0);
282         gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
283
284         /* for message label */
285         hbox = gtk_hbox_new(FALSE, 0);
286         gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
287
288         /* message label */
289         label = gtk_label_new(message);
290         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
291         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
292
293         /* Claws: custom widget */
294         if (custom_widget) {
295                 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
296                 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
297                                    FALSE, 0);
298                 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
299                                    FALSE, 24);
300         }
301         if (can_disable) {
302                 hbox = gtk_hbox_new(FALSE, 0);
303                 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
304                 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
305
306                 disable_chkbtn = gtk_check_button_new_with_label
307                         (_("Show this message next time"));
308                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
309                                              TRUE);
310                 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
311                                    FALSE, FALSE, 0);
312                 gtk_signal_connect(GTK_OBJECT(disable_chkbtn), "toggled",
313                                    GTK_SIGNAL_FUNC(alertpanel_button_toggled),
314                                    GUINT_TO_POINTER(G_ALERTDISABLE));
315         } else {
316                 spc_vbox = gtk_vbox_new(FALSE, 0);
317                 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
318                 gtk_widget_set_usize(spc_vbox, -1, 20);
319         }
320
321         /* for button(s) */
322         if (!button1_label)
323                 button1_label = _("OK");
324         label2 = button2_label;
325         label3 = button3_label;
326         if (label2 && *label2 == '+') label2++;
327         if (label3 && *label3 == '+') label3++;
328
329         gtkut_button_set_create(&confirm_area,
330                                 &button1, button1_label,
331                                 button2_label ? &button2 : NULL, label2,
332                                 button3_label ? &button3 : NULL, label3);
333
334         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
335         gtk_widget_grab_default(button1);
336         gtk_widget_grab_focus(button1);
337         if (button2_label && *button2_label == '+') {
338                 gtk_widget_grab_default(button2);
339                 gtk_widget_grab_focus(button2);
340         }
341         if (button3_label && *button3_label == '+') {
342                 gtk_widget_grab_default(button3);
343                 gtk_widget_grab_focus(button3);
344         }
345
346         gtk_signal_connect(GTK_OBJECT(button1), "clicked",
347                            GTK_SIGNAL_FUNC(alertpanel_button_clicked),
348                            GUINT_TO_POINTER(G_ALERTDEFAULT));
349         if (button2_label)
350                 gtk_signal_connect(GTK_OBJECT(button2), "clicked",
351                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
352                                    GUINT_TO_POINTER(G_ALERTALTERNATE));
353         if (button3_label)
354                 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
355                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
356                                    GUINT_TO_POINTER(G_ALERTOTHER));
357
358         gtk_widget_show_all(dialog);
359 }
360
361 static void alertpanel_button_toggled(GtkToggleButton *button,
362                                       gpointer data)
363 {
364         if (gtk_toggle_button_get_active(button))
365                 value &= ~GPOINTER_TO_UINT(data);
366         else
367                 value |= GPOINTER_TO_UINT(data);
368 }
369
370 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
371 {
372         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
373 }
374
375 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
376                                gpointer data)
377 {
378         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
379         return TRUE;
380 }
381
382 static void alertpanel_close(GtkWidget *widget, GdkEventAny *event,
383                              gpointer data)
384 {
385         if (event->type == GDK_KEY_PRESS)
386                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
387                         return;
388
389         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
390 }