wtools.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994-2023
  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-2022
  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. WRect r;
  58. /* get dialog under h */
  59. if (top_dlg != NULL)
  60. {
  61. if (top_dlg->data != (void *) h)
  62. prev_dlg = DIALOG (top_dlg->data);
  63. else
  64. {
  65. GList *p;
  66. /* Top dialog is current if it is visible.
  67. Get previous dialog in stack */
  68. p = g_list_next (top_dlg);
  69. if (p != NULL)
  70. prev_dlg = DIALOG (p->data);
  71. }
  72. }
  73. /* if previous dialog is not fullscreen'd -- overlap it */
  74. if (prev_dlg == NULL || (WIDGET (prev_dlg)->pos_flags & WPOS_FULLSCREEN) != 0)
  75. ypos = LINES / 3 - (w->rect.lines - 3) / 2;
  76. else
  77. ypos = WIDGET (prev_dlg)->rect.y + 2;
  78. /* if dialog is too high, place it centered */
  79. if (ypos + w->rect.lines < LINES / 2)
  80. w->pos_flags |= WPOS_CENTER;
  81. xpos = COLS / 2 - w->rect.cols / 2;
  82. /* set position */
  83. rect_init (&r, ypos, xpos, w->rect.lines, w->rect.cols);
  84. return dlg_default_callback (w, NULL, MSG_RESIZE, 0, &r);
  85. }
  86. MC_FALLTHROUGH;
  87. default:
  88. return dlg_default_callback (w, sender, msg, parm, data);
  89. }
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /** Create message dialog */
  93. static WDialog *
  94. do_create_message (int flags, const char *title, const char *text)
  95. {
  96. char *p;
  97. WDialog *d;
  98. /* Add empty lines before and after the message */
  99. p = g_strconcat ("\n", text, "\n", (char *) NULL);
  100. query_dialog (title, p, flags, 0);
  101. d = last_query_dlg;
  102. /* do resize before initing and running */
  103. send_message (d, NULL, MSG_RESIZE, 0, NULL);
  104. dlg_init (d);
  105. g_free (p);
  106. return d;
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. /**
  110. * Show message dialog. Dismiss it when any key is pressed.
  111. * Not safe to call from background.
  112. */
  113. static void
  114. fg_message (int flags, const char *title, const char *text)
  115. {
  116. WDialog *d;
  117. d = do_create_message (flags, title, text);
  118. tty_getch ();
  119. dlg_run_done (d);
  120. widget_destroy (WIDGET (d));
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. /** Show message box from background */
  124. #ifdef ENABLE_BACKGROUND
  125. static void
  126. bg_message (int dummy, int *flags, char *title, const char *text)
  127. {
  128. (void) dummy;
  129. title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
  130. fg_message (*flags, title, text);
  131. g_free (title);
  132. }
  133. #endif /* ENABLE_BACKGROUND */
  134. /* --------------------------------------------------------------------------------------------- */
  135. /**
  136. * Show dialog, not background safe.
  137. *
  138. * If the arguments "header" and "text" should be translated,
  139. * that MUST be done by the caller of fg_input_dialog_help().
  140. *
  141. * The argument "history_name" holds the name of a section
  142. * in the history file. Data entered in the input field of
  143. * the dialog box will be stored there.
  144. *
  145. */
  146. static char *
  147. fg_input_dialog_help (const char *header, const char *text, const char *help,
  148. const char *history_name, const char *def_text, gboolean strip_password,
  149. input_complete_t completion_flags)
  150. {
  151. char *p_text;
  152. char histname[64] = "inp|";
  153. gboolean is_passwd = FALSE;
  154. char *my_str;
  155. int ret;
  156. /* label text */
  157. p_text = g_strstrip (g_strdup (text));
  158. /* input history */
  159. if (history_name != NULL && *history_name != '\0')
  160. g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
  161. /* The special value of def_text is used to identify password boxes
  162. and hide characters with "*". Don't save passwords in history! */
  163. if (def_text == INPUT_PASSWORD)
  164. {
  165. is_passwd = TRUE;
  166. histname[3] = '\0';
  167. def_text = "";
  168. }
  169. {
  170. quick_widget_t quick_widgets[] = {
  171. /* *INDENT-OFF* */
  172. QUICK_LABELED_INPUT (p_text, input_label_above, def_text, histname, &my_str,
  173. NULL, is_passwd, strip_password, completion_flags),
  174. QUICK_BUTTONS_OK_CANCEL,
  175. QUICK_END
  176. /* *INDENT-ON* */
  177. };
  178. WRect r = { -1, -1, 0, COLS / 2 };
  179. quick_dialog_t qdlg = {
  180. r, header, help,
  181. quick_widgets, NULL, NULL
  182. };
  183. ret = quick_dialog (&qdlg);
  184. }
  185. g_free (p_text);
  186. return (ret != B_CANCEL) ? my_str : NULL;
  187. }
  188. /* --------------------------------------------------------------------------------------------- */
  189. #ifdef ENABLE_BACKGROUND
  190. static int
  191. wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
  192. {
  193. ev_background_parent_call_t event_data;
  194. event_data.routine = routine;
  195. event_data.ctx = ctx;
  196. event_data.argc = argc;
  197. va_start (event_data.ap, argc);
  198. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
  199. va_end (event_data.ap);
  200. return event_data.ret.i;
  201. }
  202. /* --------------------------------------------------------------------------------------------- */
  203. static char *
  204. wtools_parent_call_string (void *routine, int argc, ...)
  205. {
  206. ev_background_parent_call_t event_data;
  207. event_data.routine = routine;
  208. event_data.argc = argc;
  209. va_start (event_data.ap, argc);
  210. mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
  211. va_end (event_data.ap);
  212. return event_data.ret.s;
  213. }
  214. #endif /* ENABLE_BACKGROUND */
  215. /* --------------------------------------------------------------------------------------------- */
  216. /*** public functions ****************************************************************************/
  217. /* --------------------------------------------------------------------------------------------- */
  218. /** Used to ask questions to the user */
  219. int
  220. query_dialog (const char *header, const char *text, int flags, int count, ...)
  221. {
  222. va_list ap;
  223. WDialog *query_dlg;
  224. WGroup *g;
  225. WButton *button;
  226. int win_len = 0;
  227. int i;
  228. int result = -1;
  229. int cols, lines;
  230. const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
  231. widget_pos_flags_t pos_flags =
  232. (flags & D_CENTER) != 0 ? (WPOS_CENTER | WPOS_TRYUP) : WPOS_KEEP_DEFAULT;
  233. if (header == MSG_ERROR)
  234. header = _("Error");
  235. if (count > 0)
  236. {
  237. va_start (ap, count);
  238. for (i = 0; i < count; i++)
  239. {
  240. char *cp = va_arg (ap, char *);
  241. win_len += str_term_width1 (cp) + 6;
  242. if (strchr (cp, '&') != NULL)
  243. win_len--;
  244. }
  245. va_end (ap);
  246. }
  247. /* count coordinates */
  248. str_msg_term_size (text, &lines, &cols);
  249. cols = 6 + MAX (win_len, MAX (str_term_width1 (header), cols));
  250. lines += 4 + (count > 0 ? 2 : 0);
  251. /* prepare dialog */
  252. query_dlg =
  253. dlg_create (TRUE, 0, 0, lines, cols, pos_flags, FALSE, query_colors, query_default_callback,
  254. NULL, "[QueryBox]", header);
  255. g = GROUP (query_dlg);
  256. if (count > 0)
  257. {
  258. WButton *defbutton = NULL;
  259. group_add_widget_autopos (g, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
  260. NULL);
  261. group_add_widget (g, hline_new (lines - 4, -1, -1));
  262. cols = (cols - win_len - 2) / 2 + 2;
  263. va_start (ap, count);
  264. for (i = 0; i < count; i++)
  265. {
  266. int xpos;
  267. char *cur_name;
  268. cur_name = va_arg (ap, char *);
  269. xpos = str_term_width1 (cur_name) + 6;
  270. if (strchr (cur_name, '&') != NULL)
  271. xpos--;
  272. button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, NULL);
  273. group_add_widget (g, button);
  274. cols += xpos;
  275. if (i == sel_pos)
  276. defbutton = button;
  277. }
  278. va_end (ap);
  279. /* do resize before running and selecting any widget */
  280. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  281. if (defbutton != NULL)
  282. widget_select (WIDGET (defbutton));
  283. /* run dialog and make result */
  284. switch (dlg_run (query_dlg))
  285. {
  286. case B_CANCEL:
  287. break;
  288. default:
  289. result = query_dlg->ret_value - B_USER;
  290. }
  291. /* free used memory */
  292. widget_destroy (WIDGET (query_dlg));
  293. }
  294. else
  295. {
  296. group_add_widget_autopos (g, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
  297. NULL);
  298. group_add_widget (g, button_new (0, 0, 0, HIDDEN_BUTTON, "-", NULL));
  299. last_query_dlg = query_dlg;
  300. }
  301. sel_pos = 0;
  302. return result;
  303. }
  304. /* --------------------------------------------------------------------------------------------- */
  305. void
  306. query_set_sel (int new_sel)
  307. {
  308. sel_pos = new_sel;
  309. }
  310. /* --------------------------------------------------------------------------------------------- */
  311. /**
  312. * Create message dialog. The caller must call dlg_run_done() and
  313. * widget_destroy() to dismiss it. Not safe to call from background.
  314. */
  315. WDialog *
  316. create_message (int flags, const char *title, const char *text, ...)
  317. {
  318. va_list args;
  319. WDialog *d;
  320. char *p;
  321. va_start (args, text);
  322. p = g_strdup_vprintf (text, args);
  323. va_end (args);
  324. d = do_create_message (flags, title, p);
  325. g_free (p);
  326. return d;
  327. }
  328. /* --------------------------------------------------------------------------------------------- */
  329. /** Show message box, background safe */
  330. void
  331. message (int flags, const char *title, const char *text, ...)
  332. {
  333. char *p;
  334. va_list ap;
  335. va_start (ap, text);
  336. p = g_strdup_vprintf (text, ap);
  337. va_end (ap);
  338. if (title == MSG_ERROR)
  339. title = _("Error");
  340. #ifdef ENABLE_BACKGROUND
  341. if (mc_global.we_are_background)
  342. {
  343. union
  344. {
  345. void *p;
  346. void (*f) (int, int *, char *, const char *);
  347. } func;
  348. func.f = bg_message;
  349. wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
  350. strlen (p), p);
  351. }
  352. else
  353. #endif /* ENABLE_BACKGROUND */
  354. fg_message (flags, title, p);
  355. g_free (p);
  356. }
  357. /* --------------------------------------------------------------------------------------------- */
  358. /** Show error message box */
  359. gboolean
  360. mc_error_message (GError ** mcerror, int *code)
  361. {
  362. if (mcerror == NULL || *mcerror == NULL)
  363. return FALSE;
  364. if ((*mcerror)->code == 0)
  365. message (D_ERROR, MSG_ERROR, "%s", (*mcerror)->message);
  366. else
  367. message (D_ERROR, MSG_ERROR, _("%s (%d)"), (*mcerror)->message, (*mcerror)->code);
  368. if (code != NULL)
  369. *code = (*mcerror)->code;
  370. g_error_free (*mcerror);
  371. *mcerror = NULL;
  372. return TRUE;
  373. }
  374. /* --------------------------------------------------------------------------------------------- */
  375. /**
  376. * Show input dialog, background safe.
  377. *
  378. * If the arguments "header" and "text" should be translated,
  379. * that MUST be done by the caller of these wrappers.
  380. */
  381. char *
  382. input_dialog_help (const char *header, const char *text, const char *help,
  383. const char *history_name, const char *def_text, gboolean strip_password,
  384. input_complete_t completion_flags)
  385. {
  386. #ifdef ENABLE_BACKGROUND
  387. if (mc_global.we_are_background)
  388. {
  389. union
  390. {
  391. void *p;
  392. char *(*f) (const char *, const char *, const char *, const char *, const char *,
  393. gboolean, input_complete_t);
  394. } func;
  395. func.f = fg_input_dialog_help;
  396. return wtools_parent_call_string (func.p, 7,
  397. strlen (header), header, strlen (text),
  398. text, strlen (help), help,
  399. strlen (history_name), history_name,
  400. strlen (def_text), def_text,
  401. sizeof (gboolean), strip_password,
  402. sizeof (input_complete_t), completion_flags);
  403. }
  404. else
  405. #endif /* ENABLE_BACKGROUND */
  406. return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password,
  407. completion_flags);
  408. }
  409. /* --------------------------------------------------------------------------------------------- */
  410. /** Show input dialog with default help, background safe */
  411. char *
  412. input_dialog (const char *header, const char *text, const char *history_name, const char *def_text,
  413. input_complete_t completion_flags)
  414. {
  415. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE,
  416. completion_flags);
  417. }
  418. /* --------------------------------------------------------------------------------------------- */
  419. char *
  420. input_expand_dialog (const char *header, const char *text,
  421. const char *history_name, const char *def_text,
  422. input_complete_t completion_flags)
  423. {
  424. char *result;
  425. result = input_dialog (header, text, history_name, def_text, completion_flags);
  426. if (result)
  427. {
  428. char *expanded;
  429. expanded = tilde_expand (result);
  430. g_free (result);
  431. return expanded;
  432. }
  433. return result;
  434. }
  435. /* --------------------------------------------------------------------------------------------- */
  436. /**
  437. * Create status message window object and initialize it
  438. *
  439. * @param title window title
  440. * @param delay initial delay to raise window in seconds
  441. * @param init_cb callback to initialize user-defined part of status message
  442. * @param update_cb callback to update of status message
  443. * @param deinit_cb callback to deinitialize user-defined part of status message
  444. *
  445. * @return newly allocate status message window
  446. */
  447. status_msg_t *
  448. status_msg_create (const char *title, double delay, status_msg_cb init_cb,
  449. status_msg_update_cb update_cb, status_msg_cb deinit_cb)
  450. {
  451. status_msg_t *sm;
  452. sm = g_try_new (status_msg_t, 1);
  453. status_msg_init (sm, title, delay, init_cb, update_cb, deinit_cb);
  454. return sm;
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. /**
  458. * Destroy status message window object
  459. *
  460. * @param sm status message window object
  461. */
  462. void
  463. status_msg_destroy (status_msg_t * sm)
  464. {
  465. status_msg_deinit (sm);
  466. g_free (sm);
  467. }
  468. /* --------------------------------------------------------------------------------------------- */
  469. /**
  470. * Initialize already created status message window object
  471. *
  472. * @param sm status message window object
  473. * @param title window title
  474. * @param delay initial delay to raise window in seconds
  475. * @param init_cb callback to initialize user-defined part of status message
  476. * @param update_cb callback to update of status message
  477. * @param deinit_cb callback to deinitialize user-defined part of status message
  478. */
  479. void
  480. status_msg_init (status_msg_t * sm, const char *title, double delay, status_msg_cb init_cb,
  481. status_msg_update_cb update_cb, status_msg_cb deinit_cb)
  482. {
  483. gint64 start;
  484. /* repaint screen to remove previous finished dialog */
  485. mc_refresh ();
  486. start = g_get_monotonic_time ();
  487. sm->dlg = dlg_create (TRUE, 0, 0, 7, MIN (MAX (40, COLS / 2), COLS), WPOS_CENTER, FALSE,
  488. dialog_colors, NULL, NULL, NULL, title);
  489. sm->start = start;
  490. sm->delay = (gint64) (delay * G_USEC_PER_SEC);
  491. sm->block = FALSE;
  492. sm->init = init_cb;
  493. sm->update = update_cb;
  494. sm->deinit = deinit_cb;
  495. if (sm->init != NULL)
  496. sm->init (sm);
  497. if (mc_time_elapsed (&start, sm->delay))
  498. {
  499. /* We will manage the dialog without any help, that's why we have to call dlg_init */
  500. dlg_init (sm->dlg);
  501. }
  502. }
  503. /* --------------------------------------------------------------------------------------------- */
  504. /**
  505. * Deinitialize status message window object
  506. *
  507. * @param sm status message window object
  508. */
  509. void
  510. status_msg_deinit (status_msg_t * sm)
  511. {
  512. if (sm == NULL)
  513. return;
  514. if (sm->deinit != NULL)
  515. sm->deinit (sm);
  516. /* close and destroy dialog */
  517. dlg_run_done (sm->dlg);
  518. widget_destroy (WIDGET (sm->dlg));
  519. }
  520. /* --------------------------------------------------------------------------------------------- */
  521. /**
  522. * Update status message window
  523. *
  524. * @param sm status message window object
  525. *
  526. * @return value of pressed key
  527. */
  528. int
  529. status_msg_common_update (status_msg_t * sm)
  530. {
  531. int c;
  532. Gpm_Event event;
  533. if (sm == NULL)
  534. return B_ENTER;
  535. /* This should not happen, but... */
  536. if (sm->dlg == NULL)
  537. return B_ENTER;
  538. if (widget_get_state (WIDGET (sm->dlg), WST_CONSTRUCT))
  539. {
  540. /* dialog is not shown yet */
  541. /* do not change sm->start */
  542. gint64 start = sm->start;
  543. if (mc_time_elapsed (&start, sm->delay))
  544. dlg_init (sm->dlg);
  545. return B_ENTER;
  546. }
  547. event.x = -1; /* Don't show the GPM cursor */
  548. c = tty_get_event (&event, FALSE, sm->block);
  549. if (c == EV_NONE)
  550. return B_ENTER;
  551. /* Reinitialize by non-B_CANCEL value to avoid old values
  552. after events other than selecting a button */
  553. sm->dlg->ret_value = B_ENTER;
  554. dlg_process_event (sm->dlg, c, &event);
  555. return sm->dlg->ret_value;
  556. }
  557. /* --------------------------------------------------------------------------------------------- */
  558. /**
  559. * Callback to initialize already created simple status message window object
  560. *
  561. * @param sm status message window object
  562. */
  563. void
  564. simple_status_msg_init_cb (status_msg_t * sm)
  565. {
  566. simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
  567. Widget *wd = WIDGET (sm->dlg);
  568. WGroup *wg = GROUP (sm->dlg);
  569. WRect r;
  570. const char *b_name = N_("&Abort");
  571. int b_width;
  572. int wd_width, y;
  573. Widget *b;
  574. #ifdef ENABLE_NLS
  575. b_name = _(b_name);
  576. #endif
  577. b_width = str_term_width1 (b_name) + 4;
  578. wd_width = MAX (wd->rect.cols, b_width + 6);
  579. y = 2;
  580. ssm->label = label_new (y++, 3, "");
  581. group_add_widget_autopos (wg, ssm->label, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
  582. group_add_widget (wg, hline_new (y++, -1, -1));
  583. b = WIDGET (button_new (y++, 3, B_CANCEL, NORMAL_BUTTON, b_name, NULL));
  584. group_add_widget_autopos (wg, b, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
  585. r = wd->rect;
  586. r.lines = y + 2;
  587. r.cols = wd_width;
  588. widget_set_size_rect (wd, &r);
  589. }
  590. /* --------------------------------------------------------------------------------------------- */