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 #define DEFAULT_TITLE_FONT      "Helvetica 16"
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 *hbox;
226         GtkWidget *vbox;
227         GtkWidget *spc_vbox;
228         GtkWidget *msg_vbox;
229         GtkWidget *disable_chkbtn;
230         GtkWidget *confirm_area;
231         GtkWidget *button1;
232         GtkWidget *button2;
233         GtkWidget *button3;
234         const gchar *label2;
235         const gchar *label3;
236
237         debug_print("Creating alert panel dialog...\n");
238
239         dialog = gtk_dialog_new();
240         gtk_window_set_title(GTK_WINDOW(dialog), title);
241         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
242         gtk_container_set_border_width
243                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
244         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
245         g_signal_connect(G_OBJECT(dialog), "delete_event",
246                          G_CALLBACK(alertpanel_deleted),
247                          (gpointer)G_ALERTOTHER);
248         g_signal_connect(G_OBJECT(dialog), "key_press_event",
249                          G_CALLBACK(alertpanel_close),
250                          (gpointer)G_ALERTOTHER);
251         gtk_widget_realize(dialog);
252
253         /* for title label */
254         hbox = gtk_hbox_new(FALSE, 0);
255         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
256                            hbox, TRUE, TRUE, 16);
257
258         /* title label */
259         /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
260         /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
261         label = gtk_label_new(title);
262         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
263         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
264         if (!font_desc) {
265                 gchar *fontstr = prefs_common.titlefont
266                                         ? prefs_common.titlefont
267                                         : DEFAULT_TITLE_FONT;
268                 font_desc = pango_font_description_from_string (fontstr);
269         }
270         if (font_desc) {
271                 gtk_widget_modify_font (label, font_desc);
272         }
273
274         /* for message and button(s) */
275         vbox = gtk_vbox_new(FALSE, 0);
276         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
277                           vbox);
278
279         spc_vbox = gtk_vbox_new(FALSE, 0);
280         gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
281         gtk_widget_set_size_request(spc_vbox, -1, 16);
282
283         msg_vbox = gtk_vbox_new(FALSE, 0);
284         gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
285
286         /* for message label */
287         hbox = gtk_hbox_new(FALSE, 0);
288         gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
289
290         /* message label */
291         label = gtk_label_new(message);
292         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
293         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
294
295         /* Claws: custom widget */
296         if (custom_widget) {
297                 GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
298                 gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
299                                    FALSE, 0);
300                 gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
301                                    FALSE, 24);
302         }
303         if (can_disable) {
304                 hbox = gtk_hbox_new(FALSE, 0);
305                 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
306                 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
307
308                 disable_chkbtn = gtk_check_button_new_with_label
309                         (_("Show this message next time"));
310                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
311                                              TRUE);
312                 gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
313                                    FALSE, FALSE, 0);
314                 g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
315                                  G_CALLBACK(alertpanel_button_toggled),
316                                  GUINT_TO_POINTER(G_ALERTDISABLE));
317         } else {
318                 spc_vbox = gtk_vbox_new(FALSE, 0);
319                 gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
320                 gtk_widget_set_size_request(spc_vbox, -1, 20);
321         }
322
323         /* for button(s) */
324         if (!button1_label)
325                 button1_label = _("OK");
326         label2 = button2_label;
327         label3 = button3_label;
328         if (label2 && *label2 == '+') label2++;
329         if (label3 && *label3 == '+') label3++;
330
331         gtkut_button_set_create(&confirm_area,
332                                 &button1, button1_label,
333                                 button2_label ? &button2 : NULL, label2,
334                                 button3_label ? &button3 : NULL, label3);
335
336         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
337         gtk_widget_grab_default(button1);
338         gtk_widget_grab_focus(button1);
339         if (button2_label && *button2_label == '+') {
340                 gtk_widget_grab_default(button2);
341                 gtk_widget_grab_focus(button2);
342         }
343         if (button3_label && *button3_label == '+') {
344                 gtk_widget_grab_default(button3);
345                 gtk_widget_grab_focus(button3);
346         }
347
348         g_signal_connect(G_OBJECT(button1), "clicked",
349                          G_CALLBACK(alertpanel_button_clicked),
350                          GUINT_TO_POINTER(G_ALERTDEFAULT));
351         if (button2_label)
352                 g_signal_connect(G_OBJECT(button2), "clicked",
353                                  G_CALLBACK(alertpanel_button_clicked),
354                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
355         if (button3_label)
356                 g_signal_connect(G_OBJECT(button3), "clicked",
357                                  G_CALLBACK(alertpanel_button_clicked),
358                                  GUINT_TO_POINTER(G_ALERTOTHER));
359
360         gtk_widget_show_all(dialog);
361 }
362
363 static void alertpanel_button_toggled(GtkToggleButton *button,
364                                       gpointer data)
365 {
366         if (gtk_toggle_button_get_active(button))
367                 value &= ~GPOINTER_TO_UINT(data);
368         else
369                 value |= GPOINTER_TO_UINT(data);
370 }
371
372 static void alertpanel_button_clicked(GtkWidget *widget, gpointer data)
373 {
374         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
375 }
376
377 static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
378                                gpointer data)
379 {
380         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
381         return TRUE;
382 }
383
384 static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
385                              gpointer data)
386 {
387         if (event->type == GDK_KEY_PRESS)
388                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
389                         return FALSE;
390
391         value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
392         return FALSE;
393 }