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
=
52 .type
= MC_SEARCH_T_NORMAL
,
55 .only_in_selection
= FALSE
,
57 .all_codepages
= FALSE
60 /*** file scope macro definitions **************************************/
62 #define SEARCH_DLG_WIDTH 58
63 #define SEARCH_DLG_MIN_HEIGHT 13
64 #define SEARCH_DLG_HEIGHT_SUPPLY 3
66 #define REPLACE_DLG_WIDTH 58
67 #define REPLACE_DLG_MIN_HEIGHT 17
68 #define REPLACE_DLG_HEIGHT_SUPPLY 5
70 /*** file scope type declarations **************************************/
72 /*** file scope variables **********************************************/
74 /*** file scope functions **********************************************/
77 editcmd_dialog_raw_key_query_cb (struct Dlg_head
*h
, Widget
*sender
,
78 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
== '\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
[] =
106 /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
107 /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
109 /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 11, REPLACE_DLG_HEIGHT
, N_("All charsets"),
110 &edit_search_options
.all_codepages
),
112 /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 10, REPLACE_DLG_HEIGHT
, N_("&Whole words"),
113 &edit_search_options
.whole_words
),
114 /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 9, REPLACE_DLG_HEIGHT
, N_("In se&lection"),
115 &edit_search_options
.only_in_selection
),
116 /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 8, REPLACE_DLG_HEIGHT
, N_("&Backwards"),
117 &edit_search_options
.backwards
),
118 /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH
, 7, REPLACE_DLG_HEIGHT
, N_("case &Sensitive"),
119 &edit_search_options
.case_sens
),
120 /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH
, 7, REPLACE_DLG_HEIGHT
,
121 num_of_types
, (const char **) list_of_types
,
122 (int *) &edit_search_options
.type
),
123 /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH
, 4, REPLACE_DLG_HEIGHT
, N_(" Enter replacement string:")),
124 /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH
, 5, REPLACE_DLG_HEIGHT
,
125 replace_default
, REPLACE_DLG_WIDTH
- 6, 0, "replace", replace_text
),
126 /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH
, 2, REPLACE_DLG_HEIGHT
, N_(" Enter search string:")),
127 /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH
, 3, REPLACE_DLG_HEIGHT
,
128 search_default
, REPLACE_DLG_WIDTH
- 6, 0, MC_HISTORY_SHARED_SEARCH
, search_text
),
132 QuickDialog Quick_input
=
134 REPLACE_DLG_WIDTH
, REPLACE_DLG_HEIGHT
, -1, -1, N_(" Replace "),
135 "[Input Line Keys]", quick_widgets
, FALSE
138 if (quick_dialog (&Quick_input
) != B_CANCEL
) {
139 edit
->replace_mode
= 0;
141 *replace_text
= NULL
;
145 g_strfreev (list_of_types
);
149 /* --------------------------------------------------------------------------------------------- */
152 editcmd_dialog_search_show (WEdit
* edit
, char **search_text
)
156 if (*search_text
== '\0')
157 *search_text
= INPUT_LAST_TEXT
;
161 gchar
**list_of_types
= mc_search_get_types_strings_array (&num_of_types
);
162 int SEARCH_DLG_HEIGHT
= SEARCH_DLG_MIN_HEIGHT
+ num_of_types
- SEARCH_DLG_HEIGHT_SUPPLY
;
167 QuickWidget quick_widgets
[] =
170 QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
172 QUICK_BUTTON (4, 10, 11, SEARCH_DLG_HEIGHT
, N_("&Find all"), B_USER
, NULL
),
174 QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
177 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 9, SEARCH_DLG_HEIGHT
, N_("All charsets"),
178 &edit_search_options
.all_codepages
),
181 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 8, SEARCH_DLG_HEIGHT
, N_("&Whole words"),
182 &edit_search_options
.whole_words
),
184 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 7, SEARCH_DLG_HEIGHT
, N_("In se&lection"),
185 &edit_search_options
.only_in_selection
),
187 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 6, SEARCH_DLG_HEIGHT
, N_("&Backwards"),
188 &edit_search_options
.backwards
),
190 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
, N_("case &Sensitive"),
191 &edit_search_options
.case_sens
),
193 QUICK_RADIO ( 3, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
,
194 num_of_types
, (const char **) list_of_types
,
195 (int *) &edit_search_options
.type
),
197 QUICK_INPUT (3, SEARCH_DLG_WIDTH
, 3, SEARCH_DLG_HEIGHT
,
198 *search_text
, SEARCH_DLG_WIDTH
- 6, 0,
199 MC_HISTORY_SHARED_SEARCH
, search_text
),
201 QUICK_LABEL (2, SEARCH_DLG_WIDTH
, 2, SEARCH_DLG_HEIGHT
, N_(" Enter search string:")),
206 size_t last_checkbox
= 7;
208 size_t last_checkbox
= 6;
211 QuickDialog Quick_input
=
213 SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
, -1, -1, N_("Search"),
214 "[Input Line Keys]", quick_widgets
, TRUE
218 char **list_of_types_nls
;
221 Quick_input
.title
= _(Quick_input
.title
);
223 for (i
= 0; i
< 3; i
++)
224 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
226 for (i
= 3; i
<= last_checkbox
; i
++)
227 quick_widgets
[i
].u
.checkbox
.text
= _(quick_widgets
[i
].u
.checkbox
.text
);
229 quick_widgets
[10].u
.label
.text
= _(quick_widgets
[10].u
.label
.text
);
232 /* create copy of radio items to avoid memory leak */
233 list_of_types_nls
= g_new0 (char *, num_of_types
+ 1);
234 for (i
= 0; i
< num_of_types
; i
++)
235 list_of_types_nls
[i
] = g_strdup (_(list_of_types
[i
]));
236 g_strfreev (list_of_types
);
237 list_of_types
= list_of_types_nls
;
238 quick_widgets
[last_checkbox
+ 1].u
.radio
.items
= (const char **) list_of_types
;
241 /* calculate widget coordinates */
245 gchar
**radio
= list_of_types
;
246 int b0_len
, b1_len
, b2_len
;
247 const int button_gap
= 2;
249 /* length of radiobuttons */
250 while (*radio
!= NULL
) {
251 len
= max (len
, str_term_width1 (*radio
));
254 /* length of checkboxes */
255 for (i
= 3; i
<= last_checkbox
; i
++)
256 len
= max (len
, str_term_width1 (quick_widgets
[i
].u
.checkbox
.text
) + 4);
258 /* preliminary dialog width */
259 dlg_width
= max (len
* 2, str_term_width1 (Quick_input
.title
)) + 4;
261 /* length of buttons */
262 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
263 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 3;
264 b2_len
= str_term_width1 (quick_widgets
[2].u
.button
.text
) + 5; /* default button */
265 len
= b0_len
+ b1_len
+ b2_len
+ button_gap
* 2;
268 Quick_input
.xlen
= max (SEARCH_DLG_WIDTH
, max (dlg_width
, len
+ 6));
270 /* correct widget coordinates */
271 for (i
= 0; i
< sizeof (quick_widgets
)/sizeof (quick_widgets
[0]); i
++)
272 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
274 /* checkbox positions */
275 for (i
= 3; i
<= last_checkbox
; i
++)
276 quick_widgets
[i
].relative_x
= Quick_input
.xlen
/2 + 2;
278 quick_widgets
[last_checkbox
+ 2].u
.input
.len
= Quick_input
.xlen
- 6;
279 /* button positions */
280 quick_widgets
[2].relative_x
= Quick_input
.xlen
/2 - len
/2;
281 quick_widgets
[1].relative_x
= quick_widgets
[2].relative_x
+ b2_len
+ button_gap
;
282 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ button_gap
;
285 dialog_result
= quick_dialog (&Quick_input
);
287 g_strfreev (list_of_types
);
289 if (dialog_result
== B_CANCEL
)
291 else if (dialog_result
== B_USER
)
292 search_create_bookmark
= 1;
296 /* --------------------------------------------------------------------------------------------- */
298 /* gets a raw key from the keyboard. Passing cancel = 1 draws
299 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
300 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
301 and Esc are cannot returned */
304 editcmd_dialog_raw_key_query (const char *heading
, const char *query
, int cancel
)
306 int w
= str_term_width1 (query
) + 7;
307 struct Dlg_head
*raw_dlg
=
308 create_dlg (0, 0, 7, w
, dialog_colors
, editcmd_dialog_raw_key_query_cb
,
309 NULL
, heading
, DLG_CENTER
| DLG_TRYUP
| DLG_WANT_TAB
);
311 input_new (3 - cancel
, w
- 5, INPUT_COLOR
, 2, "", 0, INPUT_COMPLETE_DEFAULT
));
312 add_widget (raw_dlg
, label_new (3 - cancel
, 2, query
));
314 add_widget (raw_dlg
, button_new (4, w
/ 2 - 5, B_CANCEL
, NORMAL_BUTTON
, _("Cancel"), 0));
315 w
= run_dlg (raw_dlg
);
316 destroy_dlg (raw_dlg
);
318 if (w
== XCTRL ('g') || w
== XCTRL ('c') || w
== ESC_CHAR
|| w
== B_CANCEL
)
325 /* --------------------------------------------------------------------------------------------- */
327 /* let the user select its preferred completion */
329 editcmd_dialog_completion_show (WEdit
* edit
, int max_len
, int word_len
,
330 struct selection
*compl, int num_compl
)
333 int start_x
, start_y
, offset
, i
;
336 WListbox
*compl_list
;
337 int compl_dlg_h
; /* completion dialog height */
338 int compl_dlg_w
; /* completion dialog width */
340 /* calculate the dialog metrics */
341 compl_dlg_h
= num_compl
+ 2;
342 compl_dlg_w
= max_len
+ 4;
343 start_x
= edit
->curs_col
+ edit
->start_col
- (compl_dlg_w
/ 2) +
344 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
345 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
349 if (compl_dlg_w
> COLS
)
351 if (compl_dlg_h
> LINES
- 2)
352 compl_dlg_h
= LINES
- 2;
354 offset
= start_x
+ compl_dlg_w
- COLS
;
357 offset
= start_y
+ compl_dlg_h
- LINES
;
359 start_y
-= (offset
+ 1);
361 /* create the dialog */
363 create_dlg (start_y
, start_x
, compl_dlg_h
, compl_dlg_w
,
364 dialog_colors
, NULL
, "[Completion]", NULL
, DLG_COMPACT
);
366 /* create the listbox */
367 compl_list
= listbox_new (1, 1, compl_dlg_h
- 2, compl_dlg_w
- 2, FALSE
, NULL
);
370 add_widget (compl_dlg
, compl_list
);
372 /* fill the listbox with the completions */
373 for (i
= num_compl
- 1; i
>= 0; i
--) /* reverse order */
374 listbox_add_item (compl_list
, LISTBOX_APPEND_AT_END
, 0, (char *) compl[i
].text
, NULL
);
376 /* pop up the dialog and apply the choosen completion */
377 if (run_dlg (compl_dlg
) == B_ENTER
) {
378 listbox_get_current (compl_list
, &curr
, NULL
);
381 GString
*temp
, *temp2
;
382 temp
= g_string_new("");
383 for (curr
+= word_len
; *curr
; curr
++)
384 g_string_append_c(temp
, *curr
);
386 temp2
= str_convert_to_input (temp
->str
);
388 if (temp2
&& temp2
->len
){
389 g_string_free(temp
, TRUE
);
393 g_string_free(temp2
, TRUE
);
394 for (curr
= temp
->str
; *curr
; curr
++)
395 edit_insert (edit
, *curr
);
396 g_string_free(temp
, TRUE
);
398 for (curr
+= word_len
; *curr
; curr
++)
399 edit_insert (edit
, *curr
);
404 /* destroy dialog before return */
405 destroy_dlg (compl_dlg
);
408 /* --------------------------------------------------------------------------------------------- */
410 /* let the user select where function definition */
412 editcmd_dialog_select_definition_show (WEdit
* edit
, char *match_expr
, int max_len
, int word_len
,
413 etags_hash_t
* def_hash
, int num_lines
)
416 int start_x
, start_y
, offset
, i
;
418 etags_hash_t
*curr_def
= NULL
;
421 int def_dlg_h
; /* dialog height */
422 int def_dlg_w
; /* dialog width */
423 char *label_def
= NULL
;
426 /* calculate the dialog metrics */
427 def_dlg_h
= num_lines
+ 2;
428 def_dlg_w
= max_len
+ 4;
429 start_x
= edit
->curs_col
+ edit
->start_col
- (def_dlg_w
/ 2) +
430 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
431 start_y
= edit
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
+ 1;
435 if (def_dlg_w
> COLS
)
437 if (def_dlg_h
> LINES
- 2)
438 def_dlg_h
= LINES
- 2;
440 offset
= start_x
+ def_dlg_w
- COLS
;
443 offset
= start_y
+ def_dlg_h
- LINES
;
445 start_y
-= (offset
+ 1);
447 /* create the dialog */
448 def_dlg
= create_dlg (start_y
, start_x
, def_dlg_h
, def_dlg_w
,
449 dialog_colors
, NULL
, "[Definitions]", match_expr
, DLG_COMPACT
);
451 /* create the listbox */
452 def_list
= listbox_new (1, 1, def_dlg_h
- 2, def_dlg_w
- 2, FALSE
, NULL
);
455 add_widget (def_dlg
, def_list
);
457 /* fill the listbox with the completions */
458 for (i
= 0; i
< num_lines
; i
++) {
460 g_strdup_printf ("%s -> %s:%ld", def_hash
[i
].short_define
, def_hash
[i
].filename
,
462 listbox_add_item (def_list
, LISTBOX_APPEND_AT_END
, 0, label_def
, &def_hash
[i
]);
466 /* pop up the dialog and apply the choosen completion */
467 if (run_dlg (def_dlg
) == B_ENTER
) {
468 char *tmp_curr_def
= (char *) curr_def
;
471 listbox_get_current (def_list
, &curr
, (void **) &tmp_curr_def
);
472 curr_def
= (etags_hash_t
*) tmp_curr_def
;
473 if (edit
->modified
) {
474 if (!edit_query_dialog2
476 _(" Current text was modified without a file save. \n"
477 " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
478 edit
->force
|= REDRAW_COMPLETELY
;
485 if (curr
&& do_moveto
) {
486 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
) {
487 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
489 edit_history_moveto
[edit_stack_iterator
].filename
=
490 g_strdup_printf ("%s/%s", edit
->dir
, edit
->filename
);
492 edit_history_moveto
[edit_stack_iterator
].filename
= g_strdup (edit
->filename
);
494 edit_history_moveto
[edit_stack_iterator
].line
= edit
->start_line
+
496 edit_stack_iterator
++;
497 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
498 edit_history_moveto
[edit_stack_iterator
].filename
=
499 g_strdup ((char *) curr_def
->fullpath
);
500 edit_history_moveto
[edit_stack_iterator
].line
= curr_def
->line
;
501 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
502 edit_history_moveto
[edit_stack_iterator
].line
);
507 /* clear definition hash */
508 for (i
= 0; i
< MAX_DEFINITIONS
; i
++) {
509 g_free (def_hash
[i
].filename
);
512 /* destroy dialog before return */
513 destroy_dlg (def_dlg
);
516 /* --------------------------------------------------------------------------------------------- */
519 editcmd_dialog_replace_prompt_show (WEdit
* edit
, char *from_text
, char *to_text
, int xpos
, int ypos
)
529 char *repl_from
, *repl_to
;
530 char tmp
[BUF_MEDIUM
];
532 QuickWidget quick_widgets
[] =
534 /* 0 */ QUICK_BUTTON (44, dlg_width
, 6, dlg_height
, N_("&Cancel"), B_CANCEL
, NULL
),
535 /* 1 */ QUICK_BUTTON (29, dlg_width
, 6, dlg_height
, N_("&Skip"), B_SKIP_REPLACE
, NULL
),
536 /* 2 */ QUICK_BUTTON (21, dlg_width
, 6, dlg_height
, N_("A&ll"), B_REPLACE_ALL
, NULL
),
537 /* 3 */ QUICK_BUTTON ( 4, dlg_width
, 6, dlg_height
, N_("&Replace"), B_ENTER
, NULL
),
538 /* 4 */ QUICK_LABEL ( 3, dlg_width
, 2, dlg_height
, NULL
),
539 /* 5 */ QUICK_LABEL ( 2, dlg_width
, 3, dlg_height
, N_(" Replace with: ")),
540 /* 6 */ QUICK_LABEL ( 3, dlg_width
, 4, dlg_height
, NULL
),
545 for (i
= 0; i
< 4; i
++)
546 quick_widgets
[i
].u
.button
.text
= _(quick_widgets
[i
].u
.button
.text
);
549 /* calculate button positions */
552 for (i
= 3; i
> -1; i
--) {
553 quick_widgets
[i
].relative_x
= btn_pos
;
554 btn_pos
+= str_term_width1 (quick_widgets
[i
].u
.button
.text
) + 5;
555 if (i
== 3) /* default button */
559 dlg_width
= btn_pos
+ 2;
561 /* correct widget coordinates */
562 for (i
= 0; i
< 7; i
++)
563 quick_widgets
[i
].x_divisions
= dlg_width
;
565 g_snprintf (tmp
, sizeof (tmp
), " '%s'", from_text
);
566 repl_from
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
568 g_snprintf (tmp
, sizeof (tmp
), " '%s'", to_text
);
569 repl_to
= g_strdup (str_fit_to_term (tmp
, dlg_width
- 7, J_LEFT
));
571 quick_widgets
[4].u
.label
.text
= repl_from
;
572 quick_widgets
[6].u
.label
.text
= repl_to
;
575 xpos
= (edit
->num_widget_columns
- dlg_width
) / 2;
578 ypos
= edit
->num_widget_lines
* 2 / 3;
581 QuickDialog Quick_input
=
583 dlg_width
, dlg_height
, 0, 0, N_ (" Confirm replace "),
584 "[Input Line Keys]", quick_widgets
, FALSE
587 /* Sometimes menu can hide replaced text. I don't like it */
588 if ((edit
->curs_row
>= ypos
- 1) && (edit
->curs_row
<= ypos
+ dlg_height
- 1))
591 Quick_input
.ypos
= ypos
;
592 Quick_input
.xpos
= xpos
;
594 retval
= quick_dialog (&Quick_input
);
602 /* --------------------------------------------------------------------------------------------- */