2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2021 the Claws Mail team and Hiroyuki Yamamoto
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
29 #include "inputdialog.h"
30 #include "manage_window.h"
34 #include "prefs_common.h"
37 #define INPUT_DIALOG_WIDTH 420
42 INPUT_DIALOG_INVISIBLE
,
49 static InputDialogType type
;
51 static GtkWidget
*dialog
;
52 static GtkWidget
*msg_title
;
53 static GtkWidget
*msg_label
;
54 static GtkWidget
*entry
;
55 static GtkWidget
*combo
;
56 static GtkWidget
*remember_checkbtn
;
57 static GtkWidget
*ok_button
;
58 static GtkWidget
*icon_q
, *icon_p
;
59 static gboolean is_pass
= FALSE
;
60 static void input_dialog_create (gboolean is_password
);
61 static gchar
*input_dialog_open (const gchar
*title
,
63 const gchar
*checkbtn_label
,
64 const gchar
*default_string
,
65 gboolean default_checkbtn_state
,
67 static void input_dialog_set (const gchar
*title
,
69 const gchar
*default_string
);
71 static void ok_clicked (GtkWidget
*widget
,
73 static void cancel_clicked (GtkWidget
*widget
,
75 static gint
delete_event (GtkWidget
*widget
,
78 static gboolean
key_pressed (GtkWidget
*widget
,
81 static void entry_activated (GtkEditable
*editable
);
82 static void combo_activated (GtkEditable
*editable
);
85 gchar
*input_dialog(const gchar
*title
, const gchar
*message
,
86 const gchar
*default_string
)
88 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
91 input_dialog_create(FALSE
);
93 type
= INPUT_DIALOG_NORMAL
;
94 gtk_widget_hide(combo
);
95 gtk_widget_show(entry
);
97 gtk_widget_hide(remember_checkbtn
);
99 gtk_widget_show(icon_q
);
100 gtk_widget_hide(icon_p
);
102 gtk_entry_set_visibility(GTK_ENTRY(entry
), TRUE
);
104 return input_dialog_open(title
, message
, NULL
, default_string
, FALSE
, NULL
);
107 gchar
*input_dialog_with_invisible(const gchar
*title
, const gchar
*message
,
108 const gchar
*default_string
)
110 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
113 input_dialog_create(TRUE
);
115 type
= INPUT_DIALOG_INVISIBLE
;
116 gtk_widget_hide(combo
);
117 gtk_widget_show(entry
);
118 gtk_widget_hide(remember_checkbtn
);
120 gtk_widget_hide(icon_q
);
121 gtk_widget_show(icon_p
);
123 gtk_entry_set_visibility(GTK_ENTRY(entry
), FALSE
);
125 return input_dialog_open(title
, message
, NULL
, default_string
, FALSE
, NULL
);
128 gchar
*input_dialog_with_invisible_checkbtn(const gchar
*title
, const gchar
*message
,
129 const gchar
*default_string
, const gchar
*checkbtn_label
,
130 gboolean
*checkbtn_state
)
132 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
135 input_dialog_create(TRUE
);
137 type
= INPUT_DIALOG_INVISIBLE
;
138 gtk_widget_hide(combo
);
139 gtk_widget_show(entry
);
141 if (checkbtn_label
&& checkbtn_state
) {
142 gtk_widget_show(remember_checkbtn
);
143 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn
), *checkbtn_state
);
146 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn
), FALSE
);
147 gtk_widget_hide(remember_checkbtn
);
150 gtk_widget_hide(icon_q
);
151 gtk_widget_show(icon_p
);
153 gtk_entry_set_visibility(GTK_ENTRY(entry
), FALSE
);
155 return input_dialog_open(title
, message
, checkbtn_label
, default_string
, (checkbtn_state
? *checkbtn_state
:FALSE
), checkbtn_state
);
158 gchar
*input_dialog_combo(const gchar
*title
, const gchar
*message
,
159 const gchar
*default_string
, GList
*list
)
161 return input_dialog_combo_remember(title
, message
,
162 default_string
, list
, FALSE
);
165 gchar
*input_dialog_combo_remember(const gchar
*title
, const gchar
*message
,
166 const gchar
*default_string
, GList
*list
,
169 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
172 input_dialog_create(FALSE
);
174 type
= INPUT_DIALOG_COMBO
;
175 gtk_widget_hide(entry
);
176 gtk_widget_show(combo
);
178 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn
), FALSE
);
180 gtk_widget_show(remember_checkbtn
);
182 gtk_widget_hide(remember_checkbtn
);
184 gtk_widget_show(icon_q
);
185 gtk_widget_hide(icon_p
);
187 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(combo
));
188 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(combo
), list
);
189 return input_dialog_open(title
, message
, NULL
, default_string
, FALSE
, remember
);
192 gchar
*input_dialog_with_checkbtn(const gchar
*title
,
193 const gchar
*message
,
194 const gchar
*default_string
,
195 const gchar
*checkbtn_label
,
196 gboolean
*checkbtn_state
)
198 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
201 input_dialog_create(FALSE
);
203 type
= INPUT_DIALOG_NORMAL
;
204 gtk_widget_hide(combo
);
205 gtk_widget_show(entry
);
207 if (checkbtn_label
&& checkbtn_state
)
208 gtk_widget_show(remember_checkbtn
);
210 gtk_widget_hide(remember_checkbtn
);
212 gtk_widget_show(icon_q
);
213 gtk_widget_hide(icon_p
);
215 gtk_entry_set_visibility(GTK_ENTRY(entry
), TRUE
);
217 return input_dialog_open(title
, message
, checkbtn_label
, default_string
,
218 prefs_common
.inherit_folder_props
, checkbtn_state
);
221 gchar
*input_dialog_query_password(const gchar
*server
, const gchar
*user
)
227 message
= g_strdup_printf(_("Input password for %s on %s:"),
230 message
= g_strdup_printf(_("Input password for %s:"),
233 message
= g_strdup_printf(_("Input password for %s:"),
236 message
= g_strdup_printf(_("Input password:"));
237 pass
= input_dialog_with_invisible(_("Input password"), message
, NULL
);
243 gchar
*input_dialog_query_password_keep(const gchar
*server
, const gchar
*user
, gchar
**keep
)
249 message
= g_strdup_printf(_("Input password for %s on %s:"),
252 message
= g_strdup_printf(_("Input password for %s:"),
255 message
= g_strdup_printf(_("Input password for %s:"),
258 message
= g_strdup_printf(_("Input password:"));
261 pass
= g_strdup (*keep
);
264 gboolean state
= prefs_common
.session_passwords
;
265 pass
= input_dialog_with_invisible_checkbtn(_("Input password"),
267 _("Remember password for this session"),
270 *keep
= g_strdup (pass
);
271 debug_print("keeping session password for account\n");
273 prefs_common
.session_passwords
= state
;
277 pass
= input_dialog_with_invisible(_("Input password"), message
, NULL
);
284 static void input_dialog_create(gboolean is_password
)
286 static PangoFontDescription
*font_desc
;
289 GtkWidget
*cancel_button
;
291 dialog
= gtk_dialog_new();
293 gtk_window_set_resizable(GTK_WINDOW(dialog
), FALSE
);
294 gtk_window_set_default_size(GTK_WINDOW(dialog
), 375, 100);
295 gtk_window_set_title(GTK_WINDOW(dialog
), "");
297 g_signal_connect(G_OBJECT(dialog
), "delete_event",
298 G_CALLBACK(delete_event
), NULL
);
299 g_signal_connect(G_OBJECT(dialog
), "key_press_event",
300 G_CALLBACK(key_pressed
), NULL
);
301 MANAGE_WINDOW_SIGNALS_CONNECT(dialog
);
303 vbox
= gtk_dialog_get_content_area (GTK_DIALOG (dialog
));
304 gtk_box_set_spacing (GTK_BOX (vbox
), 14);
305 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 12);
306 gtk_container_set_border_width (GTK_CONTAINER (hbox
), 5);
307 gtk_widget_show (hbox
);
308 gtk_box_pack_start (GTK_BOX (vbox
), hbox
,
311 /* for title label */
312 icon_q
= gtk_image_new_from_icon_name("dialog-question",
313 GTK_ICON_SIZE_DIALOG
);
314 gtk_widget_set_halign(icon_q
, GTK_ALIGN_CENTER
);
315 gtk_widget_set_valign(icon_q
, GTK_ALIGN_START
);
316 gtk_box_pack_start (GTK_BOX (hbox
), icon_q
, FALSE
, FALSE
, 0);
317 icon_p
= gtk_image_new_from_icon_name("dialog-password",
318 GTK_ICON_SIZE_DIALOG
);
319 gtk_widget_set_halign(icon_p
, GTK_ALIGN_CENTER
);
320 gtk_widget_set_valign(icon_p
, GTK_ALIGN_START
);
321 gtk_box_pack_start (GTK_BOX (hbox
), icon_p
, FALSE
, FALSE
, 0);
323 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 12);
324 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, TRUE
, TRUE
, 16);
325 gtk_widget_show (vbox
);
327 msg_title
= gtk_label_new("");
328 gtk_label_set_xalign(GTK_LABEL(msg_title
), 0.0);
329 gtk_label_set_justify(GTK_LABEL(msg_title
), GTK_JUSTIFY_LEFT
);
330 gtk_label_set_use_markup (GTK_LABEL (msg_title
), TRUE
);
331 gtk_box_pack_start(GTK_BOX(vbox
), msg_title
, FALSE
, FALSE
, 0);
332 gtk_label_set_line_wrap(GTK_LABEL(msg_title
), TRUE
);
336 size
= pango_font_description_get_size
337 (gtk_widget_get_style(msg_title
)->font_desc
);
338 font_desc
= pango_font_description_new();
339 pango_font_description_set_weight
340 (font_desc
, PANGO_WEIGHT_BOLD
);
341 pango_font_description_set_size
342 (font_desc
, size
* PANGO_SCALE_LARGE
);
345 gtk_widget_override_font(msg_title
, font_desc
);
347 msg_label
= gtk_label_new("");
348 gtk_label_set_xalign(GTK_LABEL(msg_label
), 0.0);
349 gtk_label_set_justify(GTK_LABEL(msg_label
), GTK_JUSTIFY_LEFT
);
350 gtk_box_pack_start(GTK_BOX(vbox
), msg_label
, FALSE
, FALSE
, 0);
351 gtk_widget_show(msg_label
);
353 entry
= gtk_entry_new();
354 gtk_box_pack_start(GTK_BOX(vbox
), entry
, FALSE
, FALSE
, 0);
355 g_signal_connect(G_OBJECT(entry
), "activate",
356 G_CALLBACK(entry_activated
), NULL
);
358 combo
= gtk_combo_box_text_new_with_entry();
359 gtk_box_pack_start(GTK_BOX(vbox
), combo
, FALSE
, FALSE
, 0);
360 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((combo
)))), "activate",
361 G_CALLBACK(combo_activated
), NULL
);
363 remember_checkbtn
= gtk_check_button_new_with_label(_("Remember this"));
364 gtk_box_pack_start(GTK_BOX(vbox
), remember_checkbtn
, FALSE
, FALSE
, 0);
366 cancel_button
= gtk_dialog_add_button(GTK_DIALOG(dialog
), _("_Cancel"),
368 ok_button
= gtk_dialog_add_button(GTK_DIALOG(dialog
),_("_OK"),
371 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog
)));
373 gtk_widget_hide(remember_checkbtn
);
376 gtk_widget_hide(icon_q
);
378 gtk_widget_hide(icon_p
);
380 is_pass
= is_password
;
382 gtk_widget_grab_default(ok_button
);
384 g_signal_connect(G_OBJECT(ok_button
), "clicked",
385 G_CALLBACK(ok_clicked
), NULL
);
386 g_signal_connect(G_OBJECT(cancel_button
), "clicked",
387 G_CALLBACK(cancel_clicked
), NULL
);
390 static gchar
*input_dialog_open(const gchar
*title
, const gchar
*message
,
391 const gchar
*checkbtn_label
,
392 const gchar
*default_string
,
393 gboolean default_checkbtn_state
,
398 if (dialog
&& gtk_widget_get_visible(dialog
)) return NULL
;
401 input_dialog_create(FALSE
);
404 gtk_button_set_label(GTK_BUTTON(remember_checkbtn
), checkbtn_label
);
406 gtk_button_set_label(GTK_BUTTON(remember_checkbtn
), _("Remember this"));
408 input_dialog_set(title
, message
, default_string
);
409 gtk_window_present(GTK_WINDOW(dialog
));
411 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn
),
412 default_checkbtn_state
);
414 gtk_widget_show(remember_checkbtn
);
416 gtk_widget_hide(remember_checkbtn
);
418 manage_window_set_transient(GTK_WINDOW(dialog
));
419 gtk_window_set_modal(GTK_WINDOW(dialog
), TRUE
);
424 gtk_main_iteration();
426 manage_window_focus_out(dialog
, NULL
, NULL
);
429 GtkEditable
*editable
;
431 if (type
== INPUT_DIALOG_COMBO
)
432 editable
= GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo
))));
434 editable
= GTK_EDITABLE(entry
);
436 str
= gtk_editable_get_chars(editable
, 0, -1);
437 if (str
&& *str
== '\0' && !is_pass
) {
447 *remember
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn
));
450 gtk_widget_destroy(dialog
);
454 debug_print("return string = %s\n", str
? "********": ("none"));
456 debug_print("return string = %s\n", str
? str
: "(none)");
460 static void input_dialog_set(const gchar
*title
, const gchar
*message
,
461 const gchar
*default_string
)
465 if (type
== INPUT_DIALOG_COMBO
)
466 entry_
= gtk_bin_get_child(GTK_BIN((combo
)));
470 gtk_window_set_title(GTK_WINDOW(dialog
), title
);
471 gtk_label_set_text(GTK_LABEL(msg_title
), title
);
472 gtk_label_set_text(GTK_LABEL(msg_label
), message
);
473 if (default_string
&& *default_string
) {
474 gtk_entry_set_text(GTK_ENTRY(entry_
), default_string
);
475 gtk_editable_set_position(GTK_EDITABLE(entry_
), 0);
476 gtk_editable_select_region(GTK_EDITABLE(entry_
), 0, -1);
478 gtk_entry_set_text(GTK_ENTRY(entry_
), "");
480 gtk_widget_grab_focus(ok_button
);
481 gtk_widget_grab_focus(entry_
);
484 static void ok_clicked(GtkWidget
*widget
, gpointer data
)
490 static void cancel_clicked(GtkWidget
*widget
, gpointer data
)
496 static gint
delete_event(GtkWidget
*widget
, GdkEventAny
*event
, gpointer data
)
504 static gboolean
key_pressed(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
506 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
509 } else if (event
&& (event
->keyval
== GDK_KEY_KP_Enter
||
510 event
->keyval
== GDK_KEY_Return
)) {
513 return TRUE
; /* do not let Return pass - it
514 * pops up the combo on validating */
520 static void entry_activated(GtkEditable
*editable
)
526 static void combo_activated(GtkEditable
*editable
)