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