This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / alertpanel.c
blob47c4600b63cfa7310a6183fbcf478f6859862688
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
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"
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
42 static gboolean alertpanel_is_open = FALSE;
43 static AlertValue value;
45 static GtkWidget *dialog;
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 GtkWidget *custom_widget);
56 static void alertpanel_button_toggled (GtkToggleButton *button,
57 gpointer data);
58 static void alertpanel_button_clicked (GtkWidget *widget,
59 gpointer data);
60 static gint alertpanel_deleted (GtkWidget *widget,
61 GdkEventAny *event,
62 gpointer data);
63 static void alertpanel_close (GtkWidget *widget,
64 GdkEventAny *event,
65 gpointer data);
67 AlertValue alertpanel(const gchar *title,
68 const gchar *message,
69 const gchar *button1_label,
70 const gchar *button2_label,
71 const gchar *button3_label)
73 if (alertpanel_is_open)
74 return -1;
75 else
76 alertpanel_is_open = TRUE;
78 alertpanel_create(title, message, button1_label, button2_label,
79 button3_label, FALSE, NULL);
80 alertpanel_show();
82 debug_print("return value = %d\n", value);
83 return value;
86 AlertValue alertpanel_with_widget(const gchar *title,
87 const gchar *message,
88 const gchar *button1_label,
89 const gchar *button2_label,
90 const gchar *button3_label,
91 GtkWidget *widget)
93 if (alertpanel_is_open)
94 return -1;
95 else
96 alertpanel_is_open = TRUE;
97 alertpanel_create(title, message, button1_label, button2_label,
98 button3_label, FALSE, widget);
99 alertpanel_show();
101 debug_print("return value = %d\n", value);
102 return value;
104 void alertpanel_message(const gchar *title, const gchar *message)
106 if (alertpanel_is_open)
107 return;
108 else
109 alertpanel_is_open = TRUE;
111 alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL);
112 alertpanel_show();
115 AlertValue alertpanel_message_with_disable(const gchar *title,
116 const gchar *message)
118 if (alertpanel_is_open)
119 return 0;
120 else
121 alertpanel_is_open = TRUE;
123 alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL);
124 alertpanel_show();
126 return value;
129 void alertpanel_notice(const gchar *format, ...)
131 va_list args;
132 gchar buf[256];
134 va_start(args, format);
135 g_vsnprintf(buf, sizeof(buf), format, args);
136 va_end(args);
137 strretchomp(buf);
139 alertpanel_message(_("Notice"), buf);
142 void alertpanel_warning(const gchar *format, ...)
144 va_list args;
145 gchar buf[256];
147 va_start(args, format);
148 g_vsnprintf(buf, sizeof(buf), format, args);
149 va_end(args);
150 strretchomp(buf);
152 alertpanel_message(_("Warning"), buf);
155 void alertpanel_error(const gchar *format, ...)
157 va_list args;
158 gchar buf[256];
160 va_start(args, format);
161 g_vsnprintf(buf, sizeof(buf), format, args);
162 va_end(args);
163 strretchomp(buf);
165 alertpanel_message(_("Error"), buf);
168 static void alertpanel_show(void)
170 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
171 manage_window_set_transient(GTK_WINDOW(dialog));
172 value = G_ALERTWAIT;
174 if (gdk_pointer_is_grabbed())
175 gdk_pointer_ungrab(GDK_CURRENT_TIME);
176 inc_lock();
177 while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
178 gtk_main_iteration();
180 gtk_widget_destroy(dialog);
181 GTK_EVENTS_FLUSH();
183 alertpanel_is_open = FALSE;
184 inc_unlock();
187 static void alertpanel_create(const gchar *title,
188 const gchar *message,
189 const gchar *button1_label,
190 const gchar *button2_label,
191 const gchar *button3_label,
192 gboolean can_disable,
193 GtkWidget *custom_widget)
195 static GdkFont *titlefont;
196 GtkStyle *style;
197 GtkWidget *label;
198 GtkWidget *hbox;
199 GtkWidget *vbox;
200 GtkWidget *spc_vbox;
201 GtkWidget *msg_vbox;
202 GtkWidget *disable_chkbtn;
203 GtkWidget *confirm_area;
204 GtkWidget *button1;
205 GtkWidget *button2;
206 GtkWidget *button3;
207 const gchar *label2;
208 const gchar *label3;
210 debug_print("Creating alert panel dialog...\n");
212 dialog = gtk_dialog_new();
213 gtk_window_set_title(GTK_WINDOW(dialog), title);
214 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
215 gtk_container_set_border_width
216 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
217 gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
218 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
219 GTK_SIGNAL_FUNC(alertpanel_deleted),
220 (gpointer)G_ALERTOTHER);
221 gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
222 GTK_SIGNAL_FUNC(alertpanel_close),
223 (gpointer)G_ALERTOTHER);
224 gtk_widget_realize(dialog);
226 /* for title label */
227 hbox = gtk_hbox_new(FALSE, 0);
228 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
229 hbox, TRUE, TRUE, 16);
231 /* title label */
232 /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
233 /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
234 label = gtk_label_new(title);
235 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
236 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
237 style = gtk_style_copy(gtk_widget_get_style(label));
238 if (!titlefont)
239 titlefont = gtkut_font_load(TITLE_FONT);
240 if (titlefont)
241 style->font = titlefont;
242 gtk_widget_set_style(label, style);
244 /* for message and button(s) */
245 vbox = gtk_vbox_new(FALSE, 0);
246 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
247 vbox);
249 spc_vbox = gtk_vbox_new(FALSE, 0);
250 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
251 gtk_widget_set_usize(spc_vbox, -1, 16);
253 msg_vbox = gtk_vbox_new(FALSE, 0);
254 gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
256 /* for message label */
257 hbox = gtk_hbox_new(FALSE, 0);
258 gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
260 /* message label */
261 label = gtk_label_new(message);
262 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
263 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
265 /* Claws: custom widget */
266 if (custom_widget) {
267 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
268 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
269 FALSE, 0);
270 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
271 FALSE, 24);
273 if (can_disable) {
274 hbox = gtk_hbox_new(FALSE, 0);
275 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
276 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
278 disable_chkbtn = gtk_check_button_new_with_label
279 (_("Show this message next time"));
280 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
281 TRUE);
282 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
283 FALSE, FALSE, 0);
284 gtk_signal_connect(GTK_OBJECT(disable_chkbtn), "toggled",
285 GTK_SIGNAL_FUNC(alertpanel_button_toggled),
286 GUINT_TO_POINTER(G_ALERTDISABLE));
287 } else {
288 spc_vbox = gtk_vbox_new(FALSE, 0);
289 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
290 gtk_widget_set_usize(spc_vbox, -1, 20);
293 /* for button(s) */
294 if (!button1_label)
295 button1_label = _("OK");
296 label2 = button2_label;
297 label3 = button3_label;
298 if (label2 && *label2 == '+') label2++;
299 if (label3 && *label3 == '+') label3++;
301 gtkut_button_set_create(&confirm_area,
302 &button1, button1_label,
303 button2_label ? &button2 : NULL, label2,
304 button3_label ? &button3 : NULL, label3);
306 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
307 gtk_widget_grab_default(button1);
308 gtk_widget_grab_focus(button1);
309 if (button2_label && *button2_label == '+') {
310 gtk_widget_grab_default(button2);
311 gtk_widget_grab_focus(button2);
313 if (button3_label && *button3_label == '+') {
314 gtk_widget_grab_default(button3);
315 gtk_widget_grab_focus(button3);
318 gtk_signal_connect(GTK_OBJECT(button1), "clicked",
319 GTK_SIGNAL_FUNC(alertpanel_button_clicked),
320 GUINT_TO_POINTER(G_ALERTDEFAULT));
321 if (button2_label)
322 gtk_signal_connect(GTK_OBJECT(button2), "clicked",
323 GTK_SIGNAL_FUNC(alertpanel_button_clicked),
324 GUINT_TO_POINTER(G_ALERTALTERNATE));
325 if (button3_label)
326 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
327 GTK_SIGNAL_FUNC(alertpanel_button_clicked),
328 GUINT_TO_POINTER(G_ALERTOTHER));
330 gtk_widget_show_all(dialog);
333 static void alertpanel_button_toggled(GtkToggleButton *button,
334 gpointer data)
336 if (gtk_toggle_button_get_active(button))
337 value &= ~GPOINTER_TO_UINT(data);
338 else
339 value |= GPOINTER_TO_UINT(data);
342 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
344 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
347 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
348 gpointer data)
350 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
351 return TRUE;
354 static void alertpanel_close(GtkWidget *widget, GdkEventAny *event,
355 gpointer data)
357 if (event->type == GDK_KEY_PRESS)
358 if (((GdkEventKey *)event)->keyval != GDK_Escape)
359 return;
361 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;