sync with 0.7.5cvs9
[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                           gboolean case_sensitive)
121 {
122         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
123
124         if (!dialog)
125                 input_dialog_create();
126
127         type = INPUT_DIALOG_COMBO;
128         gtk_widget_hide(entry);
129         gtk_widget_show(combo);
130
131         if (!list) {
132                 GList empty_list;
133
134                 empty_list.data = (gpointer)"";
135                 empty_list.next = NULL;
136                 empty_list.prev = NULL;
137                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
138         } else
139                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
140
141         gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
142
143         return input_dialog_open(title, message, default_string);
144 }
145
146 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
147 {
148         gchar *message;
149         gchar *pass;
150
151         message = g_strdup_printf(_("Input password for %s on %s:"),
152                                   user, server);
153         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
154         g_free(message);
155
156         return pass;
157 }
158
159 static void input_dialog_create(void)
160 {
161         GtkWidget *vbox;
162         GtkWidget *hbox;
163         GtkWidget *confirm_area;
164         GtkWidget *cancel_button;
165
166         dialog = gtk_dialog_new();
167         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
168         gtk_widget_set_usize(dialog, INPUT_DIALOG_WIDTH, -1);
169         gtk_container_set_border_width
170                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
171         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
172         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
173                            GTK_SIGNAL_FUNC(delete_event), NULL);
174         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
175                            GTK_SIGNAL_FUNC(key_pressed), NULL);
176         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
177
178         gtk_widget_realize(dialog);
179
180         vbox = gtk_vbox_new(FALSE, 8);
181         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
182         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
183
184         hbox = gtk_hbox_new(FALSE, 0);
185         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
186
187         msg_label = gtk_label_new("");
188         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
189         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
190
191         entry = gtk_entry_new();
192         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
193         gtk_signal_connect(GTK_OBJECT(entry), "activate",
194                            GTK_SIGNAL_FUNC(entry_activated), NULL);
195
196         combo = gtk_combo_new();
197         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
198         gtk_signal_connect(GTK_OBJECT(GTK_COMBO(combo)->entry), "activate",
199                            GTK_SIGNAL_FUNC(combo_activated), NULL);
200
201         gtkut_button_set_create(&confirm_area,
202                                 &ok_button,     _("OK"),
203                                 &cancel_button, _("Cancel"),
204                                 NULL, NULL);
205         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
206                           confirm_area);
207         gtk_widget_grab_default(ok_button);
208
209         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
210                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
211         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
212                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
213
214
215         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
216 }
217
218 static gchar *input_dialog_open(const gchar *title, const gchar *message,
219                                 const gchar *default_string)
220 {
221         gchar *str;
222
223         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
224
225         if (!dialog)
226                 input_dialog_create();
227
228         input_dialog_set(title, message, default_string);
229         gtk_widget_show(dialog);
230         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
231         manage_window_set_transient(GTK_WINDOW(dialog));
232
233         gtk_main();
234
235         manage_window_focus_out(dialog, NULL, NULL);
236         gtk_widget_hide(dialog);
237
238         if (ack) {
239                 GtkEditable *editable;
240
241                 if (type == INPUT_DIALOG_COMBO)
242                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
243                 else
244                         editable = GTK_EDITABLE(entry);
245
246                 str = gtk_editable_get_chars(editable, 0, -1);
247                 if (str && *str == '\0') {
248                         g_free(str);
249                         str = NULL;
250                 }
251         } else
252                 str = NULL;
253
254         GTK_EVENTS_FLUSH();
255
256         debug_print("return string = %s\n", str ? str : "(none)");
257         return str;
258 }
259
260 static void input_dialog_set(const gchar *title, const gchar *message,
261                              const gchar *default_string)
262 {
263         GtkWidget *entry_;
264
265         if (type == INPUT_DIALOG_COMBO)
266                 entry_ = GTK_COMBO(combo)->entry;
267         else
268                 entry_ = entry;
269
270         gtk_window_set_title(GTK_WINDOW(dialog), title);
271         gtk_label_set_text(GTK_LABEL(msg_label), message);
272         if (default_string && *default_string)
273                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
274         else
275                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
276         gtk_entry_set_position(GTK_ENTRY(entry_), 0);
277         gtk_entry_select_region(GTK_ENTRY(entry_), 0, -1);
278
279         gtk_widget_grab_focus(ok_button);
280         gtk_widget_grab_focus(entry_);
281 }
282
283 static void ok_clicked(GtkWidget *widget, gpointer data)
284 {
285         ack = TRUE;
286         gtk_main_quit();
287 }
288
289 static void cancel_clicked(GtkWidget *widget, gpointer data)
290 {
291         ack = FALSE;
292         gtk_main_quit();
293 }
294
295 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
296 {
297         ack = FALSE;
298         gtk_main_quit();
299
300         return TRUE;
301 }
302
303 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
304 {
305         if (event && event->keyval == GDK_Escape) {
306                 ack = FALSE;
307                 gtk_main_quit();
308         }
309 }
310
311 static void entry_activated(GtkEditable *editable)
312 {
313         ack = TRUE;
314         gtk_main_quit();
315 }
316
317 static void combo_activated(GtkEditable *editable)
318 {
319         ack = TRUE;
320         gtk_main_quit();
321 }