2ffb28ae69e6e44bb56de28f0a5e6a0db69135ef
[claws.git] / src / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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 <gdk/gdkkeysyms.h>
26 #include <gtk/gtkmain.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkdialog.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkbutton.h>
36 #include <gtk/gtkhbbox.h>
37
38 #include "intl.h"
39 #include "inputdialog.h"
40 #include "manage_window.h"
41 #include "gtkutils.h"
42 #include "utils.h"
43
44 #define INPUT_DIALOG_WIDTH      420
45
46 static gboolean ack;
47
48 static GtkWidget *dialog;
49 static GtkWidget *msg_label;
50 static GtkWidget *entry;
51 static GtkWidget *ok_button;
52
53 static void input_dialog_create (void);
54 static void input_dialog_set    (const gchar    *title,
55                                  const gchar    *message,
56                                  const gchar    *default_string);
57
58 static void ok_clicked          (GtkWidget      *widget,
59                                  gpointer        data);
60 static void cancel_clicked      (GtkWidget      *widget,
61                                  gpointer        data);
62 static gint delete_event        (GtkWidget      *widget,
63                                  GdkEventAny    *event,
64                                  gpointer        data);
65 static void key_pressed         (GtkWidget      *widget,
66                                  GdkEventKey    *event,
67                                  gpointer        data);
68 static void entry_activated     (GtkEditable    *editable);
69
70 gchar *input_dialog(const gchar *title, const gchar *message,
71                     const gchar *default_string)
72 {
73         gchar *str;
74
75         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
76
77         if (!dialog)
78                 input_dialog_create();
79
80         input_dialog_set(title, message, default_string);
81         gtk_widget_show(dialog);
82         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
83         manage_window_set_transient(GTK_WINDOW(dialog));
84
85         gtk_main();
86
87         manage_window_focus_out(dialog, NULL, NULL);
88         gtk_widget_hide(dialog);
89         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
90
91         if (ack) {
92                 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
93                 if (str && *str == '\0') {
94                         g_free(str);
95                         str = NULL;
96                 }
97         } else
98                 str = NULL;
99
100         GTK_EVENTS_FLUSH();
101
102         debug_print("return string = %s\n", str ? str : "(none)");
103         return str;
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         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
115
116         return input_dialog(title, message, default_string);
117 }
118
119 static void input_dialog_create(void)
120 {
121         GtkWidget *vbox;
122         GtkWidget *hbox;
123         GtkWidget *confirm_area;
124         GtkWidget *cancel_button;
125
126         dialog = gtk_dialog_new();
127         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
128         gtk_widget_set_usize(dialog, INPUT_DIALOG_WIDTH, -1);
129         gtk_container_set_border_width
130                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
131         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
132         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
133                            GTK_SIGNAL_FUNC(delete_event), NULL);
134         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
135                            GTK_SIGNAL_FUNC(key_pressed), NULL);
136         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
137                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
138         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
139                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
140
141         gtk_widget_realize(dialog);
142
143         vbox = gtk_vbox_new(FALSE, 8);
144         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
145         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
146
147         hbox = gtk_hbox_new(FALSE, 0);
148         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
149
150         msg_label = gtk_label_new("");
151         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
152         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
153
154         entry = gtk_entry_new();
155         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
156         gtk_signal_connect(GTK_OBJECT(entry), "activate",
157                            GTK_SIGNAL_FUNC(entry_activated), NULL);
158
159         gtkut_button_set_create(&confirm_area,
160                                 &ok_button,     _("OK"),
161                                 &cancel_button, _("Cancel"),
162                                 NULL, NULL);
163         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
164                           confirm_area);
165         gtk_widget_grab_default(ok_button);
166
167         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
168                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
169         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
170                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
171
172
173         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
174 }
175
176 static void input_dialog_set(const gchar *title, const gchar *message,
177                              const gchar *default_string)
178 {
179         gtk_window_set_title(GTK_WINDOW(dialog), title);
180         gtk_label_set_text(GTK_LABEL(msg_label), message);
181         if (default_string && *default_string)
182                 gtk_entry_set_text(GTK_ENTRY(entry), default_string);
183         else
184                 gtk_entry_set_text(GTK_ENTRY(entry), "");
185         gtk_entry_set_position(GTK_ENTRY(entry), 0);
186         gtk_entry_select_region(GTK_ENTRY(entry), 0, -1);
187
188         gtk_widget_grab_focus(ok_button);
189         gtk_widget_grab_focus(entry);
190 }
191
192 static void ok_clicked(GtkWidget *widget, gpointer data)
193 {
194         ack = TRUE;
195         gtk_main_quit();
196 }
197
198 static void cancel_clicked(GtkWidget *widget, gpointer data)
199 {
200         ack = FALSE;
201         gtk_main_quit();
202 }
203
204 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
205 {
206         ack = FALSE;
207         gtk_main_quit();
208
209         return TRUE;
210 }
211
212 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
213 {
214         if (event && event->keyval == GDK_Escape) {
215                 ack = FALSE;
216                 gtk_main_quit();
217         }
218 }
219
220 static void entry_activated(GtkEditable *editable)
221 {
222         ack = TRUE;
223         gtk_main_quit();
224 }