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