dialog.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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, 2013
  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 (WDialog * 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 (WDialog * 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 (WDialog * 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, NULL, msg, 0, NULL);
  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 (WDialog * 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 (WDialog * 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, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
  142. {
  143. send_message (h, current, MSG_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 = WIDGET (a);
  154. widget_cb_fn f = (widget_cb_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 (WDialog * 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 (widget_overlapped (w0, WIDGET (h->current->data)))
  192. {
  193. send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
  194. send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
  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 (WDialog * 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 (WDialog * 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 (send_message (h, NULL, MSG_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 (WDialog * h, Gpm_Event * event)
  285. {
  286. Widget *wh = WIDGET (h);
  287. GList *p, *first;
  288. /* close the dialog by mouse left click out of dialog area */
  289. if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
  290. && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
  291. {
  292. h->ret_value = B_CANCEL;
  293. dlg_stop (h);
  294. return MOU_NORMAL;
  295. }
  296. if (wh->mouse != NULL)
  297. {
  298. int mou;
  299. mou = wh->mouse (event, wh);
  300. if (mou != MOU_UNHANDLED)
  301. return mou;
  302. }
  303. first = h->current;
  304. p = first;
  305. do
  306. {
  307. Widget *w = WIDGET (p->data);
  308. p = dlg_widget_prev (h, p);
  309. if ((w->options & W_DISABLED) == 0 && w->mouse != NULL)
  310. {
  311. /* put global cursor position to the widget */
  312. int ret;
  313. ret = w->mouse (event, w);
  314. if (ret != MOU_UNHANDLED)
  315. return ret;
  316. }
  317. }
  318. while (p != first);
  319. return MOU_UNHANDLED;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. static cb_ret_t
  323. dlg_try_hotkey (WDialog * h, int d_key)
  324. {
  325. GList *hot_cur;
  326. Widget *current;
  327. cb_ret_t handled;
  328. int c;
  329. if (h->widgets == NULL)
  330. return MSG_NOT_HANDLED;
  331. if (h->current == NULL)
  332. h->current = h->widgets;
  333. /*
  334. * Explanation: we don't send letter hotkeys to other widgets if
  335. * the currently selected widget is an input line
  336. */
  337. current = WIDGET (h->current->data);
  338. if ((current->options & W_DISABLED) != 0)
  339. return MSG_NOT_HANDLED;
  340. if (current->options & W_IS_INPUT)
  341. {
  342. /* skip ascii control characters, anything else can valid character in
  343. * some encoding */
  344. if (d_key >= 32 && d_key < 256)
  345. return MSG_NOT_HANDLED;
  346. }
  347. /* If it's an alt key, send the message */
  348. c = d_key & ~ALT (0);
  349. if (d_key & ALT (0) && g_ascii_isalpha (c))
  350. d_key = g_ascii_tolower (c);
  351. handled = MSG_NOT_HANDLED;
  352. if ((current->options & W_WANT_HOTKEY) != 0)
  353. handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
  354. /* If not used, send hotkey to other widgets */
  355. if (handled == MSG_HANDLED)
  356. return MSG_HANDLED;
  357. hot_cur = dlg_widget_next (h, h->current);
  358. /* send it to all widgets */
  359. while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
  360. {
  361. current = WIDGET (hot_cur->data);
  362. if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
  363. handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
  364. if (handled == MSG_NOT_HANDLED)
  365. hot_cur = dlg_widget_next (h, hot_cur);
  366. }
  367. if (handled == MSG_HANDLED)
  368. do_select_widget (h, hot_cur, SELECT_EXACT);
  369. return handled;
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. static void
  373. dlg_key_event (WDialog * h, int d_key)
  374. {
  375. cb_ret_t handled;
  376. if (h->widgets == NULL)
  377. return;
  378. if (h->current == NULL)
  379. h->current = h->widgets;
  380. /* TAB used to cycle */
  381. if ((h->flags & DLG_WANT_TAB) == 0)
  382. {
  383. if (d_key == '\t')
  384. {
  385. dlg_one_down (h);
  386. return;
  387. }
  388. else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
  389. {
  390. dlg_one_up (h);
  391. return;
  392. }
  393. }
  394. /* first can dlg_callback handle the key */
  395. handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
  396. /* next try the hotkey */
  397. if (handled == MSG_NOT_HANDLED)
  398. handled = dlg_try_hotkey (h, d_key);
  399. if (handled == MSG_HANDLED)
  400. send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
  401. else
  402. /* not used - then try widget_callback */
  403. handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
  404. /* not used- try to use the unhandled case */
  405. if (handled == MSG_NOT_HANDLED)
  406. handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
  407. if (handled == MSG_NOT_HANDLED)
  408. handled = dlg_handle_key (h, d_key);
  409. (void) handled;
  410. send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
  411. }
  412. /* --------------------------------------------------------------------------------------------- */
  413. static void
  414. frontend_dlg_run (WDialog * h)
  415. {
  416. Gpm_Event event;
  417. event.x = -1;
  418. /* close opened editors, viewers, etc */
  419. if (!h->modal && mc_global.midnight_shutdown)
  420. {
  421. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  422. return;
  423. }
  424. while (h->state == DLG_ACTIVE)
  425. {
  426. int d_key;
  427. if (mc_global.tty.winch_flag != 0)
  428. dialog_change_screen_size ();
  429. if (is_idle ())
  430. {
  431. if (idle_hook)
  432. execute_hooks (idle_hook);
  433. while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
  434. send_message (h, NULL, MSG_IDLE, 0, NULL);
  435. /* Allow terminating the dialog from the idle handler */
  436. if (h->state != DLG_ACTIVE)
  437. break;
  438. }
  439. update_cursor (h);
  440. /* Clear interrupt flag */
  441. tty_got_interrupt ();
  442. d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
  443. dlg_process_event (h, d_key, &event);
  444. if (h->state == DLG_CLOSED)
  445. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  446. }
  447. }
  448. /* --------------------------------------------------------------------------------------------- */
  449. static int
  450. dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
  451. {
  452. Widget *w = WIDGET (a);
  453. unsigned long id = GPOINTER_TO_UINT (b);
  454. return w->id == id ? 0 : 1;
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. /*** public functions ****************************************************************************/
  458. /* --------------------------------------------------------------------------------------------- */
  459. /** Clean the dialog area, draw the frame and the title */
  460. void
  461. dlg_default_repaint (WDialog * h)
  462. {
  463. Widget *wh = WIDGET (h);
  464. int space;
  465. if (h->state != DLG_ACTIVE)
  466. return;
  467. space = (h->flags & DLG_COMPACT) ? 0 : 1;
  468. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  469. dlg_erase (h);
  470. tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
  471. if (h->title != NULL)
  472. {
  473. tty_setcolor (h->color[DLG_COLOR_TITLE]);
  474. widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
  475. tty_print_string (h->title);
  476. }
  477. }
  478. /* --------------------------------------------------------------------------------------------- */
  479. /** this function allows to set dialog position */
  480. void
  481. dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
  482. {
  483. Widget *wh = WIDGET (h);
  484. /* save old positions, will be used to reposition childs */
  485. int ox, oy, oc, ol;
  486. int shift_x, shift_y, scale_x, scale_y;
  487. /* save old positions, will be used to reposition childs */
  488. ox = wh->x;
  489. oy = wh->y;
  490. oc = wh->cols;
  491. ol = wh->lines;
  492. wh->x = x1;
  493. wh->y = y1;
  494. wh->lines = y2 - y1;
  495. wh->cols = x2 - x1;
  496. /* dialog is empty */
  497. if (h->widgets == NULL)
  498. return;
  499. if (h->current == NULL)
  500. h->current = h->widgets;
  501. /* values by which controls should be moved */
  502. shift_x = wh->x - ox;
  503. shift_y = wh->y - oy;
  504. scale_x = wh->cols - oc;
  505. scale_y = wh->lines - ol;
  506. if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
  507. {
  508. GList *w;
  509. for (w = h->widgets; w != NULL; w = g_list_next (w))
  510. {
  511. /* there are, mainly, 2 generally possible
  512. situations:
  513. 1. control sticks to one side - it
  514. should be moved
  515. 2. control sticks to two sides of
  516. one direction - it should be sized */
  517. Widget *c = WIDGET (w->data);
  518. int x = c->x;
  519. int y = c->y;
  520. int cols = c->cols;
  521. int lines = c->lines;
  522. if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
  523. x = wh->x + (wh->cols - c->cols) / 2;
  524. else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
  525. {
  526. x += shift_x;
  527. cols += scale_x;
  528. }
  529. else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
  530. x += shift_x;
  531. else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
  532. x += shift_x + scale_x;
  533. if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
  534. y = wh->y + (wh->lines - c->lines) / 2;
  535. else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
  536. {
  537. y += shift_y;
  538. lines += scale_y;
  539. }
  540. else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
  541. y += shift_y;
  542. else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
  543. y += shift_y + scale_y;
  544. widget_set_size (c, y, x, lines, cols);
  545. }
  546. }
  547. }
  548. /* --------------------------------------------------------------------------------------------- */
  549. /** this function sets only size, leaving positioning to automatic methods */
  550. void
  551. dlg_set_size (WDialog * h, int lines, int cols)
  552. {
  553. int x = WIDGET (h)->x;
  554. int y = WIDGET (h)->y;
  555. if (h->flags & DLG_CENTER)
  556. {
  557. y = (LINES - lines) / 2;
  558. x = (COLS - cols) / 2;
  559. }
  560. if ((h->flags & DLG_TRYUP) && (y > 3))
  561. y -= 2;
  562. dlg_set_position (h, y, x, y + lines, x + cols);
  563. }
  564. /* --------------------------------------------------------------------------------------------- */
  565. /** Default dialog callback */
  566. cb_ret_t
  567. dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  568. {
  569. WDialog *h = DIALOG (w);
  570. (void) sender;
  571. (void) parm;
  572. (void) data;
  573. switch (msg)
  574. {
  575. case MSG_DRAW:
  576. if (h->color != NULL)
  577. {
  578. dlg_default_repaint (h);
  579. return MSG_HANDLED;
  580. }
  581. return MSG_NOT_HANDLED;
  582. case MSG_IDLE:
  583. dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
  584. return MSG_HANDLED;
  585. case MSG_RESIZE:
  586. /* this is default resizing mechanism */
  587. /* the main idea of this code is to resize dialog
  588. according to flags (if any of flags require automatic
  589. resizing, like DLG_CENTER, end after that reposition
  590. controls in dialog according to flags of widget) */
  591. dlg_set_size (h, w->lines, w->cols);
  592. return MSG_HANDLED;
  593. default:
  594. break;
  595. }
  596. return MSG_NOT_HANDLED;
  597. }
  598. /* --------------------------------------------------------------------------------------------- */
  599. WDialog *
  600. dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
  601. const int *colors, widget_cb_fn callback, mouse_h mouse_handler,
  602. const char *help_ctx, const char *title, dlg_flags_t flags)
  603. {
  604. WDialog *new_d;
  605. Widget *w;
  606. new_d = g_new0 (WDialog, 1);
  607. w = WIDGET (new_d);
  608. widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
  609. mouse_handler);
  610. widget_want_cursor (w, FALSE);
  611. new_d->state = DLG_CONSTRUCT;
  612. new_d->modal = modal;
  613. if (colors != NULL)
  614. memmove (new_d->color, colors, sizeof (dlg_colors_t));
  615. new_d->help_ctx = help_ctx;
  616. new_d->flags = flags;
  617. new_d->data = NULL;
  618. dlg_set_size (new_d, lines, cols);
  619. new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
  620. new_d->mouse_status = MOU_UNHANDLED;
  621. /* Strip existing spaces, add one space before and after the title */
  622. if (title != NULL && *title != '\0')
  623. {
  624. char *t;
  625. t = g_strstrip (g_strdup (title));
  626. if (*t != '\0')
  627. new_d->title = g_strdup_printf (" %s ", t);
  628. g_free (t);
  629. }
  630. /* unique name of event group for this dialog */
  631. new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
  632. return new_d;
  633. }
  634. /* --------------------------------------------------------------------------------------------- */
  635. void
  636. dlg_set_default_colors (void)
  637. {
  638. dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
  639. dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
  640. dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
  641. dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
  642. dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
  643. alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
  644. alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
  645. alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
  646. alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
  647. alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
  648. }
  649. /* --------------------------------------------------------------------------------------------- */
  650. void
  651. dlg_erase (WDialog * h)
  652. {
  653. if ((h != NULL) && (h->state == DLG_ACTIVE))
  654. {
  655. Widget *wh = WIDGET (h);
  656. tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
  657. }
  658. }
  659. /* --------------------------------------------------------------------------------------------- */
  660. /**
  661. * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
  662. */
  663. unsigned long
  664. add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
  665. {
  666. Widget *wh = WIDGET (h);
  667. Widget *widget;
  668. /* Don't accept 0 widgets */
  669. if (w == NULL)
  670. abort ();
  671. widget = WIDGET (w);
  672. if ((pos_flags & WPOS_CENTER_HORZ) != 0)
  673. widget->x = (wh->cols - widget->cols) / 2;
  674. widget->x += wh->x;
  675. if ((pos_flags & WPOS_CENTER_VERT) != 0)
  676. widget->y = (wh->lines - widget->lines) / 2;
  677. widget->y += wh->y;
  678. widget->owner = h;
  679. widget->pos_flags = pos_flags;
  680. widget->id = h->widget_id++;
  681. if (h->widgets == NULL || before == NULL)
  682. {
  683. h->widgets = g_list_append (h->widgets, widget);
  684. h->current = g_list_last (h->widgets);
  685. }
  686. else
  687. {
  688. GList *b;
  689. b = g_list_find (h->widgets, before);
  690. /* don't accept widget not from dialog. This shouldn't happen */
  691. if (b == NULL)
  692. abort ();
  693. b = g_list_next (b);
  694. h->widgets = g_list_insert_before (h->widgets, b, widget);
  695. if (b != NULL)
  696. h->current = g_list_previous (b);
  697. else
  698. h->current = g_list_last (h->widgets);
  699. }
  700. /* widget has been added in runtime */
  701. if (h->state == DLG_ACTIVE)
  702. {
  703. send_message (widget, NULL, MSG_INIT, 0, NULL);
  704. send_message (widget, NULL, MSG_DRAW, 0, NULL);
  705. send_message (widget, NULL, MSG_FOCUS, 0, NULL);
  706. }
  707. return widget->id;
  708. }
  709. /* --------------------------------------------------------------------------------------------- */
  710. /** wrapper to simply add lefttop positioned controls */
  711. unsigned long
  712. add_widget (WDialog * h, void *w)
  713. {
  714. return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
  715. h->current != NULL ? h->current->data : NULL);
  716. }
  717. /* --------------------------------------------------------------------------------------------- */
  718. unsigned long
  719. add_widget_before (WDialog * h, void *w, void *before)
  720. {
  721. return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
  722. }
  723. /* --------------------------------------------------------------------------------------------- */
  724. /** delete widget from dialog */
  725. void
  726. del_widget (void *w)
  727. {
  728. WDialog *h;
  729. GList *d;
  730. /* Don't accept NULL widget. This shouldn't happen */
  731. if (w == NULL)
  732. abort ();
  733. h = WIDGET (w)->owner;
  734. d = g_list_find (h->widgets, w);
  735. if (d == h->current)
  736. h->current = dlg_widget_next (h, d);
  737. h->widgets = g_list_remove_link (h->widgets, d);
  738. send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
  739. g_free (d->data);
  740. g_list_free_1 (d);
  741. /* widget has been deleted in runtime */
  742. if (h->state == DLG_ACTIVE)
  743. {
  744. dlg_redraw (h);
  745. dlg_focus (h);
  746. }
  747. }
  748. /* --------------------------------------------------------------------------------------------- */
  749. void
  750. do_refresh (void)
  751. {
  752. GList *d = top_dlg;
  753. if (fast_refresh)
  754. {
  755. if ((d != NULL) && (d->data != NULL))
  756. dlg_redraw (DIALOG (d->data));
  757. }
  758. else
  759. {
  760. /* Search first fullscreen dialog */
  761. for (; d != NULL; d = g_list_next (d))
  762. if (d->data != NULL && DIALOG (d->data)->fullscreen)
  763. break;
  764. /* back to top dialog */
  765. for (; d != NULL; d = g_list_previous (d))
  766. if (d->data != NULL)
  767. dlg_redraw (DIALOG (d->data));
  768. }
  769. }
  770. /* --------------------------------------------------------------------------------------------- */
  771. /** broadcast a message to all the widgets in a dialog */
  772. void
  773. dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
  774. {
  775. dlg_broadcast_msg_to (h, msg, FALSE, 0);
  776. }
  777. /* --------------------------------------------------------------------------------------------- */
  778. gboolean
  779. dlg_focus (WDialog * h)
  780. {
  781. /* cannot focus disabled widget */
  782. if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
  783. {
  784. Widget *current = WIDGET (h->current->data);
  785. if (((current->options & W_DISABLED) == 0)
  786. && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
  787. {
  788. send_message (h, current, MSG_FOCUS, 0, NULL);
  789. return TRUE;
  790. }
  791. }
  792. return FALSE;
  793. }
  794. /* --------------------------------------------------------------------------------------------- */
  795. /** Find the widget with the given callback in the dialog h */
  796. Widget *
  797. find_widget_type (const WDialog * h, widget_cb_fn callback)
  798. {
  799. GList *w;
  800. w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
  801. return (w == NULL) ? NULL : WIDGET (w->data);
  802. }
  803. /* --------------------------------------------------------------------------------------------- */
  804. /** Find the widget with the given id */
  805. Widget *
  806. dlg_find_by_id (const WDialog * h, unsigned long id)
  807. {
  808. GList *w;
  809. w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
  810. return w != NULL ? WIDGET (w->data) : NULL;
  811. }
  812. /* --------------------------------------------------------------------------------------------- */
  813. /** Find the widget with the given id in the dialog h and select it */
  814. void
  815. dlg_select_by_id (const WDialog * h, unsigned long id)
  816. {
  817. Widget *w;
  818. w = dlg_find_by_id (h, id);
  819. if (w != NULL)
  820. dlg_select_widget (w);
  821. }
  822. /* --------------------------------------------------------------------------------------------- */
  823. /**
  824. * Try to select widget in the dialog.
  825. */
  826. void
  827. dlg_select_widget (void *w)
  828. {
  829. Widget *widget = WIDGET (w);
  830. WDialog *h = widget->owner;
  831. do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
  832. }
  833. /* --------------------------------------------------------------------------------------------- */
  834. /**
  835. * Set widget at top of widget list and make it current.
  836. */
  837. void
  838. dlg_set_top_widget (void *w)
  839. {
  840. Widget *widget = WIDGET (w);
  841. WDialog *h = widget->owner;
  842. GList *l;
  843. l = g_list_find (h->widgets, w);
  844. if (l == NULL)
  845. abort (); /* widget is not in dialog, this should not happen */
  846. /* unfocus prevoius widget and focus current one before widget reordering */
  847. if (h->state == DLG_ACTIVE)
  848. do_select_widget (h, l, SELECT_EXACT);
  849. /* widget reordering */
  850. h->widgets = g_list_remove_link (h->widgets, l);
  851. h->widgets = g_list_concat (h->widgets, l);
  852. h->current = l;
  853. }
  854. /* --------------------------------------------------------------------------------------------- */
  855. /** Try to select previous widget in the tab order */
  856. void
  857. dlg_one_up (WDialog * h)
  858. {
  859. if (h->widgets != NULL)
  860. do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
  861. }
  862. /* --------------------------------------------------------------------------------------------- */
  863. /** Try to select next widget in the tab order */
  864. void
  865. dlg_one_down (WDialog * h)
  866. {
  867. if (h->widgets != NULL)
  868. do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
  869. }
  870. /* --------------------------------------------------------------------------------------------- */
  871. void
  872. update_cursor (WDialog * h)
  873. {
  874. GList *p = h->current;
  875. if ((p != NULL) && (h->state == DLG_ACTIVE))
  876. {
  877. Widget *w;
  878. w = WIDGET (p->data);
  879. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  880. send_message (w, NULL, MSG_CURSOR, 0, NULL);
  881. else
  882. do
  883. {
  884. p = dlg_widget_next (h, p);
  885. if (p == h->current)
  886. break;
  887. w = WIDGET (p->data);
  888. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  889. if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
  890. break;
  891. }
  892. while (TRUE);
  893. }
  894. }
  895. /* --------------------------------------------------------------------------------------------- */
  896. /**
  897. * Redraw the widgets in reverse order, leaving the current widget
  898. * as the last one
  899. */
  900. void
  901. dlg_redraw (WDialog * h)
  902. {
  903. if (h->state != DLG_ACTIVE)
  904. return;
  905. if (h->winch_pending)
  906. {
  907. h->winch_pending = FALSE;
  908. send_message (h, NULL, MSG_RESIZE, 0, NULL);
  909. }
  910. send_message (h, NULL, MSG_DRAW, 0, NULL);
  911. dlg_broadcast_msg (h, MSG_DRAW);
  912. update_cursor (h);
  913. }
  914. /* --------------------------------------------------------------------------------------------- */
  915. void
  916. dlg_stop (WDialog * h)
  917. {
  918. h->state = DLG_CLOSED;
  919. }
  920. /* --------------------------------------------------------------------------------------------- */
  921. /** Init the process */
  922. void
  923. dlg_init (WDialog * h)
  924. {
  925. if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
  926. h->modal = TRUE;
  927. /* add dialog to the stack */
  928. top_dlg = g_list_prepend (top_dlg, h);
  929. /* Initialize dialog manager and widgets */
  930. if (h->state == DLG_CONSTRUCT)
  931. {
  932. if (!h->modal)
  933. dialog_switch_add (h);
  934. send_message (h, NULL, MSG_INIT, 0, NULL);
  935. dlg_broadcast_msg (h, MSG_INIT);
  936. dlg_read_history (h);
  937. }
  938. h->state = DLG_ACTIVE;
  939. /* first send MSG_DRAW to dialog itself and all widgets... */
  940. dlg_redraw (h);
  941. /* ...then send MSG_FOCUS to select the first widget that can take focus */
  942. while (h->current != NULL && !dlg_focus (h))
  943. h->current = dlg_widget_next (h, h->current);
  944. h->ret_value = 0;
  945. }
  946. /* --------------------------------------------------------------------------------------------- */
  947. void
  948. dlg_process_event (WDialog * h, int key, Gpm_Event * event)
  949. {
  950. if (key == EV_NONE)
  951. {
  952. if (tty_got_interrupt ())
  953. if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
  954. dlg_execute_cmd (h, CK_Cancel);
  955. return;
  956. }
  957. if (key == EV_MOUSE)
  958. h->mouse_status = dlg_mouse_event (h, event);
  959. else
  960. dlg_key_event (h, key);
  961. }
  962. /* --------------------------------------------------------------------------------------------- */
  963. /** Shutdown the dlg_run */
  964. void
  965. dlg_run_done (WDialog * h)
  966. {
  967. top_dlg = g_list_remove (top_dlg, h);
  968. if (h->state == DLG_CLOSED)
  969. {
  970. send_message (h, h->current->data, MSG_END, 0, NULL);
  971. if (!h->modal)
  972. dialog_switch_remove (h);
  973. }
  974. }
  975. /* --------------------------------------------------------------------------------------------- */
  976. /**
  977. * Standard run dialog routine
  978. * We have to keep this routine small so that we can duplicate it's
  979. * behavior on complex routines like the file routines, this way,
  980. * they can call the dlg_process_event without rewriting all the code
  981. */
  982. int
  983. dlg_run (WDialog * h)
  984. {
  985. dlg_init (h);
  986. frontend_dlg_run (h);
  987. dlg_run_done (h);
  988. return h->ret_value;
  989. }
  990. /* --------------------------------------------------------------------------------------------- */
  991. void
  992. dlg_destroy (WDialog * h)
  993. {
  994. /* if some widgets have history, save all history at one moment here */
  995. dlg_save_history (h);
  996. dlg_broadcast_msg (h, MSG_DESTROY);
  997. g_list_free_full (h->widgets, g_free);
  998. mc_event_group_del (h->event_group);
  999. g_free (h->event_group);
  1000. g_free (h->title);
  1001. g_free (h);
  1002. do_refresh ();
  1003. }
  1004. /* --------------------------------------------------------------------------------------------- */
  1005. /**
  1006. * Write history to the ${XDG_CACHE_HOME}/mc/history file
  1007. */
  1008. void
  1009. dlg_save_history (WDialog * h)
  1010. {
  1011. char *profile;
  1012. int i;
  1013. if (num_history_items_recorded == 0) /* this is how to disable */
  1014. return;
  1015. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  1016. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  1017. if (i != -1)
  1018. close (i);
  1019. /* Make sure the history is only readable by the user */
  1020. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  1021. {
  1022. ev_history_load_save_t event_data;
  1023. event_data.cfg = mc_config_init (profile, FALSE);
  1024. event_data.receiver = NULL;
  1025. /* get all histories in dialog */
  1026. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  1027. mc_config_save_file (event_data.cfg, NULL);
  1028. mc_config_deinit (event_data.cfg);
  1029. }
  1030. g_free (profile);
  1031. }
  1032. /* --------------------------------------------------------------------------------------------- */
  1033. char *
  1034. dlg_get_title (const WDialog * h, size_t len)
  1035. {
  1036. char *t;
  1037. if (h == NULL)
  1038. abort ();
  1039. if (h->get_title != NULL)
  1040. t = h->get_title (h, len);
  1041. else
  1042. t = g_strdup ("");
  1043. return t;
  1044. }
  1045. /* --------------------------------------------------------------------------------------------- */