2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
33 #include "mainwindow.h"
34 #include "alertpanel.h"
35 #include "manage_window.h"
39 #include "logwindow.h"
40 #include "prefs_common.h"
42 #define ALERT_PANEL_WIDTH 380
43 #define TITLE_HEIGHT 72
44 #define MESSAGE_HEIGHT 62
46 static AlertValue value
;
47 static gboolean alertpanel_is_open
= FALSE
;
48 static GtkWidget
*dialog
;
50 static void alertpanel_show (void);
51 static void alertpanel_create (const gchar
*title
,
53 const gchar
*button1_label
,
54 const gchar
*button2_label
,
55 const gchar
*button3_label
,
57 GtkWidget
*custom_widget
,
59 AlertValue default_value
);
61 static void alertpanel_button_toggled (GtkToggleButton
*button
,
63 static void alertpanel_button_clicked (GtkWidget
*widget
,
65 static gint
alertpanel_deleted (GtkWidget
*widget
,
68 static gboolean
alertpanel_close (GtkWidget
*widget
,
72 AlertValue
alertpanel_with_widget(const gchar
*title
,
74 const gchar
*button1_label
,
75 const gchar
*button2_label
,
76 const gchar
*button3_label
,
78 AlertValue default_value
,
81 return alertpanel_full(title
, message
, button1_label
,
82 button2_label
, button3_label
,
83 can_disable
, widget
, ALERT_QUESTION
,
87 AlertValue
alertpanel_full(const gchar
*title
, const gchar
*message
,
88 const gchar
*button1_label
,
89 const gchar
*button2_label
,
90 const gchar
*button3_label
,
94 AlertValue default_value
)
96 if (alertpanel_is_open
)
99 alertpanel_is_open
= TRUE
;
100 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST
, &alertpanel_is_open
);
102 alertpanel_create(title
, message
, button1_label
, button2_label
,
103 button3_label
, can_disable
, widget
, alert_type
,
107 debug_print("return value = %d\n", value
);
111 AlertValue
alertpanel(const gchar
*title
,
112 const gchar
*message
,
113 const gchar
*button1_label
,
114 const gchar
*button2_label
,
115 const gchar
*button3_label
)
117 return alertpanel_full(title
, message
, button1_label
, button2_label
,
118 button3_label
, FALSE
, NULL
, ALERT_QUESTION
,
122 static void alertpanel_message(const gchar
*title
, const gchar
*message
, gint type
)
124 if (alertpanel_is_open
)
127 alertpanel_is_open
= TRUE
;
128 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST
, &alertpanel_is_open
);
131 alertpanel_create(title
, message
, GTK_STOCK_CLOSE
, NULL
, NULL
,
132 FALSE
, NULL
, type
, G_ALERTDEFAULT
);
136 void alertpanel_notice(const gchar
*format
, ...)
141 va_start(args
, format
);
142 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
146 alertpanel_message(_("Notice"), buf
, ALERT_NOTICE
);
149 void alertpanel_warning(const gchar
*format
, ...)
154 va_start(args
, format
);
155 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
159 alertpanel_message(_("Warning"), buf
, ALERT_WARNING
);
162 void alertpanel_error(const gchar
*format
, ...)
167 va_start(args
, format
);
168 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
172 alertpanel_message(_("Error"), buf
, ALERT_ERROR
);
176 *\brief display an error with a View Log button
179 void alertpanel_error_log(const gchar
*format
, ...)
186 va_start(args
, format
);
187 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
191 mainwin
= mainwindow_get_mainwindow();
193 if (mainwin
&& mainwin
->logwin
) {
194 mainwindow_clear_error(mainwin
);
195 val
= alertpanel_full(_("Error"), buf
, GTK_STOCK_CLOSE
,
196 _("_View log"), NULL
, FALSE
, NULL
,
197 ALERT_ERROR
, G_ALERTDEFAULT
);
198 if (val
== G_ALERTALTERNATE
)
199 log_window_show(mainwin
->logwin
);
201 alertpanel_error("%s", buf
);
204 static void alertpanel_show(void)
206 gtk_window_set_modal(GTK_WINDOW(dialog
), TRUE
);
207 manage_window_set_transient(GTK_WINDOW(dialog
));
208 gtk_widget_show_all(dialog
);
211 if (gdk_pointer_is_grabbed())
212 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
214 while ((value
& G_ALERT_VALUE_MASK
) == G_ALERTWAIT
)
215 gtk_main_iteration();
217 gtk_widget_destroy(dialog
);
220 alertpanel_is_open
= FALSE
;
221 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST
, &alertpanel_is_open
);
226 static void alertpanel_create(const gchar
*title
,
227 const gchar
*message
,
228 const gchar
*button1_label
,
229 const gchar
*button2_label
,
230 const gchar
*button3_label
,
231 gboolean can_disable
,
232 GtkWidget
*custom_widget
,
234 AlertValue default_value
)
236 static PangoFontDescription
*font_desc
;
241 GtkWidget
*disable_checkbtn
;
242 GtkWidget
*confirm_area
;
248 gchar
*tmp
= title
?g_markup_printf_escaped("%s", title
)
250 gchar
*title_full
= g_strdup_printf("<span weight=\"bold\" "
251 "size=\"larger\">%s</span>",
254 debug_print("Creating alert panel dialog...\n");
256 dialog
= gtk_dialog_new();
257 gtk_window_set_title(GTK_WINDOW(dialog
), title
);
258 gtk_window_set_resizable(GTK_WINDOW(dialog
), TRUE
);
260 gtk_window_set_default_size(GTK_WINDOW(dialog
), 375, 100);
262 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_CENTER
);
263 g_signal_connect(G_OBJECT(dialog
), "delete_event",
264 G_CALLBACK(alertpanel_deleted
),
265 (gpointer
)G_ALERTCANCEL
);
266 g_signal_connect(G_OBJECT(dialog
), "key_press_event",
267 G_CALLBACK(alertpanel_close
),
268 (gpointer
)G_ALERTCANCEL
);
270 /* for title icon, label and message */
271 hbox
= gtk_hbox_new(FALSE
, 12);
272 gtk_container_set_border_width(GTK_CONTAINER(hbox
), 12);
273 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog
))),
274 hbox
, FALSE
, FALSE
, 0);
277 switch (alert_type
) {
279 image
= gtk_image_new_from_stock
280 (GTK_STOCK_DIALOG_QUESTION
, GTK_ICON_SIZE_DIALOG
);
283 image
= gtk_image_new_from_stock
284 (GTK_STOCK_DIALOG_WARNING
, GTK_ICON_SIZE_DIALOG
);
287 image
= gtk_image_new_from_stock
288 (GTK_STOCK_DIALOG_ERROR
, GTK_ICON_SIZE_DIALOG
);
292 image
= gtk_image_new_from_stock
293 (GTK_STOCK_DIALOG_INFO
, GTK_ICON_SIZE_DIALOG
);
296 gtk_misc_set_alignment(GTK_MISC(image
), 0.5, 0.0);
297 gtk_box_pack_start(GTK_BOX(hbox
), image
, FALSE
, FALSE
, 0);
299 vbox
= gtk_vbox_new (FALSE
, 12);
300 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, TRUE
, TRUE
, 0);
301 gtk_widget_show (vbox
);
303 label
= gtk_label_new(title_full
);
304 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
305 gtk_label_set_justify(GTK_LABEL(label
), GTK_JUSTIFY_LEFT
);
306 gtk_label_set_use_markup(GTK_LABEL (label
), TRUE
);
307 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 0);
308 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
312 size
= pango_font_description_get_size
313 (gtk_widget_get_style(label
)->font_desc
);
314 font_desc
= pango_font_description_new();
315 pango_font_description_set_weight
316 (font_desc
, PANGO_WEIGHT_BOLD
);
317 pango_font_description_set_size
318 (font_desc
, size
* PANGO_SCALE_LARGE
);
321 gtk_widget_modify_font(label
, font_desc
);
324 label
= gtk_label_new(message
);
325 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
326 gtk_label_set_justify(GTK_LABEL(label
), GTK_JUSTIFY_LEFT
);
327 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
328 gtk_label_set_selectable(GTK_LABEL(label
), TRUE
);
329 gtk_label_set_use_markup(GTK_LABEL(label
), TRUE
);
330 gtkut_widget_set_can_focus(label
, FALSE
);
331 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 0);
332 gtk_widget_show(label
);
334 /* Claws: custom widget */
336 gtk_box_pack_start(GTK_BOX(vbox
), custom_widget
, FALSE
,
341 hbox
= gtk_hbox_new(FALSE
, 0);
342 gtk_box_pack_start(GTK_BOX(
343 gtk_dialog_get_content_area(GTK_DIALOG(dialog
))), hbox
,
346 disable_checkbtn
= gtk_check_button_new_with_label
347 (_("Show this message next time"));
348 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_checkbtn
),
350 gtk_box_pack_start(GTK_BOX(hbox
), disable_checkbtn
,
352 g_signal_connect(G_OBJECT(disable_checkbtn
), "toggled",
353 G_CALLBACK(alertpanel_button_toggled
),
354 GUINT_TO_POINTER(G_ALERTDISABLE
));
359 button1_label
= GTK_STOCK_OK
;
360 label2
= button2_label
;
361 label3
= button3_label
;
362 if (label2
&& *label2
== '+') label2
++;
363 if (label3
&& *label3
== '+') label3
++;
365 gtkut_stock_button_set_create(&confirm_area
,
366 &button1
, button1_label
,
367 button2_label
? &button2
: NULL
, label2
,
368 button3_label
? &button3
: NULL
, label3
);
370 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog
))),
371 confirm_area
, FALSE
, FALSE
, 0);
372 gtk_container_set_border_width(GTK_CONTAINER(confirm_area
), 5);
373 gtk_widget_grab_default(button1
);
374 gtk_widget_grab_focus(button1
);
376 (default_value
== G_ALERTALTERNATE
|| *button2_label
== '+')) {
377 gtk_widget_grab_default(button2
);
378 gtk_widget_grab_focus(button2
);
381 (default_value
== G_ALERTOTHER
|| *button3_label
== '+')) {
382 gtk_widget_grab_default(button3
);
383 gtk_widget_grab_focus(button3
);
386 g_signal_connect(G_OBJECT(button1
), "clicked",
387 G_CALLBACK(alertpanel_button_clicked
),
388 GUINT_TO_POINTER(G_ALERTDEFAULT
));
390 g_signal_connect(G_OBJECT(button2
), "clicked",
391 G_CALLBACK(alertpanel_button_clicked
),
392 GUINT_TO_POINTER(G_ALERTALTERNATE
));
394 g_signal_connect(G_OBJECT(button3
), "clicked",
395 G_CALLBACK(alertpanel_button_clicked
),
396 GUINT_TO_POINTER(G_ALERTOTHER
));
398 gtk_widget_show_all(dialog
);
401 static void alertpanel_button_toggled(GtkToggleButton
*button
,
404 if (gtk_toggle_button_get_active(button
))
405 value
&= ~GPOINTER_TO_UINT(data
);
407 value
|= GPOINTER_TO_UINT(data
);
410 static void alertpanel_button_clicked(GtkWidget
*widget
, gpointer data
)
412 value
= (value
& ~G_ALERT_VALUE_MASK
) | (AlertValue
)data
;
415 static gint
alertpanel_deleted(GtkWidget
*widget
, GdkEventAny
*event
,
418 value
= (value
& ~G_ALERT_VALUE_MASK
) | (AlertValue
)data
;
422 static gboolean
alertpanel_close(GtkWidget
*widget
, GdkEventAny
*event
,
425 if (event
->type
== GDK_KEY_PRESS
)
426 if (((GdkEventKey
*)event
)->keyval
!= GDK_KEY_Escape
)
429 value
= (value
& ~G_ALERT_VALUE_MASK
) | (AlertValue
)data
;