2005-11-18 [cleroy] 1.9.100cvs17
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 static GtkWidget *icon_q, *icon_p;
68
69 static void input_dialog_create (gboolean is_password);
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(FALSE);
98
99         type = INPUT_DIALOG_NORMAL;
100         gtk_widget_hide(combo);
101         gtk_widget_show(entry);
102
103         gtk_widget_show(icon_q);
104         gtk_widget_hide(icon_p);
105         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
106
107         return input_dialog_open(title, message, default_string);
108 }
109
110 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
111                                    const gchar *default_string)
112 {
113         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
114
115         if (!dialog)
116                 input_dialog_create(TRUE);
117
118         type = INPUT_DIALOG_INVISIBLE;
119         gtk_widget_hide(combo);
120         gtk_widget_show(entry);
121
122         gtk_widget_hide(icon_q);
123         gtk_widget_show(icon_p);
124         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
125
126         return input_dialog_open(title, message, default_string);
127 }
128
129 gchar *input_dialog_combo(const gchar *title, const gchar *message,
130                           const gchar *default_string, GList *list,
131                           gboolean case_sensitive)
132 {
133         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
134
135         if (!dialog)
136                 input_dialog_create(FALSE);
137
138         type = INPUT_DIALOG_COMBO;
139         gtk_widget_hide(entry);
140         gtk_widget_show(combo);
141
142         gtk_widget_show(icon_q);
143         gtk_widget_hide(icon_p);
144
145         if (!list) {
146                 GList empty_list;
147
148                 empty_list.data = (gpointer)"";
149                 empty_list.next = NULL;
150                 empty_list.prev = NULL;
151                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
152         } else
153                 gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
154
155         gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
156
157         return input_dialog_open(title, message, default_string);
158 }
159
160 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
161 {
162         gchar *message;
163         gchar *pass;
164
165         message = g_strdup_printf(_("Input password for %s on %s:"),
166                                   user, server);
167         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
168         g_free(message);
169
170         return pass;
171 }
172
173 static void input_dialog_create(gboolean is_password)
174 {
175         static PangoFontDescription *font_desc;
176         GtkWidget *w_hbox;
177         GtkWidget *hbox;
178         GtkWidget *vbox;
179         GtkWidget *cancel_button;
180         GtkWidget *confirm_area;
181
182         dialog = gtk_dialog_new();
183
184         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
185         gtk_window_set_title(GTK_WINDOW(dialog), "");
186         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
187
188         g_signal_connect(G_OBJECT(dialog), "delete_event",
189                          G_CALLBACK(delete_event), NULL);
190         g_signal_connect(G_OBJECT(dialog), "key_press_event",
191                          G_CALLBACK(key_pressed), NULL);
192         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
193
194         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
195         hbox = gtk_hbox_new (FALSE, 12);
196         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
197         gtk_widget_show (hbox);
198         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
199                             FALSE, FALSE, 0);
200
201         /* for title label */
202         w_hbox = gtk_hbox_new(FALSE, 0);
203         
204         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
205                                 GTK_ICON_SIZE_DIALOG); 
206         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
207         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
208         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
209                                 GTK_ICON_SIZE_DIALOG); 
210         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
211         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
212         
213         vbox = gtk_vbox_new (FALSE, 12);
214         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
215         gtk_widget_show (vbox);
216         
217         msg_title = gtk_label_new("");
218         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
219         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
220         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
221         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
222         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
223         if (!font_desc) {
224                 gint size;
225
226                 size = pango_font_description_get_size
227                         (msg_title->style->font_desc);
228                 font_desc = pango_font_description_new();
229                 pango_font_description_set_weight
230                         (font_desc, PANGO_WEIGHT_BOLD);
231                 pango_font_description_set_size
232                         (font_desc, size * PANGO_SCALE_LARGE);
233         }
234         if (font_desc)
235                 gtk_widget_modify_font(msg_title, font_desc);
236         
237         msg_label = gtk_label_new("");
238         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
239         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
240         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
241         gtk_widget_show(msg_label);
242                 
243         entry = gtk_entry_new();
244         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
245         g_signal_connect(G_OBJECT(entry), "activate",
246                          G_CALLBACK(entry_activated), NULL);
247
248         combo = gtk_combo_new();
249         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
250         g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
251                          G_CALLBACK(combo_activated), NULL);
252
253         hbox = gtk_hbox_new(TRUE, 0);
254
255         gtkut_stock_button_set_create(&confirm_area,
256                                       &ok_button, GTK_STOCK_OK,
257                                       &cancel_button, GTK_STOCK_CANCEL,
258                                       NULL, NULL);
259
260         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
261                          confirm_area, FALSE, FALSE, 0);
262         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
263
264         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
265         
266         if (is_password)
267                 gtk_widget_hide(icon_q);
268         else
269                 gtk_widget_hide(icon_p);
270
271         gtk_widget_grab_default(ok_button);
272
273         g_signal_connect(G_OBJECT(ok_button), "clicked",
274                          G_CALLBACK(ok_clicked), NULL);
275         g_signal_connect(G_OBJECT(cancel_button), "clicked",
276                          G_CALLBACK(cancel_clicked), NULL);
277 }
278
279 static gchar *input_dialog_open(const gchar *title, const gchar *message,
280                                 const gchar *default_string)
281 {
282         gchar *str;
283
284         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
285
286         if (!dialog)
287                 input_dialog_create(FALSE);
288
289         input_dialog_set(title, message, default_string);
290         gtk_widget_show(dialog);
291
292         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
293         manage_window_set_transient(GTK_WINDOW(dialog));
294
295         ack = fin = FALSE;
296
297         while (fin == FALSE)
298                 gtk_main_iteration();
299
300         manage_window_focus_out(dialog, NULL, NULL);
301         gtk_widget_hide(dialog);
302
303         if (ack) {
304                 GtkEditable *editable;
305
306                 if (type == INPUT_DIALOG_COMBO)
307                         editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
308                 else
309                         editable = GTK_EDITABLE(entry);
310
311                 str = gtk_editable_get_chars(editable, 0, -1);
312                 if (str && *str == '\0') {
313                         g_free(str);
314                         str = NULL;
315                 }
316         } else
317                 str = NULL;
318
319         GTK_EVENTS_FLUSH();
320
321         debug_print("return string = %s\n", str ? str : "(none)");
322         return str;
323 }
324
325 static void input_dialog_set(const gchar *title, const gchar *message,
326                              const gchar *default_string)
327 {
328         GtkWidget *entry_;
329
330         if (type == INPUT_DIALOG_COMBO)
331                 entry_ = GTK_COMBO(combo)->entry;
332         else
333                 entry_ = entry;
334
335         gtk_window_set_title(GTK_WINDOW(dialog), title);
336         gtk_label_set_text(GTK_LABEL(msg_title), title);
337         gtk_label_set_text(GTK_LABEL(msg_label), message);
338         if (default_string && *default_string) {
339                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
340                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
341                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
342         } else
343                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
344
345         gtk_widget_grab_focus(ok_button);
346         gtk_widget_grab_focus(entry_);
347 }
348
349 static void ok_clicked(GtkWidget *widget, gpointer data)
350 {
351         ack = TRUE;
352         fin = TRUE;
353 }
354
355 static void cancel_clicked(GtkWidget *widget, gpointer data)
356 {
357         ack = FALSE;
358         fin = TRUE;
359 }
360
361 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
362 {
363         ack = FALSE;
364         fin = TRUE;
365
366         return TRUE;
367 }
368
369 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
370 {
371         if (event && event->keyval == GDK_Escape) {
372                 ack = FALSE;
373                 fin = TRUE;
374         } else if (event && event->keyval == GDK_Return) {
375                 ack = TRUE;
376                 fin = TRUE;
377                 return TRUE; /* do not let Return pass - it
378                               * pops up the combo on validating */
379         }
380
381         return FALSE;
382 }
383
384 static void entry_activated(GtkEditable *editable)
385 {
386         ack = TRUE;
387         fin = TRUE;
388 }
389
390 static void combo_activated(GtkEditable *editable)
391 {
392         ack = TRUE;
393         fin = TRUE;
394 }