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 "../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 **********************************************/
69 editcmd_dialog_raw_key_query_cb (struct Dlg_head
*h
, dlg_msg_t msg
, int parm
)
77 return default_dlg_callback (h
, msg
, parm
);
81 /*** public functions **************************************************/
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
),
99 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 11, REPLACE_DLG_HEIGHT
, N_("All charsets"), &edit
->all_codepages
),
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
),
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;
125 *replace_text
= NULL
;
129 g_strfreev (list_of_types
);
133 /* --------------------------------------------------------------------------------------------- */
136 editcmd_dialog_search_show (WEdit
* edit
, char **search_text
)
139 if (*search_text
== '\0')
140 *search_text
= INPUT_LAST_TEXT
;
143 gchar
**list_of_types
= mc_search_get_types_strings_array();
144 int SEARCH_DLG_HEIGHT
= SEARCH_DLG_MIN_HEIGHT
+ g_strv_length (list_of_types
) - SEARCH_DLG_HEIGHT_SUPPLY
;
147 int btn_pos
, dlg_width
;
150 QuickWidget quick_widgets
[] =
153 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
155 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Find all"), B_USER
, NULL
),
157 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
160 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 9, SEARCH_DLG_HEIGHT
, N_("All charsets"), &edit
->all_codepages
),
163 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 8, SEARCH_DLG_HEIGHT
, N_("&Whole words"), &edit
->whole_words
),
165 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 7, SEARCH_DLG_HEIGHT
, N_("In se&lection"), &edit
->only_in_selection
),
167 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 6, SEARCH_DLG_HEIGHT
, N_("&Backwards"), &edit
->replace_backwards
),
169 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
, N_("case &Sensitive"), &edit
->replace_case
),
171 QUICK_RADIO ( 3, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
,
172 g_strv_length (list_of_types
), (const char **) list_of_types
, &edit
->search_type
),
174 QUICK_INPUT (3, SEARCH_DLG_WIDTH
, 3, SEARCH_DLG_HEIGHT
,
175 *search_text
, SEARCH_DLG_WIDTH
- 6, 0, MC_HISTORY_SHARED_SEARCH
, search_text
),
177 QUICK_LABEL (2, SEARCH_DLG_WIDTH
, 2, SEARCH_DLG_HEIGHT
, N_(" Enter search string:")),
181 QuickDialog Quick_input
=
183 SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
, -1, -1, N_("Search"),
184 "[Input Line Keys]", quick_widgets
, TRUE
190 for (i
= 0; i
< 3; i
++)
191 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
193 for (i
= 3; i
< 8; i
++)
194 quick_widgets
[i
].u
.checkbox
.text
= _(quick_widgets
[i
].u
.checkbox
.text
);
196 quick_widgets
[10].u
.label
.text
= _(quick_widgets
[10].u
.label
.text
);
198 /* calculate button positions */
201 for (i
= 2; i
>= 0; i
--) {
202 quick_widgets
[i
].relative_x
= btn_pos
;
203 btn_pos
+= str_term_width1 (quick_widgets
[i
].u
.button
.text
) + 7;
204 if (i
== 2) /* default button */
208 dlg_width
= btn_pos
+ 2;
210 /* correct widget coordinates */
211 for (i
= 0; i
< sizeof (quick_widgets
)/sizeof (quick_widgets
[0]); i
++)
212 quick_widgets
[i
].x_divisions
= dlg_width
;
214 dialog_result
= quick_dialog (&Quick_input
);
216 if (dialog_result
== B_CANCEL
)
218 else if (dialog_result
== B_USER
)
219 search_create_bookmark
= 1;
223 /* --------------------------------------------------------------------------------------------- */
225 /* gets a raw key from the keyboard. Passing cancel = 1 draws
226 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
227 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
228 and Esc are cannot returned */
231 editcmd_dialog_raw_key_query (const char *heading
, const char *query
, int cancel
)
233 int w
= str_term_width1 (query
) + 7;
234 struct Dlg_head
*raw_dlg
=
235 create_dlg (0, 0, 7, w
, dialog_colors
, editcmd_dialog_raw_key_query_cb
,
237 DLG_CENTER
| DLG_TRYUP
| DLG_WANT_TAB
);
239 input_new (3 - cancel
, w
- 5, INPUT_COLOR
, 2, "", 0, INPUT_COMPLETE_DEFAULT
));
240 add_widget (raw_dlg
, label_new (3 - cancel
, 2, query
));
242 add_widget (raw_dlg
, button_new (4, w
/ 2 - 5, B_CANCEL
, NORMAL_BUTTON
, _("Cancel"), 0));
243 w
= run_dlg (raw_dlg
);
244 destroy_dlg (raw_dlg
);
246 if (w
== XCTRL ('g') || w
== XCTRL ('c') || w
== ESC_CHAR
|| w
== B_CANCEL
)
253 /* --------------------------------------------------------------------------------------------- */
255 /* let the user select its preferred completion */
257 editcmd_dialog_completion_show (WEdit
* edit
, int max_len
, int word_len
,
258 struct selection
*compl, int num_compl
)
261 int start_x
, start_y
, offset
, i
;
264 WListbox
*compl_list
;
265 int compl_dlg_h
; /* completion dialog height */
266 int compl_dlg_w
; /* completion dialog width */
268 /* calculate the dialog metrics */
269 compl_dlg_h
= num_compl
+ 2;
270 compl_dlg_w
= max_len
+ 4;
271 start_x
= edit
->curs_col
+ edit
->start_col
- (compl_dlg_w
/ 2) +
272 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
273 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
277 if (compl_dlg_w
> COLS
)
279 if (compl_dlg_h
> LINES
- 2)
280 compl_dlg_h
= LINES
- 2;
282 offset
= start_x
+ compl_dlg_w
- COLS
;
285 offset
= start_y
+ compl_dlg_h
- LINES
;
287 start_y
-= (offset
+ 1);
289 /* create the dialog */
291 create_dlg (start_y
, start_x
, compl_dlg_h
, compl_dlg_w
,
292 dialog_colors
, NULL
, "[Completion]", NULL
, DLG_COMPACT
);
294 /* create the listbox */
295 compl_list
= listbox_new (1, 1, compl_dlg_h
- 2, compl_dlg_w
- 2, NULL
);
298 add_widget (compl_dlg
, compl_list
);
300 /* fill the listbox with the completions */
301 for (i
= 0; i
< num_compl
; i
++)
302 listbox_add_item (compl_list
, LISTBOX_APPEND_AT_END
, 0, (char *) compl[i
].text
, NULL
);
304 /* pop up the dialog and apply the choosen completion */
305 if (run_dlg (compl_dlg
) == B_ENTER
) {
306 listbox_get_current (compl_list
, &curr
, NULL
);
308 for (curr
+= word_len
; *curr
; curr
++)
309 edit_insert (edit
, *curr
);
312 /* destroy dialog before return */
313 destroy_dlg (compl_dlg
);
316 /* --------------------------------------------------------------------------------------------- */
318 /* let the user select where function definition */
320 editcmd_dialog_select_definition_show (WEdit
* edit
, char *match_expr
, int max_len
, int word_len
,
321 etags_hash_t
* def_hash
, int num_lines
)
324 int start_x
, start_y
, offset
, i
;
326 etags_hash_t
*curr_def
= NULL
;
329 int def_dlg_h
; /* dialog height */
330 int def_dlg_w
; /* dialog width */
331 char *label_def
= NULL
;
334 /* calculate the dialog metrics */
335 def_dlg_h
= num_lines
+ 2;
336 def_dlg_w
= max_len
+ 4;
337 start_x
= edit
->curs_col
+ edit
->start_col
- (def_dlg_w
/ 2) +
338 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
339 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
343 if (def_dlg_w
> COLS
)
345 if (def_dlg_h
> LINES
- 2)
346 def_dlg_h
= LINES
- 2;
348 offset
= start_x
+ def_dlg_w
- COLS
;
351 offset
= start_y
+ def_dlg_h
- LINES
;
353 start_y
-= (offset
+ 1);
355 /* create the dialog */
356 def_dlg
= create_dlg (start_y
, start_x
, def_dlg_h
, def_dlg_w
,
357 dialog_colors
, NULL
, "[Definitions]", match_expr
, DLG_COMPACT
);
359 /* create the listbox */
360 def_list
= listbox_new (1, 1, def_dlg_h
- 2, def_dlg_w
- 2, NULL
);
363 add_widget (def_dlg
, def_list
);
366 /* fill the listbox with the completions */
367 for (i
= 0; i
< num_lines
; i
++) {
369 g_strdup_printf ("%s -> %s:%ld", def_hash
[i
].short_define
, def_hash
[i
].filename
,
371 listbox_add_item (def_list
, LISTBOX_APPEND_AT_END
, 0, label_def
, &def_hash
[i
]);
374 /* pop up the dialog */
377 /* apply the choosen completion */
378 if (def_dlg
->ret_value
== B_ENTER
) {
379 char *tmp_curr_def
= (char *) curr_def
;
382 listbox_get_current (def_list
, &curr
, &tmp_curr_def
);
383 curr_def
= (etags_hash_t
*) tmp_curr_def
;
384 if (edit
->modified
) {
385 if (!edit_query_dialog2
387 _(" Current text was modified without a file save. \n"
388 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
389 edit
->force
|= REDRAW_COMPLETELY
;
395 if (curr
&& do_moveto
) {
396 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
) {
397 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
399 edit_history_moveto
[edit_stack_iterator
].filename
=
400 g_strdup_printf ("%s/%s", edit
->dir
, edit
->filename
);
402 edit_history_moveto
[edit_stack_iterator
].filename
= g_strdup (edit
->filename
);
404 edit_history_moveto
[edit_stack_iterator
].line
= edit
->start_line
+
406 edit_stack_iterator
++;
407 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
408 edit_history_moveto
[edit_stack_iterator
].filename
=
409 g_strdup ((char *) curr_def
->fullpath
);
410 edit_history_moveto
[edit_stack_iterator
].line
= curr_def
->line
;
411 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
412 edit_history_moveto
[edit_stack_iterator
].line
);
417 /* clear definition hash */
418 for (i
= 0; i
< MAX_DEFINITIONS
; i
++) {
419 g_free (def_hash
[i
].filename
);
422 /* destroy dialog before return */
423 destroy_dlg (def_dlg
);
426 /* --------------------------------------------------------------------------------------------- */
429 editcmd_dialog_replace_prompt_show (WEdit
* edit
, char *from_text
, char *to_text
, int xpos
, int ypos
)
439 char *repl_from
, *repl_to
;
440 char tmp
[BUF_MEDIUM
];
442 QuickWidget quick_widgets
[] =
444 /* 0 */ QUICK_BUTTON (44, dlg_width
, 6, dlg_height
, N_("&Cancel"), B_CANCEL
, NULL
),
445 /* 1 */ QUICK_BUTTON (29, dlg_width
, 6, dlg_height
, N_("&Skip"), B_SKIP_REPLACE
, NULL
),
446 /* 2 */ QUICK_BUTTON (21, dlg_width
, 6, dlg_height
, N_("A&ll"), B_REPLACE_ALL
, NULL
),
447 /* 3 */ QUICK_BUTTON ( 4, dlg_width
, 6, dlg_height
, N_("&Replace"), B_ENTER
, NULL
),
448 /* 4 */ QUICK_LABEL ( 3, dlg_width
, 2, dlg_height
, NULL
),
449 /* 5 */ QUICK_LABEL ( 2, dlg_width
, 3, dlg_height
, N_(" Replace with: ")),
450 /* 6 */ QUICK_LABEL ( 3, dlg_width
, 4, dlg_height
, NULL
),
455 for (i
= 0; i
< 4; i
++)
456 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
459 /* calculate button positions */
462 for (i
= 3; i
> -1; i
--) {
463 quick_widgets
[i
].relative_x
= btn_pos
;
464 btn_pos
+= str_term_width1 (quick_widgets
[i
].u
.button
.text
) + 5;
465 if (i
== 3) /* default button */
469 dlg_width
= btn_pos
+ 2;
471 /* correct widget coordinates */
472 for (i
= 0; i
< 7; i
++)
473 quick_widgets
[i
].x_divisions
= dlg_width
;
475 g_snprintf (tmp
, sizeof (tmp
), " '%s'", from_text
);
476 repl_from
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
478 g_snprintf (tmp
, sizeof (tmp
), " '%s'", to_text
);
479 repl_to
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
481 quick_widgets
[4].u
.label
.text
= repl_from
;
482 quick_widgets
[6].u
.label
.text
= repl_to
;
485 xpos
= (edit
->num_widget_columns
- dlg_width
) / 2;
488 ypos
= edit
->num_widget_lines
* 2 / 3;
491 QuickDialog Quick_input
=
493 dlg_width
, dlg_height
, 0, 0, N_ (" Confirm replace "),
494 "[Input Line Keys]", quick_widgets
, FALSE
497 /* Sometimes menu can hide replaced text. I don't like it */
498 if ((edit
->curs_row
>= ypos
- 1) && (edit
->curs_row
<= ypos
+ dlg_height
- 1))
501 Quick_input
.ypos
= ypos
;
502 Quick_input
.xpos
= xpos
;
504 retval
= quick_dialog (&Quick_input
);
512 /* --------------------------------------------------------------------------------------------- */