dialog.c 33 KB

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