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