dialog.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*
  2. Dialog box features module for the Midnight Commander
  3. Copyright (C) 1994-2014
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /** \file dialog.c
  18. * \brief Source: dialog box features module
  19. */
  20. #include <config.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h> /* open() */
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h"
  31. #include "lib/skin.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. dlg_colors_t listbox_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 (WDialog * 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 (WDialog * 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 (WDialog * 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, NULL, msg, 0, NULL);
  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 (WDialog * 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, TRUE);
  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 (WDialog * 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, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
  142. {
  143. send_message (h, current, MSG_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 = WIDGET (a);
  154. widget_cb_fn f = (widget_cb_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 (WDialog * 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 (widget_overlapped (w0, WIDGET (h->current->data)))
  192. {
  193. send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
  194. send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
  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 (WDialog * 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 (WDialog * 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 (send_message (h, NULL, MSG_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 (WDialog * h, Gpm_Event * event)
  285. {
  286. Widget *wh = WIDGET (h);
  287. GList *p, *first;
  288. /* close the dialog by mouse left click out of dialog area */
  289. if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
  290. && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
  291. {
  292. h->ret_value = B_CANCEL;
  293. dlg_stop (h);
  294. return MOU_NORMAL;
  295. }
  296. if (wh->mouse != NULL)
  297. {
  298. int mou;
  299. mou = wh->mouse (event, wh);
  300. if (mou != MOU_UNHANDLED)
  301. return mou;
  302. }
  303. first = h->current;
  304. p = first;
  305. do
  306. {
  307. Widget *w = WIDGET (p->data);
  308. p = dlg_widget_prev (h, p);
  309. if ((w->options & W_DISABLED) == 0 && w->mouse != NULL)
  310. {
  311. /* put global cursor position to the widget */
  312. int ret;
  313. ret = w->mouse (event, w);
  314. if (ret != MOU_UNHANDLED)
  315. return ret;
  316. }
  317. }
  318. while (p != first);
  319. return MOU_UNHANDLED;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. static cb_ret_t
  323. dlg_try_hotkey (WDialog * h, int d_key)
  324. {
  325. GList *hot_cur;
  326. Widget *current;
  327. cb_ret_t handled;
  328. int c;
  329. if (h->widgets == NULL)
  330. return MSG_NOT_HANDLED;
  331. if (h->current == NULL)
  332. h->current = h->widgets;
  333. /*
  334. * Explanation: we don't send letter hotkeys to other widgets if
  335. * the currently selected widget is an input line
  336. */
  337. current = WIDGET (h->current->data);
  338. if ((current->options & W_DISABLED) != 0)
  339. return MSG_NOT_HANDLED;
  340. if (current->options & W_IS_INPUT)
  341. {
  342. /* skip ascii control characters, anything else can valid character in
  343. * some encoding */
  344. if (d_key >= 32 && d_key < 256)
  345. return MSG_NOT_HANDLED;
  346. }
  347. /* If it's an alt key, send the message */
  348. c = d_key & ~ALT (0);
  349. if (d_key & ALT (0) && g_ascii_isalpha (c))
  350. d_key = g_ascii_tolower (c);
  351. handled = MSG_NOT_HANDLED;
  352. if ((current->options & W_WANT_HOTKEY) != 0)
  353. handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
  354. /* If not used, send hotkey to other widgets */
  355. if (handled == MSG_HANDLED)
  356. return MSG_HANDLED;
  357. hot_cur = dlg_widget_next (h, h->current);
  358. /* send it to all widgets */
  359. while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
  360. {
  361. current = WIDGET (hot_cur->data);
  362. if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
  363. handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
  364. if (handled == MSG_NOT_HANDLED)
  365. hot_cur = dlg_widget_next (h, hot_cur);
  366. }
  367. if (handled == MSG_HANDLED)
  368. do_select_widget (h, hot_cur, SELECT_EXACT);
  369. return handled;
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. static void
  373. dlg_key_event (WDialog * h, int d_key)
  374. {
  375. cb_ret_t handled;
  376. if (h->widgets == NULL)
  377. return;
  378. if (h->current == NULL)
  379. h->current = h->widgets;
  380. /* TAB used to cycle */
  381. if ((h->flags & DLG_WANT_TAB) == 0)
  382. {
  383. if (d_key == '\t')
  384. {
  385. dlg_one_down (h);
  386. return;
  387. }
  388. else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
  389. {
  390. dlg_one_up (h);
  391. return;
  392. }
  393. }
  394. /* first can dlg_callback handle the key */
  395. handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
  396. /* next try the hotkey */
  397. if (handled == MSG_NOT_HANDLED)
  398. handled = dlg_try_hotkey (h, d_key);
  399. if (handled == MSG_HANDLED)
  400. send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
  401. else
  402. /* not used - then try widget_callback */
  403. handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
  404. /* not used- try to use the unhandled case */
  405. if (handled == MSG_NOT_HANDLED)
  406. handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
  407. if (handled == MSG_NOT_HANDLED)
  408. handled = dlg_handle_key (h, d_key);
  409. (void) handled;
  410. send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
  411. }
  412. /* --------------------------------------------------------------------------------------------- */
  413. static void
  414. frontend_dlg_run (WDialog * h)
  415. {
  416. Gpm_Event event;
  417. event.x = -1;
  418. /* close opened editors, viewers, etc */
  419. if (!h->modal && mc_global.midnight_shutdown)
  420. {
  421. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  422. return;
  423. }
  424. while (h->state == DLG_ACTIVE)
  425. {
  426. int d_key;
  427. if (mc_global.tty.winch_flag != 0)
  428. dialog_change_screen_size ();
  429. if (is_idle ())
  430. {
  431. if (idle_hook)
  432. execute_hooks (idle_hook);
  433. while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
  434. send_message (h, NULL, MSG_IDLE, 0, NULL);
  435. /* Allow terminating the dialog from the idle handler */
  436. if (h->state != DLG_ACTIVE)
  437. break;
  438. }
  439. update_cursor (h);
  440. /* Clear interrupt flag */
  441. tty_got_interrupt ();
  442. d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
  443. dlg_process_event (h, d_key, &event);
  444. if (h->state == DLG_CLOSED)
  445. send_message (h, NULL, MSG_VALIDATE, 0, NULL);
  446. }
  447. }
  448. /* --------------------------------------------------------------------------------------------- */
  449. static int
  450. dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
  451. {
  452. Widget *w = WIDGET (a);
  453. unsigned long id = GPOINTER_TO_UINT (b);
  454. return w->id == id ? 0 : 1;
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. /*** public functions ****************************************************************************/
  458. /* --------------------------------------------------------------------------------------------- */
  459. /** Clean the dialog area, draw the frame and the title */
  460. void
  461. dlg_default_repaint (WDialog * h)
  462. {
  463. Widget *wh = WIDGET (h);
  464. int space;
  465. if (h->state != DLG_ACTIVE)
  466. return;
  467. space = (h->flags & DLG_COMPACT) ? 0 : 1;
  468. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  469. dlg_erase (h);
  470. tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
  471. if (h->title != NULL)
  472. {
  473. tty_setcolor (h->color[DLG_COLOR_TITLE]);
  474. widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
  475. tty_print_string (h->title);
  476. }
  477. }
  478. /* --------------------------------------------------------------------------------------------- */
  479. /** this function allows to set dialog position */
  480. void
  481. dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
  482. {
  483. Widget *wh = WIDGET (h);
  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 = wh->x;
  489. oy = wh->y;
  490. oc = wh->cols;
  491. ol = wh->lines;
  492. wh->x = x1;
  493. wh->y = y1;
  494. wh->lines = y2 - y1;
  495. wh->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 = wh->x - ox;
  503. shift_y = wh->y - oy;
  504. scale_x = wh->cols - oc;
  505. scale_y = wh->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_CENTER_HORZ) != 0)
  523. x = wh->x + (wh->cols - c->cols) / 2;
  524. else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
  525. {
  526. x += shift_x;
  527. cols += scale_x;
  528. }
  529. else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
  530. x += shift_x;
  531. else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
  532. x += shift_x + scale_x;
  533. if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
  534. y = wh->y + (wh->lines - c->lines) / 2;
  535. else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
  536. {
  537. y += shift_y;
  538. lines += scale_y;
  539. }
  540. else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
  541. y += shift_y;
  542. else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
  543. y += shift_y + scale_y;
  544. widget_set_size (c, y, x, lines, cols);
  545. }
  546. }
  547. }
  548. /* --------------------------------------------------------------------------------------------- */
  549. /** Set dialog size and position */
  550. void
  551. dlg_set_size (WDialog * h, int lines, int cols)
  552. {
  553. int x = WIDGET (h)->x;
  554. int y = WIDGET (h)->y;
  555. if ((h->flags & DLG_CENTER) != 0)
  556. {
  557. y = (LINES - lines) / 2;
  558. x = (COLS - cols) / 2;
  559. }
  560. if ((h->flags & DLG_TRYUP) != 0)
  561. {
  562. if (y > 3)
  563. y -= 2;
  564. else if (y == 3)
  565. y = 2;
  566. }
  567. dlg_set_position (h, y, x, y + lines, x + cols);
  568. }
  569. /* --------------------------------------------------------------------------------------------- */
  570. /** Default dialog callback */
  571. cb_ret_t
  572. dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  573. {
  574. WDialog *h = DIALOG (w);
  575. (void) sender;
  576. (void) parm;
  577. (void) data;
  578. switch (msg)
  579. {
  580. case MSG_DRAW:
  581. if (h->color != NULL)
  582. {
  583. dlg_default_repaint (h);
  584. return MSG_HANDLED;
  585. }
  586. return MSG_NOT_HANDLED;
  587. case MSG_IDLE:
  588. dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
  589. return MSG_HANDLED;
  590. case MSG_RESIZE:
  591. /* this is default resizing mechanism */
  592. /* the main idea of this code is to resize dialog
  593. according to flags (if any of flags require automatic
  594. resizing, like DLG_CENTER, end after that reposition
  595. controls in dialog according to flags of widget) */
  596. dlg_set_size (h, w->lines, w->cols);
  597. return MSG_HANDLED;
  598. default:
  599. break;
  600. }
  601. return MSG_NOT_HANDLED;
  602. }
  603. /* --------------------------------------------------------------------------------------------- */
  604. WDialog *
  605. dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
  606. const int *colors, widget_cb_fn callback, mouse_h mouse_handler,
  607. const char *help_ctx, const char *title, dlg_flags_t flags)
  608. {
  609. WDialog *new_d;
  610. Widget *w;
  611. new_d = g_new0 (WDialog, 1);
  612. w = WIDGET (new_d);
  613. widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
  614. mouse_handler);
  615. widget_want_cursor (w, FALSE);
  616. new_d->state = DLG_CONSTRUCT;
  617. new_d->modal = modal;
  618. new_d->color = colors;
  619. new_d->help_ctx = help_ctx;
  620. new_d->flags = flags;
  621. new_d->data = NULL;
  622. dlg_set_size (new_d, lines, cols);
  623. new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->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 && *title != '\0')
  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 of 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. listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
  653. listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
  654. listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
  655. listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
  656. listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
  657. }
  658. /* --------------------------------------------------------------------------------------------- */
  659. void
  660. dlg_erase (WDialog * h)
  661. {
  662. if ((h != NULL) && (h->state == DLG_ACTIVE))
  663. {
  664. Widget *wh = WIDGET (h);
  665. tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
  666. }
  667. }
  668. /* --------------------------------------------------------------------------------------------- */
  669. /**
  670. * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
  671. */
  672. unsigned long
  673. add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
  674. {
  675. Widget *wh = WIDGET (h);
  676. Widget *widget;
  677. /* Don't accept 0 widgets */
  678. if (w == NULL)
  679. abort ();
  680. widget = WIDGET (w);
  681. if ((pos_flags & WPOS_CENTER_HORZ) != 0)
  682. widget->x = (wh->cols - widget->cols) / 2;
  683. widget->x += wh->x;
  684. if ((pos_flags & WPOS_CENTER_VERT) != 0)
  685. widget->y = (wh->lines - widget->lines) / 2;
  686. widget->y += wh->y;
  687. widget->owner = h;
  688. widget->pos_flags = pos_flags;
  689. widget->id = h->widget_id++;
  690. if (h->widgets == NULL || before == NULL)
  691. {
  692. h->widgets = g_list_append (h->widgets, widget);
  693. h->current = g_list_last (h->widgets);
  694. }
  695. else
  696. {
  697. GList *b;
  698. b = g_list_find (h->widgets, before);
  699. /* don't accept widget not from dialog. This shouldn't happen */
  700. if (b == NULL)
  701. abort ();
  702. b = g_list_next (b);
  703. h->widgets = g_list_insert_before (h->widgets, b, widget);
  704. if (b != NULL)
  705. h->current = g_list_previous (b);
  706. else
  707. h->current = g_list_last (h->widgets);
  708. }
  709. /* widget has been added in runtime */
  710. if (h->state == DLG_ACTIVE)
  711. {
  712. send_message (widget, NULL, MSG_INIT, 0, NULL);
  713. send_message (widget, NULL, MSG_DRAW, 0, NULL);
  714. send_message (widget, NULL, MSG_FOCUS, 0, NULL);
  715. }
  716. return widget->id;
  717. }
  718. /* --------------------------------------------------------------------------------------------- */
  719. /** wrapper to simply add lefttop positioned controls */
  720. unsigned long
  721. add_widget (WDialog * h, void *w)
  722. {
  723. return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
  724. h->current != NULL ? h->current->data : NULL);
  725. }
  726. /* --------------------------------------------------------------------------------------------- */
  727. unsigned long
  728. add_widget_before (WDialog * h, void *w, void *before)
  729. {
  730. return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
  731. }
  732. /* --------------------------------------------------------------------------------------------- */
  733. /** delete widget from dialog */
  734. void
  735. del_widget (void *w)
  736. {
  737. WDialog *h;
  738. GList *d;
  739. /* Don't accept NULL widget. This shouldn't happen */
  740. if (w == NULL)
  741. abort ();
  742. h = WIDGET (w)->owner;
  743. d = g_list_find (h->widgets, w);
  744. if (d == h->current)
  745. h->current = dlg_widget_next (h, d);
  746. h->widgets = g_list_remove_link (h->widgets, d);
  747. send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
  748. g_free (d->data);
  749. g_list_free_1 (d);
  750. /* widget has been deleted in runtime */
  751. if (h->state == DLG_ACTIVE)
  752. {
  753. dlg_redraw (h);
  754. dlg_focus (h);
  755. }
  756. }
  757. /* --------------------------------------------------------------------------------------------- */
  758. void
  759. do_refresh (void)
  760. {
  761. GList *d = top_dlg;
  762. if (fast_refresh)
  763. {
  764. if ((d != NULL) && (d->data != NULL))
  765. dlg_redraw (DIALOG (d->data));
  766. }
  767. else
  768. {
  769. /* Search first fullscreen dialog */
  770. for (; d != NULL; d = g_list_next (d))
  771. if (d->data != NULL && DIALOG (d->data)->fullscreen)
  772. break;
  773. /* back to top dialog */
  774. for (; d != NULL; d = g_list_previous (d))
  775. if (d->data != NULL)
  776. dlg_redraw (DIALOG (d->data));
  777. }
  778. }
  779. /* --------------------------------------------------------------------------------------------- */
  780. /** broadcast a message to all the widgets in a dialog */
  781. void
  782. dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
  783. {
  784. dlg_broadcast_msg_to (h, msg, FALSE, 0);
  785. }
  786. /* --------------------------------------------------------------------------------------------- */
  787. gboolean
  788. dlg_focus (WDialog * h)
  789. {
  790. /* cannot focus disabled widget */
  791. if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
  792. {
  793. Widget *current = WIDGET (h->current->data);
  794. if (((current->options & W_DISABLED) == 0)
  795. && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
  796. {
  797. send_message (h, current, MSG_FOCUS, 0, NULL);
  798. return TRUE;
  799. }
  800. }
  801. return FALSE;
  802. }
  803. /* --------------------------------------------------------------------------------------------- */
  804. /** Find the widget with the given callback in the dialog h */
  805. Widget *
  806. find_widget_type (const WDialog * h, widget_cb_fn callback)
  807. {
  808. GList *w;
  809. w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
  810. return (w == NULL) ? NULL : WIDGET (w->data);
  811. }
  812. /* --------------------------------------------------------------------------------------------- */
  813. /** Find the widget with the given id */
  814. Widget *
  815. dlg_find_by_id (const WDialog * h, unsigned long id)
  816. {
  817. GList *w;
  818. w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
  819. return w != NULL ? WIDGET (w->data) : NULL;
  820. }
  821. /* --------------------------------------------------------------------------------------------- */
  822. /** Find the widget with the given id in the dialog h and select it */
  823. void
  824. dlg_select_by_id (const WDialog * h, unsigned long id)
  825. {
  826. Widget *w;
  827. w = dlg_find_by_id (h, id);
  828. if (w != NULL)
  829. dlg_select_widget (w);
  830. }
  831. /* --------------------------------------------------------------------------------------------- */
  832. /**
  833. * Try to select widget in the dialog.
  834. */
  835. void
  836. dlg_select_widget (void *w)
  837. {
  838. Widget *widget = WIDGET (w);
  839. WDialog *h = widget->owner;
  840. do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
  841. }
  842. /* --------------------------------------------------------------------------------------------- */
  843. /**
  844. * Set widget at top of widget list and make it current.
  845. */
  846. void
  847. dlg_set_top_widget (void *w)
  848. {
  849. Widget *widget = WIDGET (w);
  850. WDialog *h = widget->owner;
  851. GList *l;
  852. l = g_list_find (h->widgets, w);
  853. if (l == NULL)
  854. abort (); /* widget is not in dialog, this should not happen */
  855. /* unfocus prevoius widget and focus current one before widget reordering */
  856. if (h->state == DLG_ACTIVE)
  857. do_select_widget (h, l, SELECT_EXACT);
  858. /* widget reordering */
  859. h->widgets = g_list_remove_link (h->widgets, l);
  860. h->widgets = g_list_concat (h->widgets, l);
  861. h->current = l;
  862. }
  863. /* --------------------------------------------------------------------------------------------- */
  864. /** Try to select previous widget in the tab order */
  865. void
  866. dlg_one_up (WDialog * h)
  867. {
  868. if (h->widgets != NULL)
  869. do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
  870. }
  871. /* --------------------------------------------------------------------------------------------- */
  872. /** Try to select next widget in the tab order */
  873. void
  874. dlg_one_down (WDialog * h)
  875. {
  876. if (h->widgets != NULL)
  877. do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
  878. }
  879. /* --------------------------------------------------------------------------------------------- */
  880. void
  881. update_cursor (WDialog * h)
  882. {
  883. GList *p = h->current;
  884. if ((p != NULL) && (h->state == DLG_ACTIVE))
  885. {
  886. Widget *w;
  887. w = WIDGET (p->data);
  888. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  889. send_message (w, NULL, MSG_CURSOR, 0, NULL);
  890. else
  891. do
  892. {
  893. p = dlg_widget_next (h, p);
  894. if (p == h->current)
  895. break;
  896. w = WIDGET (p->data);
  897. if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
  898. if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
  899. break;
  900. }
  901. while (TRUE);
  902. }
  903. }
  904. /* --------------------------------------------------------------------------------------------- */
  905. /**
  906. * Redraw the widgets in reverse order, leaving the current widget
  907. * as the last one
  908. */
  909. void
  910. dlg_redraw (WDialog * h)
  911. {
  912. if (h->state != DLG_ACTIVE)
  913. return;
  914. if (h->winch_pending)
  915. {
  916. h->winch_pending = FALSE;
  917. send_message (h, NULL, MSG_RESIZE, 0, NULL);
  918. }
  919. send_message (h, NULL, MSG_DRAW, 0, NULL);
  920. dlg_broadcast_msg (h, MSG_DRAW);
  921. update_cursor (h);
  922. }
  923. /* --------------------------------------------------------------------------------------------- */
  924. void
  925. dlg_stop (WDialog * h)
  926. {
  927. h->state = DLG_CLOSED;
  928. }
  929. /* --------------------------------------------------------------------------------------------- */
  930. /** Init the process */
  931. void
  932. dlg_init (WDialog * h)
  933. {
  934. if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
  935. h->modal = TRUE;
  936. /* add dialog to the stack */
  937. top_dlg = g_list_prepend (top_dlg, h);
  938. /* Initialize dialog manager and widgets */
  939. if (h->state == DLG_CONSTRUCT)
  940. {
  941. if (!h->modal)
  942. dialog_switch_add (h);
  943. send_message (h, NULL, MSG_INIT, 0, NULL);
  944. dlg_broadcast_msg (h, MSG_INIT);
  945. dlg_read_history (h);
  946. }
  947. h->state = DLG_ACTIVE;
  948. /* first send MSG_DRAW to dialog itself and all widgets... */
  949. dlg_redraw (h);
  950. /* ...then send MSG_FOCUS to select the first widget that can take focus */
  951. while (h->current != NULL && !dlg_focus (h))
  952. h->current = dlg_widget_next (h, h->current);
  953. h->ret_value = 0;
  954. }
  955. /* --------------------------------------------------------------------------------------------- */
  956. void
  957. dlg_process_event (WDialog * h, int key, Gpm_Event * event)
  958. {
  959. if (key == EV_NONE)
  960. {
  961. if (tty_got_interrupt ())
  962. if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
  963. dlg_execute_cmd (h, CK_Cancel);
  964. return;
  965. }
  966. if (key == EV_MOUSE)
  967. h->mouse_status = dlg_mouse_event (h, event);
  968. else
  969. dlg_key_event (h, key);
  970. }
  971. /* --------------------------------------------------------------------------------------------- */
  972. /** Shutdown the dlg_run */
  973. void
  974. dlg_run_done (WDialog * h)
  975. {
  976. top_dlg = g_list_remove (top_dlg, h);
  977. if (h->state == DLG_CLOSED)
  978. {
  979. send_message (h, h->current->data, MSG_END, 0, NULL);
  980. if (!h->modal)
  981. dialog_switch_remove (h);
  982. }
  983. }
  984. /* --------------------------------------------------------------------------------------------- */
  985. /**
  986. * Standard run dialog routine
  987. * We have to keep this routine small so that we can duplicate it's
  988. * behavior on complex routines like the file routines, this way,
  989. * they can call the dlg_process_event without rewriting all the code
  990. */
  991. int
  992. dlg_run (WDialog * h)
  993. {
  994. dlg_init (h);
  995. frontend_dlg_run (h);
  996. dlg_run_done (h);
  997. return h->ret_value;
  998. }
  999. /* --------------------------------------------------------------------------------------------- */
  1000. void
  1001. dlg_destroy (WDialog * h)
  1002. {
  1003. /* if some widgets have history, save all history at one moment here */
  1004. dlg_save_history (h);
  1005. dlg_broadcast_msg (h, MSG_DESTROY);
  1006. g_list_free_full (h->widgets, g_free);
  1007. mc_event_group_del (h->event_group);
  1008. g_free (h->event_group);
  1009. g_free (h->title);
  1010. g_free (h);
  1011. do_refresh ();
  1012. }
  1013. /* --------------------------------------------------------------------------------------------- */
  1014. /**
  1015. * Write history to the ${XDG_CACHE_HOME}/mc/history file
  1016. */
  1017. void
  1018. dlg_save_history (WDialog * h)
  1019. {
  1020. char *profile;
  1021. int i;
  1022. if (num_history_items_recorded == 0) /* this is how to disable */
  1023. return;
  1024. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  1025. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  1026. if (i != -1)
  1027. close (i);
  1028. /* Make sure the history is only readable by the user */
  1029. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  1030. {
  1031. ev_history_load_save_t event_data;
  1032. event_data.cfg = mc_config_init (profile, FALSE);
  1033. event_data.receiver = NULL;
  1034. /* get all histories in dialog */
  1035. mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
  1036. mc_config_save_file (event_data.cfg, NULL);
  1037. mc_config_deinit (event_data.cfg);
  1038. }
  1039. g_free (profile);
  1040. }
  1041. /* --------------------------------------------------------------------------------------------- */
  1042. char *
  1043. dlg_get_title (const WDialog * h, size_t len)
  1044. {
  1045. char *t;
  1046. if (h == NULL)
  1047. abort ();
  1048. if (h->get_title != NULL)
  1049. t = h->get_title (h, len);
  1050. else
  1051. t = g_strdup ("");
  1052. return t;
  1053. }
  1054. /* --------------------------------------------------------------------------------------------- */