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