dialog.c 37 KB

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