2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
32 #include "mainwindow.h"
33 #include "alertpanel.h"
34 #include "manage_window.h"
38 #include "logwindow.h"
39 #include "prefs_common.h"
41 #define ALERT_PANEL_WIDTH 380
42 #define TITLE_HEIGHT 72
43 #define MESSAGE_HEIGHT 62
45 static AlertValue value;
46 static gboolean alertpanel_is_open = FALSE;
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,
58 AlertValue default_value);
60 static void alertpanel_button_toggled (GtkToggleButton *button,
62 static void alertpanel_button_clicked (GtkWidget *widget,
64 static gint alertpanel_deleted (GtkWidget *widget,
67 static gboolean alertpanel_close (GtkWidget *widget,
71 AlertValue alertpanel_with_widget(const gchar *title,
73 const gchar *button1_label,
74 const gchar *button2_label,
75 const gchar *button3_label,
77 AlertValue default_value,
80 return alertpanel_full(title, message, button1_label,
81 button2_label, button3_label,
82 can_disable, widget, ALERT_QUESTION,
86 AlertValue alertpanel_full(const gchar *title, const gchar *message,
87 const gchar *button1_label,
88 const gchar *button2_label,
89 const gchar *button3_label,
93 AlertValue default_value)
95 if (alertpanel_is_open)
98 alertpanel_is_open = TRUE;
99 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST, &alertpanel_is_open);
101 alertpanel_create(title, message, button1_label, button2_label,
102 button3_label, can_disable, widget, alert_type,
106 debug_print("return value = %d\n", value);
110 AlertValue alertpanel(const gchar *title,
111 const gchar *message,
112 const gchar *button1_label,
113 const gchar *button2_label,
114 const gchar *button3_label)
116 return alertpanel_full(title, message, button1_label, button2_label,
117 button3_label, FALSE, NULL, ALERT_QUESTION,
121 static void alertpanel_message(const gchar *title, const gchar *message, gint type)
123 if (alertpanel_is_open)
126 alertpanel_is_open = TRUE;
127 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST, &alertpanel_is_open);
130 alertpanel_create(title, message, GTK_STOCK_CLOSE, NULL, NULL,
131 FALSE, NULL, type, G_ALERTDEFAULT);
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 mainwindow_clear_error(mainwin);
194 val = alertpanel_full(_("Error"), buf, GTK_STOCK_CLOSE,
195 _("_View log"), NULL, FALSE, NULL,
196 ALERT_ERROR, G_ALERTDEFAULT);
197 if (val == G_ALERTALTERNATE)
198 log_window_show(mainwin->logwin);
200 alertpanel_error("%s", buf);
203 static void alertpanel_show(void)
205 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
206 manage_window_set_transient(GTK_WINDOW(dialog));
209 if (gdk_pointer_is_grabbed())
210 gdk_pointer_ungrab(GDK_CURRENT_TIME);
212 while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
213 gtk_main_iteration();
215 gtk_widget_destroy(dialog);
218 alertpanel_is_open = FALSE;
219 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST, &alertpanel_is_open);
224 static void alertpanel_create(const gchar *title,
225 const gchar *message,
226 const gchar *button1_label,
227 const gchar *button2_label,
228 const gchar *button3_label,
229 gboolean can_disable,
230 GtkWidget *custom_widget,
232 AlertValue default_value)
234 static PangoFontDescription *font_desc;
239 GtkWidget *disable_checkbtn;
240 GtkWidget *confirm_area;
246 gchar *tmp = title?g_markup_printf_escaped("%s", title)
248 gchar *title_full = g_strdup_printf("<span weight=\"bold\" "
249 "size=\"larger\">%s</span>",
252 debug_print("Creating alert panel dialog...\n");
254 dialog = gtk_dialog_new();
255 gtk_window_set_title(GTK_WINDOW(dialog), title);
256 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
258 gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
259 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
261 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
262 g_signal_connect(G_OBJECT(dialog), "delete_event",
263 G_CALLBACK(alertpanel_deleted),
264 (gpointer)G_ALERTCANCEL);
265 g_signal_connect(G_OBJECT(dialog), "key_press_event",
266 G_CALLBACK(alertpanel_close),
267 (gpointer)G_ALERTCANCEL);
269 /* for title icon, label and message */
270 hbox = gtk_hbox_new(FALSE, 12);
271 gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
272 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
273 hbox, FALSE, FALSE, 0);
276 switch (alert_type) {
278 image = gtk_image_new_from_stock
279 (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
282 image = gtk_image_new_from_stock
283 (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
286 image = gtk_image_new_from_stock
287 (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
291 image = gtk_image_new_from_stock
292 (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
295 gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
296 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
298 vbox = gtk_vbox_new (FALSE, 12);
299 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
300 gtk_widget_show (vbox);
302 label = gtk_label_new(title_full);
303 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
304 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
305 gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
306 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
307 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
311 size = pango_font_description_get_size
312 (label->style->font_desc);
313 font_desc = pango_font_description_new();
314 pango_font_description_set_weight
315 (font_desc, PANGO_WEIGHT_BOLD);
316 pango_font_description_set_size
317 (font_desc, size * PANGO_SCALE_LARGE);
320 gtk_widget_modify_font(label, font_desc);
323 label = gtk_label_new(message);
324 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
325 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
326 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
327 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
328 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
329 GTK_WIDGET_UNSET_FLAGS(label, GTK_CAN_FOCUS);
330 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
331 gtk_widget_show(label);
333 /* Claws: custom widget */
335 gtk_box_pack_start(GTK_BOX(vbox), custom_widget, FALSE,
340 hbox = gtk_hbox_new(FALSE, 0);
341 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
344 disable_checkbtn = gtk_check_button_new_with_label
345 (_("Show this message next time"));
346 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_checkbtn),
348 gtk_box_pack_start(GTK_BOX(hbox), disable_checkbtn,
350 g_signal_connect(G_OBJECT(disable_checkbtn), "toggled",
351 G_CALLBACK(alertpanel_button_toggled),
352 GUINT_TO_POINTER(G_ALERTDISABLE));
357 button1_label = GTK_STOCK_OK;
358 label2 = button2_label;
359 label3 = button3_label;
360 if (label2 && *label2 == '+') label2++;
361 if (label3 && *label3 == '+') label3++;
363 gtkut_stock_button_set_create(&confirm_area,
364 &button1, button1_label,
365 button2_label ? &button2 : NULL, label2,
366 button3_label ? &button3 : NULL, label3);
368 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
369 confirm_area, FALSE, FALSE, 0);
370 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
371 gtk_widget_grab_default(button1);
372 gtk_widget_grab_focus(button1);
374 (default_value == G_ALERTALTERNATE || *button2_label == '+')) {
375 gtk_widget_grab_default(button2);
376 gtk_widget_grab_focus(button2);
379 (default_value == G_ALERTOTHER || *button3_label == '+')) {
380 gtk_widget_grab_default(button3);
381 gtk_widget_grab_focus(button3);
384 g_signal_connect(G_OBJECT(button1), "clicked",
385 G_CALLBACK(alertpanel_button_clicked),
386 GUINT_TO_POINTER(G_ALERTDEFAULT));
388 g_signal_connect(G_OBJECT(button2), "clicked",
389 G_CALLBACK(alertpanel_button_clicked),
390 GUINT_TO_POINTER(G_ALERTALTERNATE));
392 g_signal_connect(G_OBJECT(button3), "clicked",
393 G_CALLBACK(alertpanel_button_clicked),
394 GUINT_TO_POINTER(G_ALERTOTHER));
396 gtk_widget_show_all(dialog);
399 static void alertpanel_button_toggled(GtkToggleButton *button,
402 if (gtk_toggle_button_get_active(button))
403 value &= ~GPOINTER_TO_UINT(data);
405 value |= GPOINTER_TO_UINT(data);
408 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
410 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
413 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
416 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
420 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
423 if (event->type == GDK_KEY_PRESS)
424 if (((GdkEventKey *)event)->keyval != GDK_Escape)
427 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;