wtools.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. int xpos = COLS / 2 - h->cols / 2;
  51. int ypos = LINES / 3 - (h->lines - 3) / 2;
  52. /* set position */
  53. dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
  54. }
  55. return MSG_HANDLED;
  56. default:
  57. return default_dlg_callback (h, sender, msg, parm, data);
  58. }
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. /** Create message dialog */
  62. static struct Dlg_head *
  63. do_create_message (int flags, const char *title, const char *text)
  64. {
  65. char *p;
  66. Dlg_head *d;
  67. /* Add empty lines before and after the message */
  68. p = g_strconcat ("\n", text, "\n", (char *) NULL);
  69. query_dialog (title, p, flags, 0);
  70. d = last_query_dlg;
  71. /* do resize before initing and running */
  72. default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
  73. init_dlg (d);
  74. g_free (p);
  75. return d;
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. /**
  79. * Show message dialog. Dismiss it when any key is pressed.
  80. * Not safe to call from background.
  81. */
  82. static void
  83. fg_message (int flags, const char *title, const char *text)
  84. {
  85. Dlg_head *d;
  86. d = do_create_message (flags, title, text);
  87. tty_getch ();
  88. dlg_run_done (d);
  89. destroy_dlg (d);
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /** Show message box from background */
  93. #ifdef WITH_BACKGROUND
  94. static void
  95. bg_message (int dummy, int *flags, char *title, const char *text)
  96. {
  97. (void) dummy;
  98. title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
  99. fg_message (*flags, title, text);
  100. g_free (title);
  101. }
  102. #endif /* WITH_BACKGROUND */
  103. /* --------------------------------------------------------------------------------------------- */
  104. /**
  105. * Show dialog, not background safe.
  106. *
  107. * If the arguments "header" and "text" should be translated,
  108. * that MUST be done by the caller of fg_input_dialog_help().
  109. *
  110. * The argument "history_name" holds the name of a section
  111. * in the history file. Data entered in the input field of
  112. * the dialog box will be stored there.
  113. *
  114. */
  115. static char *
  116. fg_input_dialog_help (const char *header, const char *text, const char *help,
  117. const char *history_name, const char *def_text)
  118. {
  119. char *my_str;
  120. QuickWidget quick_widgets[] = {
  121. /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
  122. /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
  123. /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
  124. /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
  125. QUICK_END
  126. };
  127. int b0_len, b1_len, b_len, gap;
  128. char histname[64] = "inp|";
  129. int lines, cols;
  130. int len;
  131. int i;
  132. char *p_text;
  133. int ret;
  134. /* buttons */
  135. #ifdef ENABLE_NLS
  136. quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
  137. quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
  138. #endif /* ENABLE_NLS */
  139. b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
  140. b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
  141. b_len = b0_len + b1_len + 2; /* including gap */
  142. /* input line */
  143. if (history_name != NULL && *history_name != '\0')
  144. {
  145. g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
  146. quick_widgets[2].u.input.histname = histname;
  147. }
  148. /* The special value of def_text is used to identify password boxes
  149. and hide characters with "*". Don't save passwords in history! */
  150. if (def_text == INPUT_PASSWORD)
  151. {
  152. quick_widgets[2].u.input.flags = 1;
  153. histname[3] = '\0';
  154. quick_widgets[2].u.input.text = "";
  155. }
  156. /* text */
  157. p_text = g_strstrip (g_strdup (text));
  158. str_msg_term_size (p_text, &lines, &cols);
  159. quick_widgets[3].u.label.text = p_text;
  160. /* dialog width */
  161. len = str_term_width1 (header);
  162. len = max (max (len, cols) + 4, 64);
  163. len = min (max (len, b_len + 6), COLS);
  164. /* button locations */
  165. gap = (len - 8 - b_len) / 3;
  166. quick_widgets[1].relative_x = 3 + gap;
  167. quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
  168. {
  169. QuickDialog Quick_input = {
  170. len, lines + 6, -1, -1, header,
  171. help, quick_widgets, NULL, TRUE
  172. };
  173. for (i = 0; i < 4; i++)
  174. {
  175. quick_widgets[i].x_divisions = Quick_input.xlen;
  176. quick_widgets[i].y_divisions = Quick_input.ylen;
  177. }
  178. for (i = 0; i < 3; i++)
  179. quick_widgets[i].relative_y += 2 + lines;
  180. /* input line length */
  181. quick_widgets[2].u.input.len = Quick_input.xlen - 6;
  182. ret = quick_dialog (&Quick_input);
  183. }
  184. g_free (p_text);
  185. return (ret != B_CANCEL) ? my_str : NULL;
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. #ifdef WITH_BACKGROUND
  189. static int
  190. wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
  191. {
  192. ev_background_parent_call_t event_data;
  193. event_data.routine = routine;
  194. event_data.ctx = ctx;
  195. event_data.argc = argc;
  196. va_start (event_data.ap, argc);
  197. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
  198. va_end (event_data.ap);
  199. return event_data.ret.i;
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. static char *
  203. wtools_parent_call_string (void *routine, int argc, ...)
  204. {
  205. ev_background_parent_call_t event_data;
  206. event_data.routine = routine;
  207. event_data.argc = argc;
  208. va_start (event_data.ap, argc);
  209. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
  210. va_end (event_data.ap);
  211. return event_data.ret.s;
  212. }
  213. #endif /* WITH_BACKGROUND */
  214. /* --------------------------------------------------------------------------------------------- */
  215. /*** public functions ****************************************************************************/
  216. /* --------------------------------------------------------------------------------------------- */
  217. /** Used to ask questions to the user */
  218. int
  219. query_dialog (const char *header, const char *text, int flags, int count, ...)
  220. {
  221. va_list ap;
  222. Dlg_head *query_dlg;
  223. WButton *button;
  224. WButton *defbutton = NULL;
  225. int win_len = 0;
  226. int i;
  227. int result = -1;
  228. int cols, lines;
  229. char *cur_name;
  230. const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
  231. if (header == MSG_ERROR)
  232. header = _("Error");
  233. if (count > 0)
  234. {
  235. va_start (ap, count);
  236. for (i = 0; i < count; i++)
  237. {
  238. char *cp = va_arg (ap, char *);
  239. win_len += str_term_width1 (cp) + 6;
  240. if (strchr (cp, '&') != NULL)
  241. win_len--;
  242. }
  243. va_end (ap);
  244. }
  245. /* count coordinates */
  246. str_msg_term_size (text, &lines, &cols);
  247. cols = 6 + max (win_len, max (str_term_width1 (header), cols));
  248. lines += 4 + (count > 0 ? 2 : 0);
  249. /* prepare dialog */
  250. query_dlg =
  251. create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
  252. "[QueryBox]", header, DLG_NONE);
  253. if (count > 0)
  254. {
  255. cols = (cols - win_len - 2) / 2 + 2;
  256. va_start (ap, count);
  257. for (i = 0; i < count; i++)
  258. {
  259. int xpos;
  260. cur_name = va_arg (ap, char *);
  261. xpos = str_term_width1 (cur_name) + 6;
  262. if (strchr (cur_name, '&') != NULL)
  263. xpos--;
  264. button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
  265. add_widget (query_dlg, button);
  266. cols += xpos;
  267. if (i == sel_pos)
  268. defbutton = button;
  269. }
  270. va_end (ap);
  271. add_widget (query_dlg, label_new (2, 3, text));
  272. /* do resize before running and selecting any widget */
  273. default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
  274. if (defbutton)
  275. dlg_select_widget (defbutton);
  276. /* run dialog and make result */
  277. switch (run_dlg (query_dlg))
  278. {
  279. case B_CANCEL:
  280. break;
  281. default:
  282. result = query_dlg->ret_value - B_USER;
  283. }
  284. /* free used memory */
  285. destroy_dlg (query_dlg);
  286. }
  287. else
  288. {
  289. add_widget (query_dlg, label_new (2, 3, text));
  290. add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
  291. last_query_dlg = query_dlg;
  292. }
  293. sel_pos = 0;
  294. return result;
  295. }
  296. /* --------------------------------------------------------------------------------------------- */
  297. void
  298. query_set_sel (int new_sel)
  299. {
  300. sel_pos = new_sel;
  301. }
  302. /* --------------------------------------------------------------------------------------------- */
  303. /**
  304. * Create message dialog. The caller must call dlg_run_done() and
  305. * destroy_dlg() to dismiss it. Not safe to call from background.
  306. */
  307. struct Dlg_head *
  308. create_message (int flags, const char *title, const char *text, ...)
  309. {
  310. va_list args;
  311. Dlg_head *d;
  312. char *p;
  313. va_start (args, text);
  314. p = g_strdup_vprintf (text, args);
  315. va_end (args);
  316. d = do_create_message (flags, title, p);
  317. g_free (p);
  318. return d;
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. /** Show message box, background safe */
  322. void
  323. message (int flags, const char *title, const char *text, ...)
  324. {
  325. char *p;
  326. va_list ap;
  327. va_start (ap, text);
  328. p = g_strdup_vprintf (text, ap);
  329. va_end (ap);
  330. if (title == MSG_ERROR)
  331. title = _("Error");
  332. #ifdef WITH_BACKGROUND
  333. if (mc_global.we_are_background)
  334. {
  335. union
  336. {
  337. void *p;
  338. void (*f) (int, int *, char *, const char *);
  339. } func;
  340. func.f = bg_message;
  341. wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
  342. strlen (p), p);
  343. }
  344. else
  345. #endif /* WITH_BACKGROUND */
  346. fg_message (flags, title, p);
  347. g_free (p);
  348. }
  349. /* --------------------------------------------------------------------------------------------- */
  350. /**
  351. * Show input dialog, background safe.
  352. *
  353. * If the arguments "header" and "text" should be translated,
  354. * that MUST be done by the caller of these wrappers.
  355. */
  356. char *
  357. input_dialog_help (const char *header, const char *text, const char *help,
  358. const char *history_name, const char *def_text)
  359. {
  360. #ifdef WITH_BACKGROUND
  361. if (mc_global.we_are_background)
  362. {
  363. union
  364. {
  365. void *p;
  366. char *(*f) (const char *, const char *, const char *, const char *, const char *);
  367. } func;
  368. func.f = fg_input_dialog_help;
  369. return wtools_parent_call_string (func.p, 5,
  370. strlen (header), header, strlen (text),
  371. text, strlen (help), help,
  372. strlen (history_name), history_name,
  373. strlen (def_text), def_text);
  374. }
  375. else
  376. #endif /* WITH_BACKGROUND */
  377. return fg_input_dialog_help (header, text, help, history_name, def_text);
  378. }
  379. /* --------------------------------------------------------------------------------------------- */
  380. /** Show input dialog with default help, background safe */
  381. char *
  382. input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
  383. {
  384. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
  385. }
  386. /* --------------------------------------------------------------------------------------------- */
  387. char *
  388. input_expand_dialog (const char *header, const char *text,
  389. const char *history_name, const char *def_text)
  390. {
  391. char *result;
  392. char *expanded;
  393. result = input_dialog (header, text, history_name, def_text);
  394. if (result)
  395. {
  396. expanded = tilde_expand (result);
  397. g_free (result);
  398. return expanded;
  399. }
  400. return result;
  401. }
  402. /* --------------------------------------------------------------------------------------------- */
  403. void
  404. mc_refresh (void)
  405. {
  406. #ifdef WITH_BACKGROUND
  407. if (mc_global.we_are_background)
  408. return;
  409. #endif /* WITH_BACKGROUND */
  410. if (mc_global.tty.winch_flag == FALSE)
  411. tty_refresh ();
  412. else
  413. {
  414. /* if winch was caugth, we should do not only redraw screen, but
  415. reposition/resize all */
  416. dialog_change_screen_size ();
  417. }
  418. }
  419. /* --------------------------------------------------------------------------------------------- */