widget-common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2019
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012, 2013
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file widget-common.c
  25. * \brief Source: shared stuff of widgets
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/color.h"
  33. #include "lib/skin.h"
  34. #include "lib/strutil.h"
  35. #include "lib/widget.h"
  36. /*** global variables ****************************************************************************/
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** file scope variables ************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static void
  44. widget_do_focus (Widget * w, gboolean enable)
  45. {
  46. if (w != NULL && widget_get_state (WIDGET (w->owner), WST_FOCUSED))
  47. widget_set_state (w, WST_FOCUSED, enable);
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. /**
  51. * Focus specified widget in it's owner.
  52. *
  53. * @param w widget to be focused.
  54. */
  55. static void
  56. widget_focus (Widget * w)
  57. {
  58. WDialog *h = DIALOG (w->owner);
  59. if (h == NULL)
  60. return;
  61. if (WIDGET (h->current->data) != w)
  62. {
  63. widget_do_focus (WIDGET (h->current->data), FALSE);
  64. /* Test if focus lost was allowed and focus has really been loose */
  65. if (h->current == NULL || !widget_get_state (WIDGET (h->current->data), WST_FOCUSED))
  66. {
  67. widget_do_focus (w, TRUE);
  68. h->current = dlg_find (h, w);
  69. }
  70. }
  71. else if (!widget_get_state (w, WST_FOCUSED))
  72. widget_do_focus (w, TRUE);
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. /**
  76. * Put widget on top or bottom of Z-order.
  77. */
  78. static void
  79. widget_reorder (GList * l, gboolean set_top)
  80. {
  81. WDialog *h = WIDGET (l->data)->owner;
  82. h->widgets = g_list_remove_link (h->widgets, l);
  83. if (set_top)
  84. h->widgets = g_list_concat (h->widgets, l);
  85. else
  86. h->widgets = g_list_concat (l, h->widgets);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static gboolean
  90. hotkey_cmp (const char *s1, const char *s2)
  91. {
  92. gboolean n1, n2;
  93. n1 = s1 != NULL;
  94. n2 = s2 != NULL;
  95. if (n1 != n2)
  96. return FALSE;
  97. if (n1 && n2 && strcmp (s1, s2) != 0)
  98. return FALSE;
  99. return TRUE;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. /*** public functions ****************************************************************************/
  103. /* --------------------------------------------------------------------------------------------- */
  104. struct hotkey_t
  105. hotkey_new (const char *text)
  106. {
  107. hotkey_t result;
  108. const char *cp, *p;
  109. if (text == NULL)
  110. text = "";
  111. /* search for '&', that is not on the of text */
  112. cp = strchr (text, '&');
  113. if (cp != NULL && cp[1] != '\0')
  114. {
  115. result.start = g_strndup (text, cp - text);
  116. /* skip '&' */
  117. cp++;
  118. p = str_cget_next_char (cp);
  119. result.hotkey = g_strndup (cp, p - cp);
  120. cp = p;
  121. result.end = g_strdup (cp);
  122. }
  123. else
  124. {
  125. result.start = g_strdup (text);
  126. result.hotkey = NULL;
  127. result.end = NULL;
  128. }
  129. return result;
  130. }
  131. /* --------------------------------------------------------------------------------------------- */
  132. void
  133. hotkey_free (const hotkey_t hotkey)
  134. {
  135. g_free (hotkey.start);
  136. g_free (hotkey.hotkey);
  137. g_free (hotkey.end);
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. int
  141. hotkey_width (const hotkey_t hotkey)
  142. {
  143. int result;
  144. result = str_term_width1 (hotkey.start);
  145. result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
  146. result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
  147. return result;
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. gboolean
  151. hotkey_equal (const hotkey_t hotkey1, const hotkey_t hotkey2)
  152. {
  153. /* *INDENT-OFF* */
  154. return (strcmp (hotkey1.start, hotkey2.start) == 0) &&
  155. hotkey_cmp (hotkey1.hotkey, hotkey2.hotkey) &&
  156. hotkey_cmp (hotkey1.end, hotkey2.end);
  157. /* *INDENT-ON* */
  158. }
  159. /* --------------------------------------------------------------------------------------------- */
  160. void
  161. hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
  162. {
  163. if (hotkey.start[0] != '\0')
  164. {
  165. widget_selectcolor (w, focused, FALSE);
  166. tty_print_string (hotkey.start);
  167. }
  168. if (hotkey.hotkey != NULL)
  169. {
  170. widget_selectcolor (w, focused, TRUE);
  171. tty_print_string (hotkey.hotkey);
  172. }
  173. if (hotkey.end != NULL)
  174. {
  175. widget_selectcolor (w, focused, FALSE);
  176. tty_print_string (hotkey.end);
  177. }
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. char *
  181. hotkey_get_text (const hotkey_t hotkey)
  182. {
  183. GString *text;
  184. text = g_string_new (hotkey.start);
  185. if (hotkey.hotkey != NULL)
  186. {
  187. g_string_append_c (text, '&');
  188. g_string_append (text, hotkey.hotkey);
  189. }
  190. if (hotkey.end != NULL)
  191. g_string_append (text, hotkey.end);
  192. return g_string_free (text, FALSE);
  193. }
  194. /* --------------------------------------------------------------------------------------------- */
  195. void
  196. widget_init (Widget * w, int y, int x, int lines, int cols,
  197. widget_cb_fn callback, widget_mouse_cb_fn mouse_callback)
  198. {
  199. w->x = x;
  200. w->y = y;
  201. w->cols = cols;
  202. w->lines = lines;
  203. w->pos_flags = WPOS_KEEP_DEFAULT;
  204. w->callback = callback;
  205. w->mouse_callback = mouse_callback;
  206. w->owner = NULL;
  207. w->mouse.forced_capture = FALSE;
  208. w->mouse.capture = FALSE;
  209. w->mouse.last_msg = MSG_MOUSE_NONE;
  210. w->mouse.last_buttons_down = 0;
  211. w->options = WOP_DEFAULT;
  212. w->state = WST_DEFAULT;
  213. }
  214. /* --------------------------------------------------------------------------------------------- */
  215. void
  216. widget_destroy (Widget * w)
  217. {
  218. send_message (w, NULL, MSG_DESTROY, 0, NULL);
  219. g_free (w);
  220. }
  221. /* --------------------------------------------------------------------------------------------- */
  222. /* Default callback for widgets */
  223. cb_ret_t
  224. widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  225. {
  226. (void) w;
  227. (void) sender;
  228. (void) parm;
  229. (void) data;
  230. switch (msg)
  231. {
  232. case MSG_INIT:
  233. case MSG_FOCUS:
  234. case MSG_UNFOCUS:
  235. case MSG_ENABLE:
  236. case MSG_DISABLE:
  237. case MSG_DRAW:
  238. case MSG_DESTROY:
  239. case MSG_CURSOR:
  240. case MSG_IDLE:
  241. return MSG_HANDLED;
  242. default:
  243. return MSG_NOT_HANDLED;
  244. }
  245. }
  246. /* --------------------------------------------------------------------------------------------- */
  247. /**
  248. * Apply new options to widget.
  249. *
  250. * @param w widget
  251. * @param options widget option flags to modify. Several flags per call can be modified.
  252. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  253. */
  254. void
  255. widget_set_options (Widget * w, widget_options_t options, gboolean enable)
  256. {
  257. if (enable)
  258. w->options |= options;
  259. else
  260. w->options &= ~options;
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. /**
  264. * Modify state of widget.
  265. *
  266. * @param w widget
  267. * @param state widget state flag to modify
  268. * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
  269. * Only one flag per call can be modified.
  270. * @return MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
  271. */
  272. cb_ret_t
  273. widget_set_state (Widget * w, widget_state_t state, gboolean enable)
  274. {
  275. gboolean ret = MSG_HANDLED;
  276. if (enable)
  277. w->state |= state;
  278. else
  279. w->state &= ~state;
  280. if (enable)
  281. {
  282. /* exclusive bits */
  283. if ((state & WST_CONSTRUCT) != 0)
  284. w->state &= ~(WST_ACTIVE | WST_SUSPENDED | WST_CLOSED);
  285. else if ((state & WST_ACTIVE) != 0)
  286. w->state &= ~(WST_CONSTRUCT | WST_SUSPENDED | WST_CLOSED);
  287. else if ((state & WST_SUSPENDED) != 0)
  288. w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_CLOSED);
  289. else if ((state & WST_CLOSED) != 0)
  290. w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_SUSPENDED);
  291. }
  292. if (w->owner == NULL)
  293. return MSG_NOT_HANDLED;
  294. switch (state)
  295. {
  296. case WST_DISABLED:
  297. ret = send_message (w, NULL, enable ? MSG_DISABLE : MSG_ENABLE, 0, NULL);
  298. if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
  299. ret = send_message (w, NULL, MSG_DRAW, 0, NULL);
  300. break;
  301. case WST_FOCUSED:
  302. {
  303. widget_msg_t msg;
  304. msg = enable ? MSG_FOCUS : MSG_UNFOCUS;
  305. ret = send_message (w, NULL, msg, 0, NULL);
  306. if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
  307. {
  308. send_message (w, NULL, MSG_DRAW, 0, NULL);
  309. /* Notify owner that focus was moved from one widget to another */
  310. send_message (w->owner, w, MSG_CHANGED_FOCUS, 0, NULL);
  311. }
  312. }
  313. break;
  314. default:
  315. break;
  316. }
  317. return ret;
  318. }
  319. /* --------------------------------------------------------------------------------------------- */
  320. void
  321. widget_adjust_position (widget_pos_flags_t pos_flags, int *y, int *x, int *lines, int *cols)
  322. {
  323. if ((pos_flags & WPOS_FULLSCREEN) != 0)
  324. {
  325. *y = 0;
  326. *x = 0;
  327. *lines = LINES;
  328. *cols = COLS;
  329. }
  330. else
  331. {
  332. if ((pos_flags & WPOS_CENTER_HORZ) != 0)
  333. *x = (COLS - *cols) / 2;
  334. if ((pos_flags & WPOS_CENTER_VERT) != 0)
  335. *y = (LINES - *lines) / 2;
  336. if ((pos_flags & WPOS_TRYUP) != 0)
  337. {
  338. if (*y > 3)
  339. *y -= 2;
  340. else if (*y == 3)
  341. *y = 2;
  342. }
  343. }
  344. }
  345. /* --------------------------------------------------------------------------------------------- */
  346. void
  347. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  348. {
  349. widget->x = x;
  350. widget->y = y;
  351. widget->cols = cols;
  352. widget->lines = lines;
  353. send_message (widget, NULL, MSG_RESIZE, 0, NULL);
  354. if (widget->owner != NULL && widget_get_state (WIDGET (widget->owner), WST_ACTIVE))
  355. send_message (widget, NULL, MSG_DRAW, 0, NULL);
  356. }
  357. /* --------------------------------------------------------------------------------------------- */
  358. void
  359. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  360. {
  361. WDialog *h = w->owner;
  362. int color;
  363. if (widget_get_state (w, WST_DISABLED))
  364. color = DISABLED_COLOR;
  365. else if (hotkey)
  366. {
  367. if (focused)
  368. color = h->color[DLG_COLOR_HOT_FOCUS];
  369. else
  370. color = h->color[DLG_COLOR_HOT_NORMAL];
  371. }
  372. else
  373. {
  374. if (focused)
  375. color = h->color[DLG_COLOR_FOCUS];
  376. else
  377. color = h->color[DLG_COLOR_NORMAL];
  378. }
  379. tty_setcolor (color);
  380. }
  381. /* --------------------------------------------------------------------------------------------- */
  382. void
  383. widget_erase (Widget * w)
  384. {
  385. if (w != NULL)
  386. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  387. }
  388. /* --------------------------------------------------------------------------------------------- */
  389. /**
  390. * Check whether widget is active or not.
  391. * @param w the widget
  392. *
  393. * @return TRUE if the widget is active, FALSE otherwise
  394. */
  395. gboolean
  396. widget_is_active (const void *w)
  397. {
  398. return (w == CONST_WIDGET (w)->owner->current->data);
  399. }
  400. /* --------------------------------------------------------------------------------------------- */
  401. void
  402. widget_draw (Widget * w)
  403. {
  404. if (w != NULL)
  405. {
  406. WDialog *h = w->owner;
  407. if (h != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
  408. w->callback (w, NULL, MSG_DRAW, 0, NULL);
  409. }
  410. }
  411. /* --------------------------------------------------------------------------------------------- */
  412. /**
  413. * Replace widget in the dialog.
  414. *
  415. * @param old_w old widget that need to be replaced
  416. * @param new_w new widget that will replace @old_w
  417. */
  418. void
  419. widget_replace (Widget * old_w, Widget * new_w)
  420. {
  421. WDialog *h = old_w->owner;
  422. gboolean should_focus = FALSE;
  423. GList *holder;
  424. if (h->widgets == NULL)
  425. return;
  426. if (h->current == NULL)
  427. h->current = h->widgets;
  428. /* locate widget position in the list */
  429. if (old_w == h->current->data)
  430. holder = h->current;
  431. else
  432. holder = g_list_find (h->widgets, old_w);
  433. /* if old widget is focused, we should focus the new one... */
  434. if (widget_get_state (old_w, WST_FOCUSED))
  435. should_focus = TRUE;
  436. /* ...but if new widget isn't selectable, we cannot focus it */
  437. if (!widget_get_options (new_w, WOP_SELECTABLE))
  438. should_focus = FALSE;
  439. /* if new widget isn't selectable, select other widget before replace */
  440. if (!should_focus)
  441. {
  442. GList *l;
  443. for (l = dlg_get_widget_next_of (holder);
  444. !widget_get_options (WIDGET (l->data), WOP_SELECTABLE)
  445. && !widget_get_state (WIDGET (l->data), WST_DISABLED); l = dlg_get_widget_next_of (l))
  446. ;
  447. widget_select (WIDGET (l->data));
  448. }
  449. /* replace widget */
  450. new_w->owner = h;
  451. new_w->id = old_w->id;
  452. holder->data = new_w;
  453. send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
  454. send_message (new_w, NULL, MSG_INIT, 0, NULL);
  455. if (should_focus)
  456. widget_select (new_w);
  457. else
  458. widget_draw (new_w);
  459. }
  460. /* --------------------------------------------------------------------------------------------- */
  461. /**
  462. * Select specified widget in it's owner.
  463. *
  464. * Note: this function (and widget_focus(), which it calls) is a no-op
  465. * if the widget is already selected.
  466. *
  467. * @param w widget to be selected
  468. */
  469. void
  470. widget_select (Widget * w)
  471. {
  472. WDialog *h;
  473. if (!widget_get_options (w, WOP_SELECTABLE))
  474. return;
  475. h = w->owner;
  476. if (h != NULL)
  477. {
  478. if (widget_get_options (w, WOP_TOP_SELECT))
  479. {
  480. GList *l;
  481. l = dlg_find (h, w);
  482. widget_reorder (l, TRUE);
  483. }
  484. widget_focus (w);
  485. }
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. /**
  489. * Set widget at bottom of widget list.
  490. */
  491. void
  492. widget_set_bottom (Widget * w)
  493. {
  494. widget_reorder (dlg_find (w->owner, w), FALSE);
  495. }
  496. /* --------------------------------------------------------------------------------------------- */
  497. /**
  498. * Check whether two widgets are overlapped or not.
  499. * @param a 1st widget
  500. * @param b 2nd widget
  501. *
  502. * @return TRUE if widgets are overlapped, FALSE otherwise.
  503. */
  504. gboolean
  505. widget_overlapped (const Widget * a, const Widget * b)
  506. {
  507. return !((b->x >= a->x + a->cols)
  508. || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
  509. }
  510. /* --------------------------------------------------------------------------------------------- */
  511. /* get mouse pointer location within widget */
  512. Gpm_Event
  513. mouse_get_local (const Gpm_Event * global, const Widget * w)
  514. {
  515. Gpm_Event local;
  516. local.buttons = global->buttons;
  517. #ifdef HAVE_LIBGPM
  518. local.clicks = 0;
  519. local.margin = 0;
  520. local.modifiers = 0;
  521. local.vc = 0;
  522. #endif
  523. local.x = global->x - w->x;
  524. local.y = global->y - w->y;
  525. local.type = global->type;
  526. return local;
  527. }
  528. /* --------------------------------------------------------------------------------------------- */
  529. gboolean
  530. mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
  531. {
  532. return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
  533. && (event->y <= w->y + w->lines);
  534. }
  535. /* --------------------------------------------------------------------------------------------- */