Updated italian translation.
[midnight-commander.git] / src / editor / editcmd_dialogs.c
blobd30ea94967704636f3e15c0147010e919bd8bf45
1 /*
2 Editor dialogs for high level editing commands
4 Copyright (C) 2009 The Free Software Foundation, Inc.
6 Written by:
7 Slava Zanko <slavazanko@gmail.com>, 2009.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software; you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 MA 02110-1301, USA.
27 #include <config.h>
29 #include "lib/global.h"
30 #include "lib/tty/tty.h"
31 #include "lib/skin.h" /* INPUT_COLOR */
32 #include "lib/tty/key.h"
33 #include "lib/search.h"
34 #include "lib/strutil.h"
36 #include "src/dialog.h"
37 #include "src/widget.h"
38 #include "src/wtools.h"
39 #include "src/main.h"
40 #include "src/history.h"
41 #include "src/charsets.h"
43 #include "src/editor/edit-widget.h"
44 #include "src/editor/etags.h"
45 #include "src/editor/editcmd_dialogs.h"
48 /*** global variables **************************************************/
50 /*** file scope macro definitions **************************************/
52 #define SEARCH_DLG_WIDTH 58
53 #define SEARCH_DLG_MIN_HEIGHT 13
54 #define SEARCH_DLG_HEIGHT_SUPPLY 3
56 #define REPLACE_DLG_WIDTH 58
57 #define REPLACE_DLG_MIN_HEIGHT 17
58 #define REPLACE_DLG_HEIGHT_SUPPLY 5
60 /*** file scope type declarations **************************************/
62 /*** file scope variables **********************************************/
65 /*** file scope functions **********************************************/
67 static cb_ret_t
68 editcmd_dialog_raw_key_query_cb (struct Dlg_head *h, Widget *sender,
69 dlg_msg_t msg, int parm, void *data)
71 switch (msg) {
72 case DLG_KEY:
73 h->ret_value = parm;
74 dlg_stop (h);
75 return MSG_HANDLED;
76 default:
77 return default_dlg_callback (h, sender, msg, parm, data);
81 /*** public functions **************************************************/
83 void
84 editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
85 /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
87 if (*search_default == '\0')
88 search_default = INPUT_LAST_TEXT;
91 size_t num_of_types;
92 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
93 int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + num_of_types - REPLACE_DLG_HEIGHT_SUPPLY;
95 QuickWidget quick_widgets[] =
97 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
98 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
99 #ifdef HAVE_CHARSET
100 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
101 #endif
102 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
103 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
104 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
105 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
106 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT,
107 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
108 /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")),
109 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT,
110 replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text),
111 /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:")),
112 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT,
113 search_default, REPLACE_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
114 QUICK_END
117 QuickDialog Quick_input =
119 REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, -1, N_(" Replace "),
120 "[Input Line Keys]", quick_widgets, FALSE
123 if (quick_dialog (&Quick_input) != B_CANCEL) {
124 edit->replace_mode = 0;
125 } else {
126 *replace_text = NULL;
127 *search_text = NULL;
130 g_strfreev (list_of_types);
134 /* --------------------------------------------------------------------------------------------- */
136 void
137 editcmd_dialog_search_show (WEdit * edit, char **search_text)
139 if (*search_text == '\0')
140 *search_text = INPUT_LAST_TEXT;
143 size_t num_of_types;
144 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
145 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
146 size_t i;
148 int dialog_result;
150 QuickWidget quick_widgets[] =
152 /* 0 */
153 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
154 /* 1 */
155 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT, N_("&Find all"), B_USER, NULL),
156 /* 2 */
157 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
158 #ifdef HAVE_CHARSET
159 /* 3 */
160 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
161 #endif
162 /* 4 */
163 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
164 /* 5 */
165 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
166 /* 6 */
167 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
168 /* 7 */
169 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
170 /* 8 */
171 QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
172 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
173 /* 9 */
174 QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
175 *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
176 /* 10 */
177 QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
178 QUICK_END
181 #ifdef HAVE_CHARSET
182 size_t last_checkbox = 7;
183 #else
184 size_t last_checkbox = 6;
185 #endif
187 QuickDialog Quick_input =
189 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"),
190 "[Input Line Keys]", quick_widgets, TRUE
193 #ifdef ENABLE_NLS
194 char **list_of_types_nls;
196 /* header title */
197 Quick_input.title = _(Quick_input.title);
198 /* buttons */
199 for (i = 0; i < 3; i++)
200 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
201 /* checkboxes */
202 for (i = 3; i <= last_checkbox; i++)
203 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
204 /* label */
205 quick_widgets[10].u.label.text = _(quick_widgets[10].u.label.text);
207 /* radiobuttons */
208 /* create copy of radio items to avoid memory leak */
209 list_of_types_nls = g_new0 (char *, num_of_types + 1);
210 for (i = 0; i < num_of_types; i++)
211 list_of_types_nls[i] = g_strdup (_(list_of_types[i]));
212 g_strfreev (list_of_types);
213 list_of_types = list_of_types_nls;
214 quick_widgets[last_checkbox + 1].u.radio.items = (const char **) list_of_types;
215 #endif
217 /* calculate widget coordinates */
219 int len = 0;
220 int dlg_width;
221 gchar **radio = list_of_types;
222 int b0_len, b1_len, b2_len;
223 const int button_gap = 2;
225 /* length of radiobuttons */
226 while (*radio != NULL) {
227 len = max (len, str_term_width1 (*radio));
228 radio++;
230 /* length of checkboxes */
231 for (i = 3; i <= last_checkbox; i++)
232 len = max (len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 4);
234 /* preliminary dialog width */
235 dlg_width = max (len * 2, str_term_width1 (Quick_input.title)) + 4;
237 /* length of buttons */
238 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
239 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 3;
240 b2_len = str_term_width1 (quick_widgets[2].u.button.text) + 5; /* default button */
241 len = b0_len + b1_len + b2_len + button_gap * 2;
243 /* dialog width */
244 Quick_input.xlen = max (SEARCH_DLG_WIDTH, max (dlg_width, len + 6));
246 /* correct widget coordinates */
247 for (i = 0; i < sizeof (quick_widgets)/sizeof (quick_widgets[0]); i++)
248 quick_widgets[i].x_divisions = Quick_input.xlen;
250 /* checkbox positions */
251 for (i = 3; i <= last_checkbox; i++)
252 quick_widgets[i].relative_x = Quick_input.xlen/2 + 2;
253 /* input length */
254 quick_widgets[last_checkbox + 2].u.input.len = Quick_input.xlen - 6;
255 /* button positions */
256 quick_widgets[2].relative_x = Quick_input.xlen/2 - len/2;
257 quick_widgets[1].relative_x = quick_widgets[2].relative_x + b2_len + button_gap;
258 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + button_gap;
261 dialog_result = quick_dialog (&Quick_input);
263 g_strfreev (list_of_types);
265 if (dialog_result == B_CANCEL)
266 *search_text = NULL;
267 else if (dialog_result == B_USER)
268 search_create_bookmark = 1;
272 /* --------------------------------------------------------------------------------------------- */
274 /* gets a raw key from the keyboard. Passing cancel = 1 draws
275 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
276 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
277 and Esc are cannot returned */
280 editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
282 int w = str_term_width1 (query) + 7;
283 struct Dlg_head *raw_dlg =
284 create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
285 NULL, heading, DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
286 add_widget (raw_dlg,
287 input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0, INPUT_COMPLETE_DEFAULT));
288 add_widget (raw_dlg, label_new (3 - cancel, 2, query));
289 if (cancel)
290 add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0));
291 w = run_dlg (raw_dlg);
292 destroy_dlg (raw_dlg);
293 if (cancel) {
294 if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL)
295 return 0;
298 return w;
301 /* --------------------------------------------------------------------------------------------- */
303 /* let the user select its preferred completion */
304 void
305 editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
306 struct selection *compl, int num_compl)
309 int start_x, start_y, offset, i;
310 char *curr = NULL;
311 Dlg_head *compl_dlg;
312 WListbox *compl_list;
313 int compl_dlg_h; /* completion dialog height */
314 int compl_dlg_w; /* completion dialog width */
316 /* calculate the dialog metrics */
317 compl_dlg_h = num_compl + 2;
318 compl_dlg_w = max_len + 4;
319 start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
320 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
321 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
323 if (start_x < 0)
324 start_x = 0;
325 if (compl_dlg_w > COLS)
326 compl_dlg_w = COLS;
327 if (compl_dlg_h > LINES - 2)
328 compl_dlg_h = LINES - 2;
330 offset = start_x + compl_dlg_w - COLS;
331 if (offset > 0)
332 start_x -= offset;
333 offset = start_y + compl_dlg_h - LINES;
334 if (offset > 0)
335 start_y -= (offset + 1);
337 /* create the dialog */
338 compl_dlg =
339 create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
340 dialog_colors, NULL, "[Completion]", NULL, DLG_COMPACT);
342 /* create the listbox */
343 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, FALSE, NULL);
345 /* add the dialog */
346 add_widget (compl_dlg, compl_list);
348 /* fill the listbox with the completions */
349 for (i = num_compl - 1; i >= 0; i--) /* reverse order */
350 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
352 /* pop up the dialog and apply the choosen completion */
353 if (run_dlg (compl_dlg) == B_ENTER) {
354 listbox_get_current (compl_list, &curr, NULL);
355 if (curr) {
356 #ifdef HAVE_CHARSET
357 GString *temp, *temp2;
358 temp = g_string_new("");
359 for (curr += word_len; *curr; curr++)
360 g_string_append_c(temp, *curr);
362 temp2 = str_convert_to_input (temp->str);
364 if (temp2 && temp2->len){
365 g_string_free(temp, TRUE);
366 temp = temp2;
368 else
369 g_string_free(temp2, TRUE);
370 for (curr = temp->str; *curr; curr++)
371 edit_insert (edit, *curr);
372 g_string_free(temp, TRUE);
373 #else
374 for (curr += word_len; *curr; curr++)
375 edit_insert (edit, *curr);
376 #endif
380 /* destroy dialog before return */
381 destroy_dlg (compl_dlg);
384 /* --------------------------------------------------------------------------------------------- */
386 /* let the user select where function definition */
387 void
388 editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
389 etags_hash_t * def_hash, int num_lines)
392 int start_x, start_y, offset, i;
393 char *curr = NULL;
394 etags_hash_t *curr_def = NULL;
395 Dlg_head *def_dlg;
396 WListbox *def_list;
397 int def_dlg_h; /* dialog height */
398 int def_dlg_w; /* dialog width */
399 char *label_def = NULL;
401 (void) word_len;
402 /* calculate the dialog metrics */
403 def_dlg_h = num_lines + 2;
404 def_dlg_w = max_len + 4;
405 start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
406 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
407 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
409 if (start_x < 0)
410 start_x = 0;
411 if (def_dlg_w > COLS)
412 def_dlg_w = COLS;
413 if (def_dlg_h > LINES - 2)
414 def_dlg_h = LINES - 2;
416 offset = start_x + def_dlg_w - COLS;
417 if (offset > 0)
418 start_x -= offset;
419 offset = start_y + def_dlg_h - LINES;
420 if (offset > 0)
421 start_y -= (offset + 1);
423 /* create the dialog */
424 def_dlg = create_dlg (start_y, start_x, def_dlg_h, def_dlg_w,
425 dialog_colors, NULL, "[Definitions]", match_expr, DLG_COMPACT);
427 /* create the listbox */
428 def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, FALSE, NULL);
430 /* add the dialog */
431 add_widget (def_dlg, def_list);
433 /* fill the listbox with the completions */
434 for (i = 0; i < num_lines; i++) {
435 label_def =
436 g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
437 def_hash[i].line);
438 listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
439 g_free (label_def);
442 /* pop up the dialog and apply the choosen completion */
443 if (run_dlg (def_dlg) == B_ENTER) {
444 char *tmp_curr_def = (char *) curr_def;
445 int do_moveto = 0;
447 listbox_get_current (def_list, &curr, &tmp_curr_def);
448 curr_def = (etags_hash_t *) tmp_curr_def;
449 if (edit->modified) {
450 if (!edit_query_dialog2
451 (_("Warning"),
452 _(" Current text was modified without a file save. \n"
453 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
454 edit->force |= REDRAW_COMPLETELY;
455 do_moveto = 1;
457 } else {
458 do_moveto = 1;
461 if (curr && do_moveto) {
462 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO) {
463 g_free (edit_history_moveto[edit_stack_iterator].filename);
464 if (edit->dir) {
465 edit_history_moveto[edit_stack_iterator].filename =
466 g_strdup_printf ("%s/%s", edit->dir, edit->filename);
467 } else {
468 edit_history_moveto[edit_stack_iterator].filename = g_strdup (edit->filename);
470 edit_history_moveto[edit_stack_iterator].line = edit->start_line +
471 edit->curs_row + 1;
472 edit_stack_iterator++;
473 g_free (edit_history_moveto[edit_stack_iterator].filename);
474 edit_history_moveto[edit_stack_iterator].filename =
475 g_strdup ((char *) curr_def->fullpath);
476 edit_history_moveto[edit_stack_iterator].line = curr_def->line;
477 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
478 edit_history_moveto[edit_stack_iterator].line);
483 /* clear definition hash */
484 for (i = 0; i < MAX_DEFINITIONS; i++) {
485 g_free (def_hash[i].filename);
488 /* destroy dialog before return */
489 destroy_dlg (def_dlg);
492 /* --------------------------------------------------------------------------------------------- */
495 editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos, int ypos)
497 /* dialog sizes */
498 int dlg_height = 9;
499 int dlg_width = 8;
501 int retval;
502 int i;
503 int btn_pos;
505 char *repl_from, *repl_to;
506 char tmp [BUF_MEDIUM];
508 QuickWidget quick_widgets[] =
510 /* 0 */ QUICK_BUTTON (44, dlg_width, 6, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
511 /* 1 */ QUICK_BUTTON (29, dlg_width, 6, dlg_height, N_("&Skip"), B_SKIP_REPLACE, NULL),
512 /* 2 */ QUICK_BUTTON (21, dlg_width, 6, dlg_height, N_("A&ll"), B_REPLACE_ALL, NULL),
513 /* 3 */ QUICK_BUTTON ( 4, dlg_width, 6, dlg_height, N_("&Replace"), B_ENTER, NULL),
514 /* 4 */ QUICK_LABEL ( 3, dlg_width, 2, dlg_height, NULL),
515 /* 5 */ QUICK_LABEL ( 2, dlg_width, 3, dlg_height, N_(" Replace with: ")),
516 /* 6 */ QUICK_LABEL ( 3, dlg_width, 4, dlg_height, NULL),
517 QUICK_END
520 #ifdef ENABLE_NLS
521 for (i = 0; i < 4; i++)
522 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
523 #endif
525 /* calculate button positions */
526 btn_pos = 4;
528 for (i = 3; i > -1; i--) {
529 quick_widgets[i].relative_x = btn_pos;
530 btn_pos += str_term_width1 (quick_widgets[i].u.button.text) + 5;
531 if (i == 3) /* default button */
532 btn_pos += 2;
535 dlg_width = btn_pos + 2;
537 /* correct widget coordinates */
538 for (i = 0; i < 7; i++)
539 quick_widgets[i].x_divisions = dlg_width;
541 g_snprintf (tmp, sizeof (tmp), " '%s'", from_text);
542 repl_from = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
544 g_snprintf (tmp, sizeof (tmp), " '%s'", to_text);
545 repl_to = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
547 quick_widgets[4].u.label.text = repl_from;
548 quick_widgets[6].u.label.text = repl_to;
550 if (xpos == -1)
551 xpos = (edit->num_widget_columns - dlg_width) / 2;
553 if (ypos == -1)
554 ypos = edit->num_widget_lines * 2 / 3;
557 QuickDialog Quick_input =
559 dlg_width, dlg_height, 0, 0, N_ (" Confirm replace "),
560 "[Input Line Keys]", quick_widgets, FALSE
563 /* Sometimes menu can hide replaced text. I don't like it */
564 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
565 ypos -= dlg_height;
567 Quick_input.ypos = ypos;
568 Quick_input.xpos = xpos;
570 retval = quick_dialog (&Quick_input);
571 g_free (repl_from);
572 g_free (repl_to);
574 return retval;
578 /* --------------------------------------------------------------------------------------------- */