2006-08-17 [colin] 2.4.0cvs57
[claws.git] / src / gtk / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkdialog.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtksignal.h>
32 #include <gtk/gtkvbox.h>
33 #include <gtk/gtkhbox.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkcombo.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkstock.h>
40 #include <gtk/gtkimage.h>
41 #include <gtk/gtktogglebutton.h>
42 #include <gtk/gtkcheckbutton.h>
43
44 #include "inputdialog.h"
45 #include "manage_window.h"
46 #include "gtkutils.h"
47 #include "utils.h"
48
49 #define INPUT_DIALOG_WIDTH      420
50
51 typedef enum
52 {
53         INPUT_DIALOG_NORMAL,
54         INPUT_DIALOG_INVISIBLE,
55         INPUT_DIALOG_COMBO
56 } InputDialogType;
57
58 static gboolean ack;
59 static gboolean fin;
60
61 static InputDialogType type;
62
63 static GtkWidget *dialog;
64 static GtkWidget *msg_title;
65 static GtkWidget *msg_label;
66 static GtkWidget *entry;
67 static GtkWidget *combo;
68 static GtkWidget *remember_chkbtn;
69 static GtkWidget *ok_button;
70 static GtkWidget *icon_q, *icon_p;
71 static gboolean is_pass = FALSE;
72 static void input_dialog_create (gboolean is_password);
73 static gchar *input_dialog_open (const gchar    *title,
74                                  const gchar    *message,
75                                  const gchar    *default_string,
76                                  gboolean       *remember);
77 static void input_dialog_set    (const gchar    *title,
78                                  const gchar    *message,
79                                  const gchar    *default_string);
80
81 static void ok_clicked          (GtkWidget      *widget,
82                                  gpointer        data);
83 static void cancel_clicked      (GtkWidget      *widget,
84                                  gpointer        data);
85 static gint delete_event        (GtkWidget      *widget,
86                                  GdkEventAny    *event,
87                                  gpointer        data);
88 static gboolean key_pressed     (GtkWidget      *widget,
89                                  GdkEventKey    *event,
90                                  gpointer        data);
91 static void entry_activated     (GtkEditable    *editable);
92 static void combo_activated     (GtkEditable    *editable);
93
94
95 gchar *input_dialog(const gchar *title, const gchar *message,
96                     const gchar *default_string)
97 {
98         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
99
100         if (!dialog)
101                 input_dialog_create(FALSE);
102
103         type = INPUT_DIALOG_NORMAL;
104         gtk_widget_hide(combo);
105         gtk_widget_show(entry);
106
107         gtk_widget_show(icon_q);
108         gtk_widget_hide(icon_p);
109         is_pass = FALSE;
110         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
111
112         return input_dialog_open(title, message, default_string, NULL);
113 }
114
115 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
116                                    const gchar *default_string)
117 {
118         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
119
120         if (!dialog)
121                 input_dialog_create(TRUE);
122
123         type = INPUT_DIALOG_INVISIBLE;
124         gtk_widget_hide(combo);
125         gtk_widget_show(entry);
126
127         gtk_widget_hide(icon_q);
128         gtk_widget_show(icon_p);
129         is_pass = TRUE;
130         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
131
132         return input_dialog_open(title, message, default_string, NULL);
133 }
134
135 gchar *input_dialog_combo(const gchar *title, const gchar *message,
136                           const gchar *default_string, GList *list,
137                           gboolean case_sensitive)
138 {
139         return input_dialog_combo_remember(title, message, 
140                 default_string, list, case_sensitive, NULL);
141 }
142
143 gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
144                           const gchar *default_string, GList *list,
145                           gboolean case_sensitive, gboolean *remember)
146 {
147         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
148
149         if (!dialog)
150                 input_dialog_create(FALSE);
151
152         type = INPUT_DIALOG_COMBO;
153         gtk_widget_hide(entry);
154         gtk_widget_show(combo);
155
156         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_chkbtn), FALSE);
157         if (remember)
158                 gtk_widget_show(remember_chkbtn);
159         else
160                 gtk_widget_hide(remember_chkbtn);
161
162         gtk_widget_show(icon_q);
163         gtk_widget_hide(icon_p);
164         is_pass = FALSE;
165
166         if (!list) {
167                 GList empty_list;
168
169                 empty_list.data = (gpointer)"";
170                 empty_list.next = NULL;
171                 empty_list.prev = NULL;
172                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
173         } else
174                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
175
176         gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
177
178         return input_dialog_open(title, message, default_string, remember);
179 }
180
181 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
182 {
183         gchar *message;
184         gchar *pass;
185
186         message = g_strdup_printf(_("Input password for %s on %s:"),
187                                   user, server);
188         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
189         g_free(message);
190
191         return pass;
192 }
193
194 static void input_dialog_create(gboolean is_password)
195 {
196         static PangoFontDescription *font_desc;
197         GtkWidget *w_hbox;
198         GtkWidget *hbox;
199         GtkWidget *vbox;
200         GtkWidget *cancel_button;
201         GtkWidget *confirm_area;
202
203         dialog = gtk_dialog_new();
204
205         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
206         gtk_window_set_title(GTK_WINDOW(dialog), "");
207         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
208
209         g_signal_connect(G_OBJECT(dialog), "delete_event",
210                          G_CALLBACK(delete_event), NULL);
211         g_signal_connect(G_OBJECT(dialog), "key_press_event",
212                          G_CALLBACK(key_pressed), NULL);
213         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
214
215         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
216         hbox = gtk_hbox_new (FALSE, 12);
217         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
218         gtk_widget_show (hbox);
219         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
220                             FALSE, FALSE, 0);
221
222         /* for title label */
223         w_hbox = gtk_hbox_new(FALSE, 0);
224         
225         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
226                                 GTK_ICON_SIZE_DIALOG); 
227         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
228         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
229         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
230                                 GTK_ICON_SIZE_DIALOG); 
231         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
232         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
233         
234         vbox = gtk_vbox_new (FALSE, 12);
235         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
236         gtk_widget_show (vbox);
237         
238         msg_title = gtk_label_new("");
239         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
240         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
241         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
242         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
243         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
244         if (!font_desc) {
245                 gint size;
246
247                 size = pango_font_description_get_size
248                         (msg_title->style->font_desc);
249                 font_desc = pango_font_description_new();
250                 pango_font_description_set_weight
251                         (font_desc, PANGO_WEIGHT_BOLD);
252                 pango_font_description_set_size
253                         (font_desc, size * PANGO_SCALE_LARGE);
254         }
255         if (font_desc)
256                 gtk_widget_modify_font(msg_title, font_desc);
257         
258         msg_label = gtk_label_new("");
259         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
260         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
261         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
262         gtk_widget_show(msg_label);
263                 
264         entry = gtk_entry_new();
265         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
266         g_signal_connect(G_OBJECT(entry), "activate",
267                          G_CALLBACK(entry_activated), NULL);
268
269         combo = gtk_combo_new();
270         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
271         g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
272                          G_CALLBACK(combo_activated), NULL);
273
274         remember_chkbtn = gtk_check_button_new_with_label(_("Remember this"));
275         gtk_box_pack_start(GTK_BOX(vbox), remember_chkbtn, FALSE, FALSE, 0);
276
277         hbox = gtk_hbox_new(TRUE, 0);
278
279         gtkut_stock_button_set_create(&confirm_area,
280                                       &cancel_button, GTK_STOCK_CANCEL,
281                                       &ok_button, GTK_STOCK_OK,
282                                       NULL, NULL);
283
284         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
285                          confirm_area, FALSE, FALSE, 0);
286         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
287
288         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
289         
290         gtk_widget_hide(remember_chkbtn);
291
292         if (is_password)
293                 gtk_widget_hide(icon_q);
294         else
295                 gtk_widget_hide(icon_p);
296
297         is_pass = is_password;
298
299         gtk_widget_grab_default(ok_button);
300
301         g_signal_connect(G_OBJECT(ok_button), "clicked",
302                          G_CALLBACK(ok_clicked), NULL);
303         g_signal_connect(G_OBJECT(cancel_button), "clicked",
304                          G_CALLBACK(cancel_clicked), NULL);
305 }
306
307 static gchar *input_dialog_open(const gchar *title, const gchar *message,
308                                 const gchar *default_string, gboolean *remember)
309 {
310         gchar *str;
311
312         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
313
314         if (!dialog)
315                 input_dialog_create(FALSE);
316
317         input_dialog_set(title, message, default_string);
318         gtk_widget_show(dialog);
319
320         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
321         manage_window_set_transient(GTK_WINDOW(dialog));
322
323         ack = fin = FALSE;
324
325         while (fin == FALSE)
326                 gtk_main_iteration();
327
328         manage_window_focus_out(dialog, NULL, NULL);
329         gtk_widget_hide(dialog);
330
331         if (ack) {
332                 GtkEditable *editable;
333
334                 if (type == INPUT_DIALOG_COMBO)
335                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
336                 else
337                         editable = GTK_EDITABLE(entry);
338
339                 str = gtk_editable_get_chars(editable, 0, -1);
340                 if (str && *str == '\0' && !is_pass) {
341                         g_free(str);
342                         str = NULL;
343                 }
344         } else
345                 str = NULL;
346
347         GTK_EVENTS_FLUSH();
348
349         if (remember) {
350                 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_chkbtn));
351         }
352         debug_print("return string = %s\n", str ? str : "(none)");
353         return str;
354 }
355
356 static void input_dialog_set(const gchar *title, const gchar *message,
357                              const gchar *default_string)
358 {
359         GtkWidget *entry_;
360
361         if (type == INPUT_DIALOG_COMBO)
362                 entry_ = GTK_COMBO(combo)->entry;
363         else
364                 entry_ = entry;
365
366         gtk_window_set_title(GTK_WINDOW(dialog), title);
367         gtk_label_set_text(GTK_LABEL(msg_title), title);
368         gtk_label_set_text(GTK_LABEL(msg_label), message);
369         if (default_string && *default_string) {
370                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
371                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
372                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
373         } else
374                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
375
376         gtk_widget_grab_focus(ok_button);
377         gtk_widget_grab_focus(entry_);
378 }
379
380 static void ok_clicked(GtkWidget *widget, gpointer data)
381 {
382         ack = TRUE;
383         fin = TRUE;
384 }
385
386 static void cancel_clicked(GtkWidget *widget, gpointer data)
387 {
388         ack = FALSE;
389         fin = TRUE;
390 }
391
392 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
393 {
394         ack = FALSE;
395         fin = TRUE;
396
397         return TRUE;
398 }
399
400 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
401 {
402         if (event && event->keyval == GDK_Escape) {
403                 ack = FALSE;
404                 fin = TRUE;
405         } else if (event && event->keyval == GDK_Return) {
406                 ack = TRUE;
407                 fin = TRUE;
408                 return TRUE; /* do not let Return pass - it
409                               * pops up the combo on validating */
410         }
411
412         return FALSE;
413 }
414
415 static void entry_activated(GtkEditable *editable)
416 {
417         ack = TRUE;
418         fin = TRUE;
419 }
420
421 static void combo_activated(GtkEditable *editable)
422 {
423         ack = TRUE;
424         fin = TRUE;
425 }