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