wtools.c 20 KB

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