editcmd_dialogs.c 17 KB

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