dialog.c 37 KB

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