wtools.c 14 KB

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