wtools.c 16 KB

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