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