editcmd_dialogs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 11
  39. #define SEARCH_DLG_HEIGHT_SUPPLY 3
  40. #define REPLACE_DLG_WIDTH 58
  41. #define REPLACE_DLG_MIN_HEIGHT 16
  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. h->running = 0;
  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. int treplace_backwards = edit->replace_backwards;
  64. int treplace_case = edit->replace_case;
  65. int tall_codepages = edit->all_codepages;
  66. mc_search_type_t ttype_of_search = edit->search_type;
  67. int dialog_result;
  68. gchar **list_of_types = mc_search_get_types_strings_array();
  69. int REPLACE_DLG_HEIGHT = REPLACE_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - REPLACE_DLG_HEIGHT_SUPPLY;
  70. if (!*search_default)
  71. search_default = INPUT_LAST_TEXT;
  72. QuickWidget quick_widgets[] = {
  73. {quick_button, 6, 10, 12, REPLACE_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL,
  74. 0, 0, NULL, NULL, NULL},
  75. {quick_button, 2, 10, 12, REPLACE_DLG_HEIGHT, N_("&OK"), 0, B_ENTER,
  76. 0, 0, NULL, NULL, NULL},
  77. #ifdef HAVE_CHARSET
  78. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("All charsets"), 0, 0,
  79. &tall_codepages, 0, NULL, NULL, NULL},
  80. #endif
  81. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), 0, 0,
  82. &treplace_backwards, 0, NULL, NULL, NULL},
  83. {quick_checkbox, 33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), 0, 0,
  84. &treplace_case, 0, NULL, NULL, NULL},
  85. {quick_radio, 3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, "", g_strv_length (list_of_types), ttype_of_search,
  86. (void *) &ttype_of_search, const_cast (char **, list_of_types), NULL, NULL, NULL},
  87. {quick_label, 2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:"),
  88. 0, 0, 0, 0, 0, NULL, NULL},
  89. {quick_input, 3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT, replace_default, 52, 0,
  90. 0, replace_text, "replace", NULL, NULL},
  91. {quick_label, 2, REPLACE_DLG_WIDTH, 2, REPLACE_DLG_HEIGHT, N_(" Enter search string:"), 0,
  92. 0, 0, 0, 0, NULL, NULL},
  93. {quick_input, 3, REPLACE_DLG_WIDTH, 3, REPLACE_DLG_HEIGHT, search_default, 52, 0,
  94. 0, search_text, MC_HISTORY_SHARED_SEARCH, NULL, NULL},
  95. NULL_QuickWidget
  96. };
  97. QuickDialog Quick_input = {
  98. REPLACE_DLG_WIDTH,
  99. REPLACE_DLG_HEIGHT,
  100. -1,
  101. 0,
  102. N_(" Replace "),
  103. "[Input Line Keys]",
  104. quick_widgets,
  105. 0
  106. };
  107. dialog_result = quick_dialog (&Quick_input);
  108. g_strfreev (list_of_types);
  109. if (dialog_result != B_CANCEL) {
  110. edit->search_type = ttype_of_search;
  111. edit->replace_mode = 0;
  112. edit->all_codepages = tall_codepages;
  113. edit->replace_backwards = treplace_backwards;
  114. edit->replace_case = treplace_case;
  115. return;
  116. } else {
  117. *replace_text = NULL;
  118. *search_text = NULL;
  119. return;
  120. }
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. void
  124. editcmd_dialog_search_show (WEdit * edit, char **search_text)
  125. {
  126. int tsearch_case = edit->replace_case;
  127. int tsearch_backwards = edit->replace_backwards;
  128. int tall_codepages = edit->all_codepages;
  129. mc_search_type_t ttype_of_search = edit->search_type;
  130. gchar **list_of_types = mc_search_get_types_strings_array();
  131. int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + g_strv_length (list_of_types) - SEARCH_DLG_HEIGHT_SUPPLY;
  132. if (!*search_text)
  133. *search_text = INPUT_LAST_TEXT;
  134. QuickWidget quick_widgets[] = {
  135. {quick_button, 6, 10, 9, SEARCH_DLG_HEIGHT, N_("&Cancel"), 0, B_CANCEL, 0,
  136. 0, NULL, NULL, NULL},
  137. {quick_button, 2, 10, 9, SEARCH_DLG_HEIGHT, N_("&OK"), 0, B_ENTER, 0,
  138. 0, NULL, NULL, NULL},
  139. #ifdef HAVE_CHARSET
  140. {quick_checkbox, 33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("All charsets"), 0, 0,
  141. &tall_codepages, 0, NULL, NULL, NULL},
  142. #endif
  143. {quick_checkbox, 33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), 0, 0,
  144. &tsearch_backwards, 0, NULL, NULL, NULL},
  145. {quick_checkbox, 33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), 0, 0,
  146. &tsearch_case, 0, NULL, NULL, NULL},
  147. {quick_radio, 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, 0, g_strv_length (list_of_types), ttype_of_search,
  148. (void *) &ttype_of_search, const_cast (char **, list_of_types), NULL, NULL, NULL},
  149. {quick_input, 3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, *search_text, 52, 0, 0,
  150. search_text, MC_HISTORY_SHARED_SEARCH, NULL, NULL},
  151. {quick_label, 2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:"), 0, 0,
  152. 0, 0, 0, NULL, NULL},
  153. NULL_QuickWidget
  154. };
  155. QuickDialog Quick_input = { SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, 0, N_("Search"),
  156. "[Input Line Keys]", quick_widgets, 0
  157. };
  158. if (quick_dialog (&Quick_input) != B_CANCEL) {
  159. edit->search_type = ttype_of_search;
  160. edit->replace_backwards = tsearch_backwards;
  161. edit->all_codepages = tall_codepages;
  162. edit->replace_case = tsearch_case;
  163. } else {
  164. *search_text = NULL;
  165. }
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. /* gets a raw key from the keyboard. Passing cancel = 1 draws
  169. a cancel button thus allowing c-c etc. Alternatively, cancel = 0
  170. will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
  171. and Esc are cannot returned */
  172. int
  173. editcmd_dialog_raw_key_query (const char *heading, const char *query, int cancel)
  174. {
  175. int w = str_term_width1 (query) + 7;
  176. struct Dlg_head *raw_dlg =
  177. create_dlg (0, 0, 7, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
  178. NULL, heading,
  179. DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
  180. add_widget (raw_dlg,
  181. input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0, INPUT_COMPLETE_DEFAULT));
  182. add_widget (raw_dlg, label_new (3 - cancel, 2, query));
  183. if (cancel)
  184. add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0));
  185. run_dlg (raw_dlg);
  186. w = raw_dlg->ret_value;
  187. destroy_dlg (raw_dlg);
  188. if (cancel) {
  189. if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL)
  190. return 0;
  191. }
  192. return w;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */
  195. /* let the user select its preferred completion */
  196. void
  197. editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
  198. struct selection *compl, int num_compl)
  199. {
  200. int start_x, start_y, offset, i;
  201. char *curr = NULL;
  202. Dlg_head *compl_dlg;
  203. WListbox *compl_list;
  204. int compl_dlg_h; /* completion dialog height */
  205. int compl_dlg_w; /* completion dialog width */
  206. /* calculate the dialog metrics */
  207. compl_dlg_h = num_compl + 2;
  208. compl_dlg_w = max_len + 4;
  209. start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
  210. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  211. start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
  212. if (start_x < 0)
  213. start_x = 0;
  214. if (compl_dlg_w > COLS)
  215. compl_dlg_w = COLS;
  216. if (compl_dlg_h > LINES - 2)
  217. compl_dlg_h = LINES - 2;
  218. offset = start_x + compl_dlg_w - COLS;
  219. if (offset > 0)
  220. start_x -= offset;
  221. offset = start_y + compl_dlg_h - LINES;
  222. if (offset > 0)
  223. start_y -= (offset + 1);
  224. /* create the dialog */
  225. compl_dlg =
  226. create_dlg (start_y, start_x, compl_dlg_h, compl_dlg_w,
  227. dialog_colors, NULL, "[Completion]", NULL, DLG_COMPACT);
  228. /* create the listbox */
  229. compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, NULL);
  230. /* add the dialog */
  231. add_widget (compl_dlg, compl_list);
  232. /* fill the listbox with the completions */
  233. for (i = 0; i < num_compl; i++)
  234. listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
  235. /* pop up the dialog */
  236. run_dlg (compl_dlg);
  237. /* apply the choosen completion */
  238. if (compl_dlg->ret_value == B_ENTER) {
  239. listbox_get_current (compl_list, &curr, NULL);
  240. if (curr)
  241. for (curr += word_len; *curr; curr++)
  242. edit_insert (edit, *curr);
  243. }
  244. /* destroy dialog before return */
  245. destroy_dlg (compl_dlg);
  246. }
  247. /* --------------------------------------------------------------------------------------------- */
  248. /* let the user select where function definition */
  249. void
  250. editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
  251. etags_hash_t * def_hash, int num_lines)
  252. {
  253. int start_x, start_y, offset, i;
  254. char *curr = NULL;
  255. etags_hash_t *curr_def = NULL;
  256. Dlg_head *def_dlg;
  257. WListbox *def_list;
  258. int def_dlg_h; /* dialog height */
  259. int def_dlg_w; /* dialog width */
  260. char *label_def = NULL;
  261. (void) word_len;
  262. /* calculate the dialog metrics */
  263. def_dlg_h = num_lines + 2;
  264. def_dlg_w = max_len + 4;
  265. start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
  266. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  267. start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + 1;
  268. if (start_x < 0)
  269. start_x = 0;
  270. if (def_dlg_w > COLS)
  271. def_dlg_w = COLS;
  272. if (def_dlg_h > LINES - 2)
  273. def_dlg_h = LINES - 2;
  274. offset = start_x + def_dlg_w - COLS;
  275. if (offset > 0)
  276. start_x -= offset;
  277. offset = start_y + def_dlg_h - LINES;
  278. if (offset > 0)
  279. start_y -= (offset + 1);
  280. /* create the dialog */
  281. def_dlg = create_dlg (start_y, start_x, def_dlg_h, def_dlg_w,
  282. dialog_colors, NULL, "[Definitions]", match_expr, DLG_COMPACT);
  283. /* create the listbox */
  284. def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, NULL);
  285. /* add the dialog */
  286. add_widget (def_dlg, def_list);
  287. /* fill the listbox with the completions */
  288. for (i = 0; i < num_lines; i++) {
  289. label_def =
  290. g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
  291. def_hash[i].line);
  292. listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
  293. g_free (label_def);
  294. }
  295. /* pop up the dialog */
  296. run_dlg (def_dlg);
  297. /* apply the choosen completion */
  298. if (def_dlg->ret_value == B_ENTER) {
  299. char *tmp_curr_def = (char *) curr_def;
  300. int do_moveto = 0;
  301. listbox_get_current (def_list, &curr, &tmp_curr_def);
  302. curr_def = (etags_hash_t *) tmp_curr_def;
  303. if (edit->modified) {
  304. if (!edit_query_dialog2
  305. (_("Warning"),
  306. _(" Current text was modified without a file save. \n"
  307. " Continue discards these changes. "), _("C&ontinue"), _("&Cancel"))) {
  308. edit->force |= REDRAW_COMPLETELY;
  309. do_moveto = 1;
  310. }
  311. } else {
  312. do_moveto = 1;
  313. }
  314. if (curr && do_moveto) {
  315. if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO) {
  316. g_free (edit_history_moveto[edit_stack_iterator].filename);
  317. if (edit->dir) {
  318. edit_history_moveto[edit_stack_iterator].filename =
  319. g_strdup_printf ("%s/%s", edit->dir, edit->filename);
  320. } else {
  321. edit_history_moveto[edit_stack_iterator].filename = g_strdup (edit->filename);
  322. }
  323. edit_history_moveto[edit_stack_iterator].line = edit->start_line +
  324. edit->curs_row + 1;
  325. edit_stack_iterator++;
  326. g_free (edit_history_moveto[edit_stack_iterator].filename);
  327. edit_history_moveto[edit_stack_iterator].filename =
  328. g_strdup ((char *) curr_def->fullpath);
  329. edit_history_moveto[edit_stack_iterator].line = curr_def->line;
  330. edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
  331. edit_history_moveto[edit_stack_iterator].line);
  332. }
  333. }
  334. }
  335. /* clear definition hash */
  336. for (i = 0; i < MAX_DEFINITIONS; i++) {
  337. g_free (def_hash[i].filename);
  338. }
  339. /* destroy dialog before return */
  340. destroy_dlg (def_dlg);
  341. }
  342. /* --------------------------------------------------------------------------------------------- */
  343. int
  344. editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos, int ypos)
  345. {
  346. /* dialog sizes */
  347. int dlg_height = 9;
  348. int dlg_width = 8;
  349. int retval;
  350. int i;
  351. int btn_pos;
  352. char *repl_from, *repl_to;
  353. char tmp [BUF_MEDIUM];
  354. QuickWidget quick_widgets[] = {
  355. {quick_button, 44, dlg_width, 6, dlg_height, N_("&Cancel"),
  356. 0, B_CANCEL, 0, 0, NULL, NULL, NULL},
  357. {quick_button, 29, dlg_width, 6, dlg_height, N_("&Skip"),
  358. 0, B_SKIP_REPLACE, 0, 0, NULL, NULL, NULL},
  359. {quick_button, 21, dlg_width, 6, dlg_height, N_("A&ll"),
  360. 0, B_REPLACE_ALL, 0, 0, NULL, NULL, NULL},
  361. {quick_button, 4, dlg_width, 6, dlg_height, N_("&Replace"),
  362. 0, B_ENTER, 0, 0, NULL, NULL, NULL},
  363. {quick_label, 3, dlg_width, 2, dlg_height, 0,
  364. 0, 0, 0, 0, 0, NULL, NULL},
  365. {quick_label, 2, dlg_width, 3, dlg_height, 0,
  366. 0, 0, 0, 0, 0, NULL, NULL},
  367. {quick_label, 3, dlg_width, 4, dlg_height, 0,
  368. 0, 0, 0, 0, 0, NULL, NULL},
  369. NULL_QuickWidget
  370. };
  371. #ifdef ENABLE_NLS
  372. for (i = 0; i < 4; i++)
  373. quick_widgets[i].text = _(quick_widgets[i].text);
  374. #endif
  375. /* calculate button positions */
  376. btn_pos = 4;
  377. for (i = 3; i > -1; i--) {
  378. quick_widgets[i].relative_x = btn_pos;
  379. btn_pos += str_term_width1 (quick_widgets[i].text) + 5;
  380. if (i == 3) /* default button */
  381. btn_pos += 2;
  382. }
  383. dlg_width = btn_pos + 2;
  384. /* correct widget coordinates */
  385. for (i = 0; i < 7; i++)
  386. quick_widgets[i].x_divisions = dlg_width;
  387. g_snprintf (tmp, sizeof (tmp), " '%s'", from_text);
  388. repl_from = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
  389. g_snprintf (tmp, sizeof (tmp), " '%s'", to_text);
  390. repl_to = g_strdup (str_fit_to_term (tmp, dlg_width - 7, J_LEFT));
  391. quick_widgets[4].text = repl_from;
  392. quick_widgets[5].text = _(" Replace with: ");
  393. quick_widgets[6].text = repl_to;
  394. if (xpos == -1)
  395. xpos = (edit->num_widget_columns - dlg_width) / 2;
  396. if (ypos == -1)
  397. ypos = edit->num_widget_lines * 2 / 3;
  398. {
  399. QuickDialog Quick_input =
  400. {dlg_width, dlg_height, 0, 0, N_ (" Confirm replace "),
  401. "[Input Line Keys]", 0 /*quick_widgets */, 0 };
  402. Quick_input.widgets = quick_widgets;
  403. Quick_input.xpos = xpos;
  404. /* Sometimes menu can hide replaced text. I don't like it */
  405. if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
  406. ypos -= dlg_height;
  407. Quick_input.ypos = ypos;
  408. retval = quick_dialog (&Quick_input);
  409. g_free (repl_from);
  410. g_free (repl_to);
  411. return retval;
  412. }
  413. }
  414. /* --------------------------------------------------------------------------------------------- */