src/account.[ch]
[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 *vbox2;
201         GtkWidget *disable_chkbtn;
202         GtkWidget *confirm_area;
203         GtkWidget *button1;
204         GtkWidget *button2;
205         GtkWidget *button3;
206         const gchar *label2;
207         const gchar *label3;
208
209         debug_print(_("Creating alert panel dialog...\n"));
210
211         dialog = gtk_dialog_new();
212         gtk_window_set_title(GTK_WINDOW(dialog), title);
213         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
214         gtk_container_set_border_width
215                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
216         gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
217         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
218                            GTK_SIGNAL_FUNC(alertpanel_deleted),
219                            (gpointer)G_ALERTOTHER);
220         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
221                            GTK_SIGNAL_FUNC(alertpanel_close),
222                            (gpointer)G_ALERTOTHER);
223         gtk_widget_realize(dialog);
224
225         /* for title label */
226         hbox = gtk_hbox_new(FALSE, 0);
227         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
228                            hbox, TRUE, TRUE, 16);
229
230         /* title label */
231         /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
232         /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
233         label = gtk_label_new(title);
234         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
235         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
236         style = gtk_style_copy(gtk_widget_get_style(label));
237         if (!titlefont)
238                 titlefont = gdk_fontset_load(TITLE_FONT);
239         if (titlefont)
240                 style->font = titlefont;
241         gtk_widget_set_style(label, style);
242
243         /* for message and button(s) */
244         vbox = gtk_vbox_new(FALSE, 0);
245         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
246                           vbox);
247         vbox2 = gtk_vbox_new(FALSE, 0);
248         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 20);
249         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
250
251         /* for message label */
252         hbox = gtk_hbox_new(FALSE, 0);
253         gtk_box_pack_start(GTK_BOX(vbox2), hbox, FALSE, FALSE, 0);
254
255         /* message label */
256         label = gtk_label_new(message);
257         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 12);
258         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
259         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
260
261         /* custom widget */
262         if (custom_widget) {
263                 GtkWidget *hbox2 = gtk_hbox_new(FALSE, 0);
264                 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
265                 gtk_box_pack_start(GTK_BOX(hbox2), custom_widget, FALSE, FALSE, 
266                                    12);
267         }
268         /* for button(s) */
269         if (!button1_label)
270                 button1_label = _("OK");
271         label2 = button2_label;
272         label3 = button3_label;
273         if (label2 && *label2 == '+') label2++;
274         if (label3 && *label3 == '+') label3++;
275
276         gtkut_button_set_create(&confirm_area,
277                                 &button1, button1_label,
278                                 button2_label ? &button2 : NULL, label2,
279                                 button3_label ? &button3 : NULL, label3);
280
281         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
282         gtk_widget_grab_default(button1);
283         gtk_widget_grab_focus(button1);
284         if (button2_label && *button2_label == '+') {
285                 gtk_widget_grab_default(button2);
286                 gtk_widget_grab_focus(button2);
287         }
288         if (button3_label && *button3_label == '+') {
289                 gtk_widget_grab_default(button3);
290                 gtk_widget_grab_focus(button3);
291         }
292
293         gtk_signal_connect(GTK_OBJECT(button1), "clicked",
294                            GTK_SIGNAL_FUNC(alertpanel_button_clicked),
295                            GUINT_TO_POINTER(G_ALERTDEFAULT));
296         if (button2_label)
297                 gtk_signal_connect(GTK_OBJECT(button2), "clicked",
298                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
299                                    GUINT_TO_POINTER(G_ALERTALTERNATE));
300         if (button3_label)
301                 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
302                                    GTK_SIGNAL_FUNC(alertpanel_button_clicked),
303                                    GUINT_TO_POINTER(G_ALERTOTHER));
304
305         if (can_disable) {
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_end(GTK_BOX(vbox), 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         }
316
317         gtk_widget_show_all(dialog);
318 }
319
320 static void alertpanel_button_toggled(GtkToggleButton *button,
321                                       gpointer data)
322 {
323         if (gtk_toggle_button_get_active(button))
324                 value &= ~GPOINTER_TO_UINT(data);
325         else
326                 value |= GPOINTER_TO_UINT(data);
327 }
328
329 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
330 {
331         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
332 }
333
334 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
335                                gpointer data)
336 {
337         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
338         return TRUE;
339 }
340
341 static void alertpanel_close(GtkWidget *widget, GdkEventAny *event,
342                              gpointer data)
343 {
344         if (event->type == GDK_KEY_PRESS)
345                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
346                         return;
347
348         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
349 }