Add icon to alertpanel
[claws.git] / src / alertpanel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stddef.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
27
28 #include "intl.h"
29 #include "mainwindow.h"
30 #include "alertpanel.h"
31 #include "manage_window.h"
32 #include "utils.h"
33 #include "gtkutils.h"
34 #include "inc.h"
35 #include "logwindow.h"
36 #include "prefs_common.h"
37
38 #define ALERT_PANEL_WIDTH       380
39 #define TITLE_HEIGHT            72
40 #define MESSAGE_HEIGHT          62
41
42 #define DEFAULT_TITLE_FONT      "Sans Bold 12"
43
44 static gboolean alertpanel_is_open = FALSE;
45 static AlertValue value;
46
47 static GtkWidget *dialog;
48
49 static void alertpanel_show             (void);
50 static void alertpanel_create           (const gchar    *title,
51                                          const gchar    *message,
52                                          const gchar    *button1_label,
53                                          const gchar    *button2_label,
54                                          const gchar    *button3_label,
55                                          gboolean        can_disable,
56                                          GtkWidget      *custom_widget);
57
58 static void alertpanel_button_toggled   (GtkToggleButton        *button,
59                                          gpointer                data);
60 static void alertpanel_button_clicked   (GtkWidget              *widget,
61                                          gpointer                data);
62 static gint alertpanel_deleted          (GtkWidget              *widget,
63                                          GdkEventAny            *event,
64                                          gpointer                data);
65 static gboolean alertpanel_close        (GtkWidget              *widget,
66                                          GdkEventAny            *event,
67                                          gpointer                data);
68
69 AlertValue alertpanel(const gchar *title,
70                       const gchar *message,
71                       const gchar *button1_label,
72                       const gchar *button2_label,
73                       const gchar *button3_label)
74 {
75         if (alertpanel_is_open)
76                 return -1;
77         else
78                 alertpanel_is_open = TRUE;
79
80         alertpanel_create(title, message, button1_label, button2_label,
81                           button3_label, FALSE, NULL);
82         alertpanel_show();
83
84         debug_print("return value = %d\n", value);
85         return value;
86 }
87
88 AlertValue alertpanel_with_widget(const gchar *title,
89                                   const gchar *message,
90                                   const gchar *button1_label,
91                                   const gchar *button2_label,
92                                   const gchar *button3_label,
93                                   GtkWidget *widget)
94 {
95         if (alertpanel_is_open)
96                 return -1;
97         else
98                 alertpanel_is_open = TRUE;
99         alertpanel_create(title, message, button1_label, button2_label, 
100                           button3_label, FALSE, widget);
101         alertpanel_show();
102
103         debug_print("return value = %d\n", value);
104         return value;
105 }
106 void alertpanel_message(const gchar *title, const gchar *message)
107 {
108         if (alertpanel_is_open)
109                 return;
110         else
111                 alertpanel_is_open = TRUE;
112
113         alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL);
114         alertpanel_show();
115 }
116
117 AlertValue alertpanel_message_with_disable(const gchar *title,
118                                            const gchar *message)
119 {
120         if (alertpanel_is_open)
121                 return 0;
122         else
123                 alertpanel_is_open = TRUE;
124
125         alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL);
126         alertpanel_show();
127
128         return value;
129 }
130
131 void alertpanel_notice(const gchar *format, ...)
132 {
133         va_list args;
134         gchar buf[256];
135
136         va_start(args, format);
137         g_vsnprintf(buf, sizeof(buf), format, args);
138         va_end(args);
139         strretchomp(buf);
140
141         alertpanel_message(_("Notice"), buf);
142 }
143
144 void alertpanel_warning(const gchar *format, ...)
145 {
146         va_list args;
147         gchar buf[256];
148
149         va_start(args, format);
150         g_vsnprintf(buf, sizeof(buf), format, args);
151         va_end(args);
152         strretchomp(buf);
153
154         alertpanel_message(_("Warning"), buf);
155 }
156
157 void alertpanel_error(const gchar *format, ...)
158 {
159         va_list args;
160         gchar buf[256];
161
162         va_start(args, format);
163         g_vsnprintf(buf, sizeof(buf), format, args);
164         va_end(args);
165         strretchomp(buf);
166
167         alertpanel_message(_("Error"), buf);
168 }
169
170 /*!
171  *\brief        display an error with a View Log button
172  *
173  */
174 void alertpanel_error_log(const gchar *format, ...)
175 {
176         va_list args;
177         int val;
178         MainWindow *mainwin;
179         gchar buf[256];
180
181         va_start(args, format);
182         g_vsnprintf(buf, sizeof(buf), format, args);
183         va_end(args);
184         strretchomp(buf);
185
186         mainwin = mainwindow_get_mainwindow();
187         
188         if (mainwin && mainwin->logwin) {
189                 val = alertpanel(_("Error"), buf, _("OK"), _("View log"), NULL);
190                 if (val == G_ALERTALTERNATE)
191                         log_window_show(mainwin->logwin);
192         } else
193                 alertpanel_error(buf);
194 }
195
196 static void alertpanel_show(void)
197 {
198         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
199         manage_window_set_transient(GTK_WINDOW(dialog));
200         value = G_ALERTWAIT;
201
202         if (gdk_pointer_is_grabbed())
203                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
204         inc_lock();
205         while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
206                 gtk_main_iteration();
207
208         gtk_widget_destroy(dialog);
209         GTK_EVENTS_FLUSH();
210
211         alertpanel_is_open = FALSE;
212         inc_unlock();
213 }
214
215 static void alertpanel_create(const gchar *title,
216                               const gchar *message,
217                               const gchar *button1_label,
218                               const gchar *button2_label,
219                               const gchar *button3_label,
220                               gboolean     can_disable,
221                               GtkWidget *custom_widget)
222 {
223         static PangoFontDescription *font_desc;
224         GtkWidget *label;
225         GtkWidget *w_hbox;
226         GtkWidget *hbox;
227         GtkWidget *vbox;
228         GtkWidget *spc_vbox;
229         GtkWidget *msg_vbox;
230         GtkWidget *disable_chkbtn;
231         GtkWidget *confirm_area;
232         GtkWidget *button1;
233         GtkWidget *button2;
234         GtkWidget *button3;
235         GtkWidget *icon;
236         const gchar *label2;
237         const gchar *label3;
238
239         debug_print("Creating alert panel dialog...\n");
240
241         dialog = gtk_dialog_new();
242         gtk_window_set_title(GTK_WINDOW(dialog), title);
243         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
244         gtk_container_set_border_width
245                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
246         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
247         g_signal_connect(G_OBJECT(dialog), "delete_event",
248                          G_CALLBACK(alertpanel_deleted),
249                          (gpointer)G_ALERTOTHER);
250         g_signal_connect(G_OBJECT(dialog), "key_press_event",
251                          G_CALLBACK(alertpanel_close),
252                          (gpointer)G_ALERTOTHER);
253         gtk_widget_realize(dialog);
254
255         /* for title label */
256         w_hbox = gtk_hbox_new(FALSE, 0);
257         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,
258                                         GTK_ICON_SIZE_DIALOG); 
259         gtk_box_pack_start(GTK_BOX(w_hbox), icon, FALSE, FALSE, 16);
260         hbox = gtk_hbox_new(FALSE, 0);
261         gtk_box_pack_start(GTK_BOX(w_hbox), hbox, FALSE, FALSE, 2);
262         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
263                            w_hbox, TRUE, TRUE, 16);
264
265
266         label = gtk_label_new(title);
267         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
268         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
269         if (!font_desc) {
270                 gchar *fontstr = prefs_common.titlefont
271                                         ? prefs_common.titlefont
272                                         : DEFAULT_TITLE_FONT;
273                 font_desc = pango_font_description_from_string (fontstr);
274         }
275         if (font_desc) {
276                 gtk_widget_modify_font (label, font_desc);
277         }
278         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
279
280         /* for message and button(s) */
281         vbox = gtk_vbox_new(FALSE, 0);
282         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
283                           vbox);
284
285         spc_vbox = gtk_vbox_new(FALSE, 0);
286         gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
287         gtk_widget_set_size_request(spc_vbox, -1, 16);
288
289         msg_vbox = gtk_vbox_new(FALSE, 0);
290         gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
291
292         /* for message label */
293         hbox = gtk_hbox_new(FALSE, 0);
294         gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
295
296         /* message label */
297         label = gtk_label_new(message);
298         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
299         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
300
301         /* Claws: custom widget */
302         if (custom_widget) {
303                 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
304                 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
305                                    FALSE, 0);
306                 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
307                                    FALSE, 24);
308         }
309         if (can_disable) {
310                 hbox = gtk_hbox_new(FALSE, 0);
311                 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
312                 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
313
314                 disable_chkbtn = gtk_check_button_new_with_label
315                         (_("Show this message next time"));
316                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
317                                              TRUE);
318                 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
319                                    FALSE, FALSE, 0);
320                 g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
321                                  G_CALLBACK(alertpanel_button_toggled),
322                                  GUINT_TO_POINTER(G_ALERTDISABLE));
323         } else {
324                 spc_vbox = gtk_vbox_new(FALSE, 0);
325                 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
326                 gtk_widget_set_size_request(spc_vbox, -1, 20);
327         }
328
329         /* for button(s) */
330         if (!button1_label)
331                 button1_label = _("OK");
332         label2 = button2_label;
333         label3 = button3_label;
334         if (label2 && *label2 == '+') label2++;
335         if (label3 && *label3 == '+') label3++;
336
337         gtkut_button_set_create(&confirm_area,
338                                 &button1, button1_label,
339                                 button2_label ? &button2 : NULL, label2,
340                                 button3_label ? &button3 : NULL, label3);
341
342         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
343         gtk_widget_grab_default(button1);
344         gtk_widget_grab_focus(button1);
345         if (button2_label && *button2_label == '+') {
346                 gtk_widget_grab_default(button2);
347                 gtk_widget_grab_focus(button2);
348         }
349         if (button3_label && *button3_label == '+') {
350                 gtk_widget_grab_default(button3);
351                 gtk_widget_grab_focus(button3);
352         }
353
354         g_signal_connect(G_OBJECT(button1), "clicked",
355                          G_CALLBACK(alertpanel_button_clicked),
356                          GUINT_TO_POINTER(G_ALERTDEFAULT));
357         if (button2_label)
358                 g_signal_connect(G_OBJECT(button2), "clicked",
359                                  G_CALLBACK(alertpanel_button_clicked),
360                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
361         if (button3_label)
362                 g_signal_connect(G_OBJECT(button3), "clicked",
363                                  G_CALLBACK(alertpanel_button_clicked),
364                                  GUINT_TO_POINTER(G_ALERTOTHER));
365
366         gtk_widget_show_all(dialog);
367 }
368
369 static void alertpanel_button_toggled(GtkToggleButton *button,
370                                       gpointer data)
371 {
372         if (gtk_toggle_button_get_active(button))
373                 value &= ~GPOINTER_TO_UINT(data);
374         else
375                 value |= GPOINTER_TO_UINT(data);
376 }
377
378 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
379 {
380         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
381 }
382
383 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
384                                gpointer data)
385 {
386         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
387         return TRUE;
388 }
389
390 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
391                              gpointer data)
392 {
393         if (event->type == GDK_KEY_PRESS)
394                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
395                         return FALSE;
396
397         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
398         return FALSE;
399 }