wtools.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994-2017
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Miguel de Icaza, 1994, 1995, 1996
  7. Radek Doulik, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1995
  10. Andrew Borodin <aborodin@vmail.ru>, 2009-2014
  11. This file is part of the Midnight Commander.
  12. The Midnight Commander is free software: you can redistribute it
  13. and/or modify it under the terms of the GNU General Public License as
  14. published by the Free Software Foundation, either version 3 of the License,
  15. or (at your option) any later version.
  16. The Midnight Commander is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /** \file wtools.c
  24. * \brief Source: widget based utility functions
  25. */
  26. #include <config.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h"
  31. #include "lib/tty/key.h" /* tty_getch() */
  32. #include "lib/strutil.h"
  33. #include "lib/util.h" /* tilde_expand() */
  34. #include "lib/widget.h"
  35. #include "lib/event.h" /* mc_event_raise() */
  36. /*** global variables ****************************************************************************/
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** file scope variables ************************************************************************/
  40. static WDialog *last_query_dlg;
  41. static int sel_pos = 0;
  42. /* --------------------------------------------------------------------------------------------- */
  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 ((w->pos_flags & WPOS_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 || (WIDGET (prev_dlg)->pos_flags & WPOS_FULLSCREEN) != 0)
  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, w->lines, 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 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. int win_len = 0;
  221. int i;
  222. int result = -1;
  223. int cols, lines;
  224. const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
  225. widget_pos_flags_t pos_flags =
  226. (flags & D_CENTER) != 0 ? (WPOS_CENTER | WPOS_TRYUP) : WPOS_KEEP_DEFAULT;
  227. if (header == MSG_ERROR)
  228. header = _("Error");
  229. if (count > 0)
  230. {
  231. va_start (ap, count);
  232. for (i = 0; i < count; i++)
  233. {
  234. char *cp = va_arg (ap, char *);
  235. win_len += str_term_width1 (cp) + 6;
  236. if (strchr (cp, '&') != NULL)
  237. win_len--;
  238. }
  239. va_end (ap);
  240. }
  241. /* count coordinates */
  242. str_msg_term_size (text, &lines, &cols);
  243. cols = 6 + MAX (win_len, MAX (str_term_width1 (header), cols));
  244. lines += 4 + (count > 0 ? 2 : 0);
  245. /* prepare dialog */
  246. query_dlg =
  247. dlg_create (TRUE, 0, 0, lines, cols, pos_flags, FALSE, query_colors, query_default_callback,
  248. NULL, "[QueryBox]", header);
  249. if (count > 0)
  250. {
  251. WButton *defbutton = NULL;
  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. char *cur_name;
  261. cur_name = va_arg (ap, char *);
  262. xpos = str_term_width1 (cur_name) + 6;
  263. if (strchr (cur_name, '&') != NULL)
  264. xpos--;
  265. button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, NULL);
  266. add_widget (query_dlg, button);
  267. cols += xpos;
  268. if (i == sel_pos)
  269. defbutton = button;
  270. }
  271. va_end (ap);
  272. /* do resize before running and selecting any widget */
  273. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  274. if (defbutton != NULL)
  275. widget_select (WIDGET (defbutton));
  276. /* run dialog and make result */
  277. switch (dlg_run (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. dlg_destroy (query_dlg);
  286. }
  287. else
  288. {
  289. add_widget_autopos (query_dlg, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
  290. NULL);
  291. add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", NULL));
  292. last_query_dlg = query_dlg;
  293. }
  294. sel_pos = 0;
  295. return result;
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. void
  299. query_set_sel (int new_sel)
  300. {
  301. sel_pos = new_sel;
  302. }
  303. /* --------------------------------------------------------------------------------------------- */
  304. /**
  305. * Create message dialog. The caller must call dlg_run_done() and
  306. * dlg_destroy() to dismiss it. Not safe to call from background.
  307. */
  308. WDialog *
  309. create_message (int flags, const char *title, const char *text, ...)
  310. {
  311. va_list args;
  312. WDialog *d;
  313. char *p;
  314. va_start (args, text);
  315. p = g_strdup_vprintf (text, args);
  316. va_end (args);
  317. d = do_create_message (flags, title, p);
  318. g_free (p);
  319. return d;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. /** Show message box, background safe */
  323. void
  324. message (int flags, const char *title, const char *text, ...)
  325. {
  326. char *p;
  327. va_list ap;
  328. va_start (ap, text);
  329. p = g_strdup_vprintf (text, ap);
  330. va_end (ap);
  331. if (title == MSG_ERROR)
  332. title = _("Error");
  333. #ifdef ENABLE_BACKGROUND
  334. if (mc_global.we_are_background)
  335. {
  336. union
  337. {
  338. void *p;
  339. void (*f) (int, int *, char *, const char *);
  340. } func;
  341. func.f = bg_message;
  342. wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
  343. strlen (p), p);
  344. }
  345. else
  346. #endif /* ENABLE_BACKGROUND */
  347. fg_message (flags, title, p);
  348. g_free (p);
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. /** Show error message box */
  352. gboolean
  353. mc_error_message (GError ** mcerror, int *code)
  354. {
  355. if (mcerror == NULL || *mcerror == NULL)
  356. return FALSE;
  357. if ((*mcerror)->code == 0)
  358. message (D_ERROR, MSG_ERROR, "%s", (*mcerror)->message);
  359. else
  360. message (D_ERROR, MSG_ERROR, _("%s (%d)"), (*mcerror)->message, (*mcerror)->code);
  361. if (code != NULL)
  362. *code = (*mcerror)->code;
  363. g_error_free (*mcerror);
  364. *mcerror = NULL;
  365. return TRUE;
  366. }
  367. /* --------------------------------------------------------------------------------------------- */
  368. /**
  369. * Show input dialog, background safe.
  370. *
  371. * If the arguments "header" and "text" should be translated,
  372. * that MUST be done by the caller of these wrappers.
  373. */
  374. char *
  375. input_dialog_help (const char *header, const char *text, const char *help,
  376. const char *history_name, const char *def_text, gboolean strip_password,
  377. input_complete_t completion_flags)
  378. {
  379. #ifdef ENABLE_BACKGROUND
  380. if (mc_global.we_are_background)
  381. {
  382. union
  383. {
  384. void *p;
  385. char *(*f) (const char *, const char *, const char *, const char *, const char *,
  386. gboolean, input_complete_t);
  387. } func;
  388. func.f = fg_input_dialog_help;
  389. return wtools_parent_call_string (func.p, 7,
  390. strlen (header), header, strlen (text),
  391. text, strlen (help), help,
  392. strlen (history_name), history_name,
  393. strlen (def_text), def_text,
  394. sizeof (gboolean), strip_password,
  395. sizeof (input_complete_t), completion_flags);
  396. }
  397. else
  398. #endif /* ENABLE_BACKGROUND */
  399. return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password,
  400. completion_flags);
  401. }
  402. /* --------------------------------------------------------------------------------------------- */
  403. /** Show input dialog with default help, background safe */
  404. char *
  405. input_dialog (const char *header, const char *text, const char *history_name, const char *def_text,
  406. input_complete_t completion_flags)
  407. {
  408. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE,
  409. completion_flags);
  410. }
  411. /* --------------------------------------------------------------------------------------------- */
  412. char *
  413. input_expand_dialog (const char *header, const char *text,
  414. const char *history_name, const char *def_text,
  415. input_complete_t completion_flags)
  416. {
  417. char *result;
  418. result = input_dialog (header, text, history_name, def_text, completion_flags);
  419. if (result)
  420. {
  421. char *expanded;
  422. expanded = tilde_expand (result);
  423. g_free (result);
  424. return expanded;
  425. }
  426. return result;
  427. }
  428. /* --------------------------------------------------------------------------------------------- */
  429. /**
  430. * Create status message window object and initialize it
  431. *
  432. * @param title window title
  433. * @param delay initial delay to raise window in seconds
  434. * @param init_cb callback to initialize user-defined part of status message
  435. * @param update_cb callback to update of status message
  436. * @param deinit_cb callback to deinitialize user-defined part of status message
  437. *
  438. * @return newly allocate status message window
  439. */
  440. status_msg_t *
  441. status_msg_create (const char *title, double delay, status_msg_cb init_cb,
  442. status_msg_update_cb update_cb, status_msg_cb deinit_cb)
  443. {
  444. status_msg_t *sm;
  445. sm = g_try_new (status_msg_t, 1);
  446. status_msg_init (sm, title, delay, init_cb, update_cb, deinit_cb);
  447. return sm;
  448. }
  449. /* --------------------------------------------------------------------------------------------- */
  450. /**
  451. * Destroy status message window object
  452. *
  453. * @param sm status message window object
  454. */
  455. void
  456. status_msg_destroy (status_msg_t * sm)
  457. {
  458. status_msg_deinit (sm);
  459. g_free (sm);
  460. }
  461. /* --------------------------------------------------------------------------------------------- */
  462. /**
  463. * Initialize already created status message window object
  464. *
  465. * @param sm status message window object
  466. * @param title window title
  467. * @param delay initial delay to raise window in seconds
  468. * @param init_cb callback to initialize user-defined part of status message
  469. * @param update_cb callback to update of status message
  470. * @param deinit_cb callback to deinitialize user-defined part of status message
  471. */
  472. void
  473. status_msg_init (status_msg_t * sm, const char *title, double delay, status_msg_cb init_cb,
  474. status_msg_update_cb update_cb, status_msg_cb deinit_cb)
  475. {
  476. guint64 start;
  477. /* repaint screen to remove previous finished dialog */
  478. mc_refresh ();
  479. start = mc_timer_elapsed (mc_global.timer);
  480. sm->dlg = dlg_create (TRUE, 0, 0, 7, MIN (MAX (40, COLS / 2), COLS), WPOS_CENTER, FALSE,
  481. dialog_colors, NULL, NULL, NULL, title);
  482. sm->start = start;
  483. sm->delay = (guint64) (delay * G_USEC_PER_SEC);
  484. sm->block = FALSE;
  485. sm->init = init_cb;
  486. sm->update = update_cb;
  487. sm->deinit = deinit_cb;
  488. if (sm->init != NULL)
  489. sm->init (sm);
  490. if (mc_time_elapsed (&start, sm->delay))
  491. {
  492. /* We will manage the dialog without any help, that's why we have to call dlg_init */
  493. dlg_init (sm->dlg);
  494. }
  495. }
  496. /* --------------------------------------------------------------------------------------------- */
  497. /**
  498. * Deinitialize status message window object
  499. *
  500. * @param sm status message window object
  501. */
  502. void
  503. status_msg_deinit (status_msg_t * sm)
  504. {
  505. if (sm == NULL)
  506. return;
  507. if (sm->deinit != NULL)
  508. sm->deinit (sm);
  509. /* close and destroy dialog */
  510. dlg_run_done (sm->dlg);
  511. dlg_destroy (sm->dlg);
  512. }
  513. /* --------------------------------------------------------------------------------------------- */
  514. /**
  515. * Update status message window
  516. *
  517. * @param sm status message window object
  518. *
  519. * @return value of pressed key
  520. */
  521. int
  522. status_msg_common_update (status_msg_t * sm)
  523. {
  524. int c;
  525. Gpm_Event event;
  526. if (sm == NULL)
  527. return B_ENTER;
  528. /* This should not happen, but... */
  529. if (sm->dlg == NULL)
  530. return B_ENTER;
  531. if (widget_get_state (WIDGET (sm->dlg), WST_CONSTRUCT))
  532. {
  533. /* dialog is not shown yet */
  534. /* do not change sm->start */
  535. guint64 start = sm->start;
  536. if (mc_time_elapsed (&start, sm->delay))
  537. dlg_init (sm->dlg);
  538. return B_ENTER;
  539. }
  540. event.x = -1; /* Don't show the GPM cursor */
  541. c = tty_get_event (&event, FALSE, sm->block);
  542. if (c == EV_NONE)
  543. return B_ENTER;
  544. /* Reinitialize by non-B_CANCEL value to avoid old values
  545. after events other than selecting a button */
  546. sm->dlg->ret_value = B_ENTER;
  547. dlg_process_event (sm->dlg, c, &event);
  548. return sm->dlg->ret_value;
  549. }
  550. /* --------------------------------------------------------------------------------------------- */
  551. /**
  552. * Callback to initialize already created simple status message window object
  553. *
  554. * @param sm status message window object
  555. */
  556. void
  557. simple_status_msg_init_cb (status_msg_t * sm)
  558. {
  559. simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
  560. Widget *wd = WIDGET (sm->dlg);
  561. const char *b_name = N_("&Abort");
  562. int b_width;
  563. int wd_width, y;
  564. Widget *b;
  565. #ifdef ENABLE_NLS
  566. b_name = _(b_name);
  567. #endif
  568. b_width = str_term_width1 (b_name) + 4;
  569. wd_width = MAX (wd->cols, b_width + 6);
  570. y = 2;
  571. ssm->label = label_new (y++, 3, "");
  572. add_widget_autopos (sm->dlg, ssm->label, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
  573. add_widget (sm->dlg, hline_new (y++, -1, -1));
  574. b = WIDGET (button_new (y++, 3, B_CANCEL, NORMAL_BUTTON, b_name, NULL));
  575. add_widget_autopos (sm->dlg, b, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
  576. widget_set_size (wd, wd->y, wd->x, y + 2, wd_width);
  577. }
  578. /* --------------------------------------------------------------------------------------------- */