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