dialog.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. Dialog box features module for the Midnight Commander
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /** \file dialog.c
  18. * \brief Source: dialog box features module
  19. */
  20. #include <config.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "lib/global.h"
  29. #include "lib/tty/tty.h"
  30. #include "lib/skin.h"
  31. #include "lib/tty/key.h"
  32. #include "lib/strutil.h"
  33. #include "lib/fileloc.h" /* MC_HISTORY_FILE */
  34. #include "lib/event.h" /* mc_event_raise() */
  35. #include "lib/util.h" /* MC_PTR_FREE */
  36. #include "lib/mcconfig.h" /* num_history_items_recorded */
  37. #include "lib/widget.h"
  38. #include "lib/widget/mouse.h"
  39. /*** global variables ****************************************************************************/
  40. /* Color styles for normal and error dialogs */
  41. dlg_colors_t dialog_colors;
  42. dlg_colors_t alarm_colors;
  43. dlg_colors_t listbox_colors;
  44. /* Primitive way to check if the the current dialog is our dialog */
  45. /* This is needed by async routines like load_prompt */
  46. GList *top_dlg = NULL;
  47. /* A hook list for idle events */
  48. hook_t *idle_hook = NULL;
  49. /* If set then dialogs just clean the screen when refreshing, else */
  50. /* they do a complete refresh, refreshing all the parts of the program */
  51. gboolean fast_refresh = FALSE;
  52. /* left click outside of dialog closes it */
  53. gboolean mouse_close_dialog = FALSE;
  54. const global_keymap_t *dialog_map = NULL;
  55. /*** file scope macro definitions ****************************************************************/
  56. /*** file scope type declarations ****************************************************************/
  57. /*** file scope variables ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /*** file scope functions ************************************************************************/
  60. /* --------------------------------------------------------------------------------------------- */
  61. static const int *
  62. dlg_default_get_colors (const Widget * w)
  63. {
  64. return CONST_DIALOG (w)->colors;
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. /**
  68. * Read histories from the ${XDG_CACHE_HOME}/mc/history file
  69. */
  70. static void
  71. dlg_read_history (WDialog * h)
  72. {
  73. char *profile;
  74. ev_history_load_save_t event_data;
  75. if (num_history_items_recorded == 0) /* this is how to disable */
  76. return;
  77. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  78. event_data.cfg = mc_config_init (profile, TRUE);
  79. event_data.receiver = NULL;
  80. /* create all histories in dialog */
  81. mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
  82. mc_config_deinit (event_data.cfg);
  83. g_free (profile);
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. static void
  87. refresh_cmd (void)
  88. {
  89. #ifdef HAVE_SLANG
  90. tty_touch_screen ();
  91. mc_refresh ();
  92. #else
  93. /* Use this if the refreshes fail */
  94. tty_clear_screen ();
  95. repaint_screen ();
  96. #endif /* HAVE_SLANG */
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. static cb_ret_t
  100. dlg_execute_cmd (WDialog * h, long command)
  101. {
  102. WGroup *g = GROUP (h);
  103. cb_ret_t ret = MSG_HANDLED;
  104. if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED)
  105. return MSG_HANDLED;
  106. switch (command)
  107. {
  108. case CK_Ok:
  109. h->ret_value = B_ENTER;
  110. dlg_stop (h);
  111. break;
  112. case CK_Cancel:
  113. h->ret_value = B_CANCEL;
  114. dlg_stop (h);
  115. break;
  116. case CK_Up:
  117. case CK_Left:
  118. group_select_prev_widget (g);
  119. break;
  120. case CK_Down:
  121. case CK_Right:
  122. group_select_next_widget (g);
  123. break;
  124. case CK_Help:
  125. {
  126. ev_help_t event_data = { NULL, h->help_ctx };
  127. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  128. }
  129. break;
  130. case CK_Suspend:
  131. mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
  132. refresh_cmd ();
  133. break;
  134. case CK_Refresh:
  135. refresh_cmd ();
  136. break;
  137. case CK_ScreenList:
  138. if (!widget_get_state (WIDGET (h), WST_MODAL))
  139. dialog_switch_list ();
  140. else
  141. ret = MSG_NOT_HANDLED;
  142. break;
  143. case CK_ScreenNext:
  144. if (!widget_get_state (WIDGET (h), WST_MODAL))
  145. dialog_switch_next ();
  146. else
  147. ret = MSG_NOT_HANDLED;
  148. break;
  149. case CK_ScreenPrev:
  150. if (!widget_get_state (WIDGET (h), WST_MODAL))
  151. dialog_switch_prev ();
  152. else
  153. ret = MSG_NOT_HANDLED;
  154. break;
  155. default:
  156. ret = MSG_NOT_HANDLED;
  157. }
  158. return ret;
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. static cb_ret_t
  162. dlg_handle_key (WDialog * h, int d_key)
  163. {
  164. long command;
  165. command = widget_lookup_key (WIDGET (h), d_key);
  166. if (command == CK_IgnoreKey)
  167. command = keybind_lookup_keymap_command (dialog_map, d_key);
  168. if (command != CK_IgnoreKey)
  169. return dlg_execute_cmd (h, command);
  170. return MSG_NOT_HANDLED;
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. static void
  174. dlg_key_event (WDialog * h, int d_key)
  175. {
  176. Widget *w = WIDGET (h);
  177. WGroup *g = GROUP (h);
  178. cb_ret_t handled;
  179. if (g->widgets == NULL)
  180. return;
  181. if (g->current == NULL)
  182. g->current = g->widgets;
  183. /* TAB used to cycle */
  184. if (!widget_get_options (w, WOP_WANT_TAB))
  185. {
  186. if (d_key == '\t')
  187. {
  188. group_select_next_widget (g);
  189. return;
  190. }
  191. else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
  192. {
  193. group_select_prev_widget (g);
  194. return;
  195. }
  196. }
  197. /* first can dlalog handle the key itself */
  198. handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
  199. if (handled == MSG_NOT_HANDLED)
  200. handled = group_default_callback (w, NULL, MSG_KEY, d_key, NULL);
  201. if (handled == MSG_NOT_HANDLED)
  202. handled = dlg_handle_key (h, d_key);
  203. (void) handled;
  204. send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. static int
  208. dlg_handle_mouse_event (Widget * w, Gpm_Event * event)
  209. {
  210. if (w->mouse_callback != NULL)
  211. {
  212. int mou;
  213. mou = mouse_handle_event (w, event);
  214. if (mou != MOU_UNHANDLED)
  215. return mou;
  216. }
  217. return group_handle_mouse_event (w, event);
  218. }
  219. /* --------------------------------------------------------------------------------------------- */
  220. static void
  221. frontend_dlg_run (WDialog * h)
  222. {
  223. Widget *wh = WIDGET (h);
  224. Gpm_Event event;
  225. event.x = -1;
  226. /* close opened editors, viewers, etc */
  227. if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
  228. {
  229. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  230. return;
  231. }
  232. while (widget_get_state (wh, WST_ACTIVE))
  233. {
  234. int d_key;
  235. if (tty_got_winch ())
  236. dialog_change_screen_size ();
  237. if (is_idle ())
  238. {
  239. if (idle_hook)
  240. execute_hooks (idle_hook);
  241. while (widget_get_state (wh, WST_IDLE) && is_idle ())
  242. send_message (wh, NULL, MSG_IDLE, 0, NULL);
  243. /* Allow terminating the dialog from the idle handler */
  244. if (!widget_get_state (wh, WST_ACTIVE))
  245. break;
  246. }
  247. widget_update_cursor (wh);
  248. /* Clear interrupt flag */
  249. tty_got_interrupt ();
  250. d_key = tty_get_event (&event, GROUP (h)->mouse_status == MOU_REPEAT, TRUE);
  251. dlg_process_event (h, d_key, &event);
  252. if (widget_get_state (wh, WST_CLOSED))
  253. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  254. }
  255. }
  256. /* --------------------------------------------------------------------------------------------- */
  257. static void
  258. dlg_default_destroy (Widget * w)
  259. {
  260. WDialog *h = DIALOG (w);
  261. /* if some widgets have history, save all histories at one moment here */
  262. dlg_save_history (h);
  263. group_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
  264. mc_event_group_del (h->event_group);
  265. g_free (h->event_group);
  266. g_free (h);
  267. do_refresh ();
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. /*** public functions ****************************************************************************/
  271. /* --------------------------------------------------------------------------------------------- */
  272. /** Default dialog callback */
  273. cb_ret_t
  274. dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  275. {
  276. switch (msg)
  277. {
  278. case MSG_INIT:
  279. /* nothing to init in dialog itself */
  280. return MSG_HANDLED;
  281. case MSG_IDLE:
  282. /* we don't want endless loop */
  283. widget_idle (w, FALSE);
  284. return MSG_HANDLED;
  285. case MSG_DESTROY:
  286. /* nothing to deinit in dialog itself */
  287. return MSG_HANDLED;
  288. default:
  289. return group_default_callback (w, sender, msg, parm, data);
  290. }
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. void
  294. dlg_default_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  295. {
  296. switch (msg)
  297. {
  298. case MSG_MOUSE_CLICK:
  299. if (event->y < 0 || event->y >= w->lines || event->x < 0 || event->x >= w->cols)
  300. {
  301. DIALOG (w)->ret_value = B_CANCEL;
  302. dlg_stop (DIALOG (w));
  303. }
  304. break;
  305. default:
  306. /* return MOU_UNHANDLED */
  307. event->result.abort = TRUE;
  308. break;
  309. }
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. WDialog *
  313. dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
  314. gboolean compact, const int *colors, widget_cb_fn callback,
  315. widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
  316. {
  317. WDialog *new_d;
  318. Widget *w;
  319. WGroup *g;
  320. new_d = g_new0 (WDialog, 1);
  321. w = WIDGET (new_d);
  322. g = GROUP (new_d);
  323. widget_adjust_position (pos_flags, &y1, &x1, &lines, &cols);
  324. group_init (g, y1, x1, lines, cols, callback != NULL ? callback : dlg_default_callback,
  325. mouse_callback != NULL ? mouse_callback : dlg_default_mouse_callback);
  326. w->pos_flags = pos_flags;
  327. w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
  328. w->state |= WST_FOCUSED;
  329. /* Temporary hack: dialog doesn't have an owner, own itself. */
  330. w->owner = g;
  331. w->keymap = dialog_map;
  332. w->mouse_handler = dlg_handle_mouse_event;
  333. w->mouse.forced_capture = mouse_close_dialog && (w->pos_flags & WPOS_FULLSCREEN) == 0;
  334. w->destroy = dlg_default_destroy;
  335. w->get_colors = dlg_default_get_colors;
  336. new_d->colors = colors;
  337. new_d->help_ctx = help_ctx;
  338. new_d->compact = compact;
  339. new_d->data = NULL;
  340. if (modal)
  341. {
  342. w->state |= WST_MODAL;
  343. new_d->bg = WIDGET (frame_new (0, 0, w->lines, w->cols, title, FALSE, new_d->compact));
  344. group_add_widget (g, new_d->bg);
  345. frame_set_title (FRAME (new_d->bg), title);
  346. }
  347. /* unique name of event group for this dialog */
  348. new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
  349. return new_d;
  350. }
  351. /* --------------------------------------------------------------------------------------------- */
  352. void
  353. dlg_set_default_colors (void)
  354. {
  355. dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
  356. dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
  357. dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
  358. dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
  359. dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
  360. alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
  361. alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
  362. alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
  363. alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
  364. alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
  365. listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
  366. listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
  367. listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
  368. listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
  369. listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. void
  373. do_refresh (void)
  374. {
  375. GList *d = top_dlg;
  376. if (fast_refresh)
  377. {
  378. if (d != NULL)
  379. widget_draw (WIDGET (d->data));
  380. }
  381. else
  382. {
  383. /* Search first fullscreen dialog */
  384. for (; d != NULL; d = g_list_next (d))
  385. if ((WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
  386. break;
  387. /* when small dialog (i.e. error message) is created first,
  388. there is no fullscreen dialog in the stack */
  389. if (d == NULL)
  390. d = g_list_last (top_dlg);
  391. /* back to top dialog */
  392. for (; d != NULL; d = g_list_previous (d))
  393. widget_draw (WIDGET (d->data));
  394. }
  395. }
  396. /* --------------------------------------------------------------------------------------------- */
  397. void
  398. dlg_stop (WDialog * h)
  399. {
  400. widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
  401. }
  402. /* --------------------------------------------------------------------------------------------- */
  403. /** Init the process */
  404. void
  405. dlg_init (WDialog * h)
  406. {
  407. WGroup *g = GROUP (h);
  408. Widget *wh = WIDGET (h);
  409. if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
  410. widget_set_state (wh, WST_MODAL, TRUE);
  411. /* add dialog to the stack */
  412. top_dlg = g_list_prepend (top_dlg, h);
  413. /* Initialize dialog manager and widgets */
  414. if (widget_get_state (wh, WST_CONSTRUCT))
  415. {
  416. if (!widget_get_state (wh, WST_MODAL))
  417. dialog_switch_add (h);
  418. send_message (h, NULL, MSG_INIT, 0, NULL);
  419. group_default_callback (wh, NULL, MSG_INIT, 0, NULL);
  420. dlg_read_history (h);
  421. }
  422. /* Select the first widget that takes focus */
  423. while (g->current != NULL && !widget_is_focusable (g->current->data))
  424. group_set_current_widget_next (g);
  425. widget_set_state (wh, WST_ACTIVE, TRUE);
  426. /* draw dialog and focus found widget */
  427. widget_set_state (wh, WST_FOCUSED, TRUE);
  428. h->ret_value = 0;
  429. }
  430. /* --------------------------------------------------------------------------------------------- */
  431. void
  432. dlg_process_event (WDialog * h, int key, Gpm_Event * event)
  433. {
  434. switch (key)
  435. {
  436. case EV_NONE:
  437. if (tty_got_interrupt ())
  438. dlg_execute_cmd (h, CK_Cancel);
  439. break;
  440. case EV_MOUSE:
  441. {
  442. Widget *w = WIDGET (h);
  443. GROUP (h)->mouse_status = w->mouse_handler (w, event);
  444. break;
  445. }
  446. default:
  447. dlg_key_event (h, key);
  448. break;
  449. }
  450. }
  451. /* --------------------------------------------------------------------------------------------- */
  452. /** Shutdown the dlg_run */
  453. void
  454. dlg_run_done (WDialog * h)
  455. {
  456. top_dlg = g_list_remove (top_dlg, h);
  457. if (widget_get_state (WIDGET (h), WST_CLOSED))
  458. {
  459. send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
  460. MSG_END, 0, NULL);
  461. if (!widget_get_state (WIDGET (h), WST_MODAL))
  462. dialog_switch_remove (h);
  463. }
  464. }
  465. /* --------------------------------------------------------------------------------------------- */
  466. /**
  467. * Standard run dialog routine
  468. * We have to keep this routine small so that we can duplicate it's
  469. * behavior on complex routines like the file routines, this way,
  470. * they can call the dlg_process_event without rewriting all the code
  471. */
  472. int
  473. dlg_run (WDialog * h)
  474. {
  475. dlg_init (h);
  476. frontend_dlg_run (h);
  477. dlg_run_done (h);
  478. return h->ret_value;
  479. }
  480. /* --------------------------------------------------------------------------------------------- */
  481. /**
  482. * Write history to the ${XDG_CACHE_HOME}/mc/history file
  483. */
  484. void
  485. dlg_save_history (WDialog * h)
  486. {
  487. char *profile;
  488. int i;
  489. if (num_history_items_recorded == 0) /* this is how to disable */
  490. return;
  491. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  492. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  493. if (i != -1)
  494. close (i);
  495. /* Make sure the history is only readable by the user */
  496. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  497. {
  498. ev_history_load_save_t event_data;
  499. event_data.cfg = mc_config_init (profile, FALSE);
  500. event_data.receiver = NULL;
  501. /* get all histories in dialog */
  502. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  503. mc_config_save_file (event_data.cfg, NULL);
  504. mc_config_deinit (event_data.cfg);
  505. }
  506. g_free (profile);
  507. }
  508. /* --------------------------------------------------------------------------------------------- */
  509. char *
  510. dlg_get_title (const WDialog * h, size_t len)
  511. {
  512. char *t;
  513. if (h == NULL)
  514. abort ();
  515. if (h->get_title != NULL)
  516. t = h->get_title (h, len);
  517. else
  518. t = g_strdup ("");
  519. return t;
  520. }
  521. /* --------------------------------------------------------------------------------------------- */