dialog.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /* Dialog box features module for the Midnight Commander
  2. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. /** \file dialog.c
  17. * \brief Source: dialog box features module
  18. */
  19. #include <config.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h> /* open() */
  28. #include "lib/global.h"
  29. #include "lib/tty/tty.h"
  30. #include "lib/skin.h"
  31. #include "lib/tty/mouse.h"
  32. #include "lib/tty/key.h"
  33. #include "lib/strutil.h"
  34. #include "lib/widget.h"
  35. #include "lib/fileloc.h" /* MC_HISTORY_FILE */
  36. #include "lib/event.h" /* mc_event_raise() */
  37. /*** global variables ****************************************************************************/
  38. /* Color styles for normal and error dialogs */
  39. dlg_colors_t dialog_colors;
  40. dlg_colors_t alarm_colors;
  41. /* Primitive way to check if the the current dialog is our dialog */
  42. /* This is needed by async routines like load_prompt */
  43. GList *top_dlg = NULL;
  44. /* A hook list for idle events */
  45. hook_t *idle_hook = NULL;
  46. /* If set then dialogs just clean the screen when refreshing, else */
  47. /* they do a complete refresh, refreshing all the parts of the program */
  48. int fast_refresh = 0;
  49. /* left click outside of dialog closes it */
  50. int mouse_close_dialog = 0;
  51. const global_keymap_t *dialog_map;
  52. /*** file scope macro definitions ****************************************************************/
  53. /*** file scope type declarations ****************************************************************/
  54. /** What to do if the requested widget doesn't take focus */
  55. typedef enum
  56. {
  57. SELECT_NEXT, /* go the the next widget */
  58. SELECT_PREV, /* go the the previous widget */
  59. SELECT_EXACT /* use current widget */
  60. } select_dir_t;
  61. /*** file scope variables ************************************************************************/
  62. /*** file scope functions ************************************************************************/
  63. /* --------------------------------------------------------------------------------------------- */
  64. /**
  65. * broadcast a message to all the widgets in a dialog that have
  66. * the options set to flags. If flags is zero, the message is sent
  67. * to all widgets.
  68. */
  69. static void
  70. dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
  71. {
  72. GList *p, *first;
  73. if (h->widgets == NULL)
  74. return;
  75. if (h->current == NULL)
  76. h->current = h->widgets;
  77. if (reverse)
  78. {
  79. p = g_list_previous (h->current);
  80. if (p == NULL)
  81. p = g_list_last (h->widgets);
  82. }
  83. else
  84. {
  85. p = g_list_next (h->current);
  86. if (p == NULL)
  87. p = h->widgets;
  88. }
  89. first = p;
  90. do
  91. {
  92. Widget *w = (Widget *) p->data;
  93. if (reverse)
  94. {
  95. p = g_list_previous (p);
  96. if (p == NULL)
  97. p = g_list_last (h->widgets);
  98. }
  99. else
  100. {
  101. p = g_list_next (p);
  102. if (p == NULL)
  103. p = h->widgets;
  104. }
  105. if ((flags == 0) || ((flags & w->options) != 0))
  106. send_message (w, msg, 0);
  107. }
  108. while (first != p);
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. /**
  112. * Read histories from the ${XDG_CACHE_HOME}/mc/history file
  113. */
  114. static void
  115. dlg_read_history (Dlg_head * h)
  116. {
  117. char *profile;
  118. ev_history_load_save_t event_data;
  119. if (num_history_items_recorded == 0) /* this is how to disable */
  120. return;
  121. profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, NULL);
  122. event_data.cfg = mc_config_init (profile);
  123. event_data.receiver = NULL;
  124. /* create all histories in dialog */
  125. mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
  126. mc_config_deinit (event_data.cfg);
  127. g_free (profile);
  128. }
  129. /* --------------------------------------------------------------------------------------------- */
  130. static int
  131. dlg_unfocus (Dlg_head * h)
  132. {
  133. /* ... but can unfocus disabled widget */
  134. if ((h->current != NULL) && (h->state == DLG_ACTIVE))
  135. {
  136. Widget *current = (Widget *) h->current->data;
  137. if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
  138. {
  139. h->callback (h, current, DLG_UNFOCUS, 0, NULL);
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. static int
  147. dlg_find_widget_callback (const void *a, const void *b)
  148. {
  149. const Widget *w = (const Widget *) a;
  150. callback_fn f = (callback_fn) b;
  151. return (w->callback == f) ? 0 : 1;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. /**
  155. * Try to select another widget. If forward is set, follow tab order.
  156. * Otherwise go to the previous widget.
  157. */
  158. static void
  159. do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
  160. {
  161. Widget *w0 = (Widget *) h->current->data;
  162. if (!dlg_unfocus (h))
  163. return;
  164. h->current = w;
  165. do
  166. {
  167. if (dlg_focus (h))
  168. break;
  169. switch (dir)
  170. {
  171. case SELECT_EXACT:
  172. h->current = g_list_find (h->widgets, w0);
  173. if (dlg_focus (h))
  174. return;
  175. /* try find another widget that can take focus */
  176. dir = SELECT_NEXT;
  177. /* fallthrough */
  178. case SELECT_NEXT:
  179. h->current = g_list_next (h->current);
  180. if (h->current == NULL)
  181. h->current = h->widgets;
  182. break;
  183. case SELECT_PREV:
  184. h->current = g_list_previous (h->current);
  185. if (h->current == NULL)
  186. h->current = g_list_last (h->widgets);
  187. break;
  188. }
  189. }
  190. while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
  191. if (dlg_overlap (w0, (Widget *) h->current->data))
  192. {
  193. send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
  194. send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
  195. }
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. static void
  199. refresh_cmd (void)
  200. {
  201. #ifdef HAVE_SLANG
  202. tty_touch_screen ();
  203. mc_refresh ();
  204. #else
  205. /* Use this if the refreshes fail */
  206. clr_scr ();
  207. repaint_screen ();
  208. #endif /* HAVE_SLANG */
  209. }
  210. /* --------------------------------------------------------------------------------------------- */
  211. static cb_ret_t
  212. dlg_execute_cmd (Dlg_head * h, unsigned long command)
  213. {
  214. cb_ret_t ret = MSG_HANDLED;
  215. switch (command)
  216. {
  217. case CK_Ok:
  218. h->ret_value = B_ENTER;
  219. dlg_stop (h);
  220. break;
  221. case CK_Cancel:
  222. h->ret_value = B_CANCEL;
  223. dlg_stop (h);
  224. break;
  225. case CK_Up:
  226. case CK_Left:
  227. dlg_one_up (h);
  228. break;
  229. case CK_Down:
  230. case CK_Right:
  231. dlg_one_down (h);
  232. break;
  233. case CK_Help:
  234. {
  235. ev_help_t event_data = { NULL, h->help_ctx };
  236. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  237. do_refresh ();
  238. }
  239. break;
  240. case CK_Suspend:
  241. mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
  242. refresh_cmd ();
  243. break;
  244. case CK_Refresh:
  245. refresh_cmd ();
  246. break;
  247. case CK_ScreenList:
  248. if (!h->modal)
  249. dialog_switch_list ();
  250. else
  251. ret = MSG_NOT_HANDLED;
  252. break;
  253. case CK_ScreenNext:
  254. if (!h->modal)
  255. dialog_switch_next ();
  256. else
  257. ret = MSG_NOT_HANDLED;
  258. break;
  259. case CK_ScreenPrev:
  260. if (!h->modal)
  261. dialog_switch_prev ();
  262. else
  263. ret = MSG_NOT_HANDLED;
  264. break;
  265. default:
  266. ret = MSG_NOT_HANDLED;
  267. }
  268. return ret;
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. static cb_ret_t
  272. dlg_handle_key (Dlg_head * h, int d_key)
  273. {
  274. unsigned long command;
  275. command = keybind_lookup_keymap_command (dialog_map, d_key);
  276. if ((command == CK_IgnoreKey) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
  277. return MSG_NOT_HANDLED;
  278. else
  279. return MSG_HANDLED;
  280. }
  281. /* --------------------------------------------------------------------------------------------- */
  282. static int
  283. dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
  284. {
  285. GList *item;
  286. GList *starting_widget = h->current;
  287. Gpm_Event new_event;
  288. int x = event->x;
  289. int y = event->y;
  290. /* close the dialog by mouse click out of dialog area */
  291. if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
  292. && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
  293. {
  294. h->ret_value = B_CANCEL;
  295. dlg_stop (h);
  296. return MOU_NORMAL;
  297. }
  298. item = starting_widget;
  299. do
  300. {
  301. Widget *widget;
  302. widget = (Widget *) item->data;
  303. item = g_list_next (item);
  304. if (item == NULL)
  305. item = h->widgets;
  306. if (((widget->options & W_DISABLED) == 0)
  307. && (x > widget->x) && (x <= widget->x + widget->cols)
  308. && (y > widget->y) && (y <= widget->y + widget->lines))
  309. {
  310. new_event = *event;
  311. new_event.x -= widget->x;
  312. new_event.y -= widget->y;
  313. if (widget->mouse != NULL)
  314. return widget->mouse (&new_event, widget);
  315. }
  316. }
  317. while (item != starting_widget);
  318. return MOU_NORMAL;
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. static cb_ret_t
  322. dlg_try_hotkey (Dlg_head * h, int d_key)
  323. {
  324. GList *hot_cur;
  325. Widget *current;
  326. cb_ret_t handled;
  327. int c;
  328. if (h->widgets == NULL)
  329. return MSG_NOT_HANDLED;
  330. if (h->current == NULL)
  331. h->current = h->widgets;
  332. /*
  333. * Explanation: we don't send letter hotkeys to other widgets if
  334. * the currently selected widget is an input line
  335. */
  336. current = (Widget *) h->current->data;
  337. if ((current->options & W_DISABLED) != 0)
  338. return MSG_NOT_HANDLED;
  339. if (current->options & W_IS_INPUT)
  340. {
  341. /* skip ascii control characters, anything else can valid character in
  342. * some encoding */
  343. if (d_key >= 32 && d_key < 256)
  344. return MSG_NOT_HANDLED;
  345. }
  346. /* If it's an alt key, send the message */
  347. c = d_key & ~ALT (0);
  348. if (d_key & ALT (0) && g_ascii_isalpha (c))
  349. d_key = g_ascii_tolower (c);
  350. handled = MSG_NOT_HANDLED;
  351. if ((current->options & W_WANT_HOTKEY) != 0)
  352. handled = send_message (current, WIDGET_HOTKEY, d_key);
  353. /* If not used, send hotkey to other widgets */
  354. if (handled == MSG_HANDLED)
  355. return MSG_HANDLED;
  356. hot_cur = g_list_next (h->current);
  357. if (hot_cur == NULL)
  358. hot_cur = h->widgets;
  359. /* send it to all widgets */
  360. while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
  361. {
  362. current = (Widget *) hot_cur->data;
  363. if ((current->options & W_WANT_HOTKEY) != 0)
  364. handled = send_message (current, WIDGET_HOTKEY, d_key);
  365. if (handled == MSG_NOT_HANDLED)
  366. {
  367. hot_cur = g_list_next (hot_cur);
  368. if (hot_cur == NULL)
  369. hot_cur = h->widgets;
  370. }
  371. }
  372. if (handled == MSG_HANDLED)
  373. do_select_widget (h, hot_cur, SELECT_EXACT);
  374. return handled;
  375. }
  376. /* --------------------------------------------------------------------------------------------- */
  377. static void
  378. dlg_key_event (Dlg_head * h, int d_key)
  379. {
  380. cb_ret_t handled;
  381. if (h->widgets == NULL)
  382. return;
  383. if (h->current == NULL)
  384. h->current = h->widgets;
  385. /* TAB used to cycle */
  386. if ((h->flags & DLG_WANT_TAB) == 0)
  387. {
  388. if (d_key == '\t')
  389. {
  390. dlg_one_down (h);
  391. return;
  392. }
  393. else if (d_key == KEY_BTAB)
  394. {
  395. dlg_one_up (h);
  396. return;
  397. }
  398. }
  399. /* first can dlg_callback handle the key */
  400. handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
  401. /* next try the hotkey */
  402. if (handled == MSG_NOT_HANDLED)
  403. handled = dlg_try_hotkey (h, d_key);
  404. if (handled == MSG_HANDLED)
  405. h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
  406. else
  407. /* not used - then try widget_callback */
  408. handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
  409. /* not used- try to use the unhandled case */
  410. if (handled == MSG_NOT_HANDLED)
  411. handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
  412. if (handled == MSG_NOT_HANDLED)
  413. handled = dlg_handle_key (h, d_key);
  414. h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
  415. }
  416. /* --------------------------------------------------------------------------------------------- */
  417. static void
  418. frontend_run_dlg (Dlg_head * h)
  419. {
  420. int d_key;
  421. Gpm_Event event;
  422. event.x = -1;
  423. /* close opened editors, viewers, etc */
  424. if (!h->modal && mc_global.widget.midnight_shutdown)
  425. {
  426. h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
  427. return;
  428. }
  429. while (h->state == DLG_ACTIVE)
  430. {
  431. if (mc_global.tty.winch_flag)
  432. dialog_change_screen_size ();
  433. if (is_idle ())
  434. {
  435. if (idle_hook)
  436. execute_hooks (idle_hook);
  437. while ((h->flags & DLG_WANT_IDLE) && is_idle ())
  438. h->callback (h, NULL, DLG_IDLE, 0, NULL);
  439. /* Allow terminating the dialog from the idle handler */
  440. if (h->state != DLG_ACTIVE)
  441. break;
  442. }
  443. update_cursor (h);
  444. /* Clear interrupt flag */
  445. tty_got_interrupt ();
  446. d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
  447. dlg_process_event (h, d_key, &event);
  448. if (h->state == DLG_CLOSED)
  449. h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
  450. }
  451. }
  452. /* --------------------------------------------------------------------------------------------- */
  453. /*** public functions ****************************************************************************/
  454. /* --------------------------------------------------------------------------------------------- */
  455. /** draw box in window */
  456. void
  457. draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
  458. {
  459. tty_draw_box (h->y + y, h->x + x, ys, xs, single);
  460. }
  461. /* --------------------------------------------------------------------------------------------- */
  462. /** Clean the dialog area, draw the frame and the title */
  463. void
  464. common_dialog_repaint (Dlg_head * h)
  465. {
  466. int space;
  467. if (h->state != DLG_ACTIVE)
  468. return;
  469. space = (h->flags & DLG_COMPACT) ? 0 : 1;
  470. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  471. dlg_erase (h);
  472. draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
  473. if (h->title != NULL)
  474. {
  475. tty_setcolor (h->color[DLG_COLOR_TITLE]);
  476. dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
  477. tty_print_string (h->title);
  478. }
  479. }
  480. /* --------------------------------------------------------------------------------------------- */
  481. /** this function allows to set dialog position */
  482. void
  483. dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
  484. {
  485. /* save old positions, will be used to reposition childs */
  486. int ox, oy, oc, ol;
  487. int shift_x, shift_y, scale_x, scale_y;
  488. /* save old positions, will be used to reposition childs */
  489. ox = h->x;
  490. oy = h->y;
  491. oc = h->cols;
  492. ol = h->lines;
  493. h->x = x1;
  494. h->y = y1;
  495. h->lines = y2 - y1;
  496. h->cols = x2 - x1;
  497. /* dialog is empty */
  498. if (h->widgets == NULL)
  499. return;
  500. if (h->current == NULL)
  501. h->current = h->widgets;
  502. /* values by which controls should be moved */
  503. shift_x = h->x - ox;
  504. shift_y = h->y - oy;
  505. scale_x = h->cols - oc;
  506. scale_y = h->lines - ol;
  507. if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
  508. {
  509. GList *w;
  510. for (w = h->widgets; w != NULL; w = g_list_next (w))
  511. {
  512. /* there are, mainly, 2 generally possible
  513. situations:
  514. 1. control sticks to one side - it
  515. should be moved
  516. 2. control sticks to two sides of
  517. one direction - it should be sized */
  518. Widget *c = (Widget *) w->data;
  519. int x = c->x;
  520. int y = c->y;
  521. int cols = c->cols;
  522. int lines = c->lines;
  523. if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
  524. {
  525. x += shift_x;
  526. cols += scale_x;
  527. }
  528. else if (c->pos_flags & WPOS_KEEP_LEFT)
  529. x += shift_x;
  530. else if (c->pos_flags & WPOS_KEEP_RIGHT)
  531. x += shift_x + scale_x;
  532. if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
  533. {
  534. y += shift_y;
  535. lines += scale_y;
  536. }
  537. else if (c->pos_flags & WPOS_KEEP_TOP)
  538. y += shift_y;
  539. else if (c->pos_flags & WPOS_KEEP_BOTTOM)
  540. y += shift_y + scale_y;
  541. widget_set_size (c, y, x, lines, cols);
  542. }
  543. }
  544. }
  545. /* --------------------------------------------------------------------------------------------- */
  546. /** this function sets only size, leaving positioning to automatic methods */
  547. void
  548. dlg_set_size (Dlg_head * h, int lines, int cols)
  549. {
  550. int x = h->x;
  551. int y = h->y;
  552. if (h->flags & DLG_CENTER)
  553. {
  554. y = (LINES - lines) / 2;
  555. x = (COLS - cols) / 2;
  556. }
  557. if ((h->flags & DLG_TRYUP) && (y > 3))
  558. y -= 2;
  559. dlg_set_position (h, y, x, y + lines, x + cols);
  560. }
  561. /* --------------------------------------------------------------------------------------------- */
  562. /** Default dialog callback */
  563. cb_ret_t
  564. default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
  565. {
  566. (void) sender;
  567. (void) parm;
  568. (void) data;
  569. switch (msg)
  570. {
  571. case DLG_DRAW:
  572. if (h->color != NULL)
  573. {
  574. common_dialog_repaint (h);
  575. return MSG_HANDLED;
  576. }
  577. return MSG_NOT_HANDLED;
  578. case DLG_IDLE:
  579. dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
  580. return MSG_HANDLED;
  581. case DLG_RESIZE:
  582. /* this is default resizing mechanism */
  583. /* the main idea of this code is to resize dialog
  584. according to flags (if any of flags require automatic
  585. resizing, like DLG_CENTER, end after that reposition
  586. controls in dialog according to flags of widget) */
  587. dlg_set_size (h, h->lines, h->cols);
  588. return MSG_HANDLED;
  589. default:
  590. break;
  591. }
  592. return MSG_NOT_HANDLED;
  593. }
  594. /* --------------------------------------------------------------------------------------------- */
  595. Dlg_head *
  596. create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
  597. const int *colors, dlg_cb_fn callback, const char *help_ctx,
  598. const char *title, dlg_flags_t flags)
  599. {
  600. Dlg_head *new_d;
  601. new_d = g_new0 (Dlg_head, 1);
  602. new_d->modal = modal;
  603. if (colors != NULL)
  604. memmove (new_d->color, colors, sizeof (dlg_colors_t));
  605. new_d->help_ctx = help_ctx;
  606. new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
  607. new_d->x = x1;
  608. new_d->y = y1;
  609. new_d->flags = flags;
  610. new_d->data = NULL;
  611. dlg_set_size (new_d, lines, cols);
  612. new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
  613. && new_d->cols == COLS && new_d->lines == LINES);
  614. new_d->mouse_status = MOU_NORMAL;
  615. /* Strip existing spaces, add one space before and after the title */
  616. if (title != NULL)
  617. {
  618. char *t;
  619. t = g_strstrip (g_strdup (title));
  620. if (*t != '\0')
  621. new_d->title = g_strdup_printf (" %s ", t);
  622. g_free (t);
  623. }
  624. /* unique name got event group for this dialog */
  625. new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
  626. return new_d;
  627. }
  628. /* --------------------------------------------------------------------------------------------- */
  629. void
  630. dlg_set_default_colors (void)
  631. {
  632. dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
  633. dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
  634. dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
  635. dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
  636. dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
  637. alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
  638. alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
  639. alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
  640. alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
  641. alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
  642. }
  643. /* --------------------------------------------------------------------------------------------- */
  644. void
  645. dlg_erase (Dlg_head * h)
  646. {
  647. if ((h != NULL) && (h->state == DLG_ACTIVE))
  648. tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
  649. }
  650. /* --------------------------------------------------------------------------------------------- */
  651. void
  652. set_idle_proc (Dlg_head * d, int enable)
  653. {
  654. if (enable)
  655. d->flags |= DLG_WANT_IDLE;
  656. else
  657. d->flags &= ~DLG_WANT_IDLE;
  658. }
  659. /* --------------------------------------------------------------------------------------------- */
  660. /**
  661. * Insert widget to dialog before current widget. For dialogs populated
  662. * from the bottom, make the widget current. Return widget number.
  663. */
  664. int
  665. add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
  666. {
  667. Widget *widget = (Widget *) w;
  668. /* Don't accept 0 widgets */
  669. if (w == NULL)
  670. abort ();
  671. widget->x += h->x;
  672. widget->y += h->y;
  673. widget->owner = h;
  674. widget->pos_flags = pos_flags;
  675. widget->id = g_list_length (h->widgets);
  676. if ((h->flags & DLG_REVERSE) != 0)
  677. h->widgets = g_list_prepend (h->widgets, widget);
  678. else
  679. h->widgets = g_list_append (h->widgets, widget);
  680. h->current = h->widgets;
  681. return widget->id;
  682. }
  683. /* --------------------------------------------------------------------------------------------- */
  684. /** wrapper to simply add lefttop positioned controls */
  685. int
  686. add_widget (Dlg_head * h, void *w)
  687. {
  688. return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
  689. }
  690. /* --------------------------------------------------------------------------------------------- */
  691. void
  692. do_refresh (void)
  693. {
  694. GList *d = top_dlg;
  695. if (fast_refresh)
  696. {
  697. if ((d != NULL) && (d->data != NULL))
  698. dlg_redraw ((Dlg_head *) d->data);
  699. }
  700. else
  701. {
  702. /* Search first fullscreen dialog */
  703. for (; d != NULL; d = g_list_next (d))
  704. if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
  705. break;
  706. /* back to top dialog */
  707. for (; d != NULL; d = g_list_previous (d))
  708. if (d->data != NULL)
  709. dlg_redraw ((Dlg_head *) d->data);
  710. }
  711. }
  712. /* --------------------------------------------------------------------------------------------- */
  713. /** broadcast a message to all the widgets in a dialog */
  714. void
  715. dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
  716. {
  717. dlg_broadcast_msg_to (h, msg, reverse, 0);
  718. }
  719. /* --------------------------------------------------------------------------------------------- */
  720. int
  721. dlg_focus (Dlg_head * h)
  722. {
  723. /* cannot focus disabled widget ... */
  724. if ((h->current != NULL) && (h->state == DLG_ACTIVE))
  725. {
  726. Widget *current = (Widget *) h->current->data;
  727. if (((current->options & W_DISABLED) == 0)
  728. && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
  729. {
  730. h->callback (h, current, DLG_FOCUS, 0, NULL);
  731. return 1;
  732. }
  733. }
  734. return 0;
  735. }
  736. /* --------------------------------------------------------------------------------------------- */
  737. /** Return true if the windows overlap */
  738. int
  739. dlg_overlap (Widget * a, Widget * b)
  740. {
  741. return !((b->x >= a->x + a->cols)
  742. || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
  743. }
  744. /* --------------------------------------------------------------------------------------------- */
  745. /** Find the widget with the given callback in the dialog h */
  746. Widget *
  747. find_widget_type (const Dlg_head * h, callback_fn callback)
  748. {
  749. GList *w;
  750. w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
  751. return (w == NULL) ? NULL : (Widget *) w->data;
  752. }
  753. /* --------------------------------------------------------------------------------------------- */
  754. /** Find the widget with the given id */
  755. Widget *
  756. dlg_find_by_id (const Dlg_head * h, unsigned int id)
  757. {
  758. if (h->widgets != NULL)
  759. {
  760. GList *w;
  761. for (w = h->widgets; w != NULL; w = g_list_next (w))
  762. if (((Widget *) w->data)->id == id)
  763. return (Widget *) w->data;
  764. }
  765. return NULL;
  766. }
  767. /* --------------------------------------------------------------------------------------------- */
  768. /** Find the widget with the given id in the dialog h and select it */
  769. void
  770. dlg_select_by_id (const Dlg_head * h, unsigned int id)
  771. {
  772. Widget *w;
  773. w = dlg_find_by_id (h, id);
  774. if (w != NULL)
  775. dlg_select_widget (w);
  776. }
  777. /* --------------------------------------------------------------------------------------------- */
  778. /*
  779. * Try to select widget in the dialog.
  780. */
  781. void
  782. dlg_select_widget (void *w)
  783. {
  784. const Widget *widget = (Widget *) w;
  785. Dlg_head *h = widget->owner;
  786. do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
  787. }
  788. /* --------------------------------------------------------------------------------------------- */
  789. /** Try to select previous widget in the tab order */
  790. void
  791. dlg_one_up (Dlg_head * h)
  792. {
  793. if (h->widgets != NULL)
  794. {
  795. GList *prev;
  796. prev = g_list_previous (h->current);
  797. if (prev == NULL)
  798. prev = g_list_last (h->widgets);
  799. do_select_widget (h, prev, SELECT_PREV);
  800. }
  801. }
  802. /* --------------------------------------------------------------------------------------------- */
  803. /** Try to select next widget in the tab order */
  804. void
  805. dlg_one_down (Dlg_head * h)
  806. {
  807. if (h->widgets != NULL)
  808. {
  809. GList *next;
  810. next = g_list_next (h->current);
  811. if (next == NULL)
  812. next = h->widgets;
  813. do_select_widget (h, next, SELECT_NEXT);
  814. }
  815. }
  816. /* --------------------------------------------------------------------------------------------- */
  817. void
  818. update_cursor (Dlg_head * h)
  819. {
  820. GList *p = h->current;
  821. if ((p != NULL) && (h->state == DLG_ACTIVE))
  822. {
  823. Widget *w;
  824. w = (Widget *) p->data;
  825. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  826. send_message (w, WIDGET_CURSOR, 0);
  827. else
  828. do
  829. {
  830. p = g_list_next (p);
  831. if (p == NULL)
  832. p = h->widgets;
  833. if (p == h->current)
  834. break;
  835. w = (Widget *) p->data;
  836. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  837. if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
  838. break;
  839. }
  840. while (TRUE);
  841. }
  842. }
  843. /* --------------------------------------------------------------------------------------------- */
  844. /**
  845. * Redraw the widgets in reverse order, leaving the current widget
  846. * as the last one
  847. */
  848. void
  849. dlg_redraw (Dlg_head * h)
  850. {
  851. if (h->state != DLG_ACTIVE)
  852. return;
  853. if (h->winch_pending)
  854. {
  855. h->winch_pending = FALSE;
  856. h->callback (h, NULL, DLG_RESIZE, 0, NULL);
  857. }
  858. h->callback (h, NULL, DLG_DRAW, 0, NULL);
  859. dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
  860. update_cursor (h);
  861. }
  862. /* --------------------------------------------------------------------------------------------- */
  863. void
  864. dlg_stop (Dlg_head * h)
  865. {
  866. h->state = DLG_CLOSED;
  867. }
  868. /* --------------------------------------------------------------------------------------------- */
  869. /** Init the process */
  870. void
  871. init_dlg (Dlg_head * h)
  872. {
  873. if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
  874. h->modal = TRUE;
  875. /* add dialog to the stack */
  876. top_dlg = g_list_prepend (top_dlg, h);
  877. /* Initialize dialog manager and widgets */
  878. if (h->state == DLG_ACTIVE)
  879. {
  880. if (!h->modal)
  881. dialog_switch_add (h);
  882. h->callback (h, NULL, DLG_INIT, 0, NULL);
  883. dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
  884. dlg_read_history (h);
  885. }
  886. h->state = DLG_ACTIVE;
  887. dlg_redraw (h);
  888. /* Select the first widget that takes focus */
  889. while (h->current != NULL && !dlg_focus (h))
  890. {
  891. h->current = g_list_next (h->current);
  892. if (h->current == NULL)
  893. h->current = h->widgets;
  894. }
  895. h->ret_value = 0;
  896. }
  897. /* --------------------------------------------------------------------------------------------- */
  898. void
  899. dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
  900. {
  901. if (key == EV_NONE)
  902. {
  903. if (tty_got_interrupt ())
  904. if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
  905. dlg_execute_cmd (h, CK_Cancel);
  906. return;
  907. }
  908. if (key == EV_MOUSE)
  909. h->mouse_status = dlg_mouse_event (h, event);
  910. else
  911. dlg_key_event (h, key);
  912. }
  913. /* --------------------------------------------------------------------------------------------- */
  914. /** Shutdown the run_dlg */
  915. void
  916. dlg_run_done (Dlg_head * h)
  917. {
  918. top_dlg = g_list_remove (top_dlg, h);
  919. if (h->state == DLG_CLOSED)
  920. {
  921. h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
  922. if (!h->modal)
  923. dialog_switch_remove (h);
  924. }
  925. }
  926. /* --------------------------------------------------------------------------------------------- */
  927. /**
  928. * Standard run dialog routine
  929. * We have to keep this routine small so that we can duplicate it's
  930. * behavior on complex routines like the file routines, this way,
  931. * they can call the dlg_process_event without rewriting all the code
  932. */
  933. int
  934. run_dlg (Dlg_head * h)
  935. {
  936. init_dlg (h);
  937. frontend_run_dlg (h);
  938. dlg_run_done (h);
  939. return h->ret_value;
  940. }
  941. /* --------------------------------------------------------------------------------------------- */
  942. void
  943. destroy_dlg (Dlg_head * h)
  944. {
  945. /* if some widgets have history, save all history at one moment here */
  946. dlg_save_history (h);
  947. dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
  948. g_list_foreach (h->widgets, (GFunc) g_free, NULL);
  949. g_list_free (h->widgets);
  950. mc_event_group_del (h->event_group);
  951. g_free (h->event_group);
  952. g_free (h->title);
  953. g_free (h);
  954. do_refresh ();
  955. }
  956. /* --------------------------------------------------------------------------------------------- */
  957. /**
  958. * Write history to the ${XDG_CACHE_HOME}/mc/history file
  959. */
  960. void
  961. dlg_save_history (Dlg_head * h)
  962. {
  963. char *profile;
  964. int i;
  965. if (num_history_items_recorded == 0) /* this is how to disable */
  966. return;
  967. profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, (char *) NULL);
  968. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  969. if (i != -1)
  970. close (i);
  971. /* Make sure the history is only readable by the user */
  972. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  973. {
  974. ev_history_load_save_t event_data;
  975. event_data.cfg = mc_config_init (profile);
  976. event_data.receiver = NULL;
  977. /* get all histories in dialog */
  978. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  979. mc_config_save_file (event_data.cfg, NULL);
  980. mc_config_deinit (event_data.cfg);
  981. }
  982. g_free (profile);
  983. }
  984. /* --------------------------------------------------------------------------------------------- */
  985. char *
  986. dlg_get_title (const Dlg_head * h, size_t len)
  987. {
  988. char *t;
  989. if (h == NULL)
  990. abort ();
  991. if (h->get_title != NULL)
  992. t = h->get_title (h, len);
  993. else
  994. t = g_strdup ("");
  995. return t;
  996. }
  997. /* --------------------------------------------------------------------------------------------- */
  998. /** Replace widget old_w for widget new_w in the dialog */
  999. void
  1000. dlg_replace_widget (Widget * old_w, Widget * new_w)
  1001. {
  1002. Dlg_head *h = old_w->owner;
  1003. gboolean should_focus = FALSE;
  1004. if (h->widgets == NULL)
  1005. return;
  1006. if (h->current == NULL)
  1007. h->current = h->widgets;
  1008. if (old_w == h->current->data)
  1009. should_focus = TRUE;
  1010. new_w->owner = h;
  1011. new_w->id = old_w->id;
  1012. if (should_focus)
  1013. h->current->data = new_w;
  1014. else
  1015. g_list_find (h->widgets, old_w)->data = new_w;
  1016. send_message (old_w, WIDGET_DESTROY, 0);
  1017. send_message (new_w, WIDGET_INIT, 0);
  1018. if (should_focus)
  1019. dlg_select_widget (new_w);
  1020. send_message (new_w, WIDGET_DRAW, 0);
  1021. }
  1022. /* --------------------------------------------------------------------------------------------- */