Merge branch '1497_stickchars'
[pantumic.git] / edit / editcmd_dialogs.c
blob0154d241e38de4cc621b3cd66300f32119ec5e35
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/tty/color.h" /* INPUT_COLOR */
33 #include "../src/tty/key.h"
35 #include "../src/search/search.h"
37 #include "../src/strutil.h"
38 #include "../src/widget.h"
39 #include "../src/wtools.h"
40 #include "../src/dialog.h" /* do_refresh() */
41 #include "../src/main.h"
42 #include "../src/history.h"
44 #include "../edit/edit-widget.h"
45 #include "../edit/etags.h"
46 #include "../edit/editcmd_dialogs.h"
49 /*** global variables **************************************************/
51 /*** file scope macro definitions **************************************/
53 #define SEARCH_DLG_WIDTH 58
54 #define SEARCH_DLG_MIN_HEIGHT 13
55 #define SEARCH_DLG_HEIGHT_SUPPLY 3
57 #define REPLACE_DLG_WIDTH 58
58 #define REPLACE_DLG_MIN_HEIGHT 17
59 #define REPLACE_DLG_HEIGHT_SUPPLY 5
61 /*** file scope type declarations **************************************/
63 /*** file scope variables **********************************************/
66 /*** file scope functions **********************************************/
68 static cb_ret_t
69 editcmd_dialog_raw_key_query_cb (struct Dlg_head *h, dlg_msg_t msg, int parm)
71 switch (msg) {
72 case DLG_KEY:
73 dlg_stop (h);
74 h->ret_value = parm;
75 return MSG_HANDLED;
76 default:
77 return default_dlg_callback (h, msg, parm);
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 gchar **list_of_types = mc_search_get_types_strings_array();
92 int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - REPLACE_DLG_HEIGHT_SUPPLY;
94 QuickWidget quick_widgets[] =
96 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
97 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
98 #ifdef HAVE_CHARSET
99 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
100 #endif
101 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
102 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
103 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
104 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
105 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT,
106 g_strv_length (list_of_types), (const char **) list_of_types, &edit->search_type),
107 /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")),
108 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT,
109 replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text),
110 /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:")),
111 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT,
112 search_default, REPLACE_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
113 QUICK_END
116 QuickDialog Quick_input =
118 REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, -1, N_(" Replace "),
119 "[Input Line Keys]", quick_widgets, FALSE
122 if (quick_dialog (&Quick_input) != B_CANCEL) {
123 edit->replace_mode = 0;
124 } else {
125 *replace_text = NULL;
126 *search_text = NULL;
129 g_strfreev (list_of_types);
133 /* --------------------------------------------------------------------------------------------- */
135 void
136 editcmd_dialog_search_show (WEdit * edit, char **search_text)
138 if (*search_text == '\0')
139 *search_text = INPUT_LAST_TEXT;
142 gchar **list_of_types = mc_search_get_types_strings_array();
143 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - SEARCH_DLG_HEIGHT_SUPPLY;
145 QuickWidget quick_widgets[] =
147 /* 0 */ QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
148 /* 1 */ QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
149 #ifdef HAVE_CHARSET
150 /* 2 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
151 #endif
152 /* 3 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
153 /* 4 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
154 /* 5 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
155 /* 6 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
156 /* 7 */ QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
157 g_strv_length (list_of_types), (const char **) list_of_types, &edit->search_type),
158 /* 8 */ QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
159 *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
160 /* 9 */ QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
161 QUICK_END
164 QuickDialog Quick_input =
166 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"),
167 "[Input Line Keys]", quick_widgets, FALSE
170 if (quick_dialog (&Quick_input) == B_CANCEL)
171 *search_text = NULL;
175 /* --------------------------------------------------------------------------------------------- */
177 /* gets a raw key from the keyboard. Passing cancel = 1 draws
178 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
179 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
180 and Esc are cannot returned */
183 editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
185 int w = str_term_width1 (query) + 7;
186 struct Dlg_head *raw_dlg =
187 create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
188 NULL, heading,
189 DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
190 add_widget (raw_dlg,
191 input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0, INPUT_COMPLETE_DEFAULT));
192 add_widget (raw_dlg, label_new (3 - cancel, 2, query));
193 if (cancel)
194 add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0));
195 w = run_dlg (raw_dlg);
196 destroy_dlg (raw_dlg);
197 if (cancel) {
198 if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL)
199 return 0;
202 return w;
205 /* --------------------------------------------------------------------------------------------- */
207 /* let the user select its preferred completion */
208 void
209 editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
210 struct selection *compl, int num_compl)
213 int start_x, start_y, offset, i;
214 char *curr = NULL;
215 Dlg_head *compl_dlg;
216 WListbox *compl_list;
217 int compl_dlg_h; /* completion dialog height */
218 int compl_dlg_w; /* completion dialog width */
220 /* calculate the dialog metrics */
221 compl_dlg_h = num_compl + 2;
222 compl_dlg_w = max_len + 4;
223 start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
224 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
225 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
227 if (start_x < 0)
228 start_x = 0;
229 if (compl_dlg_w > COLS)
230 compl_dlg_w = COLS;
231 if (compl_dlg_h > LINES - 2)
232 compl_dlg_h = LINES - 2;
234 offset = start_x + compl_dlg_w - COLS;
235 if (offset > 0)
236 start_x -= offset;
237 offset = start_y + compl_dlg_h - LINES;
238 if (offset > 0)
239 start_y -= (offset + 1);
241 /* create the dialog */
242 compl_dlg =
243 create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
244 dialog_colors, NULL, "[Completion]", NULL, DLG_COMPACT);
246 /* create the listbox */
247 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, NULL);
249 /* add the dialog */
250 add_widget (compl_dlg, compl_list);
252 /* fill the listbox with the completions */
253 for (i = 0; i < num_compl; i++)
254 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
256 /* pop up the dialog and apply the choosen completion */
257 if (run_dlg (compl_dlg) == B_ENTER) {
258 listbox_get_current (compl_list, &curr, NULL);
259 if (curr)
260 for (curr += word_len; *curr; curr++)
261 edit_insert (edit, *curr);
264 /* destroy dialog before return */
265 destroy_dlg (compl_dlg);
268 /* --------------------------------------------------------------------------------------------- */
270 /* let the user select where function definition */
271 void
272 editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
273 etags_hash_t * def_hash, int num_lines)
276 int start_x, start_y, offset, i;
277 char *curr = NULL;
278 etags_hash_t *curr_def = NULL;
279 Dlg_head *def_dlg;
280 WListbox *def_list;
281 int def_dlg_h; /* dialog height */
282 int def_dlg_w; /* dialog width */
283 char *label_def = NULL;
285 (void) word_len;
286 /* calculate the dialog metrics */
287 def_dlg_h = num_lines + 2;
288 def_dlg_w = max_len + 4;
289 start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
290 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
291 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
293 if (start_x < 0)
294 start_x = 0;
295 if (def_dlg_w > COLS)
296 def_dlg_w = COLS;
297 if (def_dlg_h > LINES - 2)
298 def_dlg_h = LINES - 2;
300 offset = start_x + def_dlg_w - COLS;
301 if (offset > 0)
302 start_x -= offset;
303 offset = start_y + def_dlg_h - LINES;
304 if (offset > 0)
305 start_y -= (offset + 1);
307 /* create the dialog */
308 def_dlg = create_dlg (start_y, start_x, def_dlg_h, def_dlg_w,
309 dialog_colors, NULL, "[Definitions]", match_expr, DLG_COMPACT);
311 /* create the listbox */
312 def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, NULL);
314 /* add the dialog */
315 add_widget (def_dlg, def_list);
318 /* fill the listbox with the completions */
319 for (i = 0; i < num_lines; i++) {
320 label_def =
321 g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
322 def_hash[i].line);
323 listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
324 g_free (label_def);
326 /* pop up the dialog */
327 run_dlg (def_dlg);
329 /* apply the choosen completion */
330 if (def_dlg->ret_value == B_ENTER) {
331 char *tmp_curr_def = (char *) curr_def;
332 int do_moveto = 0;
334 listbox_get_current (def_list, &curr, &tmp_curr_def);
335 curr_def = (etags_hash_t *) tmp_curr_def;
336 if (edit->modified) {
337 if (!edit_query_dialog2
338 (_("Warning"),
339 _(" Current text was modified without a file save. \n"
340 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
341 edit->force |= REDRAW_COMPLETELY;
342 do_moveto = 1;
344 } else {
345 do_moveto = 1;
347 if (curr && do_moveto) {
348 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO) {
349 g_free (edit_history_moveto[edit_stack_iterator].filename);
350 if (edit->dir) {
351 edit_history_moveto[edit_stack_iterator].filename =
352 g_strdup_printf ("%s/%s", edit->dir, edit->filename);
353 } else {
354 edit_history_moveto[edit_stack_iterator].filename = g_strdup (edit->filename);
356 edit_history_moveto[edit_stack_iterator].line = edit->start_line +
357 edit->curs_row + 1;
358 edit_stack_iterator++;
359 g_free (edit_history_moveto[edit_stack_iterator].filename);
360 edit_history_moveto[edit_stack_iterator].filename =
361 g_strdup ((char *) curr_def->fullpath);
362 edit_history_moveto[edit_stack_iterator].line = curr_def->line;
363 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
364 edit_history_moveto[edit_stack_iterator].line);
369 /* clear definition hash */
370 for (i = 0; i < MAX_DEFINITIONS; i++) {
371 g_free (def_hash[i].filename);
374 /* destroy dialog before return */
375 destroy_dlg (def_dlg);
378 /* --------------------------------------------------------------------------------------------- */
381 editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos, int ypos)
383 /* dialog sizes */
384 int dlg_height = 9;
385 int dlg_width = 8;
387 int retval;
388 int i;
389 int btn_pos;
391 char *repl_from, *repl_to;
392 char tmp [BUF_MEDIUM];
394 QuickWidget quick_widgets[] =
396 /* 0 */ QUICK_BUTTON (44, dlg_width, 6, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
397 /* 1 */ QUICK_BUTTON (29, dlg_width, 6, dlg_height, N_("&Skip"), B_SKIP_REPLACE, NULL),
398 /* 2 */ QUICK_BUTTON (21, dlg_width, 6, dlg_height, N_("A&ll"), B_REPLACE_ALL, NULL),
399 /* 3 */ QUICK_BUTTON ( 4, dlg_width, 6, dlg_height, N_("&Replace"), B_ENTER, NULL),
400 /* 4 */ QUICK_LABEL ( 3, dlg_width, 2, dlg_height, NULL),
401 /* 5 */ QUICK_LABEL ( 2, dlg_width, 3, dlg_height, N_(" Replace with: ")),
402 /* 6 */ QUICK_LABEL ( 3, dlg_width, 4, dlg_height, NULL),
403 QUICK_END
406 #ifdef ENABLE_NLS
407 for (i = 0; i < 4; i++)
408 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
409 #endif
411 /* calculate button positions */
412 btn_pos = 4;
414 for (i = 3; i > -1; i--) {
415 quick_widgets[i].relative_x = btn_pos;
416 btn_pos += str_term_width1 (quick_widgets[i].u.button.text) + 5;
417 if (i == 3) /* default button */
418 btn_pos += 2;
421 dlg_width = btn_pos + 2;
423 /* correct widget coordinates */
424 for (i = 0; i < 7; i++)
425 quick_widgets[i].x_divisions = dlg_width;
427 g_snprintf (tmp, sizeof (tmp), " '%s'", from_text);
428 repl_from = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
430 g_snprintf (tmp, sizeof (tmp), " '%s'", to_text);
431 repl_to = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
433 quick_widgets[4].u.label.text = repl_from;
434 quick_widgets[6].u.label.text = repl_to;
436 if (xpos == -1)
437 xpos = (edit->num_widget_columns - dlg_width) / 2;
439 if (ypos == -1)
440 ypos = edit->num_widget_lines * 2 / 3;
443 QuickDialog Quick_input =
445 dlg_width, dlg_height, 0, 0, N_ (" Confirm replace "),
446 "[Input Line Keys]", quick_widgets, FALSE
449 /* Sometimes menu can hide replaced text. I don't like it */
450 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
451 ypos -= dlg_height;
453 Quick_input.ypos = ypos;
454 Quick_input.xpos = xpos;
456 retval = quick_dialog (&Quick_input);
457 g_free (repl_from);
458 g_free (repl_to);
460 return retval;
464 /* --------------------------------------------------------------------------------------------- */