Merge branch '1904_spec_bump_epoch'
[midnight-commander.git] / edit / editcmd_dialogs.c
blob713678fa2346aa9feaf4888d07f626b4cceba5e1
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 "../src/global.h"
31 #include "../src/tty/tty.h"
32 #include "../src/skin/skin.h" /* INPUT_COLOR */
33 #include "../src/tty/key.h"
35 #include "../src/search/search.h"
37 #include "../src/dialog.h"
38 #include "../src/widget.h"
39 #include "../src/wtools.h"
40 #include "../src/strutil.h"
41 #include "../src/main.h"
42 #include "../src/history.h"
43 #include "../src/charsets.h"
45 #include "../edit/edit-widget.h"
46 #include "../edit/etags.h"
47 #include "../edit/editcmd_dialogs.h"
50 /*** global variables **************************************************/
52 /*** file scope macro definitions **************************************/
54 #define SEARCH_DLG_WIDTH 58
55 #define SEARCH_DLG_MIN_HEIGHT 13
56 #define SEARCH_DLG_HEIGHT_SUPPLY 3
58 #define REPLACE_DLG_WIDTH 58
59 #define REPLACE_DLG_MIN_HEIGHT 17
60 #define REPLACE_DLG_HEIGHT_SUPPLY 5
62 /*** file scope type declarations **************************************/
64 /*** file scope variables **********************************************/
67 /*** file scope functions **********************************************/
69 static cb_ret_t
70 editcmd_dialog_raw_key_query_cb (struct Dlg_head *h, Widget *sender,
71 dlg_msg_t msg, int parm, void *data)
73 switch (msg) {
74 case DLG_KEY:
75 h->ret_value = parm;
76 dlg_stop (h);
77 return MSG_HANDLED;
78 default:
79 return default_dlg_callback (h, sender, msg, parm, data);
83 /*** public functions **************************************************/
85 void
86 editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
87 /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
89 if (*search_default == '\0')
90 search_default = INPUT_LAST_TEXT;
93 size_t num_of_types;
94 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
95 int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + num_of_types - REPLACE_DLG_HEIGHT_SUPPLY;
97 QuickWidget quick_widgets[] =
99 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
100 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
101 #ifdef HAVE_CHARSET
102 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
103 #endif
104 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
105 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
106 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
107 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
108 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT,
109 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
110 /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")),
111 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT,
112 replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text),
113 /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:")),
114 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT,
115 search_default, REPLACE_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
116 QUICK_END
119 QuickDialog Quick_input =
121 REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, -1, N_(" Replace "),
122 "[Input Line Keys]", quick_widgets, FALSE
125 if (quick_dialog (&Quick_input) != B_CANCEL) {
126 edit->replace_mode = 0;
127 } else {
128 *replace_text = NULL;
129 *search_text = NULL;
132 g_strfreev (list_of_types);
136 /* --------------------------------------------------------------------------------------------- */
138 void
139 editcmd_dialog_search_show (WEdit * edit, char **search_text)
141 if (*search_text == '\0')
142 *search_text = INPUT_LAST_TEXT;
145 size_t num_of_types;
146 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
147 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
148 size_t i;
150 int dialog_result;
152 QuickWidget quick_widgets[] =
154 /* 0 */
155 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
156 /* 1 */
157 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT, N_("&Find all"), B_USER, NULL),
158 /* 2 */
159 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
160 #ifdef HAVE_CHARSET
161 /* 3 */
162 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
163 #endif
164 /* 4 */
165 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
166 /* 5 */
167 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
168 /* 6 */
169 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
170 /* 7 */
171 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
172 /* 8 */
173 QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
174 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
175 /* 9 */
176 QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
177 *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
178 /* 10 */
179 QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
180 QUICK_END
183 #ifdef HAVE_CHARSET
184 size_t last_checkbox = 7;
185 #else
186 size_t last_checkbox = 6;
187 #endif
189 QuickDialog Quick_input =
191 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"),
192 "[Input Line Keys]", quick_widgets, TRUE
195 #ifdef ENABLE_NLS
196 char **list_of_types_nls;
198 /* header title */
199 Quick_input.title = _(Quick_input.title);
200 /* buttons */
201 for (i = 0; i < 3; i++)
202 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
203 /* checkboxes */
204 for (i = 3; i <= last_checkbox; i++)
205 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
206 /* label */
207 quick_widgets[10].u.label.text = _(quick_widgets[10].u.label.text);
209 /* radiobuttons */
210 /* create copy of radio items to avoid memory leak */
211 list_of_types_nls = g_new0 (char *, num_of_types + 1);
212 for (i = 0; i < num_of_types; i++)
213 list_of_types_nls[i] = g_strdup (_(list_of_types[i]));
214 g_strfreev (list_of_types);
215 list_of_types = list_of_types_nls;
216 quick_widgets[last_checkbox + 1].u.radio.items = (const char **) list_of_types;
217 #endif
219 /* calculate widget coordinates */
221 int len = 0;
222 int dlg_width;
223 gchar **radio = list_of_types;
224 int b0_len, b1_len, b2_len;
225 const int button_gap = 2;
227 /* length of radiobuttons */
228 while (*radio != NULL) {
229 len = max (len, str_term_width1 (*radio));
230 radio++;
232 /* length of checkboxes */
233 for (i = 3; i <= last_checkbox; i++)
234 len = max (len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 4);
236 /* preliminary dialog width */
237 dlg_width = max (len * 2, str_term_width1 (Quick_input.title)) + 4;
239 /* length of buttons */
240 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
241 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 3;
242 b2_len = str_term_width1 (quick_widgets[2].u.button.text) + 5; /* default button */
243 len = b0_len + b1_len + b2_len + button_gap * 2;
245 /* dialog width */
246 Quick_input.xlen = max (SEARCH_DLG_WIDTH, max (dlg_width, len + 6));
248 /* correct widget coordinates */
249 for (i = 0; i < sizeof (quick_widgets)/sizeof (quick_widgets[0]); i++)
250 quick_widgets[i].x_divisions = Quick_input.xlen;
252 /* checkbox positions */
253 for (i = 3; i <= last_checkbox; i++)
254 quick_widgets[i].relative_x = Quick_input.xlen/2 + 2;
255 /* input length */
256 quick_widgets[last_checkbox + 2].u.input.len = Quick_input.xlen - 6;
257 /* button positions */
258 quick_widgets[2].relative_x = Quick_input.xlen/2 - len/2;
259 quick_widgets[1].relative_x = quick_widgets[2].relative_x + b2_len + button_gap;
260 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + button_gap;
263 dialog_result = quick_dialog (&Quick_input);
265 g_strfreev (list_of_types);
267 if (dialog_result == B_CANCEL)
268 *search_text = NULL;
269 else if (dialog_result == B_USER)
270 search_create_bookmark = 1;
274 /* --------------------------------------------------------------------------------------------- */
276 /* gets a raw key from the keyboard. Passing cancel = 1 draws
277 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
278 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
279 and Esc are cannot returned */
282 editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
284 int w = str_term_width1 (query) + 7;
285 struct Dlg_head *raw_dlg =
286 create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
287 NULL, heading, DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
288 add_widget (raw_dlg,
289 input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0, INPUT_COMPLETE_DEFAULT));
290 add_widget (raw_dlg, label_new (3 - cancel, 2, query));
291 if (cancel)
292 add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0));
293 w = run_dlg (raw_dlg);
294 destroy_dlg (raw_dlg);
295 if (cancel) {
296 if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL)
297 return 0;
300 return w;
303 /* --------------------------------------------------------------------------------------------- */
305 /* let the user select its preferred completion */
306 void
307 editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
308 struct selection *compl, int num_compl)
311 int start_x, start_y, offset, i;
312 char *curr = NULL;
313 Dlg_head *compl_dlg;
314 WListbox *compl_list;
315 int compl_dlg_h; /* completion dialog height */
316 int compl_dlg_w; /* completion dialog width */
318 /* calculate the dialog metrics */
319 compl_dlg_h = num_compl + 2;
320 compl_dlg_w = max_len + 4;
321 start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
322 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
323 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
325 if (start_x < 0)
326 start_x = 0;
327 if (compl_dlg_w > COLS)
328 compl_dlg_w = COLS;
329 if (compl_dlg_h > LINES - 2)
330 compl_dlg_h = LINES - 2;
332 offset = start_x + compl_dlg_w - COLS;
333 if (offset > 0)
334 start_x -= offset;
335 offset = start_y + compl_dlg_h - LINES;
336 if (offset > 0)
337 start_y -= (offset + 1);
339 /* create the dialog */
340 compl_dlg =
341 create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
342 dialog_colors, NULL, "[Completion]", NULL, DLG_COMPACT);
344 /* create the listbox */
345 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, NULL);
347 /* add the dialog */
348 add_widget (compl_dlg, compl_list);
350 /* fill the listbox with the completions */
351 for (i = num_compl - 1; i >= 0; i--) /* reverse order */
352 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
354 /* pop up the dialog and apply the choosen completion */
355 if (run_dlg (compl_dlg) == B_ENTER) {
356 listbox_get_current (compl_list, &curr, NULL);
357 if (curr) {
358 #ifdef HAVE_CHARSET
359 GString *temp, *temp2;
360 temp = g_string_new("");
361 for (curr += word_len; *curr; curr++)
362 g_string_append_c(temp, *curr);
364 temp2 = str_convert_to_input (temp->str);
366 if (temp2 && temp2->len){
367 g_string_free(temp, TRUE);
368 temp = temp2;
370 else
371 g_string_free(temp2, TRUE);
372 for (curr = temp->str; *curr; curr++)
373 edit_insert (edit, *curr);
374 g_string_free(temp, TRUE);
375 #else
376 for (curr += word_len; *curr; curr++)
377 edit_insert (edit, *curr);
378 #endif
382 /* destroy dialog before return */
383 destroy_dlg (compl_dlg);
386 /* --------------------------------------------------------------------------------------------- */
388 /* let the user select where function definition */
389 void
390 editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
391 etags_hash_t * def_hash, int num_lines)
394 int start_x, start_y, offset, i;
395 char *curr = NULL;
396 etags_hash_t *curr_def = NULL;
397 Dlg_head *def_dlg;
398 WListbox *def_list;
399 int def_dlg_h; /* dialog height */
400 int def_dlg_w; /* dialog width */
401 char *label_def = NULL;
403 (void) word_len;
404 /* calculate the dialog metrics */
405 def_dlg_h = num_lines + 2;
406 def_dlg_w = max_len + 4;
407 start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
408 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
409 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
411 if (start_x < 0)
412 start_x = 0;
413 if (def_dlg_w > COLS)
414 def_dlg_w = COLS;
415 if (def_dlg_h > LINES - 2)
416 def_dlg_h = LINES - 2;
418 offset = start_x + def_dlg_w - COLS;
419 if (offset > 0)
420 start_x -= offset;
421 offset = start_y + def_dlg_h - LINES;
422 if (offset > 0)
423 start_y -= (offset + 1);
425 /* create the dialog */
426 def_dlg = create_dlg (start_y, start_x, def_dlg_h, def_dlg_w,
427 dialog_colors, NULL, "[Definitions]", match_expr, DLG_COMPACT);
429 /* create the listbox */
430 def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, NULL);
432 /* add the dialog */
433 add_widget (def_dlg, def_list);
436 /* fill the listbox with the completions */
437 for (i = 0; i < num_lines; i++) {
438 label_def =
439 g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
440 def_hash[i].line);
441 listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
442 g_free (label_def);
444 /* pop up the dialog */
445 run_dlg (def_dlg);
447 /* apply the choosen completion */
448 if (def_dlg->ret_value == B_ENTER) {
449 char *tmp_curr_def = (char *) curr_def;
450 int do_moveto = 0;
452 listbox_get_current (def_list, &curr, &tmp_curr_def);
453 curr_def = (etags_hash_t *) tmp_curr_def;
454 if (edit->modified) {
455 if (!edit_query_dialog2
456 (_("Warning"),
457 _(" Current text was modified without a file save. \n"
458 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
459 edit->force |= REDRAW_COMPLETELY;
460 do_moveto = 1;
462 } else {
463 do_moveto = 1;
465 if (curr && do_moveto) {
466 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO) {
467 g_free (edit_history_moveto[edit_stack_iterator].filename);
468 if (edit->dir) {
469 edit_history_moveto[edit_stack_iterator].filename =
470 g_strdup_printf ("%s/%s", edit->dir, edit->filename);
471 } else {
472 edit_history_moveto[edit_stack_iterator].filename = g_strdup (edit->filename);
474 edit_history_moveto[edit_stack_iterator].line = edit->start_line +
475 edit->curs_row + 1;
476 edit_stack_iterator++;
477 g_free (edit_history_moveto[edit_stack_iterator].filename);
478 edit_history_moveto[edit_stack_iterator].filename =
479 g_strdup ((char *) curr_def->fullpath);
480 edit_history_moveto[edit_stack_iterator].line = curr_def->line;
481 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
482 edit_history_moveto[edit_stack_iterator].line);
487 /* clear definition hash */
488 for (i = 0; i < MAX_DEFINITIONS; i++) {
489 g_free (def_hash[i].filename);
492 /* destroy dialog before return */
493 destroy_dlg (def_dlg);
496 /* --------------------------------------------------------------------------------------------- */
499 editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos, int ypos)
501 /* dialog sizes */
502 int dlg_height = 9;
503 int dlg_width = 8;
505 int retval;
506 int i;
507 int btn_pos;
509 char *repl_from, *repl_to;
510 char tmp [BUF_MEDIUM];
512 QuickWidget quick_widgets[] =
514 /* 0 */ QUICK_BUTTON (44, dlg_width, 6, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
515 /* 1 */ QUICK_BUTTON (29, dlg_width, 6, dlg_height, N_("&Skip"), B_SKIP_REPLACE, NULL),
516 /* 2 */ QUICK_BUTTON (21, dlg_width, 6, dlg_height, N_("A&ll"), B_REPLACE_ALL, NULL),
517 /* 3 */ QUICK_BUTTON ( 4, dlg_width, 6, dlg_height, N_("&Replace"), B_ENTER, NULL),
518 /* 4 */ QUICK_LABEL ( 3, dlg_width, 2, dlg_height, NULL),
519 /* 5 */ QUICK_LABEL ( 2, dlg_width, 3, dlg_height, N_(" Replace with: ")),
520 /* 6 */ QUICK_LABEL ( 3, dlg_width, 4, dlg_height, NULL),
521 QUICK_END
524 #ifdef ENABLE_NLS
525 for (i = 0; i < 4; i++)
526 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
527 #endif
529 /* calculate button positions */
530 btn_pos = 4;
532 for (i = 3; i > -1; i--) {
533 quick_widgets[i].relative_x = btn_pos;
534 btn_pos += str_term_width1 (quick_widgets[i].u.button.text) + 5;
535 if (i == 3) /* default button */
536 btn_pos += 2;
539 dlg_width = btn_pos + 2;
541 /* correct widget coordinates */
542 for (i = 0; i < 7; i++)
543 quick_widgets[i].x_divisions = dlg_width;
545 g_snprintf (tmp, sizeof (tmp), " '%s'", from_text);
546 repl_from = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
548 g_snprintf (tmp, sizeof (tmp), " '%s'", to_text);
549 repl_to = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
551 quick_widgets[4].u.label.text = repl_from;
552 quick_widgets[6].u.label.text = repl_to;
554 if (xpos == -1)
555 xpos = (edit->num_widget_columns - dlg_width) / 2;
557 if (ypos == -1)
558 ypos = edit->num_widget_lines * 2 / 3;
561 QuickDialog Quick_input =
563 dlg_width, dlg_height, 0, 0, N_ (" Confirm replace "),
564 "[Input Line Keys]", quick_widgets, FALSE
567 /* Sometimes menu can hide replaced text. I don't like it */
568 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
569 ypos -= dlg_height;
571 Quick_input.ypos = ypos;
572 Quick_input.xpos = xpos;
574 retval = quick_dialog (&Quick_input);
575 g_free (repl_from);
576 g_free (repl_to);
578 return retval;
582 /* --------------------------------------------------------------------------------------------- */