5a329c9688545173238e2fd547793516bc276145
[claws.git] / src / gtk / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #include <gtk/gtkimage.h>
41
42 #include "inputdialog.h"
43 #include "manage_window.h"
44 #include "gtkutils.h"
45 #include "utils.h"
46
47 #define INPUT_DIALOG_WIDTH      420
48
49 typedef enum
50 {
51         INPUT_DIALOG_NORMAL,
52         INPUT_DIALOG_INVISIBLE,
53         INPUT_DIALOG_COMBO
54 } InputDialogType;
55
56 static gboolean ack;
57 static gboolean fin;
58
59 static InputDialogType type;
60
61 static GtkWidget *dialog;
62 static GtkWidget *msg_title;
63 static GtkWidget *msg_label;
64 static GtkWidget *entry;
65 static GtkWidget *combo;
66 static GtkWidget *ok_button;
67
68 static void input_dialog_create (void);
69 static gchar *input_dialog_open (const gchar    *title,
70                                  const gchar    *message,
71                                  const gchar    *default_string);
72 static void input_dialog_set    (const gchar    *title,
73                                  const gchar    *message,
74                                  const gchar    *default_string);
75
76 static void ok_clicked          (GtkWidget      *widget,
77                                  gpointer        data);
78 static void cancel_clicked      (GtkWidget      *widget,
79                                  gpointer        data);
80 static gint delete_event        (GtkWidget      *widget,
81                                  GdkEventAny    *event,
82                                  gpointer        data);
83 static gboolean key_pressed     (GtkWidget      *widget,
84                                  GdkEventKey    *event,
85                                  gpointer        data);
86 static void entry_activated     (GtkEditable    *editable);
87 static void combo_activated     (GtkEditable    *editable);
88
89
90 gchar *input_dialog(const gchar *title, const gchar *message,
91                     const gchar *default_string)
92 {
93         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
94
95         if (!dialog)
96                 input_dialog_create();
97
98         type = INPUT_DIALOG_NORMAL;
99         gtk_widget_hide(combo);
100         gtk_widget_show(entry);
101         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
102
103         return input_dialog_open(title, message, default_string);
104 }
105
106 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
107                                    const gchar *default_string)
108 {
109         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
110
111         if (!dialog)
112                 input_dialog_create();
113
114         type = INPUT_DIALOG_INVISIBLE;
115         gtk_widget_hide(combo);
116         gtk_widget_show(entry);
117         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
118
119         return input_dialog_open(title, message, default_string);
120 }
121
122 gchar *input_dialog_combo(const gchar *title, const gchar *message,
123                           const gchar *default_string, GList *list,
124                           gboolean case_sensitive)
125 {
126         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
127
128         if (!dialog)
129                 input_dialog_create();
130
131         type = INPUT_DIALOG_COMBO;
132         gtk_widget_hide(entry);
133         gtk_widget_show(combo);
134
135         if (!list) {
136                 GList empty_list;
137
138                 empty_list.data = (gpointer)"";
139                 empty_list.next = NULL;
140                 empty_list.prev = NULL;
141                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
142         } else
143                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
144
145         gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
146
147         return input_dialog_open(title, message, default_string);
148 }
149
150 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
151 {
152         gchar *message;
153         gchar *pass;
154
155         message = g_strdup_printf(_("Input password for %s on %s:"),
156                                   user, server);
157         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
158         g_free(message);
159
160         return pass;
161 }
162
163 static void input_dialog_create(void)
164 {
165         static PangoFontDescription *font_desc;
166         GtkWidget *w_hbox;
167         GtkWidget *hbox;
168         GtkWidget *vbox;
169         GtkWidget *cancel_button;
170         GtkWidget *confirm_area;
171         GtkWidget *icon;
172
173         dialog = gtk_dialog_new();
174
175         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
176         gtk_window_set_title(GTK_WINDOW(dialog), "");
177         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
178
179         g_signal_connect(G_OBJECT(dialog), "delete_event",
180                          G_CALLBACK(delete_event), NULL);
181         g_signal_connect(G_OBJECT(dialog), "key_press_event",
182                          G_CALLBACK(key_pressed), NULL);
183         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
184
185         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
186         hbox = gtk_hbox_new (FALSE, 12);
187         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
188         gtk_widget_show (hbox);
189         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
190                             FALSE, FALSE, 0);
191
192         /* for title label */
193         w_hbox = gtk_hbox_new(FALSE, 0);
194         
195         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
196                                         GTK_ICON_SIZE_DIALOG); 
197         gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
198         gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
199         
200         vbox = gtk_vbox_new (FALSE, 12);
201         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
202         gtk_widget_show (vbox);
203         
204         msg_title = gtk_label_new("");
205         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
206         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
207         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
208         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
209         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
210         if (!font_desc) {
211                 gint size;
212
213                 size = pango_font_description_get_size
214                         (msg_title->style->font_desc);
215                 font_desc = pango_font_description_new();
216                 pango_font_description_set_weight
217                         (font_desc, PANGO_WEIGHT_BOLD);
218                 pango_font_description_set_size
219                         (font_desc, size * PANGO_SCALE_LARGE);
220         }
221         if (font_desc)
222                 gtk_widget_modify_font(msg_title, font_desc);
223         
224         msg_label = gtk_label_new("");
225         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
226         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
227         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
228         gtk_widget_show(msg_label);
229                 
230         entry = gtk_entry_new();
231         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
232         g_signal_connect(G_OBJECT(entry), "activate",
233                          G_CALLBACK(entry_activated), NULL);
234
235         combo = gtk_combo_new();
236         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
237         g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
238                          G_CALLBACK(combo_activated), NULL);
239
240         hbox = gtk_hbox_new(TRUE, 0);
241
242         gtkut_stock_button_set_create(&confirm_area,
243                                       &ok_button, GTK_STOCK_OK,
244                                       &cancel_button, GTK_STOCK_CANCEL,
245                                       NULL, NULL);
246
247         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
248                          confirm_area, FALSE, FALSE, 0);
249         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
250
251         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
252         
253         gtk_widget_grab_default(ok_button);
254
255         g_signal_connect(G_OBJECT(ok_button), "clicked",
256                          G_CALLBACK(ok_clicked), NULL);
257         g_signal_connect(G_OBJECT(cancel_button), "clicked",
258                          G_CALLBACK(cancel_clicked), NULL);
259 }
260
261 static gchar *input_dialog_open(const gchar *title, const gchar *message,
262                                 const gchar *default_string)
263 {
264         gchar *str;
265
266         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
267
268         if (!dialog)
269                 input_dialog_create();
270
271         input_dialog_set(title, message, default_string);
272         gtk_widget_show(dialog);
273         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
274         manage_window_set_transient(GTK_WINDOW(dialog));
275
276         ack = fin = FALSE;
277
278         while (fin == FALSE)
279                 gtk_main_iteration();
280
281         manage_window_focus_out(dialog, NULL, NULL);
282         gtk_widget_hide(dialog);
283
284         if (ack) {
285                 GtkEditable *editable;
286
287                 if (type == INPUT_DIALOG_COMBO)
288                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
289                 else
290                         editable = GTK_EDITABLE(entry);
291
292                 str = gtk_editable_get_chars(editable, 0, -1);
293                 if (str && *str == '\0') {
294                         g_free(str);
295                         str = NULL;
296                 }
297         } else
298                 str = NULL;
299
300         GTK_EVENTS_FLUSH();
301
302         debug_print("return string = %s\n", str ? str : "(none)");
303         return str;
304 }
305
306 static void input_dialog_set(const gchar *title, const gchar *message,
307                              const gchar *default_string)
308 {
309         GtkWidget *entry_;
310
311         if (type == INPUT_DIALOG_COMBO)
312                 entry_ = GTK_COMBO(combo)->entry;
313         else
314                 entry_ = entry;
315
316         gtk_window_set_title(GTK_WINDOW(dialog), title);
317         gtk_label_set_text(GTK_LABEL(msg_title), title);
318         gtk_label_set_text(GTK_LABEL(msg_label), message);
319         if (default_string && *default_string) {
320                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
321                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
322                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
323         } else
324                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
325
326         gtk_widget_grab_focus(ok_button);
327         gtk_widget_grab_focus(entry_);
328 }
329
330 static void ok_clicked(GtkWidget *widget, gpointer data)
331 {
332         ack = TRUE;
333         fin = TRUE;
334 }
335
336 static void cancel_clicked(GtkWidget *widget, gpointer data)
337 {
338         ack = FALSE;
339         fin = TRUE;
340 }
341
342 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
343 {
344         ack = FALSE;
345         fin = TRUE;
346
347         return TRUE;
348 }
349
350 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
351 {
352         if (event && event->keyval == GDK_Escape) {
353                 ack = FALSE;
354                 fin = TRUE;
355         } else if (event && event->keyval == GDK_Return) {
356                 ack = TRUE;
357                 fin = TRUE;
358                 return TRUE; /* do not let Return pass - it
359                               * pops up the combo on validating */
360         }
361
362         return FALSE;
363 }
364
365 static void entry_activated(GtkEditable *editable)
366 {
367         ack = TRUE;
368         fin = TRUE;
369 }
370
371 static void combo_activated(GtkEditable *editable)
372 {
373         ack = TRUE;
374         fin = TRUE;
375 }