dialog.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. /*** public functions ****************************************************************************/
  258. /* --------------------------------------------------------------------------------------------- */
  259. /** Default dialog callback */
  260. cb_ret_t
  261. dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  262. {
  263. switch (msg)
  264. {
  265. case MSG_IDLE:
  266. /* we don't want endless loop */
  267. widget_idle (w, FALSE);
  268. return MSG_HANDLED;
  269. default:
  270. return group_default_callback (w, sender, msg, parm, data);
  271. }
  272. }
  273. /* --------------------------------------------------------------------------------------------- */
  274. void
  275. dlg_default_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  276. {
  277. switch (msg)
  278. {
  279. case MSG_MOUSE_CLICK:
  280. if (event->y < 0 || event->y >= w->lines || event->x < 0 || event->x >= w->cols)
  281. {
  282. DIALOG (w)->ret_value = B_CANCEL;
  283. dlg_stop (DIALOG (w));
  284. }
  285. break;
  286. default:
  287. /* return MOU_UNHANDLED */
  288. event->result.abort = TRUE;
  289. break;
  290. }
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. WDialog *
  294. dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
  295. gboolean compact, const int *colors, widget_cb_fn callback,
  296. widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
  297. {
  298. WDialog *new_d;
  299. Widget *w;
  300. WGroup *g;
  301. new_d = g_new0 (WDialog, 1);
  302. w = WIDGET (new_d);
  303. g = GROUP (new_d);
  304. widget_adjust_position (pos_flags, &y1, &x1, &lines, &cols);
  305. group_init (g, y1, x1, lines, cols, callback != NULL ? callback : dlg_default_callback,
  306. mouse_callback != NULL ? mouse_callback : dlg_default_mouse_callback);
  307. w->pos_flags = pos_flags;
  308. w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
  309. w->state |= WST_FOCUSED;
  310. /* Temporary hack: dialog doesn't have an owner, own itself. */
  311. w->owner = g;
  312. w->keymap = dialog_map;
  313. w->mouse_handler = dlg_handle_mouse_event;
  314. w->mouse.forced_capture = mouse_close_dialog && (w->pos_flags & WPOS_FULLSCREEN) == 0;
  315. w->get_colors = dlg_default_get_colors;
  316. new_d->colors = colors;
  317. new_d->help_ctx = help_ctx;
  318. new_d->compact = compact;
  319. new_d->data = NULL;
  320. if (modal)
  321. {
  322. w->state |= WST_MODAL;
  323. new_d->bg = WIDGET (frame_new (0, 0, w->lines, w->cols, title, FALSE, new_d->compact));
  324. group_add_widget (g, new_d->bg);
  325. frame_set_title (FRAME (new_d->bg), title);
  326. }
  327. /* unique name of event group for this dialog */
  328. new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
  329. return new_d;
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. void
  333. dlg_set_default_colors (void)
  334. {
  335. dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
  336. dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
  337. dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
  338. dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
  339. dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
  340. alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
  341. alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
  342. alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
  343. alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
  344. alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
  345. listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
  346. listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
  347. listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
  348. listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
  349. listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
  350. }
  351. /* --------------------------------------------------------------------------------------------- */
  352. void
  353. do_refresh (void)
  354. {
  355. GList *d = top_dlg;
  356. if (fast_refresh)
  357. {
  358. if (d != NULL)
  359. widget_draw (WIDGET (d->data));
  360. }
  361. else
  362. {
  363. /* Search first fullscreen dialog */
  364. for (; d != NULL; d = g_list_next (d))
  365. if ((WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
  366. break;
  367. /* when small dialog (i.e. error message) is created first,
  368. there is no fullscreen dialog in the stack */
  369. if (d == NULL)
  370. d = g_list_last (top_dlg);
  371. /* back to top dialog */
  372. for (; d != NULL; d = g_list_previous (d))
  373. widget_draw (WIDGET (d->data));
  374. }
  375. }
  376. /* --------------------------------------------------------------------------------------------- */
  377. void
  378. dlg_stop (WDialog * h)
  379. {
  380. widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
  381. }
  382. /* --------------------------------------------------------------------------------------------- */
  383. /** Init the process */
  384. void
  385. dlg_init (WDialog * h)
  386. {
  387. WGroup *g = GROUP (h);
  388. Widget *wh = WIDGET (h);
  389. if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
  390. widget_set_state (wh, WST_MODAL, TRUE);
  391. /* add dialog to the stack */
  392. top_dlg = g_list_prepend (top_dlg, h);
  393. /* Initialize dialog manager and widgets */
  394. if (widget_get_state (wh, WST_CONSTRUCT))
  395. {
  396. if (!widget_get_state (wh, WST_MODAL))
  397. dialog_switch_add (h);
  398. send_message (h, NULL, MSG_INIT, 0, NULL);
  399. group_default_callback (wh, NULL, MSG_INIT, 0, NULL);
  400. dlg_read_history (h);
  401. }
  402. /* Select the first widget that takes focus */
  403. while (g->current != NULL && !widget_get_options (WIDGET (g->current->data), WOP_SELECTABLE)
  404. && !widget_get_state (WIDGET (g->current->data), WST_DISABLED))
  405. group_set_current_widget_next (g);
  406. widget_set_state (wh, WST_ACTIVE, TRUE);
  407. /* draw dialog and focus found widget */
  408. widget_set_state (wh, WST_FOCUSED, TRUE);
  409. h->ret_value = 0;
  410. }
  411. /* --------------------------------------------------------------------------------------------- */
  412. void
  413. dlg_process_event (WDialog * h, int key, Gpm_Event * event)
  414. {
  415. switch (key)
  416. {
  417. case EV_NONE:
  418. if (tty_got_interrupt ())
  419. dlg_execute_cmd (h, CK_Cancel);
  420. break;
  421. case EV_MOUSE:
  422. {
  423. Widget *w = WIDGET (h);
  424. GROUP (h)->mouse_status = w->mouse_handler (w, event);
  425. break;
  426. }
  427. default:
  428. dlg_key_event (h, key);
  429. break;
  430. }
  431. }
  432. /* --------------------------------------------------------------------------------------------- */
  433. /** Shutdown the dlg_run */
  434. void
  435. dlg_run_done (WDialog * h)
  436. {
  437. top_dlg = g_list_remove (top_dlg, h);
  438. if (widget_get_state (WIDGET (h), WST_CLOSED))
  439. {
  440. send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
  441. MSG_END, 0, NULL);
  442. if (!widget_get_state (WIDGET (h), WST_MODAL))
  443. dialog_switch_remove (h);
  444. }
  445. }
  446. /* --------------------------------------------------------------------------------------------- */
  447. /**
  448. * Standard run dialog routine
  449. * We have to keep this routine small so that we can duplicate it's
  450. * behavior on complex routines like the file routines, this way,
  451. * they can call the dlg_process_event without rewriting all the code
  452. */
  453. int
  454. dlg_run (WDialog * h)
  455. {
  456. dlg_init (h);
  457. frontend_dlg_run (h);
  458. dlg_run_done (h);
  459. return h->ret_value;
  460. }
  461. /* --------------------------------------------------------------------------------------------- */
  462. void
  463. dlg_destroy (WDialog * h)
  464. {
  465. /* if some widgets have history, save all history at one moment here */
  466. dlg_save_history (h);
  467. group_default_callback (WIDGET (h), NULL, MSG_DESTROY, 0, NULL);
  468. mc_event_group_del (h->event_group);
  469. g_free (h->event_group);
  470. g_free (h);
  471. do_refresh ();
  472. }
  473. /* --------------------------------------------------------------------------------------------- */
  474. /**
  475. * Write history to the ${XDG_CACHE_HOME}/mc/history file
  476. */
  477. void
  478. dlg_save_history (WDialog * h)
  479. {
  480. char *profile;
  481. int i;
  482. if (num_history_items_recorded == 0) /* this is how to disable */
  483. return;
  484. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  485. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  486. if (i != -1)
  487. close (i);
  488. /* Make sure the history is only readable by the user */
  489. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  490. {
  491. ev_history_load_save_t event_data;
  492. event_data.cfg = mc_config_init (profile, FALSE);
  493. event_data.receiver = NULL;
  494. /* get all histories in dialog */
  495. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  496. mc_config_save_file (event_data.cfg, NULL);
  497. mc_config_deinit (event_data.cfg);
  498. }
  499. g_free (profile);
  500. }
  501. /* --------------------------------------------------------------------------------------------- */
  502. char *
  503. dlg_get_title (const WDialog * h, size_t len)
  504. {
  505. char *t;
  506. if (h == NULL)
  507. abort ();
  508. if (h->get_title != NULL)
  509. t = h->get_title (h, len);
  510. else
  511. t = g_strdup ("");
  512. return t;
  513. }
  514. /* --------------------------------------------------------------------------------------------- */