wtools.c 13 KB

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