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