Statistics
| Revision:

root / src / inputdialog.c @ 2196

History | View | Annotate | Download (8.6 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 915 hiro
 * Copyright (C) 1999-2006 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#ifdef HAVE_CONFIG_H
21 1 hiro
#  include "config.h"
22 1 hiro
#endif
23 1 hiro
24 1 hiro
#include <glib.h>
25 92 hiro
#include <glib/gi18n.h>
26 1 hiro
#include <gdk/gdkkeysyms.h>
27 1 hiro
#include <gtk/gtkmain.h>
28 1 hiro
#include <gtk/gtkwidget.h>
29 1 hiro
#include <gtk/gtkdialog.h>
30 1 hiro
#include <gtk/gtkwindow.h>
31 1 hiro
#include <gtk/gtksignal.h>
32 1 hiro
#include <gtk/gtkvbox.h>
33 1 hiro
#include <gtk/gtkhbox.h>
34 1 hiro
#include <gtk/gtklabel.h>
35 1 hiro
#include <gtk/gtkentry.h>
36 1 hiro
#include <gtk/gtkcombo.h>
37 1 hiro
#include <gtk/gtkbutton.h>
38 1 hiro
#include <gtk/gtkhbbox.h>
39 237 hiro
#include <gtk/gtkstock.h>
40 1 hiro
41 1 hiro
#include "inputdialog.h"
42 1 hiro
#include "manage_window.h"
43 1 hiro
#include "inc.h"
44 405 hiro
#include "prefs_common.h"
45 1 hiro
#include "gtkutils.h"
46 1 hiro
#include "utils.h"
47 1 hiro
48 656 hiro
#define INPUT_ENTRY_WIDTH        400
49 1 hiro
50 1 hiro
typedef enum
51 1 hiro
{
52 1 hiro
        INPUT_DIALOG_NORMAL,
53 1 hiro
        INPUT_DIALOG_INVISIBLE,
54 1 hiro
        INPUT_DIALOG_COMBO
55 1 hiro
} InputDialogType;
56 1 hiro
57 1 hiro
static gboolean ack;
58 1 hiro
static gboolean fin;
59 1 hiro
60 1 hiro
static InputDialogType type;
61 1 hiro
62 1 hiro
static GtkWidget *dialog;
63 1 hiro
static GtkWidget *msg_label;
64 1 hiro
static GtkWidget *entry;
65 1 hiro
static GtkWidget *combo;
66 405 hiro
static GtkWidget *confirm_area;
67 1 hiro
static GtkWidget *ok_button;
68 1 hiro
69 677 hiro
static void input_dialog_create        (InputDialogType        dialog_type);
70 677 hiro
71 1 hiro
static gchar *input_dialog_open        (const gchar        *title,
72 1 hiro
                                 const gchar        *message,
73 1 hiro
                                 const gchar        *default_string);
74 1 hiro
static void input_dialog_set        (const gchar        *title,
75 1 hiro
                                 const gchar        *message,
76 1 hiro
                                 const gchar        *default_string);
77 1 hiro
78 1 hiro
static void ok_clicked                (GtkWidget        *widget,
79 1 hiro
                                 gpointer         data);
80 1 hiro
static void cancel_clicked        (GtkWidget        *widget,
81 1 hiro
                                 gpointer         data);
82 1 hiro
static gint delete_event        (GtkWidget        *widget,
83 1 hiro
                                 GdkEventAny        *event,
84 1 hiro
                                 gpointer         data);
85 1 hiro
static gboolean key_pressed        (GtkWidget        *widget,
86 1 hiro
                                 GdkEventKey        *event,
87 1 hiro
                                 gpointer         data);
88 1 hiro
static void entry_activated        (GtkEditable        *editable);
89 1 hiro
static void combo_activated        (GtkEditable        *editable);
90 678 hiro
static gint focus_out                (GtkWidget        *widget,
91 678 hiro
                                 GdkEventFocus        *event,
92 678 hiro
                                 gpointer         data);
93 1 hiro
94 1 hiro
95 1 hiro
gchar *input_dialog(const gchar *title, const gchar *message,
96 1 hiro
                    const gchar *default_string)
97 1 hiro
{
98 677 hiro
        if (dialog)
99 677 hiro
                return NULL;
100 1 hiro
101 677 hiro
        input_dialog_create(INPUT_DIALOG_NORMAL);
102 1 hiro
103 1 hiro
        return input_dialog_open(title, message, default_string);
104 1 hiro
}
105 1 hiro
106 1 hiro
gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
107 1 hiro
                                   const gchar *default_string)
108 1 hiro
{
109 677 hiro
        if (dialog)
110 677 hiro
                return NULL;
111 1 hiro
112 677 hiro
        input_dialog_create(INPUT_DIALOG_INVISIBLE);
113 1 hiro
114 1 hiro
        return input_dialog_open(title, message, default_string);
115 1 hiro
}
116 1 hiro
117 1 hiro
gchar *input_dialog_combo(const gchar *title, const gchar *message,
118 1 hiro
                          const gchar *default_string, GList *list,
119 1 hiro
                          gboolean case_sensitive)
120 1 hiro
{
121 677 hiro
        if (dialog)
122 677 hiro
                return NULL;
123 1 hiro
124 677 hiro
        input_dialog_create(INPUT_DIALOG_COMBO);
125 1 hiro
126 1 hiro
        if (!list) {
127 1 hiro
                GList empty_list;
128 1 hiro
129 1 hiro
                empty_list.data = (gpointer)"";
130 1 hiro
                empty_list.next = NULL;
131 1 hiro
                empty_list.prev = NULL;
132 1 hiro
                gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
133 1 hiro
        } else
134 1 hiro
                gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
135 1 hiro
136 1 hiro
        gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
137 1 hiro
138 1 hiro
        return input_dialog_open(title, message, default_string);
139 1 hiro
}
140 1 hiro
141 1 hiro
gchar *input_dialog_query_password(const gchar *server, const gchar *user)
142 1 hiro
{
143 1 hiro
        gchar *message;
144 1 hiro
        gchar *pass;
145 1 hiro
146 1 hiro
        message = g_strdup_printf(_("Input password for %s on %s:"),
147 1 hiro
                                  user, server);
148 1 hiro
        pass = input_dialog_with_invisible(_("Input password"), message, NULL);
149 1 hiro
        g_free(message);
150 1 hiro
151 1 hiro
        return pass;
152 1 hiro
}
153 1 hiro
154 677 hiro
static void input_dialog_create(InputDialogType dialog_type)
155 1 hiro
{
156 1 hiro
        GtkWidget *vbox;
157 1 hiro
        GtkWidget *hbox;
158 1 hiro
        GtkWidget *cancel_button;
159 1 hiro
160 1 hiro
        dialog = gtk_dialog_new();
161 644 hiro
        gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
162 1 hiro
        gtk_container_set_border_width
163 1 hiro
                (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
164 915 hiro
        gtk_window_set_position(GTK_WINDOW(dialog),
165 915 hiro
                                GTK_WIN_POS_CENTER_ON_PARENT);
166 922 hiro
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
167 922 hiro
        manage_window_set_transient(GTK_WINDOW(dialog));
168 1 hiro
        g_signal_connect(G_OBJECT(dialog), "delete_event",
169 1 hiro
                         G_CALLBACK(delete_event), NULL);
170 1 hiro
        g_signal_connect(G_OBJECT(dialog), "key_press_event",
171 1 hiro
                         G_CALLBACK(key_pressed), NULL);
172 678 hiro
        g_signal_connect(G_OBJECT(dialog), "focus_out_event",
173 678 hiro
                         G_CALLBACK(focus_out), NULL);
174 1 hiro
        MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
175 1 hiro
176 1 hiro
        vbox = gtk_vbox_new(FALSE, 8);
177 1 hiro
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
178 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
179 1 hiro
180 1 hiro
        hbox = gtk_hbox_new(FALSE, 0);
181 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
182 1 hiro
183 1 hiro
        msg_label = gtk_label_new("");
184 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
185 1 hiro
        gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
186 1 hiro
187 677 hiro
        type = dialog_type;
188 1 hiro
189 677 hiro
        if (dialog_type == INPUT_DIALOG_COMBO) {
190 677 hiro
                combo = gtk_combo_new();
191 677 hiro
                gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
192 677 hiro
                gtk_widget_set_size_request(combo, INPUT_ENTRY_WIDTH, -1);
193 677 hiro
                g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
194 677 hiro
                                 G_CALLBACK(combo_activated), NULL);
195 677 hiro
        } else {
196 677 hiro
                entry = gtk_entry_new();
197 677 hiro
                gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
198 677 hiro
                gtk_widget_set_size_request(entry, INPUT_ENTRY_WIDTH, -1);
199 677 hiro
                g_signal_connect(G_OBJECT(entry), "activate",
200 677 hiro
                                 G_CALLBACK(entry_activated), NULL);
201 677 hiro
                if (dialog_type == INPUT_DIALOG_INVISIBLE)
202 677 hiro
                        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
203 677 hiro
        }
204 1 hiro
205 30 hiro
        gtkut_stock_button_set_create(&confirm_area,
206 30 hiro
                                      &ok_button, GTK_STOCK_OK,
207 30 hiro
                                      &cancel_button, GTK_STOCK_CANCEL,
208 30 hiro
                                      NULL, NULL);
209 1 hiro
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
210 1 hiro
                          confirm_area);
211 1 hiro
        gtk_widget_grab_default(ok_button);
212 1 hiro
213 1 hiro
        g_signal_connect(G_OBJECT(ok_button), "clicked",
214 1 hiro
                         G_CALLBACK(ok_clicked), NULL);
215 1 hiro
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
216 1 hiro
                         G_CALLBACK(cancel_clicked), NULL);
217 1 hiro
218 1 hiro
        gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
219 1 hiro
}
220 1 hiro
221 1 hiro
static gchar *input_dialog_open(const gchar *title, const gchar *message,
222 1 hiro
                                const gchar *default_string)
223 1 hiro
{
224 1 hiro
        gchar *str;
225 1 hiro
226 405 hiro
        gtkut_box_set_reverse_order(GTK_BOX(confirm_area),
227 405 hiro
                                    !prefs_common.comply_gnome_hig);
228 1 hiro
        input_dialog_set(title, message, default_string);
229 677 hiro
        gtk_widget_show(dialog);
230 1 hiro
231 1 hiro
        ack = fin = FALSE;
232 1 hiro
233 1 hiro
        inc_lock();
234 1 hiro
235 1 hiro
        while (fin == FALSE)
236 1 hiro
                gtk_main_iteration();
237 1 hiro
238 1 hiro
        manage_window_focus_out(dialog, NULL, NULL);
239 1 hiro
240 1 hiro
        if (ack) {
241 1 hiro
                GtkEditable *editable;
242 1 hiro
243 1 hiro
                if (type == INPUT_DIALOG_COMBO)
244 1 hiro
                        editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
245 1 hiro
                else
246 1 hiro
                        editable = GTK_EDITABLE(entry);
247 1 hiro
248 1 hiro
                str = gtk_editable_get_chars(editable, 0, -1);
249 1 hiro
                if (str && *str == '\0') {
250 1 hiro
                        g_free(str);
251 1 hiro
                        str = NULL;
252 1 hiro
                }
253 1 hiro
        } else
254 1 hiro
                str = NULL;
255 1 hiro
256 677 hiro
        gtk_widget_destroy(dialog);
257 677 hiro
        dialog = msg_label = entry = combo = confirm_area = ok_button = NULL;
258 677 hiro
259 1 hiro
        GTK_EVENTS_FLUSH();
260 1 hiro
261 1 hiro
        inc_unlock();
262 1 hiro
263 2040 hiro
        if (type != INPUT_DIALOG_INVISIBLE)
264 2040 hiro
                debug_print("return string = %s\n", str ? str : "(none)");
265 2040 hiro
266 1 hiro
        return str;
267 1 hiro
}
268 1 hiro
269 1 hiro
static void input_dialog_set(const gchar *title, const gchar *message,
270 1 hiro
                             const gchar *default_string)
271 1 hiro
{
272 1 hiro
        GtkWidget *entry_;
273 1 hiro
274 1 hiro
        if (type == INPUT_DIALOG_COMBO)
275 1 hiro
                entry_ = GTK_COMBO(combo)->entry;
276 1 hiro
        else
277 1 hiro
                entry_ = entry;
278 1 hiro
279 1 hiro
        gtk_window_set_title(GTK_WINDOW(dialog), title);
280 1 hiro
        gtk_label_set_text(GTK_LABEL(msg_label), message);
281 1 hiro
        if (default_string && *default_string) {
282 1 hiro
                gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
283 1 hiro
                gtk_entry_set_position(GTK_ENTRY(entry_), 0);
284 1 hiro
                gtk_entry_select_region(GTK_ENTRY(entry_), 0, -1);
285 1 hiro
        } else
286 1 hiro
                gtk_entry_set_text(GTK_ENTRY(entry_), "");
287 1 hiro
288 1 hiro
        gtk_widget_grab_focus(ok_button);
289 1 hiro
        gtk_widget_grab_focus(entry_);
290 1 hiro
}
291 1 hiro
292 1 hiro
static void ok_clicked(GtkWidget *widget, gpointer data)
293 1 hiro
{
294 1 hiro
        ack = TRUE;
295 1 hiro
        fin = TRUE;
296 1 hiro
}
297 1 hiro
298 1 hiro
static void cancel_clicked(GtkWidget *widget, gpointer data)
299 1 hiro
{
300 1 hiro
        ack = FALSE;
301 1 hiro
        fin = TRUE;
302 1 hiro
}
303 1 hiro
304 1 hiro
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
305 1 hiro
{
306 1 hiro
        ack = FALSE;
307 1 hiro
        fin = TRUE;
308 1 hiro
309 1 hiro
        return TRUE;
310 1 hiro
}
311 1 hiro
312 1 hiro
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
313 1 hiro
{
314 1 hiro
        if (event && event->keyval == GDK_Escape) {
315 1 hiro
                ack = FALSE;
316 1 hiro
                fin = TRUE;
317 1 hiro
        }
318 1 hiro
319 1 hiro
        return FALSE;
320 1 hiro
}
321 1 hiro
322 1 hiro
static void entry_activated(GtkEditable *editable)
323 1 hiro
{
324 1 hiro
        ack = TRUE;
325 1 hiro
        fin = TRUE;
326 1 hiro
}
327 1 hiro
328 1 hiro
static void combo_activated(GtkEditable *editable)
329 1 hiro
{
330 1 hiro
        ack = TRUE;
331 1 hiro
        fin = TRUE;
332 1 hiro
}
333 678 hiro
334 678 hiro
static gint focus_out(GtkWidget *widget, GdkEventFocus *event, gpointer data)
335 678 hiro
{
336 678 hiro
#ifdef G_OS_WIN32
337 678 hiro
        gtk_window_present(GTK_WINDOW(widget));
338 678 hiro
#endif
339 678 hiro
        return FALSE;
340 678 hiro
}