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