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