dialog.c 37 KB

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