5be55ab9c676b5d2e4cf2b42d2208708b6b42fb1
[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/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_checkbtn;
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_hide(remember_checkbtn);
108
109         gtk_widget_show(icon_q);
110         gtk_widget_hide(icon_p);
111         is_pass = FALSE;
112         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
113
114         return input_dialog_open(title, message, default_string, NULL);
115 }
116
117 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
118                                    const gchar *default_string)
119 {
120         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
121
122         if (!dialog)
123                 input_dialog_create(TRUE);
124
125         type = INPUT_DIALOG_INVISIBLE;
126         gtk_widget_hide(combo);
127         gtk_widget_show(entry);
128         gtk_widget_hide(remember_checkbtn);
129
130         gtk_widget_hide(icon_q);
131         gtk_widget_show(icon_p);
132         is_pass = TRUE;
133         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
134
135         return input_dialog_open(title, message, default_string, NULL);
136 }
137
138 gchar *input_dialog_combo(const gchar *title, const gchar *message,
139                           const gchar *default_string, GList *list,
140                           gboolean case_sensitive)
141 {
142         return input_dialog_combo_remember(title, message, 
143                 default_string, list, case_sensitive, NULL);
144 }
145
146 gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
147                           const gchar *default_string, GList *list,
148                           gboolean case_sensitive, 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         if (!list) {
170                 GList empty_list;
171
172                 empty_list.data = (gpointer)"";
173                 empty_list.next = NULL;
174                 empty_list.prev = NULL;
175                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
176         } else
177                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
178
179         gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
180
181         return input_dialog_open(title, message, default_string, remember);
182 }
183
184 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
185 {
186         gchar *message;
187         gchar *pass;
188
189         message = g_strdup_printf(_("Input password for %s on %s:"),
190                                   user, server);
191         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
192         g_free(message);
193
194         return pass;
195 }
196
197 static void input_dialog_create(gboolean is_password)
198 {
199         static PangoFontDescription *font_desc;
200         GtkWidget *w_hbox;
201         GtkWidget *hbox;
202         GtkWidget *vbox;
203         GtkWidget *cancel_button;
204         GtkWidget *confirm_area;
205
206         dialog = gtk_dialog_new();
207
208         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
209         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
210         gtk_window_set_title(GTK_WINDOW(dialog), "");
211         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
212
213         g_signal_connect(G_OBJECT(dialog), "delete_event",
214                          G_CALLBACK(delete_event), NULL);
215         g_signal_connect(G_OBJECT(dialog), "key_press_event",
216                          G_CALLBACK(key_pressed), NULL);
217         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
218
219         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
220         hbox = gtk_hbox_new (FALSE, 12);
221         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
222         gtk_widget_show (hbox);
223         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
224                             FALSE, FALSE, 0);
225
226         /* for title label */
227         w_hbox = gtk_hbox_new(FALSE, 0);
228         
229         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
230                                 GTK_ICON_SIZE_DIALOG); 
231         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
232         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
233         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
234                                 GTK_ICON_SIZE_DIALOG); 
235         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
236         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
237         
238         vbox = gtk_vbox_new (FALSE, 12);
239         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
240         gtk_widget_show (vbox);
241         
242         msg_title = gtk_label_new("");
243         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
244         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
245         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
246         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
247         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
248         if (!font_desc) {
249                 gint size;
250
251                 size = pango_font_description_get_size
252                         (msg_title->style->font_desc);
253                 font_desc = pango_font_description_new();
254                 pango_font_description_set_weight
255                         (font_desc, PANGO_WEIGHT_BOLD);
256                 pango_font_description_set_size
257                         (font_desc, size * PANGO_SCALE_LARGE);
258         }
259         if (font_desc)
260                 gtk_widget_modify_font(msg_title, font_desc);
261         
262         msg_label = gtk_label_new("");
263         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
264         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
265         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
266         gtk_widget_show(msg_label);
267                 
268         entry = gtk_entry_new();
269         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
270         g_signal_connect(G_OBJECT(entry), "activate",
271                          G_CALLBACK(entry_activated), NULL);
272
273         combo = gtk_combo_new();
274         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
275         g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
276                          G_CALLBACK(combo_activated), NULL);
277
278         remember_checkbtn = gtk_check_button_new_with_label(_("Remember this"));
279         gtk_box_pack_start(GTK_BOX(vbox), remember_checkbtn, FALSE, FALSE, 0);
280
281         hbox = gtk_hbox_new(TRUE, 0);
282
283         gtkut_stock_button_set_create(&confirm_area,
284                                       &cancel_button, GTK_STOCK_CANCEL,
285                                       &ok_button, GTK_STOCK_OK,
286                                       NULL, NULL);
287
288         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
289                          confirm_area, FALSE, FALSE, 0);
290         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
291
292         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
293         
294         gtk_widget_hide(remember_checkbtn);
295
296         if (is_password)
297                 gtk_widget_hide(icon_q);
298         else
299                 gtk_widget_hide(icon_p);
300
301         is_pass = is_password;
302
303         gtk_widget_grab_default(ok_button);
304
305         g_signal_connect(G_OBJECT(ok_button), "clicked",
306                          G_CALLBACK(ok_clicked), NULL);
307         g_signal_connect(G_OBJECT(cancel_button), "clicked",
308                          G_CALLBACK(cancel_clicked), NULL);
309 }
310
311 static gchar *input_dialog_open(const gchar *title, const gchar *message,
312                                 const gchar *default_string, gboolean *remember)
313 {
314         gchar *str;
315
316         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
317
318         if (!dialog)
319                 input_dialog_create(FALSE);
320
321         input_dialog_set(title, message, default_string);
322         gtk_widget_show(dialog);
323
324         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
325         if (remember)
326                 gtk_widget_show(remember_checkbtn);
327         else
328                 gtk_widget_hide(remember_checkbtn);
329
330         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
331         manage_window_set_transient(GTK_WINDOW(dialog));
332
333         ack = fin = FALSE;
334
335         while (fin == FALSE)
336                 gtk_main_iteration();
337
338         manage_window_focus_out(dialog, NULL, NULL);
339         gtk_widget_hide(dialog);
340
341         if (ack) {
342                 GtkEditable *editable;
343
344                 if (type == INPUT_DIALOG_COMBO)
345                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
346                 else
347                         editable = GTK_EDITABLE(entry);
348
349                 str = gtk_editable_get_chars(editable, 0, -1);
350                 if (str && *str == '\0' && !is_pass) {
351                         g_free(str);
352                         str = NULL;
353                 }
354         } else
355                 str = NULL;
356
357         GTK_EVENTS_FLUSH();
358
359         if (remember) {
360                 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
361         }
362         if (is_pass)
363                 debug_print("return string = %s\n", str ? "********": ("none"));
364         else
365                 debug_print("return string = %s\n", str ? str : "(none)");
366         return str;
367 }
368
369 static void input_dialog_set(const gchar *title, const gchar *message,
370                              const gchar *default_string)
371 {
372         GtkWidget *entry_;
373
374         if (type == INPUT_DIALOG_COMBO)
375                 entry_ = GTK_COMBO(combo)->entry;
376         else
377                 entry_ = entry;
378
379         gtk_window_set_title(GTK_WINDOW(dialog), title);
380         gtk_label_set_text(GTK_LABEL(msg_title), title);
381         gtk_label_set_text(GTK_LABEL(msg_label), message);
382         if (default_string && *default_string) {
383                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
384                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
385                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
386         } else
387                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
388
389         gtk_widget_grab_focus(ok_button);
390         gtk_widget_grab_focus(entry_);
391 }
392
393 static void ok_clicked(GtkWidget *widget, gpointer data)
394 {
395         ack = TRUE;
396         fin = TRUE;
397 }
398
399 static void cancel_clicked(GtkWidget *widget, gpointer data)
400 {
401         ack = FALSE;
402         fin = TRUE;
403 }
404
405 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
406 {
407         ack = FALSE;
408         fin = TRUE;
409
410         return TRUE;
411 }
412
413 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
414 {
415         if (event && event->keyval == GDK_Escape) {
416                 ack = FALSE;
417                 fin = TRUE;
418         } else if (event && event->keyval == GDK_Return) {
419                 ack = TRUE;
420                 fin = TRUE;
421                 return TRUE; /* do not let Return pass - it
422                               * pops up the combo on validating */
423         }
424
425         return FALSE;
426 }
427
428 static void entry_activated(GtkEditable *editable)
429 {
430         ack = TRUE;
431         fin = TRUE;
432 }
433
434 static void combo_activated(GtkEditable *editable)
435 {
436         ack = TRUE;
437         fin = TRUE;
438 }