Rename Dlg_head to WDialog.
[midnight-commander.git] / src / editor / editcmd_dialogs.c
bloba6dfc693102d687171d2516cb1fdf5805869b859
1 /*
2 Editor dialogs for high level editing commands
4 Copyright (C) 2009, 2011, 2012
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
9 Andrew Borodin, <aborodin@vmail.ru>, 2012.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
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"
35 #include "lib/widget.h"
36 #ifdef HAVE_CHARSET
37 #include "lib/charsets.h"
38 #endif
40 #include "src/history.h"
42 #include "src/editor/editwidget.h"
43 #include "src/editor/etags.h"
44 #include "src/editor/editcmd_dialogs.h"
46 #ifdef HAVE_ASPELL
47 #include "src/editor/spell.h"
48 #endif
50 /*** global variables ****************************************************************************/
52 edit_search_options_t edit_search_options = {
53 .type = MC_SEARCH_T_NORMAL,
54 .case_sens = FALSE,
55 .backwards = FALSE,
56 .only_in_selection = FALSE,
57 .whole_words = FALSE,
58 .all_codepages = FALSE
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
65 /*** file scope variables ************************************************************************/
67 /*** file scope functions ************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
70 static cb_ret_t
71 editcmd_dialog_raw_key_query_cb (WDialog *h, Widget * sender, dlg_msg_t msg, int parm, void *data)
73 switch (msg)
75 case DLG_KEY:
76 h->ret_value = parm;
77 dlg_stop (h);
78 return MSG_HANDLED;
79 default:
80 return dlg_default_callback (h, sender, msg, parm, data);
84 /* --------------------------------------------------------------------------------------------- */
85 /*** public functions ****************************************************************************/
86 /* --------------------------------------------------------------------------------------------- */
88 gboolean
89 editcmd_dialog_search_show (WEdit * edit)
91 char *search_text;
92 size_t num_of_types;
93 gchar **list_of_types;
94 int dialog_result;
96 list_of_types = mc_search_get_types_strings_array (&num_of_types);
99 quick_widget_t quick_widgets[] = {
100 /* *INDENT-OFF* */
101 QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above,
102 INPUT_LAST_TEXT, 0, MC_HISTORY_SHARED_SEARCH, &search_text, NULL),
103 QUICK_SEPARATOR (TRUE),
104 QUICK_START_COLUMNS,
105 QUICK_RADIO (num_of_types, (const char **) list_of_types,
106 (int *) &edit_search_options.type, NULL),
107 QUICK_NEXT_COLUMN,
108 QUICK_CHECKBOX (N_("Cas&e sensitive"), &edit_search_options.case_sens, NULL),
109 QUICK_CHECKBOX (N_("&Backwards"), &edit_search_options.backwards, NULL),
110 QUICK_CHECKBOX (N_("In se&lection"), &edit_search_options.only_in_selection, NULL),
111 QUICK_CHECKBOX (N_("&Whole words"), &edit_search_options.whole_words, NULL),
112 #ifdef HAVE_CHARSET
113 QUICK_CHECKBOX (N_("&All charsets"), &edit_search_options.all_codepages, NULL),
114 #endif
115 QUICK_STOP_COLUMNS,
116 QUICK_START_BUTTONS (TRUE, TRUE),
117 QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL),
118 QUICK_BUTTON (N_("&Find all"), B_USER, NULL, NULL),
119 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
120 QUICK_END
121 /* *INDENT-ON* */
124 quick_dialog_t qdlg = {
125 -1, -1, 58,
126 N_("Search"), "[Input Line Keys]",
127 quick_widgets, NULL, NULL
130 dialog_result = quick_dialog (&qdlg);
133 g_strfreev (list_of_types);
135 if ((dialog_result == B_CANCEL) || (search_text == NULL) || (search_text[0] == '\0'))
137 g_free (search_text);
138 return FALSE;
141 if (dialog_result == B_USER)
142 search_create_bookmark = TRUE;
144 #ifdef HAVE_CHARSET
146 GString *tmp;
148 tmp = str_convert_to_input (search_text);
149 if (tmp != NULL)
151 g_free (search_text);
152 search_text = g_string_free (tmp, FALSE);
155 #endif
157 g_free (edit->last_search_string);
158 edit->last_search_string = search_text;
159 mc_search_free (edit->search);
161 edit->search = mc_search_new (edit->last_search_string, -1);
162 if (edit->search != NULL)
164 edit->search->search_type = edit_search_options.type;
165 edit->search->is_all_charsets = edit_search_options.all_codepages;
166 edit->search->is_case_sensitive = edit_search_options.case_sens;
167 edit->search->whole_words = edit_search_options.whole_words;
168 edit->search->search_fn = edit_search_cmd_callback;
171 return (edit->search != NULL);
174 /* --------------------------------------------------------------------------------------------- */
176 void
177 editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
178 /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
180 size_t num_of_types;
181 gchar **list_of_types;
183 if ((search_default == NULL) || (*search_default == '\0'))
184 search_default = INPUT_LAST_TEXT;
186 list_of_types = mc_search_get_types_strings_array (&num_of_types);
189 quick_widget_t quick_widgets[] = {
190 /* *INDENT-OFF* */
191 QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above,
192 search_default, 0, MC_HISTORY_SHARED_SEARCH, search_text, NULL),
193 QUICK_LABELED_INPUT (N_("Enter replacement string:"), input_label_above,
194 replace_default, 0, "replace", replace_text, NULL),
195 QUICK_SEPARATOR (TRUE),
196 QUICK_START_COLUMNS,
197 QUICK_RADIO (num_of_types, (const char **) list_of_types,
198 (int *) &edit_search_options.type, NULL),
199 QUICK_NEXT_COLUMN,
200 QUICK_CHECKBOX (N_("Cas&e sensitive"), &edit_search_options.case_sens, NULL),
201 QUICK_CHECKBOX (N_("&Backwards"), &edit_search_options.backwards, NULL),
202 QUICK_CHECKBOX (N_("In se&lection"), &edit_search_options.only_in_selection, NULL),
203 QUICK_CHECKBOX (N_("&Whole words"), &edit_search_options.whole_words, NULL),
204 #ifdef HAVE_CHARSET
205 QUICK_CHECKBOX (N_("&All charsets"), &edit_search_options.all_codepages, NULL),
206 #endif
207 QUICK_STOP_COLUMNS,
208 QUICK_BUTTONS_OK_CANCEL,
209 QUICK_END
210 /* *INDENT-ON* */
213 quick_dialog_t qdlg = {
214 -1, -1, 58,
215 N_("Replace"), "[Input Line Keys]",
216 quick_widgets, NULL, NULL
219 if (quick_dialog (&qdlg) != B_CANCEL)
220 edit->replace_mode = 0;
221 else
223 *replace_text = NULL;
224 *search_text = NULL;
228 g_strfreev (list_of_types);
231 /* --------------------------------------------------------------------------------------------- */
234 editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos,
235 int ypos)
237 Widget *w = WIDGET (edit);
239 /* dialog size */
240 int dlg_height = 10;
242 char tmp[BUF_MEDIUM];
243 char *repl_from, *repl_to;
244 int retval;
246 if (xpos == -1)
247 xpos = w->x + option_line_state_width + 1;
248 if (ypos == -1)
249 ypos = w->y + w->lines / 2;
250 /* Sometimes menu can hide replaced text. I don't like it */
251 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
252 ypos -= dlg_height;
254 g_snprintf (tmp, sizeof (tmp), "\"%s\"", from_text);
255 repl_from = g_strdup (str_trunc (tmp, - 7));
257 g_snprintf (tmp, sizeof (tmp), "\"%s\"", to_text);
258 repl_to = g_strdup (str_trunc (tmp, - 7));
261 quick_widget_t quick_widgets[] = {
262 /* *INDENT-OFF* */
263 QUICK_LABEL (repl_from, NULL),
264 QUICK_LABEL (N_("Replace with:"), NULL),
265 QUICK_LABEL (repl_to, NULL),
266 QUICK_START_BUTTONS (TRUE, TRUE),
267 QUICK_BUTTON (N_("&Replace"), B_ENTER, NULL, NULL),
268 QUICK_BUTTON (N_("A&ll"), B_REPLACE_ALL, NULL, NULL),
269 QUICK_BUTTON (N_("&Skip"), B_SKIP_REPLACE, NULL, NULL),
270 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
271 QUICK_END
272 /* *INDENT-ON* */
275 quick_dialog_t qdlg = {
276 ypos, xpos, -1,
277 N_("Confirm replace"), NULL,
278 quick_widgets, NULL, NULL
281 retval = quick_dialog (&qdlg);
284 g_free (repl_from);
285 g_free (repl_to);
287 return retval;
290 /* --------------------------------------------------------------------------------------------- */
291 /* gets a raw key from the keyboard. Passing cancel = 1 draws
292 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
293 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
294 and Esc are cannot returned */
297 editcmd_dialog_raw_key_query (const char *heading, const char *query, gboolean cancel)
299 int w, wq;
300 int y = 2;
301 WDialog *raw_dlg;
303 w = str_term_width1 (heading) + 6;
304 wq = str_term_width1 (query);
305 w = max (w, wq + 3 * 2 + 1 + 2);
307 raw_dlg =
308 create_dlg (TRUE, 0, 0, cancel ? 7 : 5, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
309 NULL, NULL, heading, DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
311 add_widget (raw_dlg, label_new (y, 3, query));
312 add_widget (raw_dlg, input_new (y++, 3 + wq + 1, input_get_default_colors (),
313 w - (6 + wq + 1), "", 0, INPUT_COMPLETE_DEFAULT));
314 if (cancel)
316 add_widget (raw_dlg, hline_new (y++, -1, -1));
317 /* Button w/o hotkey to allow use any key as raw or macro one */
318 add_widget_autopos (raw_dlg, button_new (y, 1, B_CANCEL, NORMAL_BUTTON, _("Cancel"), NULL),
319 WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
322 w = run_dlg (raw_dlg);
323 destroy_dlg (raw_dlg);
325 return (cancel && (w == ESC_CHAR || w == B_CANCEL)) ? 0 : w;
328 /* --------------------------------------------------------------------------------------------- */
329 /* let the user select its preferred completion */
331 void
332 editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
333 struct selection *compl, int num_compl)
336 int start_x, start_y, offset, i;
337 char *curr = NULL;
338 WDialog *compl_dlg;
339 WListbox *compl_list;
340 int compl_dlg_h; /* completion dialog height */
341 int compl_dlg_w; /* completion dialog width */
343 /* calculate the dialog metrics */
344 compl_dlg_h = num_compl + 2;
345 compl_dlg_w = max_len + 4;
346 start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
347 EDIT_TEXT_HORIZONTAL_OFFSET + (edit->fullscreen ? 0 : 1) + option_line_state_width;
348 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + (edit->fullscreen ? 0 : 1) + 1;
350 if (start_x < 0)
351 start_x = 0;
352 if (compl_dlg_w > COLS)
353 compl_dlg_w = COLS;
354 if (compl_dlg_h > LINES - 2)
355 compl_dlg_h = LINES - 2;
357 offset = start_x + compl_dlg_w - COLS;
358 if (offset > 0)
359 start_x -= offset;
360 offset = start_y + compl_dlg_h - LINES;
361 if (offset > 0)
362 start_y -= (offset + 1);
364 /* create the dialog */
365 compl_dlg =
366 create_dlg (TRUE, start_y, start_x, compl_dlg_h, compl_dlg_w,
367 dialog_colors, NULL, NULL, "[Completion]", NULL, DLG_COMPACT);
369 /* create the listbox */
370 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, FALSE, NULL);
372 /* add the dialog */
373 add_widget (compl_dlg, compl_list);
375 /* fill the listbox with the completions */
376 for (i = num_compl - 1; i >= 0; i--) /* reverse order */
377 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
379 /* pop up the dialog and apply the choosen completion */
380 if (run_dlg (compl_dlg) == B_ENTER)
382 listbox_get_current (compl_list, &curr, NULL);
383 if (curr)
385 #ifdef HAVE_CHARSET
386 GString *temp, *temp2;
387 temp = g_string_new ("");
388 for (curr += word_len; *curr; curr++)
389 g_string_append_c (temp, *curr);
391 temp2 = str_convert_to_input (temp->str);
393 if (temp2 && temp2->len)
395 g_string_free (temp, TRUE);
396 temp = temp2;
398 else
399 g_string_free (temp2, TRUE);
400 for (curr = temp->str; *curr; curr++)
401 edit_insert (edit, *curr);
402 g_string_free (temp, TRUE);
403 #else
404 for (curr += word_len; *curr; curr++)
405 edit_insert (edit, *curr);
406 #endif
410 /* destroy dialog before return */
411 destroy_dlg (compl_dlg);
414 /* --------------------------------------------------------------------------------------------- */
415 /* let the user select where function definition */
417 void
418 editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
419 etags_hash_t * def_hash, int num_lines)
422 int start_x, start_y, offset, i;
423 char *curr = NULL;
424 etags_hash_t *curr_def = NULL;
425 WDialog *def_dlg;
426 WListbox *def_list;
427 int def_dlg_h; /* dialog height */
428 int def_dlg_w; /* dialog width */
429 char *label_def = NULL;
431 (void) word_len;
432 /* calculate the dialog metrics */
433 def_dlg_h = num_lines + 2;
434 def_dlg_w = max_len + 4;
435 start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
436 EDIT_TEXT_HORIZONTAL_OFFSET + (edit->fullscreen ? 0 : 1) + option_line_state_width;
437 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + (edit->fullscreen ? 0 : 1) + 1;
439 if (start_x < 0)
440 start_x = 0;
441 if (def_dlg_w > COLS)
442 def_dlg_w = COLS;
443 if (def_dlg_h > LINES - 2)
444 def_dlg_h = LINES - 2;
446 offset = start_x + def_dlg_w - COLS;
447 if (offset > 0)
448 start_x -= offset;
449 offset = start_y + def_dlg_h - LINES;
450 if (offset > 0)
451 start_y -= (offset + 1);
453 /* create the dialog */
454 def_dlg = create_dlg (TRUE, start_y, start_x, def_dlg_h, def_dlg_w,
455 dialog_colors, NULL, NULL, "[Definitions]", match_expr, DLG_COMPACT);
457 /* create the listbox */
458 def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, FALSE, NULL);
460 /* add the dialog */
461 add_widget (def_dlg, def_list);
463 /* fill the listbox with the completions */
464 for (i = 0; i < num_lines; i++)
466 label_def =
467 g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
468 def_hash[i].line);
469 listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
470 g_free (label_def);
473 /* pop up the dialog and apply the choosen completion */
474 if (run_dlg (def_dlg) == B_ENTER)
476 char *tmp_curr_def = (char *) curr_def;
477 int do_moveto = 0;
479 listbox_get_current (def_list, &curr, (void **) &tmp_curr_def);
480 curr_def = (etags_hash_t *) tmp_curr_def;
481 if (edit->modified)
483 if (!edit_query_dialog2
484 (_("Warning"),
485 _("Current text was modified without a file save.\n"
486 "Continue discards these changes."), _("C&ontinue"), _("&Cancel")))
488 edit->force |= REDRAW_COMPLETELY;
489 do_moveto = 1;
492 else
494 do_moveto = 1;
497 if (curr && do_moveto)
499 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
501 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
502 if (edit->dir_vpath != NULL)
504 edit_history_moveto[edit_stack_iterator].filename_vpath =
505 vfs_path_append_vpath_new (edit->dir_vpath, edit->filename_vpath, NULL);
507 else
509 edit_history_moveto[edit_stack_iterator].filename_vpath =
510 vfs_path_clone (edit->filename_vpath);
512 edit_history_moveto[edit_stack_iterator].line = edit->start_line +
513 edit->curs_row + 1;
514 edit_stack_iterator++;
515 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
516 edit_history_moveto[edit_stack_iterator].filename_vpath =
517 vfs_path_from_str ((char *) curr_def->fullpath);
518 edit_history_moveto[edit_stack_iterator].line = curr_def->line;
519 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
520 edit_history_moveto[edit_stack_iterator].line);
525 /* clear definition hash */
526 for (i = 0; i < MAX_DEFINITIONS; i++)
528 g_free (def_hash[i].filename);
531 /* destroy dialog before return */
532 destroy_dlg (def_dlg);
535 /* --------------------------------------------------------------------------------------------- */