Revert "misc/mc.ext.in: added 3gp video"
[midnight-commander.git] / edit / editcmd_dialogs.c
blob751cf8d521aef477b9381c3ae4decedd4d7bfc93
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/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"
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, dlg_msg_t msg, int parm)
72 switch (msg) {
73 case DLG_KEY:
74 dlg_stop (h);
75 h->ret_value = parm;
76 return MSG_HANDLED;
77 default:
78 return default_dlg_callback (h, msg, parm);
82 /*** public functions **************************************************/
84 void
85 editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
86 /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
88 if (*search_default == '\0')
89 search_default = INPUT_LAST_TEXT;
92 size_t num_of_types;
93 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
94 int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + num_of_types - REPLACE_DLG_HEIGHT_SUPPLY;
96 QuickWidget quick_widgets[] =
98 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
99 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
100 #ifdef HAVE_CHARSET
101 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
102 #endif
103 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
104 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
105 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
106 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
107 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT,
108 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
109 /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")),
110 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT,
111 replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text),
112 /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:")),
113 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT,
114 search_default, REPLACE_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
115 QUICK_END
118 QuickDialog Quick_input =
120 REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, -1, N_(" Replace "),
121 "[Input Line Keys]", quick_widgets, FALSE
124 if (quick_dialog (&Quick_input) != B_CANCEL) {
125 edit->replace_mode = 0;
126 } else {
127 *replace_text = NULL;
128 *search_text = NULL;
131 g_strfreev (list_of_types);
135 /* --------------------------------------------------------------------------------------------- */
137 void
138 editcmd_dialog_search_show (WEdit * edit, char **search_text)
140 if (*search_text == '\0')
141 *search_text = INPUT_LAST_TEXT;
144 size_t num_of_types;
145 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
146 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
147 size_t i;
149 int dialog_result;
151 QuickWidget quick_widgets[] =
153 /* 0 */
154 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
155 /* 1 */
156 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT, N_("&Find all"), B_USER, NULL),
157 /* 2 */
158 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
159 #ifdef HAVE_CHARSET
160 /* 3 */
161 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
162 #endif
163 /* 4 */
164 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
165 /* 5 */
166 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
167 /* 6 */
168 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
169 /* 7 */
170 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
171 /* 8 */
172 QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
173 num_of_types, (const char **) list_of_types, (int *) &edit->search_type),
174 /* 9 */
175 QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
176 *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
177 /* 10 */
178 QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
179 QUICK_END
182 #ifdef HAVE_CHARSET
183 size_t last_checkbox = 7;
184 #else
185 size_t last_checkbox = 6;
186 #endif
188 QuickDialog Quick_input =
190 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"),
191 "[Input Line Keys]", quick_widgets, TRUE
194 #ifdef ENABLE_NLS
195 char **list_of_types_nls;
197 /* header title */
198 Quick_input.title = _(Quick_input.title);
199 /* buttons */
200 for (i = 0; i < 3; i++)
201 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
202 /* checkboxes */
203 for (i = 3; i <= last_checkbox; i++)
204 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
205 /* label */
206 quick_widgets[10].u.label.text = _(quick_widgets[10].u.label.text);
208 /* radiobuttons */
209 /* create copy of radio items to avoid memory leak */
210 list_of_types_nls = g_new0 (char *, num_of_types + 1);
211 for (i = 0; i < num_of_types; i++)
212 list_of_types_nls[i] = g_strdup (_(list_of_types[i]));
213 g_strfreev (list_of_types);
214 list_of_types = list_of_types_nls;
215 quick_widgets[last_checkbox + 1].u.radio.items = (const char **) list_of_types;
216 #endif
218 /* calculate widget coordinates */
220 int len = 0;
221 int dlg_width;
222 gchar **radio = list_of_types;
223 int b0_len, b1_len, b2_len;
224 const int button_gap = 2;
226 /* length of radiobuttons */
227 while (*radio != NULL) {
228 len = max (len, str_term_width1 (*radio));
229 radio++;
231 /* length of checkboxes */
232 for (i = 3; i <= last_checkbox; i++)
233 len = max (len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 4);
235 /* preliminary dialog width */
236 dlg_width = max (len * 2, str_term_width1 (Quick_input.title)) + 4;
238 /* length of buttons */
239 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
240 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 3;
241 b2_len = str_term_width1 (quick_widgets[2].u.button.text) + 5; /* default button */
242 len = b0_len + b1_len + b2_len + button_gap * 2;
244 /* dialog width */
245 Quick_input.xlen = max (SEARCH_DLG_WIDTH, max (dlg_width, len + 6));
247 /* correct widget coordinates */
248 for (i = 0; i < sizeof (quick_widgets)/sizeof (quick_widgets[0]); i++)
249 quick_widgets[i].x_divisions = Quick_input.xlen;
251 /* checkbox positions */
252 for (i = 3; i <= last_checkbox; i++)
253 quick_widgets[i].relative_x = Quick_input.xlen/2 + 2;
254 /* input length */
255 quick_widgets[last_checkbox + 2].u.input.len = Quick_input.xlen - 6;
256 /* button positions */
257 quick_widgets[2].relative_x = Quick_input.xlen/2 - len/2;
258 quick_widgets[1].relative_x = quick_widgets[2].relative_x + b2_len + button_gap;
259 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + button_gap;
262 dialog_result = quick_dialog (&Quick_input);
264 g_strfreev (list_of_types);
266 if (dialog_result == B_CANCEL)
267 *search_text = NULL;
268 else if (dialog_result == B_USER)
269 search_create_bookmark = 1;
273 /* --------------------------------------------------------------------------------------------- */
275 /* gets a raw key from the keyboard. Passing cancel = 1 draws
276 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
277 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
278 and Esc are cannot returned */
281 editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
283 int w = str_term_width1 (query) + 7;
284 struct Dlg_head *raw_dlg =
285 create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
286 NULL, heading,
287 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 /* --------------------------------------------------------------------------------------------- */