This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / inputdialog.c
blobff2cd8dce7ba7834a72e93df7adf5073965b3484
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
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>
39 #include "intl.h"
40 #include "inputdialog.h"
41 #include "manage_window.h"
42 #include "gtkutils.h"
43 #include "utils.h"
45 #define INPUT_DIALOG_WIDTH 420
47 typedef enum
49 INPUT_DIALOG_NORMAL,
50 INPUT_DIALOG_INVISIBLE,
51 INPUT_DIALOG_COMBO
52 } InputDialogType;
54 static gboolean ack;
56 static InputDialogType type;
58 static GtkWidget *dialog;
59 static GtkWidget *msg_label;
60 static GtkWidget *entry;
61 static GtkWidget *combo;
62 static GtkWidget *ok_button;
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);
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);
86 gchar *input_dialog(const gchar *title, const gchar *message,
87 const gchar *default_string)
89 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
91 if (!dialog)
92 input_dialog_create();
94 type = INPUT_DIALOG_NORMAL;
95 gtk_widget_hide(combo);
96 gtk_widget_show(entry);
97 gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
99 return input_dialog_open(title, message, default_string);
102 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
103 const gchar *default_string)
105 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
107 if (!dialog)
108 input_dialog_create();
110 type = INPUT_DIALOG_INVISIBLE;
111 gtk_widget_hide(combo);
112 gtk_widget_show(entry);
113 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
115 return input_dialog_open(title, message, default_string);
118 gchar *input_dialog_combo(const gchar *title, const gchar *message,
119 const gchar *default_string, GList *list,
120 gboolean case_sensitive)
122 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
124 if (!dialog)
125 input_dialog_create();
127 type = INPUT_DIALOG_COMBO;
128 gtk_widget_hide(entry);
129 gtk_widget_show(combo);
131 if (!list) {
132 GList empty_list;
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);
141 gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
143 return input_dialog_open(title, message, default_string);
146 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
148 gchar *message;
149 gchar *pass;
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);
156 return pass;
159 static void input_dialog_create(void)
161 GtkWidget *vbox;
162 GtkWidget *hbox;
163 GtkWidget *confirm_area;
164 GtkWidget *cancel_button;
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);
178 gtk_widget_realize(dialog);
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);
184 hbox = gtk_hbox_new(FALSE, 0);
185 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
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);
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);
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);
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);
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);
215 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
218 static gchar *input_dialog_open(const gchar *title, const gchar *message,
219 const gchar *default_string)
221 gchar *str;
223 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
225 if (!dialog)
226 input_dialog_create();
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));
233 gtk_main();
235 manage_window_focus_out(dialog, NULL, NULL);
236 gtk_widget_hide(dialog);
238 if (ack) {
239 GtkEditable *editable;
241 if (type == INPUT_DIALOG_COMBO)
242 editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
243 else
244 editable = GTK_EDITABLE(entry);
246 str = gtk_editable_get_chars(editable, 0, -1);
247 if (str && *str == '\0') {
248 g_free(str);
249 str = NULL;
251 } else
252 str = NULL;
254 GTK_EVENTS_FLUSH();
256 debug_print("return string = %s\n", str ? str : "(none)");
257 return str;
260 static void input_dialog_set(const gchar *title, const gchar *message,
261 const gchar *default_string)
263 GtkWidget *entry_;
265 if (type == INPUT_DIALOG_COMBO)
266 entry_ = GTK_COMBO(combo)->entry;
267 else
268 entry_ = entry;
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);
279 gtk_widget_grab_focus(ok_button);
280 gtk_widget_grab_focus(entry_);
283 static void ok_clicked(GtkWidget *widget, gpointer data)
285 ack = TRUE;
286 gtk_main_quit();
289 static void cancel_clicked(GtkWidget *widget, gpointer data)
291 ack = FALSE;
292 gtk_main_quit();
295 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
297 ack = FALSE;
298 gtk_main_quit();
300 return TRUE;
303 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
305 if (event && event->keyval == GDK_Escape) {
306 ack = FALSE;
307 gtk_main_quit();
311 static void entry_activated(GtkEditable *editable)
313 ack = TRUE;
314 gtk_main_quit();
317 static void combo_activated(GtkEditable *editable)
319 ack = TRUE;
320 gtk_main_quit();