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.
26 #include <gdk/gdkkeysyms.h>
29 #include "mainwindow.h"
30 #include "alertpanel.h"
31 #include "manage_window.h"
35 #include "logwindow.h"
36 #include "prefs_common.h"
38 #define ALERT_PANEL_WIDTH 380
39 #define TITLE_HEIGHT 72
40 #define MESSAGE_HEIGHT 62
42 #define DEFAULT_TITLE_FONT "Sans Bold 12"
44 static gboolean alertpanel_is_open = FALSE;
45 static AlertValue value;
47 static GtkWidget *dialog;
49 static void alertpanel_show (void);
50 static void alertpanel_create (const gchar *title,
52 const gchar *button1_label,
53 const gchar *button2_label,
54 const gchar *button3_label,
56 GtkWidget *custom_widget,
59 static void alertpanel_button_toggled (GtkToggleButton *button,
61 static void alertpanel_button_clicked (GtkWidget *widget,
63 static gint alertpanel_deleted (GtkWidget *widget,
66 static gboolean alertpanel_close (GtkWidget *widget,
70 AlertValue alertpanel(const gchar *title,
72 const gchar *button1_label,
73 const gchar *button2_label,
74 const gchar *button3_label)
76 if (alertpanel_is_open)
79 alertpanel_is_open = TRUE;
81 alertpanel_create(title, message, button1_label, button2_label,
82 button3_label, FALSE, NULL, ALERT_QUESTION);
85 debug_print("return value = %d\n", value);
89 AlertValue alertpanel_with_widget(const gchar *title,
91 const gchar *button1_label,
92 const gchar *button2_label,
93 const gchar *button3_label,
96 if (alertpanel_is_open)
99 alertpanel_is_open = TRUE;
100 alertpanel_create(title, message, button1_label, button2_label,
101 button3_label, FALSE, widget, ALERT_QUESTION);
104 debug_print("return value = %d\n", value);
108 static void alertpanel_message(const gchar *title, const gchar *message, gint type)
110 if (alertpanel_is_open)
113 alertpanel_is_open = TRUE;
115 alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL, type);
119 AlertValue alertpanel_message_with_disable(const gchar *title,
120 const gchar *message,
123 if (alertpanel_is_open)
126 alertpanel_is_open = TRUE;
128 alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL,
135 void alertpanel_notice(const gchar *format, ...)
140 va_start(args, format);
141 g_vsnprintf(buf, sizeof(buf), format, args);
145 alertpanel_message(_("Notice"), buf, ALERT_NOTICE);
148 void alertpanel_warning(const gchar *format, ...)
153 va_start(args, format);
154 g_vsnprintf(buf, sizeof(buf), format, args);
158 alertpanel_message(_("Warning"), buf, ALERT_WARNING);
161 void alertpanel_error(const gchar *format, ...)
166 va_start(args, format);
167 g_vsnprintf(buf, sizeof(buf), format, args);
171 alertpanel_message(_("Error"), buf, ALERT_ERROR);
175 *\brief display an error with a View Log button
178 void alertpanel_error_log(const gchar *format, ...)
185 va_start(args, format);
186 g_vsnprintf(buf, sizeof(buf), format, args);
190 mainwin = mainwindow_get_mainwindow();
192 if (mainwin && mainwin->logwin) {
193 val = alertpanel(_("Error"), buf, _("OK"), _("View log"), NULL);
194 if (val == G_ALERTALTERNATE)
195 log_window_show(mainwin->logwin);
197 alertpanel_error(buf);
200 static void alertpanel_show(void)
202 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
203 manage_window_set_transient(GTK_WINDOW(dialog));
206 if (gdk_pointer_is_grabbed())
207 gdk_pointer_ungrab(GDK_CURRENT_TIME);
209 while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
210 gtk_main_iteration();
212 gtk_widget_destroy(dialog);
215 alertpanel_is_open = FALSE;
219 static void alertpanel_create(const gchar *title,
220 const gchar *message,
221 const gchar *button1_label,
222 const gchar *button2_label,
223 const gchar *button3_label,
224 gboolean can_disable,
225 GtkWidget *custom_widget,
228 static PangoFontDescription *font_desc;
235 GtkWidget *disable_chkbtn;
236 GtkWidget *confirm_area;
243 gchar *icon_desc[] = { GTK_STOCK_DIALOG_INFO,
244 GTK_STOCK_DIALOG_QUESTION,
245 GTK_STOCK_DIALOG_WARNING,
246 GTK_STOCK_DIALOG_ERROR };
248 debug_print("Creating alert panel dialog...\n");
250 dialog = gtk_dialog_new();
251 gtk_window_set_title(GTK_WINDOW(dialog), title);
252 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
253 gtk_container_set_border_width
254 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
255 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
256 g_signal_connect(G_OBJECT(dialog), "delete_event",
257 G_CALLBACK(alertpanel_deleted),
258 (gpointer)G_ALERTOTHER);
259 g_signal_connect(G_OBJECT(dialog), "key_press_event",
260 G_CALLBACK(alertpanel_close),
261 (gpointer)G_ALERTOTHER);
262 gtk_widget_realize(dialog);
264 /* for title label */
265 w_hbox = gtk_hbox_new(FALSE, 0);
267 if (alert_type < 0 || alert_type > 3)
269 icon = gtk_image_new_from_stock(icon_desc[alert_type],
270 GTK_ICON_SIZE_DIALOG);
271 gtk_box_pack_start(GTK_BOX(w_hbox), icon, FALSE, FALSE, 16);
272 hbox = gtk_hbox_new(FALSE, 0);
273 gtk_box_pack_start(GTK_BOX(w_hbox), hbox, FALSE, FALSE, 2);
274 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
275 w_hbox, TRUE, TRUE, 16);
278 label = gtk_label_new(title);
279 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
280 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
282 gchar *fontstr = prefs_common.titlefont
283 ? prefs_common.titlefont
284 : DEFAULT_TITLE_FONT;
285 font_desc = pango_font_description_from_string (fontstr);
288 gtk_widget_modify_font (label, font_desc);
290 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
292 /* for message and button(s) */
293 vbox = gtk_vbox_new(FALSE, 0);
294 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
297 spc_vbox = gtk_vbox_new(FALSE, 0);
298 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
299 gtk_widget_set_size_request(spc_vbox, -1, 16);
301 msg_vbox = gtk_vbox_new(FALSE, 0);
302 gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
304 /* for message label */
305 hbox = gtk_hbox_new(FALSE, 0);
306 gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
309 label = gtk_label_new(message);
310 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
311 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
313 /* Claws: custom widget */
315 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
316 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
318 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
322 hbox = gtk_hbox_new(FALSE, 0);
323 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
324 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
326 disable_chkbtn = gtk_check_button_new_with_label
327 (_("Show this message next time"));
328 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
330 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
332 g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
333 G_CALLBACK(alertpanel_button_toggled),
334 GUINT_TO_POINTER(G_ALERTDISABLE));
336 spc_vbox = gtk_vbox_new(FALSE, 0);
337 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
338 gtk_widget_set_size_request(spc_vbox, -1, 20);
343 button1_label = _("OK");
344 label2 = button2_label;
345 label3 = button3_label;
346 if (label2 && *label2 == '+') label2++;
347 if (label3 && *label3 == '+') label3++;
349 gtkut_button_set_create(&confirm_area,
350 &button1, button1_label,
351 button2_label ? &button2 : NULL, label2,
352 button3_label ? &button3 : NULL, label3);
354 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
355 gtk_widget_grab_default(button1);
356 gtk_widget_grab_focus(button1);
357 if (button2_label && *button2_label == '+') {
358 gtk_widget_grab_default(button2);
359 gtk_widget_grab_focus(button2);
361 if (button3_label && *button3_label == '+') {
362 gtk_widget_grab_default(button3);
363 gtk_widget_grab_focus(button3);
366 g_signal_connect(G_OBJECT(button1), "clicked",
367 G_CALLBACK(alertpanel_button_clicked),
368 GUINT_TO_POINTER(G_ALERTDEFAULT));
370 g_signal_connect(G_OBJECT(button2), "clicked",
371 G_CALLBACK(alertpanel_button_clicked),
372 GUINT_TO_POINTER(G_ALERTALTERNATE));
374 g_signal_connect(G_OBJECT(button3), "clicked",
375 G_CALLBACK(alertpanel_button_clicked),
376 GUINT_TO_POINTER(G_ALERTOTHER));
378 gtk_widget_show_all(dialog);
381 static void alertpanel_button_toggled(GtkToggleButton *button,
384 if (gtk_toggle_button_get_active(button))
385 value &= ~GPOINTER_TO_UINT(data);
387 value |= GPOINTER_TO_UINT(data);
390 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
392 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
395 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
398 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
402 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
405 if (event->type == GDK_KEY_PRESS)
406 if (((GdkEventKey *)event)->keyval != GDK_Escape)
409 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;