wtools.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Miguel de Icaza, 1994, 1995, 1996
  8. Radek Doulik, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1995
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU 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, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file wtools.c
  25. * \brief Source: widget based utility functions
  26. */
  27. #include <config.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/key.h" /* tty_getch() */
  33. #include "lib/strutil.h"
  34. #include "lib/util.h" /* tilde_expand() */
  35. #include "lib/widget.h"
  36. #include "lib/event.h" /* mc_event_raise() */
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. static Dlg_head *last_query_dlg;
  42. static int sel_pos = 0;
  43. /*** file scope functions ************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. /** default query callback, used to reposition query */
  46. static cb_ret_t
  47. default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
  48. {
  49. switch (msg)
  50. {
  51. case DLG_RESIZE:
  52. {
  53. Dlg_head *prev_dlg = NULL;
  54. int ypos, xpos;
  55. /* get dialog under h */
  56. if (top_dlg != NULL)
  57. {
  58. if (top_dlg->data != (void *) h)
  59. prev_dlg = (Dlg_head *) top_dlg->data;
  60. else
  61. {
  62. GList *p;
  63. /* Top dialog is current if it is visible.
  64. Get previous dialog in stack */
  65. p = g_list_next (top_dlg);
  66. if (p != NULL)
  67. prev_dlg = (Dlg_head *) p->data;
  68. }
  69. }
  70. /* if previous dialog is not fullscreen'd -- overlap it */
  71. if (prev_dlg == NULL || prev_dlg->fullscreen)
  72. ypos = LINES / 3 - (h->lines - 3) / 2;
  73. else
  74. ypos = prev_dlg->y + 2;
  75. xpos = COLS / 2 - h->cols / 2;
  76. /* set position */
  77. dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
  78. }
  79. return MSG_HANDLED;
  80. default:
  81. return default_dlg_callback (h, sender, msg, parm, data);
  82. }
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. /** Create message dialog */
  86. static struct Dlg_head *
  87. do_create_message (int flags, const char *title, const char *text)
  88. {
  89. char *p;
  90. Dlg_head *d;
  91. /* Add empty lines before and after the message */
  92. p = g_strconcat ("\n", text, "\n", (char *) NULL);
  93. query_dialog (title, p, flags, 0);
  94. d = last_query_dlg;
  95. /* do resize before initing and running */
  96. default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
  97. init_dlg (d);
  98. g_free (p);
  99. return d;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. /**
  103. * Show message dialog. Dismiss it when any key is pressed.
  104. * Not safe to call from background.
  105. */
  106. static void
  107. fg_message (int flags, const char *title, const char *text)
  108. {
  109. Dlg_head *d;
  110. d = do_create_message (flags, title, text);
  111. tty_getch ();
  112. dlg_run_done (d);
  113. destroy_dlg (d);
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. /** Show message box from background */
  117. #ifdef WITH_BACKGROUND
  118. static void
  119. bg_message (int dummy, int *flags, char *title, const char *text)
  120. {
  121. (void) dummy;
  122. title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
  123. fg_message (*flags, title, text);
  124. g_free (title);
  125. }
  126. #endif /* WITH_BACKGROUND */
  127. /* --------------------------------------------------------------------------------------------- */
  128. /**
  129. * Show dialog, not background safe.
  130. *
  131. * If the arguments "header" and "text" should be translated,
  132. * that MUST be done by the caller of fg_input_dialog_help().
  133. *
  134. * The argument "history_name" holds the name of a section
  135. * in the history file. Data entered in the input field of
  136. * the dialog box will be stored there.
  137. *
  138. */
  139. static char *
  140. fg_input_dialog_help (const char *header, const char *text, const char *help,
  141. const char *history_name, const char *def_text)
  142. {
  143. char *my_str;
  144. QuickWidget quick_widgets[] = {
  145. /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
  146. /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
  147. /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
  148. /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
  149. QUICK_END
  150. };
  151. int b0_len, b1_len, b_len, gap;
  152. char histname[64] = "inp|";
  153. int lines, cols;
  154. int len;
  155. int i;
  156. char *p_text;
  157. int ret;
  158. /* buttons */
  159. #ifdef ENABLE_NLS
  160. quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
  161. quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
  162. #endif /* ENABLE_NLS */
  163. b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
  164. b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
  165. b_len = b0_len + b1_len + 2; /* including gap */
  166. /* input line */
  167. if (history_name != NULL && *history_name != '\0')
  168. {
  169. g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
  170. quick_widgets[2].u.input.histname = histname;
  171. }
  172. /* The special value of def_text is used to identify password boxes
  173. and hide characters with "*". Don't save passwords in history! */
  174. if (def_text == INPUT_PASSWORD)
  175. {
  176. quick_widgets[2].u.input.flags = 1;
  177. histname[3] = '\0';
  178. quick_widgets[2].u.input.text = "";
  179. }
  180. /* text */
  181. p_text = g_strstrip (g_strdup (text));
  182. str_msg_term_size (p_text, &lines, &cols);
  183. quick_widgets[3].u.label.text = p_text;
  184. /* dialog width */
  185. len = str_term_width1 (header);
  186. len = max (max (len, cols) + 4, 64);
  187. len = min (max (len, b_len + 6), COLS);
  188. /* button locations */
  189. gap = (len - 8 - b_len) / 3;
  190. quick_widgets[1].relative_x = 3 + gap;
  191. quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
  192. {
  193. QuickDialog Quick_input = {
  194. len, lines + 6, -1, -1, header,
  195. help, quick_widgets, NULL, TRUE
  196. };
  197. for (i = 0; i < 4; i++)
  198. {
  199. quick_widgets[i].x_divisions = Quick_input.xlen;
  200. quick_widgets[i].y_divisions = Quick_input.ylen;
  201. }
  202. for (i = 0; i < 3; i++)
  203. quick_widgets[i].relative_y += 2 + lines;
  204. /* input line length */
  205. quick_widgets[2].u.input.len = Quick_input.xlen - 6;
  206. ret = quick_dialog (&Quick_input);
  207. }
  208. g_free (p_text);
  209. return (ret != B_CANCEL) ? my_str : NULL;
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. #ifdef WITH_BACKGROUND
  213. static int
  214. wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
  215. {
  216. ev_background_parent_call_t event_data;
  217. event_data.routine = routine;
  218. event_data.ctx = ctx;
  219. event_data.argc = argc;
  220. va_start (event_data.ap, argc);
  221. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
  222. va_end (event_data.ap);
  223. return event_data.ret.i;
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. static char *
  227. wtools_parent_call_string (void *routine, int argc, ...)
  228. {
  229. ev_background_parent_call_t event_data;
  230. event_data.routine = routine;
  231. event_data.argc = argc;
  232. va_start (event_data.ap, argc);
  233. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
  234. va_end (event_data.ap);
  235. return event_data.ret.s;
  236. }
  237. #endif /* WITH_BACKGROUND */
  238. /* --------------------------------------------------------------------------------------------- */
  239. /*** public functions ****************************************************************************/
  240. /* --------------------------------------------------------------------------------------------- */
  241. /** Used to ask questions to the user */
  242. int
  243. query_dialog (const char *header, const char *text, int flags, int count, ...)
  244. {
  245. va_list ap;
  246. Dlg_head *query_dlg;
  247. WButton *button;
  248. WButton *defbutton = NULL;
  249. int win_len = 0;
  250. int i;
  251. int result = -1;
  252. int cols, lines;
  253. char *cur_name;
  254. const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
  255. if (header == MSG_ERROR)
  256. header = _("Error");
  257. if (count > 0)
  258. {
  259. va_start (ap, count);
  260. for (i = 0; i < count; i++)
  261. {
  262. char *cp = va_arg (ap, char *);
  263. win_len += str_term_width1 (cp) + 6;
  264. if (strchr (cp, '&') != NULL)
  265. win_len--;
  266. }
  267. va_end (ap);
  268. }
  269. /* count coordinates */
  270. str_msg_term_size (text, &lines, &cols);
  271. cols = 6 + max (win_len, max (str_term_width1 (header), cols));
  272. lines += 4 + (count > 0 ? 2 : 0);
  273. /* prepare dialog */
  274. query_dlg =
  275. create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
  276. "[QueryBox]", header, DLG_NONE);
  277. if (count > 0)
  278. {
  279. cols = (cols - win_len - 2) / 2 + 2;
  280. va_start (ap, count);
  281. for (i = 0; i < count; i++)
  282. {
  283. int xpos;
  284. cur_name = va_arg (ap, char *);
  285. xpos = str_term_width1 (cur_name) + 6;
  286. if (strchr (cur_name, '&') != NULL)
  287. xpos--;
  288. button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
  289. add_widget (query_dlg, button);
  290. cols += xpos;
  291. if (i == sel_pos)
  292. defbutton = button;
  293. }
  294. va_end (ap);
  295. add_widget (query_dlg, label_new (2, 3, text));
  296. /* do resize before running and selecting any widget */
  297. default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
  298. if (defbutton)
  299. dlg_select_widget (defbutton);
  300. /* run dialog and make result */
  301. switch (run_dlg (query_dlg))
  302. {
  303. case B_CANCEL:
  304. break;
  305. default:
  306. result = query_dlg->ret_value - B_USER;
  307. }
  308. /* free used memory */
  309. destroy_dlg (query_dlg);
  310. }
  311. else
  312. {
  313. add_widget (query_dlg, label_new (2, 3, text));
  314. add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
  315. last_query_dlg = query_dlg;
  316. }
  317. sel_pos = 0;
  318. return result;
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. void
  322. query_set_sel (int new_sel)
  323. {
  324. sel_pos = new_sel;
  325. }
  326. /* --------------------------------------------------------------------------------------------- */
  327. /**
  328. * Create message dialog. The caller must call dlg_run_done() and
  329. * destroy_dlg() to dismiss it. Not safe to call from background.
  330. */
  331. struct Dlg_head *
  332. create_message (int flags, const char *title, const char *text, ...)
  333. {
  334. va_list args;
  335. Dlg_head *d;
  336. char *p;
  337. va_start (args, text);
  338. p = g_strdup_vprintf (text, args);
  339. va_end (args);
  340. d = do_create_message (flags, title, p);
  341. g_free (p);
  342. return d;
  343. }
  344. /* --------------------------------------------------------------------------------------------- */
  345. /** Show message box, background safe */
  346. void
  347. message (int flags, const char *title, const char *text, ...)
  348. {
  349. char *p;
  350. va_list ap;
  351. va_start (ap, text);
  352. p = g_strdup_vprintf (text, ap);
  353. va_end (ap);
  354. if (title == MSG_ERROR)
  355. title = _("Error");
  356. #ifdef WITH_BACKGROUND
  357. if (mc_global.we_are_background)
  358. {
  359. union
  360. {
  361. void *p;
  362. void (*f) (int, int *, char *, const char *);
  363. } func;
  364. func.f = bg_message;
  365. wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
  366. strlen (p), p);
  367. }
  368. else
  369. #endif /* WITH_BACKGROUND */
  370. fg_message (flags, title, p);
  371. g_free (p);
  372. }
  373. /* --------------------------------------------------------------------------------------------- */
  374. /**
  375. * Show input dialog, background safe.
  376. *
  377. * If the arguments "header" and "text" should be translated,
  378. * that MUST be done by the caller of these wrappers.
  379. */
  380. char *
  381. input_dialog_help (const char *header, const char *text, const char *help,
  382. const char *history_name, const char *def_text)
  383. {
  384. #ifdef WITH_BACKGROUND
  385. if (mc_global.we_are_background)
  386. {
  387. union
  388. {
  389. void *p;
  390. char *(*f) (const char *, const char *, const char *, const char *, const char *);
  391. } func;
  392. func.f = fg_input_dialog_help;
  393. return wtools_parent_call_string (func.p, 5,
  394. strlen (header), header, strlen (text),
  395. text, strlen (help), help,
  396. strlen (history_name), history_name,
  397. strlen (def_text), def_text);
  398. }
  399. else
  400. #endif /* WITH_BACKGROUND */
  401. return fg_input_dialog_help (header, text, help, history_name, def_text);
  402. }
  403. /* --------------------------------------------------------------------------------------------- */
  404. /** Show input dialog with default help, background safe */
  405. char *
  406. input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
  407. {
  408. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
  409. }
  410. /* --------------------------------------------------------------------------------------------- */
  411. char *
  412. input_expand_dialog (const char *header, const char *text,
  413. const char *history_name, const char *def_text)
  414. {
  415. char *result;
  416. char *expanded;
  417. result = input_dialog (header, text, history_name, def_text);
  418. if (result)
  419. {
  420. expanded = tilde_expand (result);
  421. g_free (result);
  422. return expanded;
  423. }
  424. return result;
  425. }
  426. /* --------------------------------------------------------------------------------------------- */