Statistics
| Revision:

root / src / inputdialog.c @ 2411

History | View | Annotate | Download (9.9 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 2399 hiro
 * Copyright (C) 1999-2009 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 2399 hiro
#include "filesel.h"
45 405 hiro
#include "prefs_common.h"
46 1 hiro
#include "gtkutils.h"
47 1 hiro
#include "utils.h"
48 1 hiro
49 2399 hiro
#define DIALOG_WIDTH        420
50 1 hiro
51 1 hiro
typedef enum
52 1 hiro
{
53 1 hiro
        INPUT_DIALOG_NORMAL,
54 1 hiro
        INPUT_DIALOG_INVISIBLE,
55 2399 hiro
        INPUT_DIALOG_COMBO,
56 2399 hiro
        INPUT_DIALOG_FILESEL
57 1 hiro
} InputDialogType;
58 1 hiro
59 1 hiro
static gboolean ack;
60 1 hiro
static gboolean fin;
61 1 hiro
62 1 hiro
static InputDialogType type;
63 2399 hiro
static GtkFileChooserAction chooser_action;
64 1 hiro
65 1 hiro
static GtkWidget *dialog;
66 1 hiro
static GtkWidget *msg_label;
67 1 hiro
static GtkWidget *entry;
68 1 hiro
static GtkWidget *combo;
69 405 hiro
static GtkWidget *confirm_area;
70 1 hiro
static GtkWidget *ok_button;
71 1 hiro
72 677 hiro
static void input_dialog_create        (InputDialogType        dialog_type);
73 677 hiro
74 1 hiro
static gchar *input_dialog_open        (const gchar        *title,
75 1 hiro
                                 const gchar        *message,
76 1 hiro
                                 const gchar        *default_string);
77 1 hiro
static void input_dialog_set        (const gchar        *title,
78 1 hiro
                                 const gchar        *message,
79 1 hiro
                                 const gchar        *default_string);
80 1 hiro
81 1 hiro
static void ok_clicked                (GtkWidget        *widget,
82 1 hiro
                                 gpointer         data);
83 1 hiro
static void cancel_clicked        (GtkWidget        *widget,
84 1 hiro
                                 gpointer         data);
85 1 hiro
static gint delete_event        (GtkWidget        *widget,
86 1 hiro
                                 GdkEventAny        *event,
87 1 hiro
                                 gpointer         data);
88 1 hiro
static gboolean key_pressed        (GtkWidget        *widget,
89 1 hiro
                                 GdkEventKey        *event,
90 1 hiro
                                 gpointer         data);
91 1 hiro
static void entry_activated        (GtkEditable        *editable);
92 1 hiro
static void combo_activated        (GtkEditable        *editable);
93 2399 hiro
static void sel_btn_clicked        (GtkButton        *button,
94 2399 hiro
                                 gpointer         data);
95 678 hiro
static gint focus_out                (GtkWidget        *widget,
96 678 hiro
                                 GdkEventFocus        *event,
97 678 hiro
                                 gpointer         data);
98 1 hiro
99 1 hiro
100 1 hiro
gchar *input_dialog(const gchar *title, const gchar *message,
101 1 hiro
                    const gchar *default_string)
102 1 hiro
{
103 677 hiro
        if (dialog)
104 677 hiro
                return NULL;
105 1 hiro
106 677 hiro
        input_dialog_create(INPUT_DIALOG_NORMAL);
107 1 hiro
108 1 hiro
        return input_dialog_open(title, message, default_string);
109 1 hiro
}
110 1 hiro
111 1 hiro
gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
112 1 hiro
                                   const gchar *default_string)
113 1 hiro
{
114 677 hiro
        if (dialog)
115 677 hiro
                return NULL;
116 1 hiro
117 677 hiro
        input_dialog_create(INPUT_DIALOG_INVISIBLE);
118 1 hiro
119 1 hiro
        return input_dialog_open(title, message, default_string);
120 1 hiro
}
121 1 hiro
122 1 hiro
gchar *input_dialog_combo(const gchar *title, const gchar *message,
123 1 hiro
                          const gchar *default_string, GList *list,
124 1 hiro
                          gboolean case_sensitive)
125 1 hiro
{
126 677 hiro
        if (dialog)
127 677 hiro
                return NULL;
128 1 hiro
129 677 hiro
        input_dialog_create(INPUT_DIALOG_COMBO);
130 1 hiro
131 1 hiro
        if (!list) {
132 1 hiro
                GList empty_list;
133 1 hiro
134 1 hiro
                empty_list.data = (gpointer)"";
135 1 hiro
                empty_list.next = NULL;
136 1 hiro
                empty_list.prev = NULL;
137 1 hiro
                gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
138 1 hiro
        } else
139 1 hiro
                gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
140 1 hiro
141 1 hiro
        gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
142 1 hiro
143 1 hiro
        return input_dialog_open(title, message, default_string);
144 1 hiro
}
145 1 hiro
146 1 hiro
gchar *input_dialog_query_password(const gchar *server, const gchar *user)
147 1 hiro
{
148 1 hiro
        gchar *message;
149 1 hiro
        gchar *pass;
150 1 hiro
151 1 hiro
        message = g_strdup_printf(_("Input password for %s on %s:"),
152 1 hiro
                                  user, server);
153 1 hiro
        pass = input_dialog_with_invisible(_("Input password"), message, NULL);
154 1 hiro
        g_free(message);
155 1 hiro
156 1 hiro
        return pass;
157 1 hiro
}
158 1 hiro
159 2399 hiro
gchar *input_dialog_with_filesel(const gchar *title, const gchar *message,
160 2399 hiro
                                 const gchar *default_string,
161 2399 hiro
                                 GtkFileChooserAction action)
162 2399 hiro
{
163 2399 hiro
        if (dialog)
164 2399 hiro
                return NULL;
165 2399 hiro
166 2399 hiro
        input_dialog_create(INPUT_DIALOG_FILESEL);
167 2399 hiro
        chooser_action = action;
168 2399 hiro
169 2399 hiro
        return input_dialog_open(title, message, default_string);
170 2399 hiro
}
171 2399 hiro
172 677 hiro
static void input_dialog_create(InputDialogType dialog_type)
173 1 hiro
{
174 1 hiro
        GtkWidget *vbox;
175 1 hiro
        GtkWidget *hbox;
176 2399 hiro
        GtkWidget *sel_btn;
177 1 hiro
        GtkWidget *cancel_button;
178 1 hiro
179 1 hiro
        dialog = gtk_dialog_new();
180 644 hiro
        gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
181 2399 hiro
        gtk_widget_set_size_request(dialog, DIALOG_WIDTH, -1);
182 1 hiro
        gtk_container_set_border_width
183 1 hiro
                (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
184 915 hiro
        gtk_window_set_position(GTK_WINDOW(dialog),
185 915 hiro
                                GTK_WIN_POS_CENTER_ON_PARENT);
186 922 hiro
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
187 922 hiro
        manage_window_set_transient(GTK_WINDOW(dialog));
188 1 hiro
        g_signal_connect(G_OBJECT(dialog), "delete_event",
189 1 hiro
                         G_CALLBACK(delete_event), NULL);
190 1 hiro
        g_signal_connect(G_OBJECT(dialog), "key_press_event",
191 1 hiro
                         G_CALLBACK(key_pressed), NULL);
192 678 hiro
        g_signal_connect(G_OBJECT(dialog), "focus_out_event",
193 678 hiro
                         G_CALLBACK(focus_out), NULL);
194 1 hiro
        MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
195 1 hiro
196 1 hiro
        vbox = gtk_vbox_new(FALSE, 8);
197 1 hiro
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
198 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
199 1 hiro
200 1 hiro
        hbox = gtk_hbox_new(FALSE, 0);
201 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
202 1 hiro
203 1 hiro
        msg_label = gtk_label_new("");
204 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
205 1 hiro
        gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
206 1 hiro
207 2399 hiro
        hbox = gtk_hbox_new(FALSE, 4);
208 2399 hiro
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
209 2399 hiro
210 677 hiro
        type = dialog_type;
211 1 hiro
212 677 hiro
        if (dialog_type == INPUT_DIALOG_COMBO) {
213 677 hiro
                combo = gtk_combo_new();
214 2399 hiro
                gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);
215 677 hiro
                g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
216 677 hiro
                                 G_CALLBACK(combo_activated), NULL);
217 677 hiro
        } else {
218 677 hiro
                entry = gtk_entry_new();
219 2399 hiro
                gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
220 677 hiro
                g_signal_connect(G_OBJECT(entry), "activate",
221 677 hiro
                                 G_CALLBACK(entry_activated), NULL);
222 2399 hiro
                if (dialog_type == INPUT_DIALOG_FILESEL) {
223 2399 hiro
                        sel_btn = gtk_button_new_with_label("...");
224 2399 hiro
                        gtk_box_pack_start(GTK_BOX(hbox), sel_btn,
225 2399 hiro
                                           FALSE, FALSE, 0);
226 2399 hiro
                        g_signal_connect(G_OBJECT(sel_btn), "clicked",
227 2399 hiro
                                         G_CALLBACK(sel_btn_clicked), NULL);
228 2399 hiro
                }
229 677 hiro
                if (dialog_type == INPUT_DIALOG_INVISIBLE)
230 677 hiro
                        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
231 677 hiro
        }
232 1 hiro
233 30 hiro
        gtkut_stock_button_set_create(&confirm_area,
234 30 hiro
                                      &ok_button, GTK_STOCK_OK,
235 30 hiro
                                      &cancel_button, GTK_STOCK_CANCEL,
236 30 hiro
                                      NULL, NULL);
237 1 hiro
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
238 1 hiro
                          confirm_area);
239 1 hiro
        gtk_widget_grab_default(ok_button);
240 1 hiro
241 1 hiro
        g_signal_connect(G_OBJECT(ok_button), "clicked",
242 1 hiro
                         G_CALLBACK(ok_clicked), NULL);
243 1 hiro
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
244 1 hiro
                         G_CALLBACK(cancel_clicked), NULL);
245 1 hiro
246 1 hiro
        gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
247 1 hiro
}
248 1 hiro
249 1 hiro
static gchar *input_dialog_open(const gchar *title, const gchar *message,
250 1 hiro
                                const gchar *default_string)
251 1 hiro
{
252 1 hiro
        gchar *str;
253 1 hiro
254 405 hiro
        gtkut_box_set_reverse_order(GTK_BOX(confirm_area),
255 405 hiro
                                    !prefs_common.comply_gnome_hig);
256 1 hiro
        input_dialog_set(title, message, default_string);
257 677 hiro
        gtk_widget_show(dialog);
258 1 hiro
259 1 hiro
        ack = fin = FALSE;
260 1 hiro
261 1 hiro
        inc_lock();
262 1 hiro
263 1 hiro
        while (fin == FALSE)
264 1 hiro
                gtk_main_iteration();
265 1 hiro
266 1 hiro
        manage_window_focus_out(dialog, NULL, NULL);
267 1 hiro
268 1 hiro
        if (ack) {
269 1 hiro
                GtkEditable *editable;
270 1 hiro
271 1 hiro
                if (type == INPUT_DIALOG_COMBO)
272 1 hiro
                        editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
273 1 hiro
                else
274 1 hiro
                        editable = GTK_EDITABLE(entry);
275 1 hiro
276 1 hiro
                str = gtk_editable_get_chars(editable, 0, -1);
277 1 hiro
                if (str && *str == '\0') {
278 1 hiro
                        g_free(str);
279 1 hiro
                        str = NULL;
280 1 hiro
                }
281 1 hiro
        } else
282 1 hiro
                str = NULL;
283 1 hiro
284 677 hiro
        gtk_widget_destroy(dialog);
285 677 hiro
        dialog = msg_label = entry = combo = confirm_area = ok_button = NULL;
286 677 hiro
287 1 hiro
        GTK_EVENTS_FLUSH();
288 1 hiro
289 1 hiro
        inc_unlock();
290 1 hiro
291 2040 hiro
        if (type != INPUT_DIALOG_INVISIBLE)
292 2040 hiro
                debug_print("return string = %s\n", str ? str : "(none)");
293 2040 hiro
294 1 hiro
        return str;
295 1 hiro
}
296 1 hiro
297 1 hiro
static void input_dialog_set(const gchar *title, const gchar *message,
298 1 hiro
                             const gchar *default_string)
299 1 hiro
{
300 1 hiro
        GtkWidget *entry_;
301 1 hiro
302 1 hiro
        if (type == INPUT_DIALOG_COMBO)
303 1 hiro
                entry_ = GTK_COMBO(combo)->entry;
304 1 hiro
        else
305 1 hiro
                entry_ = entry;
306 1 hiro
307 1 hiro
        gtk_window_set_title(GTK_WINDOW(dialog), title);
308 1 hiro
        gtk_label_set_text(GTK_LABEL(msg_label), message);
309 1 hiro
        if (default_string && *default_string) {
310 1 hiro
                gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
311 1 hiro
                gtk_entry_set_position(GTK_ENTRY(entry_), 0);
312 1 hiro
                gtk_entry_select_region(GTK_ENTRY(entry_), 0, -1);
313 1 hiro
        } else
314 1 hiro
                gtk_entry_set_text(GTK_ENTRY(entry_), "");
315 1 hiro
316 1 hiro
        gtk_widget_grab_focus(ok_button);
317 1 hiro
        gtk_widget_grab_focus(entry_);
318 1 hiro
}
319 1 hiro
320 1 hiro
static void ok_clicked(GtkWidget *widget, gpointer data)
321 1 hiro
{
322 1 hiro
        ack = TRUE;
323 1 hiro
        fin = TRUE;
324 1 hiro
}
325 1 hiro
326 1 hiro
static void cancel_clicked(GtkWidget *widget, gpointer data)
327 1 hiro
{
328 1 hiro
        ack = FALSE;
329 1 hiro
        fin = TRUE;
330 1 hiro
}
331 1 hiro
332 1 hiro
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
333 1 hiro
{
334 1 hiro
        ack = FALSE;
335 1 hiro
        fin = TRUE;
336 1 hiro
337 1 hiro
        return TRUE;
338 1 hiro
}
339 1 hiro
340 1 hiro
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
341 1 hiro
{
342 1 hiro
        if (event && event->keyval == GDK_Escape) {
343 1 hiro
                ack = FALSE;
344 1 hiro
                fin = TRUE;
345 1 hiro
        }
346 1 hiro
347 1 hiro
        return FALSE;
348 1 hiro
}
349 1 hiro
350 1 hiro
static void entry_activated(GtkEditable *editable)
351 1 hiro
{
352 1 hiro
        ack = TRUE;
353 1 hiro
        fin = TRUE;
354 1 hiro
}
355 1 hiro
356 1 hiro
static void combo_activated(GtkEditable *editable)
357 1 hiro
{
358 1 hiro
        ack = TRUE;
359 1 hiro
        fin = TRUE;
360 1 hiro
}
361 678 hiro
362 2399 hiro
static void sel_btn_clicked(GtkButton *button, gpointer data)
363 2399 hiro
{
364 2399 hiro
        gchar *file;
365 2399 hiro
        gchar *utf8_file;
366 2399 hiro
367 2400 hiro
        g_signal_handlers_block_by_func(dialog, focus_out, NULL);
368 2400 hiro
369 2399 hiro
        if (chooser_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
370 2399 hiro
                file = filesel_select_dir(NULL);
371 2399 hiro
        else
372 2399 hiro
                file = filesel_select_file(_("Select file"), NULL,
373 2399 hiro
                                           chooser_action);
374 2399 hiro
        if (file) {
375 2399 hiro
                utf8_file = conv_filename_to_utf8(file);
376 2399 hiro
                gtk_entry_set_text(GTK_ENTRY(entry), utf8_file);
377 2399 hiro
                g_free(utf8_file);
378 2399 hiro
        }
379 2400 hiro
380 2400 hiro
        g_signal_handlers_unblock_by_func(dialog, focus_out, NULL);
381 2399 hiro
}
382 2399 hiro
383 678 hiro
static gint focus_out(GtkWidget *widget, GdkEventFocus *event, gpointer data)
384 678 hiro
{
385 678 hiro
#ifdef G_OS_WIN32
386 678 hiro
        gtk_window_present(GTK_WINDOW(widget));
387 678 hiro
#endif
388 678 hiro
        return FALSE;
389 678 hiro
}