dialog.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. Dialog box features module for the Midnight Commander
  3. Copyright (C) 1994-2024
  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. /* A hook list for idle events */
  45. hook_t *idle_hook = NULL;
  46. /* left click outside of dialog closes it */
  47. gboolean mouse_close_dialog = FALSE;
  48. const global_keymap_t *dialog_map = NULL;
  49. /*** file scope macro definitions ****************************************************************/
  50. /*** file scope type declarations ****************************************************************/
  51. /*** forward declarations (file scope functions) *************************************************/
  52. /*** file scope variables ************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. /*** file scope functions ************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. static const int *
  57. dlg_default_get_colors (const Widget *w)
  58. {
  59. return CONST_DIALOG (w)->colors;
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. /**
  63. * Read histories from the ${XDG_DATA_HOME}/mc/history file
  64. */
  65. static void
  66. dlg_read_history (WDialog *h)
  67. {
  68. char *profile;
  69. ev_history_load_save_t event_data;
  70. if (num_history_items_recorded == 0) /* this is how to disable */
  71. return;
  72. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  73. event_data.cfg = mc_config_init (profile, TRUE);
  74. event_data.receiver = NULL;
  75. /* create all histories in dialog */
  76. mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
  77. mc_config_deinit (event_data.cfg);
  78. g_free (profile);
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. static void
  82. refresh_cmd (void)
  83. {
  84. #ifdef HAVE_SLANG
  85. tty_touch_screen ();
  86. mc_refresh ();
  87. #else
  88. /* Use this if the refreshes fail */
  89. tty_clear_screen ();
  90. repaint_screen ();
  91. #endif /* HAVE_SLANG */
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. static void
  95. dlg_help (const WDialog *h)
  96. {
  97. ev_help_t event_data = { NULL, h->help_ctx };
  98. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. static cb_ret_t
  102. dlg_execute_cmd (WDialog *h, long command)
  103. {
  104. WGroup *g = GROUP (h);
  105. cb_ret_t ret = MSG_HANDLED;
  106. if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED)
  107. return MSG_HANDLED;
  108. switch (command)
  109. {
  110. case CK_Ok:
  111. h->ret_value = B_ENTER;
  112. dlg_close (h);
  113. break;
  114. case CK_Cancel:
  115. h->ret_value = B_CANCEL;
  116. dlg_close (h);
  117. break;
  118. case CK_Up:
  119. case CK_Left:
  120. group_select_prev_widget (g);
  121. break;
  122. case CK_Down:
  123. case CK_Right:
  124. group_select_next_widget (g);
  125. break;
  126. case CK_Help:
  127. dlg_help (h);
  128. break;
  129. case CK_Suspend:
  130. mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
  131. refresh_cmd ();
  132. break;
  133. case CK_Refresh:
  134. refresh_cmd ();
  135. break;
  136. case CK_ScreenList:
  137. if (!widget_get_state (WIDGET (h), WST_MODAL))
  138. dialog_switch_list ();
  139. else
  140. ret = MSG_NOT_HANDLED;
  141. break;
  142. case CK_ScreenNext:
  143. if (!widget_get_state (WIDGET (h), WST_MODAL))
  144. dialog_switch_next ();
  145. else
  146. ret = MSG_NOT_HANDLED;
  147. break;
  148. case CK_ScreenPrev:
  149. if (!widget_get_state (WIDGET (h), WST_MODAL))
  150. dialog_switch_prev ();
  151. else
  152. ret = MSG_NOT_HANDLED;
  153. break;
  154. default:
  155. ret = MSG_NOT_HANDLED;
  156. }
  157. return ret;
  158. }
  159. /* --------------------------------------------------------------------------------------------- */
  160. static cb_ret_t
  161. dlg_handle_key (WDialog *h, int d_key)
  162. {
  163. long command;
  164. command = widget_lookup_key (WIDGET (h), d_key);
  165. if (command == CK_IgnoreKey)
  166. command = keybind_lookup_keymap_command (dialog_map, d_key);
  167. if (command != CK_IgnoreKey)
  168. return dlg_execute_cmd (h, command);
  169. return MSG_NOT_HANDLED;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. static void
  173. dlg_key_event (WDialog *h, int d_key)
  174. {
  175. Widget *w = WIDGET (h);
  176. WGroup *g = GROUP (h);
  177. cb_ret_t handled;
  178. if (g->widgets == NULL)
  179. return;
  180. if (g->current == NULL)
  181. g->current = g->widgets;
  182. /* TAB used to cycle */
  183. if (!widget_get_options (w, WOP_WANT_TAB))
  184. {
  185. if (d_key == '\t')
  186. {
  187. group_select_next_widget (g);
  188. return;
  189. }
  190. else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
  191. {
  192. group_select_prev_widget (g);
  193. return;
  194. }
  195. }
  196. /* first can dlalog handle the key itself */
  197. handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
  198. if (handled == MSG_NOT_HANDLED)
  199. handled = group_default_callback (w, NULL, MSG_KEY, d_key, NULL);
  200. if (handled == MSG_NOT_HANDLED)
  201. handled = dlg_handle_key (h, d_key);
  202. (void) handled;
  203. send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. static int
  207. dlg_handle_mouse_event (Widget *w, Gpm_Event *event)
  208. {
  209. if (w->mouse_callback != NULL)
  210. {
  211. int mou;
  212. mou = mouse_handle_event (w, event);
  213. if (mou != MOU_UNHANDLED)
  214. return mou;
  215. }
  216. return group_handle_mouse_event (w, event);
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. static void
  220. frontend_dlg_run (WDialog *h)
  221. {
  222. Widget *wh = WIDGET (h);
  223. Gpm_Event event;
  224. event.x = -1;
  225. /* close opened editors, viewers, etc */
  226. if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
  227. {
  228. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  229. return;
  230. }
  231. while (widget_get_state (wh, WST_ACTIVE))
  232. {
  233. int d_key;
  234. if (tty_got_winch ())
  235. dialog_change_screen_size ();
  236. if (is_idle ())
  237. {
  238. if (idle_hook)
  239. execute_hooks (idle_hook);
  240. while (widget_get_state (wh, WST_IDLE) && is_idle ())
  241. send_message (wh, NULL, MSG_IDLE, 0, NULL);
  242. /* Allow terminating the dialog from the idle handler */
  243. if (!widget_get_state (wh, WST_ACTIVE))
  244. break;
  245. }
  246. widget_update_cursor (wh);
  247. /* Clear interrupt flag */
  248. tty_got_interrupt ();
  249. d_key = tty_get_event (&event, GROUP (h)->mouse_status == MOU_REPEAT, TRUE);
  250. dlg_process_event (h, d_key, &event);
  251. if (widget_get_state (wh, WST_CLOSED))
  252. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  253. }
  254. }
  255. /* --------------------------------------------------------------------------------------------- */
  256. static void
  257. dlg_default_destroy (Widget *w)
  258. {
  259. WDialog *h = DIALOG (w);
  260. /* if some widgets have history, save all histories at one moment here */
  261. dlg_save_history (h);
  262. group_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
  263. send_message (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->rect.lines || event->x < 0 || event->x >= w->rect.cols)
  300. {
  301. DIALOG (w)->ret_value = B_CANCEL;
  302. dlg_close (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. WRect r = { y1, x1, lines, cols };
  318. WDialog *new_d;
  319. Widget *w;
  320. WGroup *g;
  321. new_d = g_new0 (WDialog, 1);
  322. w = WIDGET (new_d);
  323. g = GROUP (new_d);
  324. widget_adjust_position (pos_flags, &r);
  325. group_init (g, &r, callback != NULL ? callback : dlg_default_callback,
  326. mouse_callback != NULL ? mouse_callback : dlg_default_mouse_callback);
  327. w->pos_flags = pos_flags;
  328. w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
  329. w->state |= WST_FOCUSED;
  330. /* Temporary hack: dialog doesn't have an owner, own itself. */
  331. w->owner = g;
  332. w->keymap = dialog_map;
  333. w->mouse_handler = dlg_handle_mouse_event;
  334. w->mouse.forced_capture = mouse_close_dialog && (w->pos_flags & WPOS_FULLSCREEN) == 0;
  335. w->destroy = dlg_default_destroy;
  336. w->get_colors = dlg_default_get_colors;
  337. new_d->colors = colors;
  338. new_d->help_ctx = help_ctx;
  339. new_d->compact = compact;
  340. new_d->data.p = NULL;
  341. if (modal)
  342. {
  343. w->state |= WST_MODAL;
  344. new_d->bg =
  345. WIDGET (frame_new (0, 0, w->rect.lines, w->rect.cols, title, FALSE, new_d->compact));
  346. group_add_widget (g, new_d->bg);
  347. frame_set_title (FRAME (new_d->bg), title);
  348. }
  349. /* unique name of event group for this dialog */
  350. new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
  351. return new_d;
  352. }
  353. /* --------------------------------------------------------------------------------------------- */
  354. void
  355. dlg_set_default_colors (void)
  356. {
  357. dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
  358. dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
  359. dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
  360. dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
  361. dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
  362. alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
  363. alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
  364. alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
  365. alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
  366. alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
  367. listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
  368. listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
  369. listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
  370. listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
  371. listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
  372. }
  373. /* --------------------------------------------------------------------------------------------- */
  374. void
  375. dlg_close (WDialog *h)
  376. {
  377. widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
  378. }
  379. /* --------------------------------------------------------------------------------------------- */
  380. /** Init the process */
  381. void
  382. dlg_init (WDialog *h)
  383. {
  384. WGroup *g = GROUP (h);
  385. Widget *wh = WIDGET (h);
  386. if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
  387. widget_set_state (wh, WST_MODAL, TRUE);
  388. /* add dialog to the stack */
  389. top_dlg = g_list_prepend (top_dlg, h);
  390. /* Initialize dialog manager and widgets */
  391. if (widget_get_state (wh, WST_CONSTRUCT))
  392. {
  393. if (!widget_get_state (wh, WST_MODAL))
  394. dialog_switch_add (h);
  395. send_message (h, NULL, MSG_INIT, 0, NULL);
  396. group_default_callback (wh, NULL, MSG_INIT, 0, NULL);
  397. dlg_read_history (h);
  398. }
  399. /* Select the first widget that takes focus */
  400. while (g->current != NULL && !widget_is_focusable (g->current->data))
  401. group_set_current_widget_next (g);
  402. widget_set_state (wh, WST_ACTIVE, TRUE);
  403. widget_draw (wh);
  404. h->ret_value = 0;
  405. }
  406. /* --------------------------------------------------------------------------------------------- */
  407. void
  408. dlg_process_event (WDialog *h, int key, Gpm_Event *event)
  409. {
  410. switch (key)
  411. {
  412. case EV_NONE:
  413. if (tty_got_interrupt ())
  414. dlg_execute_cmd (h, CK_Cancel);
  415. break;
  416. case EV_MOUSE:
  417. {
  418. Widget *w = WIDGET (h);
  419. GROUP (h)->mouse_status = w->mouse_handler (w, event);
  420. break;
  421. }
  422. default:
  423. dlg_key_event (h, key);
  424. break;
  425. }
  426. }
  427. /* --------------------------------------------------------------------------------------------- */
  428. /** Shutdown the dlg_run */
  429. void
  430. dlg_run_done (WDialog *h)
  431. {
  432. top_dlg = g_list_remove (top_dlg, h);
  433. if (widget_get_state (WIDGET (h), WST_CLOSED))
  434. {
  435. send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
  436. MSG_END, 0, NULL);
  437. if (!widget_get_state (WIDGET (h), WST_MODAL))
  438. dialog_switch_remove (h);
  439. }
  440. }
  441. /* --------------------------------------------------------------------------------------------- */
  442. /**
  443. * Standard run dialog routine
  444. * We have to keep this routine small so that we can duplicate it's
  445. * behavior on complex routines like the file routines, this way,
  446. * they can call the dlg_process_event without rewriting all the code
  447. */
  448. int
  449. dlg_run (WDialog *h)
  450. {
  451. dlg_init (h);
  452. frontend_dlg_run (h);
  453. dlg_run_done (h);
  454. return h->ret_value;
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. /**
  458. * Write history to the ${XDG_DATA_HOME}/mc/history file
  459. */
  460. void
  461. dlg_save_history (WDialog *h)
  462. {
  463. char *profile;
  464. int i;
  465. if (num_history_items_recorded == 0) /* this is how to disable */
  466. return;
  467. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  468. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  469. if (i != -1)
  470. close (i);
  471. /* Make sure the history is only readable by the user */
  472. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  473. {
  474. ev_history_load_save_t event_data;
  475. event_data.cfg = mc_config_init (profile, FALSE);
  476. event_data.receiver = NULL;
  477. /* get all histories in dialog */
  478. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  479. mc_config_save_file (event_data.cfg, NULL);
  480. mc_config_deinit (event_data.cfg);
  481. }
  482. g_free (profile);
  483. }
  484. /* --------------------------------------------------------------------------------------------- */
  485. char *
  486. dlg_get_title (const WDialog *h, size_t len)
  487. {
  488. char *t;
  489. if (h == NULL)
  490. abort ();
  491. if (h->get_title != NULL)
  492. t = h->get_title (h, len);
  493. else
  494. t = g_strdup ("");
  495. return t;
  496. }
  497. /* --------------------------------------------------------------------------------------------- */