wtools.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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, 2012, 2013
  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, 2012, 2013
  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 WDialog *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. query_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  48. {
  49. WDialog *h = DIALOG (w);
  50. switch (msg)
  51. {
  52. case MSG_RESIZE:
  53. if ((h->flags & DLG_CENTER) == 0)
  54. {
  55. WDialog *prev_dlg = NULL;
  56. int ypos, xpos;
  57. /* get dialog under h */
  58. if (top_dlg != NULL)
  59. {
  60. if (top_dlg->data != (void *) h)
  61. prev_dlg = DIALOG (top_dlg->data);
  62. else
  63. {
  64. GList *p;
  65. /* Top dialog is current if it is visible.
  66. Get previous dialog in stack */
  67. p = g_list_next (top_dlg);
  68. if (p != NULL)
  69. prev_dlg = DIALOG (p->data);
  70. }
  71. }
  72. /* if previous dialog is not fullscreen'd -- overlap it */
  73. if (prev_dlg == NULL || prev_dlg->fullscreen)
  74. ypos = LINES / 3 - (w->lines - 3) / 2;
  75. else
  76. ypos = WIDGET (prev_dlg)->y + 2;
  77. xpos = COLS / 2 - w->cols / 2;
  78. /* set position */
  79. dlg_set_position (h, ypos, xpos, ypos + w->lines, xpos + w->cols);
  80. return MSG_HANDLED;
  81. }
  82. /* fallthrough */
  83. default:
  84. return dlg_default_callback (w, sender, msg, parm, data);
  85. }
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. /** Create message dialog */
  89. static struct WDialog *
  90. do_create_message (int flags, const char *title, const char *text)
  91. {
  92. char *p;
  93. WDialog *d;
  94. /* Add empty lines before and after the message */
  95. p = g_strconcat ("\n", text, "\n", (char *) NULL);
  96. query_dialog (title, p, flags, 0);
  97. d = last_query_dlg;
  98. /* do resize before initing and running */
  99. send_message (d, NULL, MSG_RESIZE, 0, NULL);
  100. dlg_init (d);
  101. g_free (p);
  102. return d;
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. /**
  106. * Show message dialog. Dismiss it when any key is pressed.
  107. * Not safe to call from background.
  108. */
  109. static void
  110. fg_message (int flags, const char *title, const char *text)
  111. {
  112. WDialog *d;
  113. d = do_create_message (flags, title, text);
  114. tty_getch ();
  115. dlg_run_done (d);
  116. dlg_destroy (d);
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. /** Show message box from background */
  120. #ifdef ENABLE_BACKGROUND
  121. static void
  122. bg_message (int dummy, int *flags, char *title, const char *text)
  123. {
  124. (void) dummy;
  125. title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
  126. fg_message (*flags, title, text);
  127. g_free (title);
  128. }
  129. #endif /* ENABLE_BACKGROUND */
  130. /* --------------------------------------------------------------------------------------------- */
  131. /**
  132. * Show dialog, not background safe.
  133. *
  134. * If the arguments "header" and "text" should be translated,
  135. * that MUST be done by the caller of fg_input_dialog_help().
  136. *
  137. * The argument "history_name" holds the name of a section
  138. * in the history file. Data entered in the input field of
  139. * the dialog box will be stored there.
  140. *
  141. */
  142. static char *
  143. fg_input_dialog_help (const char *header, const char *text, const char *help,
  144. const char *history_name, const char *def_text, gboolean strip_password,
  145. input_complete_t completion_flags)
  146. {
  147. char *p_text;
  148. char histname[64] = "inp|";
  149. gboolean is_passwd = FALSE;
  150. char *my_str;
  151. int ret;
  152. /* label text */
  153. p_text = g_strstrip (g_strdup (text));
  154. /* input history */
  155. if (history_name != NULL && *history_name != '\0')
  156. g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
  157. /* The special value of def_text is used to identify password boxes
  158. and hide characters with "*". Don't save passwords in history! */
  159. if (def_text == INPUT_PASSWORD)
  160. {
  161. is_passwd = TRUE;
  162. histname[3] = '\0';
  163. def_text = "";
  164. }
  165. {
  166. quick_widget_t quick_widgets[] = {
  167. /* *INDENT-OFF* */
  168. QUICK_LABELED_INPUT (p_text, input_label_above, def_text, histname, &my_str,
  169. NULL, is_passwd, strip_password, completion_flags),
  170. QUICK_BUTTONS_OK_CANCEL,
  171. QUICK_END
  172. /* *INDENT-ON* */
  173. };
  174. quick_dialog_t qdlg = {
  175. -1, -1, COLS / 2, header,
  176. help, quick_widgets, NULL, NULL
  177. };
  178. ret = quick_dialog (&qdlg);
  179. }
  180. g_free (p_text);
  181. return (ret != B_CANCEL) ? my_str : NULL;
  182. }
  183. /* --------------------------------------------------------------------------------------------- */
  184. #ifdef ENABLE_BACKGROUND
  185. static int
  186. wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
  187. {
  188. ev_background_parent_call_t event_data;
  189. event_data.routine = routine;
  190. event_data.ctx = ctx;
  191. event_data.argc = argc;
  192. va_start (event_data.ap, argc);
  193. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
  194. va_end (event_data.ap);
  195. return event_data.ret.i;
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. static char *
  199. wtools_parent_call_string (void *routine, int argc, ...)
  200. {
  201. ev_background_parent_call_t event_data;
  202. event_data.routine = routine;
  203. event_data.argc = argc;
  204. va_start (event_data.ap, argc);
  205. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
  206. va_end (event_data.ap);
  207. return event_data.ret.s;
  208. }
  209. #endif /* ENABLE_BACKGROUND */
  210. /* --------------------------------------------------------------------------------------------- */
  211. /*** public functions ****************************************************************************/
  212. /* --------------------------------------------------------------------------------------------- */
  213. /** Used to ask questions to the user */
  214. int
  215. query_dialog (const char *header, const char *text, int flags, int count, ...)
  216. {
  217. va_list ap;
  218. WDialog *query_dlg;
  219. WButton *button;
  220. WButton *defbutton = NULL;
  221. int win_len = 0;
  222. int i;
  223. int result = -1;
  224. int cols, lines;
  225. char *cur_name;
  226. const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
  227. dlg_flags_t dlg_flags = (flags & D_CENTER) != 0 ? (DLG_CENTER | DLG_TRYUP) : DLG_NONE;
  228. if (header == MSG_ERROR)
  229. header = _("Error");
  230. if (count > 0)
  231. {
  232. va_start (ap, count);
  233. for (i = 0; i < count; i++)
  234. {
  235. char *cp = va_arg (ap, char *);
  236. win_len += str_term_width1 (cp) + 6;
  237. if (strchr (cp, '&') != NULL)
  238. win_len--;
  239. }
  240. va_end (ap);
  241. }
  242. /* count coordinates */
  243. str_msg_term_size (text, &lines, &cols);
  244. cols = 6 + max (win_len, max (str_term_width1 (header), cols));
  245. lines += 4 + (count > 0 ? 2 : 0);
  246. /* prepare dialog */
  247. query_dlg =
  248. dlg_create (TRUE, 0, 0, lines, cols, query_colors, query_default_callback, NULL,
  249. "[QueryBox]", header, dlg_flags);
  250. if (count > 0)
  251. {
  252. add_widget_autopos (query_dlg, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
  253. NULL);
  254. add_widget (query_dlg, hline_new (lines - 4, -1, -1));
  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, NULL);
  265. add_widget (query_dlg, button);
  266. cols += xpos;
  267. if (i == sel_pos)
  268. defbutton = button;
  269. }
  270. va_end (ap);
  271. /* do resize before running and selecting any widget */
  272. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  273. if (defbutton != NULL)
  274. dlg_select_widget (defbutton);
  275. /* run dialog and make result */
  276. switch (dlg_run (query_dlg))
  277. {
  278. case B_CANCEL:
  279. break;
  280. default:
  281. result = query_dlg->ret_value - B_USER;
  282. }
  283. /* free used memory */
  284. dlg_destroy (query_dlg);
  285. }
  286. else
  287. {
  288. add_widget_autopos (query_dlg, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
  289. NULL);
  290. add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", NULL));
  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. * dlg_destroy() to dismiss it. Not safe to call from background.
  306. */
  307. struct WDialog *
  308. create_message (int flags, const char *title, const char *text, ...)
  309. {
  310. va_list args;
  311. WDialog *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 ENABLE_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 /* ENABLE_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, gboolean strip_password,
  359. input_complete_t completion_flags)
  360. {
  361. #ifdef ENABLE_BACKGROUND
  362. if (mc_global.we_are_background)
  363. {
  364. union
  365. {
  366. void *p;
  367. char *(*f) (const char *, const char *, const char *, const char *, const char *,
  368. gboolean, input_complete_t);
  369. } func;
  370. func.f = fg_input_dialog_help;
  371. return wtools_parent_call_string (func.p, 7,
  372. strlen (header), header, strlen (text),
  373. text, strlen (help), help,
  374. strlen (history_name), history_name,
  375. strlen (def_text), def_text,
  376. sizeof (gboolean), strip_password,
  377. sizeof (input_complete_t), completion_flags);
  378. }
  379. else
  380. #endif /* ENABLE_BACKGROUND */
  381. return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password,
  382. completion_flags);
  383. }
  384. /* --------------------------------------------------------------------------------------------- */
  385. /** Show input dialog with default help, background safe */
  386. char *
  387. input_dialog (const char *header, const char *text, const char *history_name, const char *def_text,
  388. input_complete_t completion_flags)
  389. {
  390. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE,
  391. completion_flags);
  392. }
  393. /* --------------------------------------------------------------------------------------------- */
  394. char *
  395. input_expand_dialog (const char *header, const char *text,
  396. const char *history_name, const char *def_text,
  397. input_complete_t completion_flags)
  398. {
  399. char *result;
  400. char *expanded;
  401. result = input_dialog (header, text, history_name, def_text, completion_flags);
  402. if (result)
  403. {
  404. expanded = tilde_expand (result);
  405. g_free (result);
  406. return expanded;
  407. }
  408. return result;
  409. }
  410. /* --------------------------------------------------------------------------------------------- */