editcmd_dialogs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. Editor dialogs for high level editing commands
  3. Copyright (C) 2009 The Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2009.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be
  12. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. MA 02110-1301, USA.
  19. */
  20. #include <config.h>
  21. #include "../src/global.h"
  22. #include "../src/tty/tty.h"
  23. #include "../src/tty/color.h" /* INPUT_COLOR */
  24. #include "../src/tty/key.h"
  25. #include "../src/search/search.h"
  26. #include "../src/strutil.h"
  27. #include "../src/widget.h"
  28. #include "../src/wtools.h"
  29. #include "../src/dialog.h" /* do_refresh() */
  30. #include "../src/main.h"
  31. #include "../src/history.h"
  32. #include "../edit/edit-widget.h"
  33. #include "../edit/etags.h"
  34. #include "../edit/editcmd_dialogs.h"
  35. /*** global variables **************************************************/
  36. /*** file scope macro definitions **************************************/
  37. #define SEARCH_DLG_WIDTH 58
  38. #define SEARCH_DLG_MIN_HEIGHT 13
  39. #define SEARCH_DLG_HEIGHT_SUPPLY 3
  40. #define REPLACE_DLG_WIDTH 58
  41. #define REPLACE_DLG_MIN_HEIGHT 17
  42. #define REPLACE_DLG_HEIGHT_SUPPLY 5
  43. /*** file scope type declarations **************************************/
  44. /*** file scope variables **********************************************/
  45. /*** file scope functions **********************************************/
  46. static cb_ret_t
  47. editcmd_dialog_raw_key_query_cb (struct Dlg_head *h, dlg_msg_t msg, int parm)
  48. {
  49. switch (msg) {
  50. case DLG_KEY:
  51. dlg_stop (h);
  52. h->ret_value = parm;
  53. return MSG_HANDLED;
  54. default:
  55. return default_dlg_callback (h, msg, parm);
  56. }
  57. }
  58. /*** public functions **************************************************/
  59. void
  60. editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
  61. /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
  62. {
  63. if (*search_default == '\0')
  64. search_default = INPUT_LAST_TEXT;
  65. {
  66. gchar **list_of_types = mc_search_get_types_strings_array();
  67. int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - REPLACE_DLG_HEIGHT_SUPPLY;
  68. QuickWidget quick_widgets[] =
  69. {
  70. /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
  71. /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
  72. #ifdef HAVE_CHARSET
  73. /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
  74. #endif
  75. /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
  76. /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
  77. /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
  78. /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
  79. /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT,
  80. g_strv_length (list_of_types), (const char **) list_of_types, &edit->search_type),
  81. /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")),
  82. /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT,
  83. replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text),
  84. /* 10 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:")),
  85. /* 11 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT,
  86. search_default, REPLACE_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
  87. QUICK_END
  88. };
  89. QuickDialog Quick_input =
  90. {
  91. REPLACE_DLG_WIDTH, REPLACE_DLG_HEIGHT, -1, -1, N_(" Replace "),
  92. "[Input Line Keys]", quick_widgets, FALSE
  93. };
  94. if (quick_dialog (&Quick_input) != B_CANCEL) {
  95. edit->replace_mode = 0;
  96. } else {
  97. *replace_text = NULL;
  98. *search_text = NULL;
  99. }
  100. g_strfreev (list_of_types);
  101. }
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. void
  105. editcmd_dialog_search_show (WEdit * edit, char **search_text)
  106. {
  107. if (*search_text == '\0')
  108. *search_text = INPUT_LAST_TEXT;
  109. {
  110. gchar **list_of_types = mc_search_get_types_strings_array();
  111. int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - SEARCH_DLG_HEIGHT_SUPPLY;
  112. QuickWidget quick_widgets[] =
  113. {
  114. /* 0 */ QUICK_BUTTON (6, 10, 11, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
  115. /* 1 */ QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
  116. #ifdef HAVE_CHARSET
  117. /* 2 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages),
  118. #endif
  119. /* 3 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words),
  120. /* 4 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection),
  121. /* 5 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards),
  122. /* 6 */ QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case),
  123. /* 7 */ QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
  124. g_strv_length (list_of_types), (const char **) list_of_types, &edit->search_type),
  125. /* 8 */ QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
  126. *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text),
  127. /* 9 */ QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
  128. QUICK_END
  129. };
  130. QuickDialog Quick_input =
  131. {
  132. SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"),
  133. "[Input Line Keys]", quick_widgets, FALSE
  134. };
  135. if (quick_dialog (&Quick_input) == B_CANCEL)
  136. *search_text = NULL;
  137. }
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. /* gets a raw key from the keyboard. Passing cancel = 1 draws
  141. a cancel button thus allowing c-c etc. Alternatively, cancel = 0
  142. will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
  143. and Esc are cannot returned */
  144. int
  145. editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
  146. {
  147. int w = str_term_width1 (query) + 7;
  148. struct Dlg_head *raw_dlg =
  149. create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
  150. NULL, heading,
  151. DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
  152. add_widget (raw_dlg,
  153. input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0, INPUT_COMPLETE_DEFAULT));
  154. add_widget (raw_dlg, label_new (3 - cancel, 2, query));
  155. if (cancel)
  156. add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0));
  157. w = run_dlg (raw_dlg);
  158. destroy_dlg (raw_dlg);
  159. if (cancel) {
  160. if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL)
  161. return 0;
  162. }
  163. return w;
  164. }
  165. /* --------------------------------------------------------------------------------------------- */
  166. /* let the user select its preferred completion */
  167. void
  168. editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
  169. struct selection *compl, int num_compl)
  170. {
  171. int start_x, start_y, offset, i;
  172. char *curr = NULL;
  173. Dlg_head *compl_dlg;
  174. WListbox *compl_list;
  175. int compl_dlg_h; /* completion dialog height */
  176. int compl_dlg_w; /* completion dialog width */
  177. /* calculate the dialog metrics */
  178. compl_dlg_h = num_compl + 2;
  179. compl_dlg_w = max_len + 4;
  180. start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
  181. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  182. start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
  183. if (start_x < 0)
  184. start_x = 0;
  185. if (compl_dlg_w > COLS)
  186. compl_dlg_w = COLS;
  187. if (compl_dlg_h > LINES - 2)
  188. compl_dlg_h = LINES - 2;
  189. offset = start_x + compl_dlg_w - COLS;
  190. if (offset > 0)
  191. start_x -= offset;
  192. offset = start_y + compl_dlg_h - LINES;
  193. if (offset > 0)
  194. start_y -= (offset + 1);
  195. /* create the dialog */
  196. compl_dlg =
  197. create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
  198. dialog_colors, NULL, "[Completion]", NULL, DLG_COMPACT);
  199. /* create the listbox */
  200. compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, NULL);
  201. /* add the dialog */
  202. add_widget (compl_dlg, compl_list);
  203. /* fill the listbox with the completions */
  204. for (i = 0; i < num_compl; i++)
  205. listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
  206. /* pop up the dialog and apply the choosen completion */
  207. if (run_dlg (compl_dlg) == B_ENTER) {
  208. listbox_get_current (compl_list, &curr, NULL);
  209. if (curr)
  210. for (curr += word_len; *curr; curr++)
  211. edit_insert (edit, *curr);
  212. }
  213. /* destroy dialog before return */
  214. destroy_dlg (compl_dlg);
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. /* let the user select where function definition */
  218. void
  219. editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
  220. etags_hash_t * def_hash, int num_lines)
  221. {
  222. int start_x, start_y, offset, i;
  223. char *curr = NULL;
  224. etags_hash_t *curr_def = NULL;
  225. Dlg_head *def_dlg;
  226. WListbox *def_list;
  227. int def_dlg_h; /* dialog height */
  228. int def_dlg_w; /* dialog width */
  229. char *label_def = NULL;
  230. (void) word_len;
  231. /* calculate the dialog metrics */
  232. def_dlg_h = num_lines + 2;
  233. def_dlg_w = max_len + 4;
  234. start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
  235. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  236. start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
  237. if (start_x < 0)
  238. start_x = 0;
  239. if (def_dlg_w > COLS)
  240. def_dlg_w = COLS;
  241. if (def_dlg_h > LINES - 2)
  242. def_dlg_h = LINES - 2;
  243. offset = start_x + def_dlg_w - COLS;
  244. if (offset > 0)
  245. start_x -= offset;
  246. offset = start_y + def_dlg_h - LINES;
  247. if (offset > 0)
  248. start_y -= (offset + 1);
  249. /* create the dialog */
  250. def_dlg = create_dlg (start_y, start_x, def_dlg_h, def_dlg_w,
  251. dialog_colors, NULL, "[Definitions]", match_expr, DLG_COMPACT);
  252. /* create the listbox */
  253. def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, NULL);
  254. /* add the dialog */
  255. add_widget (def_dlg, def_list);
  256. /* fill the listbox with the completions */
  257. for (i = 0; i < num_lines; i++) {
  258. label_def =
  259. g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
  260. def_hash[i].line);
  261. listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
  262. g_free (label_def);
  263. }
  264. /* pop up the dialog */
  265. run_dlg (def_dlg);
  266. /* apply the choosen completion */
  267. if (def_dlg->ret_value == B_ENTER) {
  268. char *tmp_curr_def = (char *) curr_def;
  269. int do_moveto = 0;
  270. listbox_get_current (def_list, &curr, &tmp_curr_def);
  271. curr_def = (etags_hash_t *) tmp_curr_def;
  272. if (edit->modified) {
  273. if (!edit_query_dialog2
  274. (_("Warning"),
  275. _(" Current text was modified without a file save. \n"
  276. " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
  277. edit->force |= REDRAW_COMPLETELY;
  278. do_moveto = 1;
  279. }
  280. } else {
  281. do_moveto = 1;
  282. }
  283. if (curr && do_moveto) {
  284. if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO) {
  285. g_free (edit_history_moveto[edit_stack_iterator].filename);
  286. if (edit->dir) {
  287. edit_history_moveto[edit_stack_iterator].filename =
  288. g_strdup_printf ("%s/%s", edit->dir, edit->filename);
  289. } else {
  290. edit_history_moveto[edit_stack_iterator].filename = g_strdup (edit->filename);
  291. }
  292. edit_history_moveto[edit_stack_iterator].line = edit->start_line +
  293. edit->curs_row + 1;
  294. edit_stack_iterator++;
  295. g_free (edit_history_moveto[edit_stack_iterator].filename);
  296. edit_history_moveto[edit_stack_iterator].filename =
  297. g_strdup ((char *) curr_def->fullpath);
  298. edit_history_moveto[edit_stack_iterator].line = curr_def->line;
  299. edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
  300. edit_history_moveto[edit_stack_iterator].line);
  301. }
  302. }
  303. }
  304. /* clear definition hash */
  305. for (i = 0; i < MAX_DEFINITIONS; i++) {
  306. g_free (def_hash[i].filename);
  307. }
  308. /* destroy dialog before return */
  309. destroy_dlg (def_dlg);
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. int
  313. editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos, int ypos)
  314. {
  315. /* dialog sizes */
  316. int dlg_height = 9;
  317. int dlg_width = 8;
  318. int retval;
  319. int i;
  320. int btn_pos;
  321. char *repl_from, *repl_to;
  322. char tmp [BUF_MEDIUM];
  323. QuickWidget quick_widgets[] =
  324. {
  325. /* 0 */ QUICK_BUTTON (44, dlg_width, 6, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
  326. /* 1 */ QUICK_BUTTON (29, dlg_width, 6, dlg_height, N_("&Skip"), B_SKIP_REPLACE, NULL),
  327. /* 2 */ QUICK_BUTTON (21, dlg_width, 6, dlg_height, N_("A&ll"), B_REPLACE_ALL, NULL),
  328. /* 3 */ QUICK_BUTTON ( 4, dlg_width, 6, dlg_height, N_("&Replace"), B_ENTER, NULL),
  329. /* 4 */ QUICK_LABEL ( 3, dlg_width, 2, dlg_height, NULL),
  330. /* 5 */ QUICK_LABEL ( 2, dlg_width, 3, dlg_height, N_(" Replace with: ")),
  331. /* 6 */ QUICK_LABEL ( 3, dlg_width, 4, dlg_height, NULL),
  332. QUICK_END
  333. };
  334. #ifdef ENABLE_NLS
  335. for (i = 0; i < 4; i++)
  336. quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
  337. #endif
  338. /* calculate button positions */
  339. btn_pos = 4;
  340. for (i = 3; i > -1; i--) {
  341. quick_widgets[i].relative_x = btn_pos;
  342. btn_pos += str_term_width1 (quick_widgets[i].u.button.text) + 5;
  343. if (i == 3) /* default button */
  344. btn_pos += 2;
  345. }
  346. dlg_width = btn_pos + 2;
  347. /* correct widget coordinates */
  348. for (i = 0; i < 7; i++)
  349. quick_widgets[i].x_divisions = dlg_width;
  350. g_snprintf (tmp, sizeof (tmp), " '%s'", from_text);
  351. repl_from = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
  352. g_snprintf (tmp, sizeof (tmp), " '%s'", to_text);
  353. repl_to = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
  354. quick_widgets[4].u.label.text = repl_from;
  355. quick_widgets[6].u.label.text = repl_to;
  356. if (xpos == -1)
  357. xpos = (edit->num_widget_columns - dlg_width) / 2;
  358. if (ypos == -1)
  359. ypos = edit->num_widget_lines * 2 / 3;
  360. {
  361. QuickDialog Quick_input =
  362. {
  363. dlg_width, dlg_height, 0, 0, N_ (" Confirm replace "),
  364. "[Input Line Keys]", quick_widgets, FALSE
  365. };
  366. /* Sometimes menu can hide replaced text. I don't like it */
  367. if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
  368. ypos -= dlg_height;
  369. Quick_input.ypos = ypos;
  370. Quick_input.xpos = xpos;
  371. retval = quick_dialog (&Quick_input);
  372. g_free (repl_from);
  373. g_free (repl_to);
  374. return retval;
  375. }
  376. }
  377. /* --------------------------------------------------------------------------------------------- */