Fix bug #3574: Template addressing
[claws.git] / src / alertpanel.c
blob6c685987d910cf93a4259878a180d3be6991a5ac
1 /*
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <stddef.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
32 #include "hooks.h"
33 #include "mainwindow.h"
34 #include "alertpanel.h"
35 #include "manage_window.h"
36 #include "utils.h"
37 #include "gtkutils.h"
38 #include "inc.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,
52 const gchar *message,
53 const gchar *button1_label,
54 const gchar *button2_label,
55 const gchar *button3_label,
56 gboolean can_disable,
57 GtkWidget *custom_widget,
58 gint alert_type,
59 AlertValue default_value);
61 static void alertpanel_button_toggled (GtkToggleButton *button,
62 gpointer data);
63 static void alertpanel_button_clicked (GtkWidget *widget,
64 gpointer data);
65 static gint alertpanel_deleted (GtkWidget *widget,
66 GdkEventAny *event,
67 gpointer data);
68 static gboolean alertpanel_close (GtkWidget *widget,
69 GdkEventAny *event,
70 gpointer data);
72 AlertValue alertpanel_with_widget(const gchar *title,
73 const gchar *message,
74 const gchar *button1_label,
75 const gchar *button2_label,
76 const gchar *button3_label,
77 gboolean can_disable,
78 AlertValue default_value,
79 GtkWidget *widget)
81 return alertpanel_full(title, message, button1_label,
82 button2_label, button3_label,
83 can_disable, widget, ALERT_QUESTION,
84 default_value);
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,
91 gboolean can_disable,
92 GtkWidget *widget,
93 AlertType alert_type,
94 AlertValue default_value)
96 if (alertpanel_is_open)
97 return -1;
98 else {
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,
104 default_value);
105 alertpanel_show();
107 debug_print("return value = %d\n", value);
108 return 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,
119 G_ALERTDEFAULT);
122 static void alertpanel_message(const gchar *title, const gchar *message, gint type)
124 if (alertpanel_is_open)
125 return;
126 else {
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);
133 alertpanel_show();
136 void alertpanel_notice(const gchar *format, ...)
138 va_list args;
139 gchar buf[256];
141 va_start(args, format);
142 g_vsnprintf(buf, sizeof(buf), format, args);
143 va_end(args);
144 strretchomp(buf);
146 alertpanel_message(_("Notice"), buf, ALERT_NOTICE);
149 void alertpanel_warning(const gchar *format, ...)
151 va_list args;
152 gchar buf[256];
154 va_start(args, format);
155 g_vsnprintf(buf, sizeof(buf), format, args);
156 va_end(args);
157 strretchomp(buf);
159 alertpanel_message(_("Warning"), buf, ALERT_WARNING);
162 void alertpanel_error(const gchar *format, ...)
164 va_list args;
165 gchar buf[512];
167 va_start(args, format);
168 g_vsnprintf(buf, sizeof(buf), format, args);
169 va_end(args);
170 strretchomp(buf);
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, ...)
181 va_list args;
182 int val;
183 MainWindow *mainwin;
184 gchar buf[256];
186 va_start(args, format);
187 g_vsnprintf(buf, sizeof(buf), format, args);
188 va_end(args);
189 strretchomp(buf);
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);
200 } else
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);
209 value = G_ALERTWAIT;
211 if (gdk_pointer_is_grabbed())
212 gdk_pointer_ungrab(GDK_CURRENT_TIME);
213 inc_lock();
214 while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
215 gtk_main_iteration();
217 gtk_widget_destroy(dialog);
218 GTK_EVENTS_FLUSH();
220 alertpanel_is_open = FALSE;
221 hooks_invoke(ALERTPANEL_OPENED_HOOKLIST, &alertpanel_is_open);
223 inc_unlock();
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,
233 gint alert_type,
234 AlertValue default_value)
236 static PangoFontDescription *font_desc;
237 GtkWidget *image;
238 GtkWidget *label;
239 GtkWidget *hbox;
240 GtkWidget *vbox;
241 GtkWidget *disable_checkbtn;
242 GtkWidget *confirm_area;
243 GtkWidget *button1;
244 GtkWidget *button2;
245 GtkWidget *button3;
246 const gchar *label2;
247 const gchar *label3;
248 gchar *tmp = title?g_markup_printf_escaped("%s", title)
249 :g_strdup("");
250 gchar *title_full = g_strdup_printf("<span weight=\"bold\" "
251 "size=\"larger\">%s</span>",
252 tmp);
253 g_free(tmp);
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);
276 /* title icon */
277 switch (alert_type) {
278 case ALERT_QUESTION:
279 image = gtk_image_new_from_stock
280 (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
281 break;
282 case ALERT_WARNING:
283 image = gtk_image_new_from_stock
284 (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
285 break;
286 case ALERT_ERROR:
287 image = gtk_image_new_from_stock
288 (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
289 break;
290 case ALERT_NOTICE:
291 default:
292 image = gtk_image_new_from_stock
293 (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
294 break;
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);
309 if (!font_desc) {
310 gint size;
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);
320 if (font_desc)
321 gtk_widget_modify_font(label, font_desc);
322 g_free(title_full);
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 */
335 if (custom_widget) {
336 gtk_box_pack_start(GTK_BOX(vbox), custom_widget, FALSE,
337 FALSE, 0);
340 if (can_disable) {
341 hbox = gtk_hbox_new(FALSE, 0);
342 gtk_box_pack_start(GTK_BOX(
343 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox,
344 FALSE, FALSE, 0);
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),
349 TRUE);
350 gtk_box_pack_start(GTK_BOX(hbox), disable_checkbtn,
351 FALSE, FALSE, 12);
352 g_signal_connect(G_OBJECT(disable_checkbtn), "toggled",
353 G_CALLBACK(alertpanel_button_toggled),
354 GUINT_TO_POINTER(G_ALERTDISABLE));
357 /* for button(s) */
358 if (!button1_label)
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);
375 if (button2_label &&
376 (default_value == G_ALERTALTERNATE || *button2_label == '+')) {
377 gtk_widget_grab_default(button2);
378 gtk_widget_grab_focus(button2);
380 if (button3_label &&
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));
389 if (button2_label)
390 g_signal_connect(G_OBJECT(button2), "clicked",
391 G_CALLBACK(alertpanel_button_clicked),
392 GUINT_TO_POINTER(G_ALERTALTERNATE));
393 if (button3_label)
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,
402 gpointer data)
404 if (gtk_toggle_button_get_active(button))
405 value &= ~GPOINTER_TO_UINT(data);
406 else
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,
416 gpointer data)
418 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
419 return TRUE;
422 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
423 gpointer data)
425 if (event->type == GDK_KEY_PRESS)
426 if (((GdkEventKey *)event)->keyval != GDK_KEY_Escape)
427 return FALSE;
429 value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
430 return FALSE;