visual indicator locked ("keep"); not yet completed
[claws.git] / src / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 static void input_dialog_create(void)
144 {
145         GtkWidget *vbox;
146         GtkWidget *hbox;
147         GtkWidget *confirm_area;
148         GtkWidget *cancel_button;
149
150         dialog = gtk_dialog_new();
151         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
152         gtk_widget_set_usize(dialog, INPUT_DIALOG_WIDTH, -1);
153         gtk_container_set_border_width
154                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
155         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
156         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
157                            GTK_SIGNAL_FUNC(delete_event), NULL);
158         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
159                            GTK_SIGNAL_FUNC(key_pressed), NULL);
160         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
161                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
162         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
163                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
164
165         gtk_widget_realize(dialog);
166
167         vbox = gtk_vbox_new(FALSE, 8);
168         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
169         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
170
171         hbox = gtk_hbox_new(FALSE, 0);
172         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
173
174         msg_label = gtk_label_new("");
175         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
176         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
177
178         entry = gtk_entry_new();
179         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
180         gtk_signal_connect(GTK_OBJECT(entry), "activate",
181                            GTK_SIGNAL_FUNC(entry_activated), NULL);
182
183         combo = gtk_combo_new();
184         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
185         gtk_signal_connect(GTK_OBJECT(GTK_COMBO(combo)->entry), "activate",
186                            GTK_SIGNAL_FUNC(combo_activated), NULL);
187
188         gtkut_button_set_create(&confirm_area,
189                                 &ok_button,     _("OK"),
190                                 &cancel_button, _("Cancel"),
191                                 NULL, NULL);
192         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
193                           confirm_area);
194         gtk_widget_grab_default(ok_button);
195
196         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
197                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
198         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
199                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
200
201
202         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
203 }
204
205 static gchar *input_dialog_open(const gchar *title, const gchar *message,
206                                 const gchar *default_string)
207 {
208         gchar *str;
209
210         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
211
212         if (!dialog)
213                 input_dialog_create();
214
215         input_dialog_set(title, message, default_string);
216         gtk_widget_show(dialog);
217         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
218         manage_window_set_transient(GTK_WINDOW(dialog));
219
220         gtk_main();
221
222         manage_window_focus_out(dialog, NULL, NULL);
223         gtk_widget_hide(dialog);
224
225         if (ack) {
226                 GtkEditable *editable;
227
228                 if (type == INPUT_DIALOG_COMBO)
229                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
230                 else
231                         editable = GTK_EDITABLE(entry);
232
233                 str = gtk_editable_get_chars(editable, 0, -1);
234                 if (str && *str == '\0') {
235                         g_free(str);
236                         str = NULL;
237                 }
238         } else
239                 str = NULL;
240
241         GTK_EVENTS_FLUSH();
242
243         debug_print("return string = %s\n", str ? str : "(none)");
244         return str;
245 }
246
247 static void input_dialog_set(const gchar *title, const gchar *message,
248                              const gchar *default_string)
249 {
250         GtkWidget *entry_;
251
252         if (type == INPUT_DIALOG_COMBO)
253                 entry_ = GTK_COMBO(combo)->entry;
254         else
255                 entry_ = entry;
256
257         gtk_window_set_title(GTK_WINDOW(dialog), title);
258         gtk_label_set_text(GTK_LABEL(msg_label), message);
259         if (default_string && *default_string)
260                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
261         else
262                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
263         gtk_entry_set_position(GTK_ENTRY(entry_), 0);
264         gtk_entry_select_region(GTK_ENTRY(entry_), 0, -1);
265
266         gtk_widget_grab_focus(ok_button);
267         gtk_widget_grab_focus(entry_);
268 }
269
270 static void ok_clicked(GtkWidget *widget, gpointer data)
271 {
272         ack = TRUE;
273         gtk_main_quit();
274 }
275
276 static void cancel_clicked(GtkWidget *widget, gpointer data)
277 {
278         ack = FALSE;
279         gtk_main_quit();
280 }
281
282 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
283 {
284         ack = FALSE;
285         gtk_main_quit();
286
287         return TRUE;
288 }
289
290 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
291 {
292         if (event && event->keyval == GDK_Escape) {
293                 ack = FALSE;
294                 gtk_main_quit();
295         }
296 }
297
298 static void entry_activated(GtkEditable *editable)
299 {
300         ack = TRUE;
301         gtk_main_quit();
302 }
303
304 static void combo_activated(GtkEditable *editable)
305 {
306         ack = TRUE;
307         gtk_main_quit();
308 }