2 Editor dialogs for high level editing commands
4 Copyright (C) 2009 The Free Software Foundation, Inc.
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,
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"
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 edit_search_options_t edit_search_options
= {
51 .type
= MC_SEARCH_T_NORMAL
,
54 .only_in_selection
= FALSE
,
56 .all_codepages
= FALSE
59 /*** file scope macro definitions **************************************/
61 #define SEARCH_DLG_WIDTH 58
62 #define SEARCH_DLG_MIN_HEIGHT 13
63 #define SEARCH_DLG_HEIGHT_SUPPLY 3
65 #define REPLACE_DLG_WIDTH 58
66 #define REPLACE_DLG_MIN_HEIGHT 17
67 #define REPLACE_DLG_HEIGHT_SUPPLY 5
69 /*** file scope type declarations **************************************/
71 /*** file scope variables **********************************************/
73 /*** file scope functions **********************************************/
76 editcmd_dialog_raw_key_query_cb (struct Dlg_head
*h
, Widget
* sender
,
77 dlg_msg_t msg
, int parm
, void *data
)
86 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
90 /*** public functions **************************************************/
93 editcmd_dialog_replace_show (WEdit
* edit
, const char *search_default
, const char *replace_default
,
94 /*@out@ */ char **search_text
, /*@out@ */ char **replace_text
)
96 if ((search_default
== NULL
) || (*search_default
== '\0'))
97 search_default
= INPUT_LAST_TEXT
;
101 gchar
**list_of_types
= mc_search_get_types_strings_array (&num_of_types
);
102 int REPLACE_DLG_HEIGHT
= REPLACE_DLG_MIN_HEIGHT
+ num_of_types
- REPLACE_DLG_HEIGHT_SUPPLY
;
104 QuickWidget quick_widgets
[] = {
105 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
106 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
108 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 11, REPLACE_DLG_HEIGHT
,
110 &edit_search_options
.all_codepages
),
112 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 10, REPLACE_DLG_HEIGHT
,
114 &edit_search_options
.whole_words
),
115 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 9, REPLACE_DLG_HEIGHT
,
117 &edit_search_options
.only_in_selection
),
118 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 8, REPLACE_DLG_HEIGHT
, N_("&Backwards"),
119 &edit_search_options
.backwards
),
120 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 7, REPLACE_DLG_HEIGHT
,
121 N_("Case &sensitive"),
122 &edit_search_options
.case_sens
),
123 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH
, 7, REPLACE_DLG_HEIGHT
,
124 num_of_types
, (const char **) list_of_types
,
125 (int *) &edit_search_options
.type
),
126 /* 8 */ QUICK_LABEL (3, REPLACE_DLG_WIDTH
, 4, REPLACE_DLG_HEIGHT
,
127 N_("Enter replacement string:")),
128 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH
, 5, REPLACE_DLG_HEIGHT
,
129 replace_default
, REPLACE_DLG_WIDTH
- 6, 0, "replace",
131 /* 10 */ QUICK_LABEL (3, REPLACE_DLG_WIDTH
, 2, REPLACE_DLG_HEIGHT
,
132 N_("Enter search string:")),
133 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH
, 3, REPLACE_DLG_HEIGHT
,
134 search_default
, REPLACE_DLG_WIDTH
- 6, 0,
135 MC_HISTORY_SHARED_SEARCH
, search_text
),
139 QuickDialog Quick_input
= {
140 REPLACE_DLG_WIDTH
, REPLACE_DLG_HEIGHT
, -1, -1, N_("Replace"),
141 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
144 if (quick_dialog (&Quick_input
) != B_CANCEL
)
146 edit
->replace_mode
= 0;
150 *replace_text
= NULL
;
154 g_strfreev (list_of_types
);
158 /* --------------------------------------------------------------------------------------------- */
161 editcmd_dialog_search_show (WEdit
* edit
, char **search_text
)
165 if (*search_text
== NULL
)
166 *search_text
= INPUT_LAST_TEXT
;
170 gchar
**list_of_types
= mc_search_get_types_strings_array (&num_of_types
);
171 int SEARCH_DLG_HEIGHT
= SEARCH_DLG_MIN_HEIGHT
+ num_of_types
- SEARCH_DLG_HEIGHT_SUPPLY
;
176 QuickWidget quick_widgets
[] = {
178 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
180 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Find all"), B_USER
, NULL
),
182 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
185 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 9, SEARCH_DLG_HEIGHT
, N_("All charsets"),
186 &edit_search_options
.all_codepages
),
189 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 8, SEARCH_DLG_HEIGHT
, N_("&Whole words"),
190 &edit_search_options
.whole_words
),
192 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 7, SEARCH_DLG_HEIGHT
, N_("In se&lection"),
193 &edit_search_options
.only_in_selection
),
195 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 6, SEARCH_DLG_HEIGHT
, N_("&Backwards"),
196 &edit_search_options
.backwards
),
198 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
, N_("Case &sensitive"),
199 &edit_search_options
.case_sens
),
201 QUICK_RADIO (3, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
,
202 num_of_types
, (const char **) list_of_types
,
203 (int *) &edit_search_options
.type
),
205 QUICK_INPUT (3, SEARCH_DLG_WIDTH
, 3, SEARCH_DLG_HEIGHT
,
206 *search_text
, SEARCH_DLG_WIDTH
- 6, 0,
207 MC_HISTORY_SHARED_SEARCH
, search_text
),
209 QUICK_LABEL (3, SEARCH_DLG_WIDTH
, 2, SEARCH_DLG_HEIGHT
, N_("Enter search string:")),
214 size_t last_checkbox
= 7;
216 size_t last_checkbox
= 6;
219 QuickDialog Quick_input
= {
220 SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
, -1, -1, N_("Search"),
221 "[Input Line Keys]", quick_widgets
, NULL
, TRUE
225 char **list_of_types_nls
;
228 Quick_input
.title
= _(Quick_input
.title
);
230 for (i
= 0; i
< 3; i
++)
231 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
233 for (i
= 3; i
<= last_checkbox
; i
++)
234 quick_widgets
[i
].u
.checkbox
.text
= _(quick_widgets
[i
].u
.checkbox
.text
);
236 quick_widgets
[10].u
.label
.text
= _(quick_widgets
[10].u
.label
.text
);
239 /* create copy of radio items to avoid memory leak */
240 list_of_types_nls
= g_new0 (char *, num_of_types
+ 1);
241 for (i
= 0; i
< num_of_types
; i
++)
242 list_of_types_nls
[i
] = g_strdup (_(list_of_types
[i
]));
243 g_strfreev (list_of_types
);
244 list_of_types
= list_of_types_nls
;
245 quick_widgets
[last_checkbox
+ 1].u
.radio
.items
= (const char **) list_of_types
;
248 /* calculate widget coordinates */
252 gchar
**radio
= list_of_types
;
253 int b0_len
, b1_len
, b2_len
;
254 const int button_gap
= 2;
256 /* length of radiobuttons */
257 while (*radio
!= NULL
)
259 len
= max (len
, str_term_width1 (*radio
));
262 /* length of checkboxes */
263 for (i
= 3; i
<= last_checkbox
; i
++)
264 len
= max (len
, str_term_width1 (quick_widgets
[i
].u
.checkbox
.text
) + 4);
266 /* preliminary dialog width */
267 dlg_width
= max (len
* 2, str_term_width1 (Quick_input
.title
)) + 4;
269 /* length of buttons */
270 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
271 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 3;
272 b2_len
= str_term_width1 (quick_widgets
[2].u
.button
.text
) + 5; /* default button */
273 len
= b0_len
+ b1_len
+ b2_len
+ button_gap
* 2;
276 Quick_input
.xlen
= max (SEARCH_DLG_WIDTH
, max (dlg_width
, len
+ 6));
278 /* correct widget coordinates */
279 for (i
= 0; i
< sizeof (quick_widgets
) / sizeof (quick_widgets
[0]); i
++)
280 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
282 /* checkbox positions */
283 for (i
= 3; i
<= last_checkbox
; i
++)
284 quick_widgets
[i
].relative_x
= Quick_input
.xlen
/ 2 + 2;
286 quick_widgets
[last_checkbox
+ 2].u
.input
.len
= Quick_input
.xlen
- 6;
287 /* button positions */
288 quick_widgets
[2].relative_x
= Quick_input
.xlen
/ 2 - len
/ 2;
289 quick_widgets
[1].relative_x
= quick_widgets
[2].relative_x
+ b2_len
+ button_gap
;
290 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ button_gap
;
293 dialog_result
= quick_dialog (&Quick_input
);
295 g_strfreev (list_of_types
);
297 if (dialog_result
== B_CANCEL
)
299 else if (dialog_result
== B_USER
)
300 search_create_bookmark
= 1;
304 /* --------------------------------------------------------------------------------------------- */
306 /* gets a raw key from the keyboard. Passing cancel = 1 draws
307 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
308 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
309 and Esc are cannot returned */
312 editcmd_dialog_raw_key_query (const char *heading
, const char *query
, int cancel
)
314 int w
= str_term_width1 (query
) + 7;
315 const int input_colors
[3] =
318 INPUT_UNCHANGED_COLOR
,
322 struct Dlg_head
*raw_dlg
=
323 create_dlg (TRUE
, 0, 0, 7, w
, dialog_colors
, editcmd_dialog_raw_key_query_cb
,
324 NULL
, heading
, DLG_CENTER
| DLG_TRYUP
| DLG_WANT_TAB
);
325 add_widget (raw_dlg
, input_new (3 - cancel
, w
- 5, (int *) input_colors
,
326 2, "", 0, INPUT_COMPLETE_DEFAULT
));
327 add_widget (raw_dlg
, label_new (3 - cancel
, 2, query
));
329 add_widget (raw_dlg
, button_new (4, w
/ 2 - 5, B_CANCEL
, NORMAL_BUTTON
, _("Cancel"), 0));
330 w
= run_dlg (raw_dlg
);
331 destroy_dlg (raw_dlg
);
334 if (w
== XCTRL ('g') || w
== XCTRL ('c') || w
== ESC_CHAR
|| w
== B_CANCEL
)
341 /* --------------------------------------------------------------------------------------------- */
343 /* let the user select its preferred completion */
345 editcmd_dialog_completion_show (WEdit
* edit
, int max_len
, int word_len
,
346 struct selection
*compl, int num_compl
)
349 int start_x
, start_y
, offset
, i
;
352 WListbox
*compl_list
;
353 int compl_dlg_h
; /* completion dialog height */
354 int compl_dlg_w
; /* completion dialog width */
356 /* calculate the dialog metrics */
357 compl_dlg_h
= num_compl
+ 2;
358 compl_dlg_w
= max_len
+ 4;
359 start_x
= edit
->curs_col
+ edit
->start_col
- (compl_dlg_w
/ 2) +
360 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
361 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
365 if (compl_dlg_w
> COLS
)
367 if (compl_dlg_h
> LINES
- 2)
368 compl_dlg_h
= LINES
- 2;
370 offset
= start_x
+ compl_dlg_w
- COLS
;
373 offset
= start_y
+ compl_dlg_h
- LINES
;
375 start_y
-= (offset
+ 1);
377 /* create the dialog */
379 create_dlg (TRUE
, start_y
, start_x
, compl_dlg_h
, compl_dlg_w
,
380 dialog_colors
, NULL
, "[Completion]", NULL
, DLG_COMPACT
);
382 /* create the listbox */
383 compl_list
= listbox_new (1, 1, compl_dlg_h
- 2, compl_dlg_w
- 2, FALSE
, NULL
);
386 add_widget (compl_dlg
, compl_list
);
388 /* fill the listbox with the completions */
389 for (i
= num_compl
- 1; i
>= 0; i
--) /* reverse order */
390 listbox_add_item (compl_list
, LISTBOX_APPEND_AT_END
, 0, (char *) compl[i
].text
, NULL
);
392 /* pop up the dialog and apply the choosen completion */
393 if (run_dlg (compl_dlg
) == B_ENTER
)
395 listbox_get_current (compl_list
, &curr
, NULL
);
399 GString
*temp
, *temp2
;
400 temp
= g_string_new ("");
401 for (curr
+= word_len
; *curr
; curr
++)
402 g_string_append_c (temp
, *curr
);
404 temp2
= str_convert_to_input (temp
->str
);
406 if (temp2
&& temp2
->len
)
408 g_string_free (temp
, TRUE
);
412 g_string_free (temp2
, TRUE
);
413 for (curr
= temp
->str
; *curr
; curr
++)
414 edit_insert (edit
, *curr
);
415 g_string_free (temp
, TRUE
);
417 for (curr
+= word_len
; *curr
; curr
++)
418 edit_insert (edit
, *curr
);
423 /* destroy dialog before return */
424 destroy_dlg (compl_dlg
);
427 /* --------------------------------------------------------------------------------------------- */
429 /* let the user select where function definition */
431 editcmd_dialog_select_definition_show (WEdit
* edit
, char *match_expr
, int max_len
, int word_len
,
432 etags_hash_t
* def_hash
, int num_lines
)
435 int start_x
, start_y
, offset
, i
;
437 etags_hash_t
*curr_def
= NULL
;
440 int def_dlg_h
; /* dialog height */
441 int def_dlg_w
; /* dialog width */
442 char *label_def
= NULL
;
445 /* calculate the dialog metrics */
446 def_dlg_h
= num_lines
+ 2;
447 def_dlg_w
= max_len
+ 4;
448 start_x
= edit
->curs_col
+ edit
->start_col
- (def_dlg_w
/ 2) +
449 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
450 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
454 if (def_dlg_w
> COLS
)
456 if (def_dlg_h
> LINES
- 2)
457 def_dlg_h
= LINES
- 2;
459 offset
= start_x
+ def_dlg_w
- COLS
;
462 offset
= start_y
+ def_dlg_h
- LINES
;
464 start_y
-= (offset
+ 1);
466 /* create the dialog */
467 def_dlg
= create_dlg (TRUE
, start_y
, start_x
, def_dlg_h
, def_dlg_w
,
468 dialog_colors
, NULL
, "[Definitions]", match_expr
, DLG_COMPACT
);
470 /* create the listbox */
471 def_list
= listbox_new (1, 1, def_dlg_h
- 2, def_dlg_w
- 2, FALSE
, NULL
);
474 add_widget (def_dlg
, def_list
);
476 /* fill the listbox with the completions */
477 for (i
= 0; i
< num_lines
; i
++)
480 g_strdup_printf ("%s -> %s:%ld", def_hash
[i
].short_define
, def_hash
[i
].filename
,
482 listbox_add_item (def_list
, LISTBOX_APPEND_AT_END
, 0, label_def
, &def_hash
[i
]);
486 /* pop up the dialog and apply the choosen completion */
487 if (run_dlg (def_dlg
) == B_ENTER
)
489 char *tmp_curr_def
= (char *) curr_def
;
492 listbox_get_current (def_list
, &curr
, (void **) &tmp_curr_def
);
493 curr_def
= (etags_hash_t
*) tmp_curr_def
;
496 if (!edit_query_dialog2
498 _("Current text was modified without a file save.\n"
499 "Continue discards these changes."), _("C&ontinue"), _("&Cancel")))
501 edit
->force
|= REDRAW_COMPLETELY
;
510 if (curr
&& do_moveto
)
512 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
514 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
517 edit_history_moveto
[edit_stack_iterator
].filename
=
518 g_strdup_printf ("%s/%s", edit
->dir
, edit
->filename
);
522 edit_history_moveto
[edit_stack_iterator
].filename
= g_strdup (edit
->filename
);
524 edit_history_moveto
[edit_stack_iterator
].line
= edit
->start_line
+
526 edit_stack_iterator
++;
527 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
528 edit_history_moveto
[edit_stack_iterator
].filename
=
529 g_strdup ((char *) curr_def
->fullpath
);
530 edit_history_moveto
[edit_stack_iterator
].line
= curr_def
->line
;
531 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
532 edit_history_moveto
[edit_stack_iterator
].line
);
537 /* clear definition hash */
538 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
540 g_free (def_hash
[i
].filename
);
543 /* destroy dialog before return */
544 destroy_dlg (def_dlg
);
547 /* --------------------------------------------------------------------------------------------- */
550 editcmd_dialog_replace_prompt_show (WEdit
* edit
, char *from_text
, char *to_text
, int xpos
,
561 char *repl_from
, *repl_to
;
562 char tmp
[BUF_MEDIUM
];
564 QuickWidget quick_widgets
[] = {
565 /* 0 */ QUICK_BUTTON (44, dlg_width
, 6, dlg_height
, N_("&Cancel"), B_CANCEL
, NULL
),
566 /* 1 */ QUICK_BUTTON (29, dlg_width
, 6, dlg_height
, N_("&Skip"), B_SKIP_REPLACE
, NULL
),
567 /* 2 */ QUICK_BUTTON (21, dlg_width
, 6, dlg_height
, N_("A&ll"), B_REPLACE_ALL
, NULL
),
568 /* 3 */ QUICK_BUTTON (4, dlg_width
, 6, dlg_height
, N_("&Replace"), B_ENTER
, NULL
),
569 /* 4 */ QUICK_LABEL (3, dlg_width
, 2, dlg_height
, NULL
),
570 /* 5 */ QUICK_LABEL (3, dlg_width
, 3, dlg_height
, N_("Replace with:")),
571 /* 6 */ QUICK_LABEL (3, dlg_width
, 4, dlg_height
, NULL
),
576 for (i
= 0; i
< 4; i
++)
577 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
580 /* calculate button positions */
583 for (i
= 3; i
> -1; i
--)
585 quick_widgets
[i
].relative_x
= btn_pos
;
586 btn_pos
+= str_term_width1 (quick_widgets
[i
].u
.button
.text
) + 5;
587 if (i
== 3) /* default button */
591 dlg_width
= btn_pos
+ 2;
593 /* correct widget coordinates */
594 for (i
= 0; i
< 7; i
++)
595 quick_widgets
[i
].x_divisions
= dlg_width
;
597 g_snprintf (tmp
, sizeof (tmp
), "\"%s\"", from_text
);
598 repl_from
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
600 g_snprintf (tmp
, sizeof (tmp
), "\"%s\"", to_text
);
601 repl_to
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
603 quick_widgets
[4].u
.label
.text
= repl_from
;
604 quick_widgets
[6].u
.label
.text
= repl_to
;
607 xpos
= (edit
->num_widget_columns
- dlg_width
) / 2;
610 ypos
= edit
->num_widget_lines
* 2 / 3;
613 QuickDialog Quick_input
= {
614 dlg_width
, dlg_height
, 0, 0, N_("Confirm replace"),
615 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
618 /* Sometimes menu can hide replaced text. I don't like it */
619 if ((edit
->curs_row
>= ypos
- 1) && (edit
->curs_row
<= ypos
+ dlg_height
- 1))
622 Quick_input
.ypos
= ypos
;
623 Quick_input
.xpos
= xpos
;
625 retval
= quick_dialog (&Quick_input
);
633 /* --------------------------------------------------------------------------------------------- */