Improve some sieve-related translations
[claws.git] / src / gtk / inputdialog.c
blob83f28c4f8ebb77c5faa5b1426532e9f35d0c8b27
1 /*
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gtk/gtk.h>
29 #include "inputdialog.h"
30 #include "manage_window.h"
31 #include "gtkutils.h"
32 #include "utils.h"
33 #include "combobox.h"
34 #include "prefs_common.h"
37 #define INPUT_DIALOG_WIDTH 420
39 typedef enum
41 INPUT_DIALOG_NORMAL,
42 INPUT_DIALOG_INVISIBLE,
43 INPUT_DIALOG_COMBO
44 } InputDialogType;
46 static gboolean ack;
47 static gboolean fin;
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,
62 const gchar *message,
63 const gchar *checkbtn_label,
64 const gchar *default_string,
65 gboolean default_checkbtn_state,
66 gboolean *remember);
67 static void input_dialog_set (const gchar *title,
68 const gchar *message,
69 const gchar *default_string);
71 static void ok_clicked (GtkWidget *widget,
72 gpointer data);
73 static void cancel_clicked (GtkWidget *widget,
74 gpointer data);
75 static gint delete_event (GtkWidget *widget,
76 GdkEventAny *event,
77 gpointer data);
78 static gboolean key_pressed (GtkWidget *widget,
79 GdkEventKey *event,
80 gpointer data);
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;
90 if (!dialog)
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);
101 is_pass = FALSE;
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;
112 if (!dialog)
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);
122 is_pass = TRUE;
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;
134 if (!dialog)
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);
145 else {
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);
152 is_pass = TRUE;
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,
167 gboolean *remember)
169 if (dialog && gtk_widget_get_visible(dialog)) return NULL;
171 if (!dialog)
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);
179 if (remember)
180 gtk_widget_show(remember_checkbtn);
181 else
182 gtk_widget_hide(remember_checkbtn);
184 gtk_widget_show(icon_q);
185 gtk_widget_hide(icon_p);
186 is_pass = FALSE;
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;
200 if (!dialog)
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);
209 else
210 gtk_widget_hide(remember_checkbtn);
212 gtk_widget_show(icon_q);
213 gtk_widget_hide(icon_p);
214 is_pass = FALSE;
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)
223 gchar *message;
224 gchar *pass;
226 if (server && user)
227 message = g_strdup_printf(_("Input password for %s on %s:"),
228 user, server);
229 else if (server)
230 message = g_strdup_printf(_("Input password for %s:"),
231 server);
232 else if (user)
233 message = g_strdup_printf(_("Input password for %s:"),
234 user);
235 else
236 message = g_strdup_printf(_("Input password:"));
237 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
238 g_free(message);
240 return pass;
243 gchar *input_dialog_query_password_keep(const gchar *server, const gchar *user, gchar **keep)
245 gchar *message;
246 gchar *pass;
248 if (server && user)
249 message = g_strdup_printf(_("Input password for %s on %s:"),
250 user, server);
251 else if (server)
252 message = g_strdup_printf(_("Input password for %s:"),
253 server);
254 else if (user)
255 message = g_strdup_printf(_("Input password for %s:"),
256 user);
257 else
258 message = g_strdup_printf(_("Input password:"));
259 if (keep) {
260 if (*keep != NULL) {
261 pass = g_strdup (*keep);
263 else {
264 gboolean state = prefs_common.session_passwords;
265 pass = input_dialog_with_invisible_checkbtn(_("Input password"),
266 message, NULL,
267 _("Remember password for this session"),
268 &state);
269 if (state) {
270 *keep = g_strdup (pass);
271 debug_print("keeping session password for account\n");
273 prefs_common.session_passwords = state;
276 else {
277 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
279 g_free(message);
281 return pass;
284 static void input_dialog_create(gboolean is_password)
286 static PangoFontDescription *font_desc;
287 GtkWidget *hbox;
288 GtkWidget *vbox;
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,
309 FALSE, FALSE, 0);
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);
333 if (!font_desc) {
334 gint size;
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);
344 if (font_desc)
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"),
367 GTK_RESPONSE_NONE);
368 ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_OK"),
369 GTK_RESPONSE_NONE);
371 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
373 gtk_widget_hide(remember_checkbtn);
375 if (is_password)
376 gtk_widget_hide(icon_q);
377 else
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,
394 gboolean *remember)
396 gchar *str;
398 if (dialog && gtk_widget_get_visible(dialog)) return NULL;
400 if (!dialog)
401 input_dialog_create(FALSE);
403 if (checkbtn_label)
404 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), checkbtn_label);
405 else
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);
413 if (remember)
414 gtk_widget_show(remember_checkbtn);
415 else
416 gtk_widget_hide(remember_checkbtn);
418 manage_window_set_transient(GTK_WINDOW(dialog));
419 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
421 ack = fin = FALSE;
423 while (fin == FALSE)
424 gtk_main_iteration();
426 manage_window_focus_out(dialog, NULL, NULL);
428 if (ack) {
429 GtkEditable *editable;
431 if (type == INPUT_DIALOG_COMBO)
432 editable = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo))));
433 else
434 editable = GTK_EDITABLE(entry);
436 str = gtk_editable_get_chars(editable, 0, -1);
437 if (str && *str == '\0' && !is_pass) {
438 g_free(str);
439 str = NULL;
441 } else
442 str = NULL;
444 GTK_EVENTS_FLUSH();
446 if (remember) {
447 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
450 gtk_widget_destroy(dialog);
451 dialog = NULL;
453 if (is_pass)
454 debug_print("return string = %s\n", str ? "********": ("none"));
455 else
456 debug_print("return string = %s\n", str ? str : "(none)");
457 return str;
460 static void input_dialog_set(const gchar *title, const gchar *message,
461 const gchar *default_string)
463 GtkWidget *entry_;
465 if (type == INPUT_DIALOG_COMBO)
466 entry_ = gtk_bin_get_child(GTK_BIN((combo)));
467 else
468 entry_ = entry;
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);
477 } else
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)
486 ack = TRUE;
487 fin = TRUE;
490 static void cancel_clicked(GtkWidget *widget, gpointer data)
492 ack = FALSE;
493 fin = TRUE;
496 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
498 ack = FALSE;
499 fin = TRUE;
501 return TRUE;
504 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
506 if (event && event->keyval == GDK_KEY_Escape) {
507 ack = FALSE;
508 fin = TRUE;
509 } else if (event && (event->keyval == GDK_KEY_KP_Enter ||
510 event->keyval == GDK_KEY_Return)) {
511 ack = TRUE;
512 fin = TRUE;
513 return TRUE; /* do not let Return pass - it
514 * pops up the combo on validating */
517 return FALSE;
520 static void entry_activated(GtkEditable *editable)
522 ack = TRUE;
523 fin = TRUE;
526 static void combo_activated(GtkEditable *editable)
528 ack = TRUE;
529 fin = TRUE;