Statistics
| Revision:

root / src / inputdialog.c @ 644

History | View | Annotate | Download (8.4 kB)

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