dialog.c 33 KB

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