dialog.c 31 KB

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