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 (2, 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 (2, 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
, 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 (2, 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
, 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 struct Dlg_head
*raw_dlg
=
316 create_dlg (0, 0, 7, w
, dialog_colors
, editcmd_dialog_raw_key_query_cb
,
317 NULL
, heading
, DLG_CENTER
| DLG_TRYUP
| DLG_WANT_TAB
);
319 input_new (3 - cancel
, w
- 5, INPUT_COLOR
, 2, "", 0, INPUT_COMPLETE_DEFAULT
));
320 add_widget (raw_dlg
, label_new (3 - cancel
, 2, query
));
322 add_widget (raw_dlg
, button_new (4, w
/ 2 - 5, B_CANCEL
, NORMAL_BUTTON
, _("Cancel"), 0));
323 w
= run_dlg (raw_dlg
);
324 destroy_dlg (raw_dlg
);
327 if (w
== XCTRL ('g') || w
== XCTRL ('c') || w
== ESC_CHAR
|| w
== B_CANCEL
)
334 /* --------------------------------------------------------------------------------------------- */
336 /* let the user select its preferred completion */
338 editcmd_dialog_completion_show (WEdit
* edit
, int max_len
, int word_len
,
339 struct selection
*compl, int num_compl
)
342 int start_x
, start_y
, offset
, i
;
345 WListbox
*compl_list
;
346 int compl_dlg_h
; /* completion dialog height */
347 int compl_dlg_w
; /* completion dialog width */
349 /* calculate the dialog metrics */
350 compl_dlg_h
= num_compl
+ 2;
351 compl_dlg_w
= max_len
+ 4;
352 start_x
= edit
->curs_col
+ edit
->start_col
- (compl_dlg_w
/ 2) +
353 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
354 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
358 if (compl_dlg_w
> COLS
)
360 if (compl_dlg_h
> LINES
- 2)
361 compl_dlg_h
= LINES
- 2;
363 offset
= start_x
+ compl_dlg_w
- COLS
;
366 offset
= start_y
+ compl_dlg_h
- LINES
;
368 start_y
-= (offset
+ 1);
370 /* create the dialog */
372 create_dlg (start_y
, start_x
, compl_dlg_h
, compl_dlg_w
,
373 dialog_colors
, NULL
, "[Completion]", NULL
, DLG_COMPACT
);
375 /* create the listbox */
376 compl_list
= listbox_new (1, 1, compl_dlg_h
- 2, compl_dlg_w
- 2, FALSE
, NULL
);
379 add_widget (compl_dlg
, compl_list
);
381 /* fill the listbox with the completions */
382 for (i
= num_compl
- 1; i
>= 0; i
--) /* reverse order */
383 listbox_add_item (compl_list
, LISTBOX_APPEND_AT_END
, 0, (char *) compl[i
].text
, NULL
);
385 /* pop up the dialog and apply the choosen completion */
386 if (run_dlg (compl_dlg
) == B_ENTER
)
388 listbox_get_current (compl_list
, &curr
, NULL
);
392 GString
*temp
, *temp2
;
393 temp
= g_string_new ("");
394 for (curr
+= word_len
; *curr
; curr
++)
395 g_string_append_c (temp
, *curr
);
397 temp2
= str_convert_to_input (temp
->str
);
399 if (temp2
&& temp2
->len
)
401 g_string_free (temp
, TRUE
);
405 g_string_free (temp2
, TRUE
);
406 for (curr
= temp
->str
; *curr
; curr
++)
407 edit_insert (edit
, *curr
);
408 g_string_free (temp
, TRUE
);
410 for (curr
+= word_len
; *curr
; curr
++)
411 edit_insert (edit
, *curr
);
416 /* destroy dialog before return */
417 destroy_dlg (compl_dlg
);
420 /* --------------------------------------------------------------------------------------------- */
422 /* let the user select where function definition */
424 editcmd_dialog_select_definition_show (WEdit
* edit
, char *match_expr
, int max_len
, int word_len
,
425 etags_hash_t
* def_hash
, int num_lines
)
428 int start_x
, start_y
, offset
, i
;
430 etags_hash_t
*curr_def
= NULL
;
433 int def_dlg_h
; /* dialog height */
434 int def_dlg_w
; /* dialog width */
435 char *label_def
= NULL
;
438 /* calculate the dialog metrics */
439 def_dlg_h
= num_lines
+ 2;
440 def_dlg_w
= max_len
+ 4;
441 start_x
= edit
->curs_col
+ edit
->start_col
- (def_dlg_w
/ 2) +
442 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
443 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
447 if (def_dlg_w
> COLS
)
449 if (def_dlg_h
> LINES
- 2)
450 def_dlg_h
= LINES
- 2;
452 offset
= start_x
+ def_dlg_w
- COLS
;
455 offset
= start_y
+ def_dlg_h
- LINES
;
457 start_y
-= (offset
+ 1);
459 /* create the dialog */
460 def_dlg
= create_dlg (start_y
, start_x
, def_dlg_h
, def_dlg_w
,
461 dialog_colors
, NULL
, "[Definitions]", match_expr
, DLG_COMPACT
);
463 /* create the listbox */
464 def_list
= listbox_new (1, 1, def_dlg_h
- 2, def_dlg_w
- 2, FALSE
, NULL
);
467 add_widget (def_dlg
, def_list
);
469 /* fill the listbox with the completions */
470 for (i
= 0; i
< num_lines
; i
++)
473 g_strdup_printf ("%s -> %s:%ld", def_hash
[i
].short_define
, def_hash
[i
].filename
,
475 listbox_add_item (def_list
, LISTBOX_APPEND_AT_END
, 0, label_def
, &def_hash
[i
]);
479 /* pop up the dialog and apply the choosen completion */
480 if (run_dlg (def_dlg
) == B_ENTER
)
482 char *tmp_curr_def
= (char *) curr_def
;
485 listbox_get_current (def_list
, &curr
, (void **) &tmp_curr_def
);
486 curr_def
= (etags_hash_t
*) tmp_curr_def
;
489 if (!edit_query_dialog2
491 _(" Current text was modified without a file save. \n"
492 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel")))
494 edit
->force
|= REDRAW_COMPLETELY
;
503 if (curr
&& do_moveto
)
505 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
507 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
510 edit_history_moveto
[edit_stack_iterator
].filename
=
511 g_strdup_printf ("%s/%s", edit
->dir
, edit
->filename
);
515 edit_history_moveto
[edit_stack_iterator
].filename
= g_strdup (edit
->filename
);
517 edit_history_moveto
[edit_stack_iterator
].line
= edit
->start_line
+
519 edit_stack_iterator
++;
520 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
521 edit_history_moveto
[edit_stack_iterator
].filename
=
522 g_strdup ((char *) curr_def
->fullpath
);
523 edit_history_moveto
[edit_stack_iterator
].line
= curr_def
->line
;
524 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
525 edit_history_moveto
[edit_stack_iterator
].line
);
530 /* clear definition hash */
531 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
533 g_free (def_hash
[i
].filename
);
536 /* destroy dialog before return */
537 destroy_dlg (def_dlg
);
540 /* --------------------------------------------------------------------------------------------- */
543 editcmd_dialog_replace_prompt_show (WEdit
* edit
, char *from_text
, char *to_text
, int xpos
,
554 char *repl_from
, *repl_to
;
555 char tmp
[BUF_MEDIUM
];
557 QuickWidget quick_widgets
[] = {
558 /* 0 */ QUICK_BUTTON (44, dlg_width
, 6, dlg_height
, N_("&Cancel"), B_CANCEL
, NULL
),
559 /* 1 */ QUICK_BUTTON (29, dlg_width
, 6, dlg_height
, N_("&Skip"), B_SKIP_REPLACE
, NULL
),
560 /* 2 */ QUICK_BUTTON (21, dlg_width
, 6, dlg_height
, N_("A&ll"), B_REPLACE_ALL
, NULL
),
561 /* 3 */ QUICK_BUTTON (4, dlg_width
, 6, dlg_height
, N_("&Replace"), B_ENTER
, NULL
),
562 /* 4 */ QUICK_LABEL (3, dlg_width
, 2, dlg_height
, NULL
),
563 /* 5 */ QUICK_LABEL (2, dlg_width
, 3, dlg_height
, N_(" Replace with: ")),
564 /* 6 */ QUICK_LABEL (3, dlg_width
, 4, dlg_height
, NULL
),
569 for (i
= 0; i
< 4; i
++)
570 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
573 /* calculate button positions */
576 for (i
= 3; i
> -1; i
--)
578 quick_widgets
[i
].relative_x
= btn_pos
;
579 btn_pos
+= str_term_width1 (quick_widgets
[i
].u
.button
.text
) + 5;
580 if (i
== 3) /* default button */
584 dlg_width
= btn_pos
+ 2;
586 /* correct widget coordinates */
587 for (i
= 0; i
< 7; i
++)
588 quick_widgets
[i
].x_divisions
= dlg_width
;
590 g_snprintf (tmp
, sizeof (tmp
), " '%s'", from_text
);
591 repl_from
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
593 g_snprintf (tmp
, sizeof (tmp
), " '%s'", to_text
);
594 repl_to
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
596 quick_widgets
[4].u
.label
.text
= repl_from
;
597 quick_widgets
[6].u
.label
.text
= repl_to
;
600 xpos
= (edit
->num_widget_columns
- dlg_width
) / 2;
603 ypos
= edit
->num_widget_lines
* 2 / 3;
606 QuickDialog Quick_input
= {
607 dlg_width
, dlg_height
, 0, 0, N_(" Confirm replace "),
608 "[Input Line Keys]", quick_widgets
, FALSE
611 /* Sometimes menu can hide replaced text. I don't like it */
612 if ((edit
->curs_row
>= ypos
- 1) && (edit
->curs_row
<= ypos
+ dlg_height
- 1))
615 Quick_input
.ypos
= ypos
;
616 Quick_input
.xpos
= xpos
;
618 retval
= quick_dialog (&Quick_input
);
626 /* --------------------------------------------------------------------------------------------- */