2f7656d55cbd5a51ef819c143fff6883051a801f
[claws.git] / src / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <glib.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtkmain.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkdialog.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkcombo.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkhbbox.h>
38
39 #include "intl.h"
40 #include "inputdialog.h"
41 #include "manage_window.h"
42 #include "gtkutils.h"
43 #include "utils.h"
44
45 #define INPUT_DIALOG_WIDTH      420
46
47 typedef enum
48 {
49         INPUT_DIALOG_NORMAL,
50         INPUT_DIALOG_INVISIBLE,
51         INPUT_DIALOG_COMBO
52 } InputDialogType;
53
54 static gboolean ack;
55
56 static InputDialogType type;
57
58 static GtkWidget *dialog;
59 static GtkWidget *msg_label;
60 static GtkWidget *entry;
61 static GtkWidget *combo;
62 static GtkWidget *ok_button;
63
64 static void input_dialog_create (void);
65 static gchar *input_dialog_open (const gchar    *title,
66                                  const gchar    *message,
67                                  const gchar    *default_string);
68 static void input_dialog_set    (const gchar    *title,
69                                  const gchar    *message,
70                                  const gchar    *default_string);
71
72 static void ok_clicked          (GtkWidget      *widget,
73                                  gpointer        data);
74 static void cancel_clicked      (GtkWidget      *widget,
75                                  gpointer        data);
76 static gint delete_event        (GtkWidget      *widget,
77                                  GdkEventAny    *event,
78                                  gpointer        data);
79 static void key_pressed         (GtkWidget      *widget,
80                                  GdkEventKey    *event,
81                                  gpointer        data);
82 static void entry_activated     (GtkEditable    *editable);
83 static void combo_activated     (GtkEditable    *editable);
84
85
86 gchar *input_dialog(const gchar *title, const gchar *message,
87                     const gchar *default_string)
88 {
89         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
90
91         if (!dialog)
92                 input_dialog_create();
93
94         type = INPUT_DIALOG_NORMAL;
95         gtk_widget_hide(combo);
96         gtk_widget_show(entry);
97         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
98
99         return input_dialog_open(title, message, default_string);
100 }
101
102 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
103                                    const gchar *default_string)
104 {
105         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
106
107         if (!dialog)
108                 input_dialog_create();
109
110         type = INPUT_DIALOG_INVISIBLE;
111         gtk_widget_hide(combo);
112         gtk_widget_show(entry);
113         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
114
115         return input_dialog_open(title, message, default_string);
116 }
117
118 gchar *input_dialog_combo(const gchar *title, const gchar *message,
119                           const gchar *default_string, GList *list)
120 {
121         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
122
123         if (!dialog)
124                 input_dialog_create();
125
126         type = INPUT_DIALOG_COMBO;
127         gtk_widget_hide(entry);
128         gtk_widget_show(combo);
129
130         if (!list) {
131                 GList empty_list;
132
133                 empty_list.data = (gpointer)"";
134                 empty_list.next = NULL;
135                 empty_list.prev = NULL;
136                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
137         } else
138                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
139
140         return input_dialog_open(title, message, default_string);
141 }
142
143 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
144 {
145         gchar *message;
146         gchar *pass;
147
148         message = g_strdup_printf(_("Input password for %s on %s:"),
149                                   user, server);
150         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
151         g_free(message);
152
153         return pass;
154 }
155
156 static void input_dialog_create(void)
157 {
158         GtkWidget *vbox;
159         GtkWidget *hbox;
160         GtkWidget *confirm_area;
161         GtkWidget *cancel_button;
162
163         dialog = gtk_dialog_new();
164         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
165         gtk_widget_set_usize(dialog, INPUT_DIALOG_WIDTH, -1);
166         gtk_container_set_border_width
167                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
168         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
169         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
170                            GTK_SIGNAL_FUNC(delete_event), NULL);
171         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
172                            GTK_SIGNAL_FUNC(key_pressed), NULL);
173         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
174
175         gtk_widget_realize(dialog);
176
177         vbox = gtk_vbox_new(FALSE, 8);
178         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
179         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
180
181         hbox = gtk_hbox_new(FALSE, 0);
182         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
183
184         msg_label = gtk_label_new("");
185         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
186         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
187
188         entry = gtk_entry_new();
189         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
190         gtk_signal_connect(GTK_OBJECT(entry), "activate",
191                            GTK_SIGNAL_FUNC(entry_activated), NULL);
192
193         combo = gtk_combo_new();
194         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
195         gtk_signal_connect(GTK_OBJECT(GTK_COMBO(combo)->entry), "activate",
196                            GTK_SIGNAL_FUNC(combo_activated), NULL);
197
198         gtkut_button_set_create(&confirm_area,
199                                 &ok_button,     _("OK"),
200                                 &cancel_button, _("Cancel"),
201                                 NULL, NULL);
202         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
203                           confirm_area);
204         gtk_widget_grab_default(ok_button);
205
206         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
207                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
208         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
209                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
210
211
212         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
213 }
214
215 static gchar *input_dialog_open(const gchar *title, const gchar *message,
216                                 const gchar *default_string)
217 {
218         gchar *str;
219
220         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
221
222         if (!dialog)
223                 input_dialog_create();
224
225         input_dialog_set(title, message, default_string);
226         gtk_widget_show(dialog);
227         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
228         manage_window_set_transient(GTK_WINDOW(dialog));
229
230         gtk_main();
231
232         manage_window_focus_out(dialog, NULL, NULL);
233         gtk_widget_hide(dialog);
234
235         if (ack) {
236                 GtkEditable *editable;
237
238                 if (type == INPUT_DIALOG_COMBO)
239                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
240                 else
241                         editable = GTK_EDITABLE(entry);
242
243                 str = gtk_editable_get_chars(editable, 0, -1);
244                 if (str && *str == '\0') {
245                         g_free(str);
246                         str = NULL;
247                 }
248         } else
249                 str = NULL;
250
251         GTK_EVENTS_FLUSH();
252
253         debug_print("return string = %s\n", str ? str : "(none)");
254         return str;
255 }
256
257 static void input_dialog_set(const gchar *title, const gchar *message,
258                              const gchar *default_string)
259 {
260         GtkWidget *entry_;
261
262         if (type == INPUT_DIALOG_COMBO)
263                 entry_ = GTK_COMBO(combo)->entry;
264         else
265                 entry_ = entry;
266
267         gtk_window_set_title(GTK_WINDOW(dialog), title);
268         gtk_label_set_text(GTK_LABEL(msg_label), message);
269         if (default_string && *default_string)
270                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
271         else
272                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
273         gtk_entry_set_position(GTK_ENTRY(entry_), 0);
274         gtk_entry_select_region(GTK_ENTRY(entry_), 0, -1);
275
276         gtk_widget_grab_focus(ok_button);
277         gtk_widget_grab_focus(entry_);
278 }
279
280 static void ok_clicked(GtkWidget *widget, gpointer data)
281 {
282         ack = TRUE;
283         gtk_main_quit();
284 }
285
286 static void cancel_clicked(GtkWidget *widget, gpointer data)
287 {
288         ack = FALSE;
289         gtk_main_quit();
290 }
291
292 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
293 {
294         ack = FALSE;
295         gtk_main_quit();
296
297         return TRUE;
298 }
299
300 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
301 {
302         if (event && event->keyval == GDK_Escape) {
303                 ack = FALSE;
304                 gtk_main_quit();
305         }
306 }
307
308 static void entry_activated(GtkEditable *editable)
309 {
310         ack = TRUE;
311         gtk_main_quit();
312 }
313
314 static void combo_activated(GtkEditable *editable)
315 {
316         ack = TRUE;
317         gtk_main_quit();
318 }