widget-common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2018
  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. /*** public functions ****************************************************************************/
  90. /* --------------------------------------------------------------------------------------------- */
  91. struct hotkey_t
  92. parse_hotkey (const char *text)
  93. {
  94. hotkey_t result;
  95. const char *cp, *p;
  96. if (text == NULL)
  97. text = "";
  98. /* search for '&', that is not on the of text */
  99. cp = strchr (text, '&');
  100. if (cp != NULL && cp[1] != '\0')
  101. {
  102. result.start = g_strndup (text, cp - text);
  103. /* skip '&' */
  104. cp++;
  105. p = str_cget_next_char (cp);
  106. result.hotkey = g_strndup (cp, p - cp);
  107. cp = p;
  108. result.end = g_strdup (cp);
  109. }
  110. else
  111. {
  112. result.start = g_strdup (text);
  113. result.hotkey = NULL;
  114. result.end = NULL;
  115. }
  116. return result;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. void
  120. release_hotkey (const hotkey_t hotkey)
  121. {
  122. g_free (hotkey.start);
  123. g_free (hotkey.hotkey);
  124. g_free (hotkey.end);
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. int
  128. hotkey_width (const hotkey_t hotkey)
  129. {
  130. int result;
  131. result = str_term_width1 (hotkey.start);
  132. result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
  133. result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
  134. return result;
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. void
  138. hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
  139. {
  140. widget_selectcolor (w, focused, FALSE);
  141. tty_print_string (hotkey.start);
  142. if (hotkey.hotkey != NULL)
  143. {
  144. widget_selectcolor (w, focused, TRUE);
  145. tty_print_string (hotkey.hotkey);
  146. widget_selectcolor (w, focused, FALSE);
  147. }
  148. if (hotkey.end != NULL)
  149. tty_print_string (hotkey.end);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. widget_init (Widget * w, int y, int x, int lines, int cols,
  154. widget_cb_fn callback, widget_mouse_cb_fn mouse_callback)
  155. {
  156. w->x = x;
  157. w->y = y;
  158. w->cols = cols;
  159. w->lines = lines;
  160. w->pos_flags = WPOS_KEEP_DEFAULT;
  161. w->callback = callback;
  162. w->mouse_callback = mouse_callback;
  163. w->owner = NULL;
  164. w->mouse.forced_capture = FALSE;
  165. w->mouse.capture = FALSE;
  166. w->mouse.last_msg = MSG_MOUSE_NONE;
  167. w->mouse.last_buttons_down = 0;
  168. w->options = WOP_DEFAULT;
  169. w->state = WST_DEFAULT;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. /* Default callback for widgets */
  173. cb_ret_t
  174. widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  175. {
  176. (void) w;
  177. (void) sender;
  178. (void) parm;
  179. (void) data;
  180. switch (msg)
  181. {
  182. case MSG_INIT:
  183. case MSG_FOCUS:
  184. case MSG_UNFOCUS:
  185. case MSG_ENABLE:
  186. case MSG_DISABLE:
  187. case MSG_DRAW:
  188. case MSG_DESTROY:
  189. case MSG_CURSOR:
  190. case MSG_IDLE:
  191. return MSG_HANDLED;
  192. default:
  193. return MSG_NOT_HANDLED;
  194. }
  195. }
  196. /* --------------------------------------------------------------------------------------------- */
  197. /**
  198. * Apply new options to widget.
  199. *
  200. * @param w widget
  201. * @param options widget option flags to modify. Several flags per call can be modified.
  202. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  203. */
  204. void
  205. widget_set_options (Widget * w, widget_options_t options, gboolean enable)
  206. {
  207. if (enable)
  208. w->options |= options;
  209. else
  210. w->options &= ~options;
  211. }
  212. /* --------------------------------------------------------------------------------------------- */
  213. /**
  214. * Modify state of widget.
  215. *
  216. * @param w widget
  217. * @param state widget state flag to modify
  218. * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
  219. * Only one flag per call can be modified.
  220. * @return MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
  221. */
  222. cb_ret_t
  223. widget_set_state (Widget * w, widget_state_t state, gboolean enable)
  224. {
  225. gboolean ret = MSG_HANDLED;
  226. if (enable)
  227. w->state |= state;
  228. else
  229. w->state &= ~state;
  230. if (enable)
  231. {
  232. /* exclusive bits */
  233. if ((state & WST_CONSTRUCT) != 0)
  234. w->state &= ~(WST_ACTIVE | WST_SUSPENDED | WST_CLOSED);
  235. else if ((state & WST_ACTIVE) != 0)
  236. w->state &= ~(WST_CONSTRUCT | WST_SUSPENDED | WST_CLOSED);
  237. else if ((state & WST_SUSPENDED) != 0)
  238. w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_CLOSED);
  239. else if ((state & WST_CLOSED) != 0)
  240. w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_SUSPENDED);
  241. }
  242. if (w->owner == NULL)
  243. return MSG_NOT_HANDLED;
  244. switch (state)
  245. {
  246. case WST_DISABLED:
  247. ret = send_message (w, NULL, enable ? MSG_DISABLE : MSG_ENABLE, 0, NULL);
  248. if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
  249. ret = send_message (w, NULL, MSG_DRAW, 0, NULL);
  250. break;
  251. case WST_FOCUSED:
  252. {
  253. widget_msg_t msg;
  254. msg = enable ? MSG_FOCUS : MSG_UNFOCUS;
  255. ret = send_message (w, NULL, msg, 0, NULL);
  256. if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
  257. {
  258. send_message (w, NULL, MSG_DRAW, 0, NULL);
  259. /* Notify owner that focus was moved from one widget to another */
  260. send_message (w->owner, w, MSG_CHANGED_FOCUS, 0, NULL);
  261. }
  262. }
  263. break;
  264. default:
  265. break;
  266. }
  267. return ret;
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. void
  271. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  272. {
  273. widget->x = x;
  274. widget->y = y;
  275. widget->cols = cols;
  276. widget->lines = lines;
  277. send_message (widget, NULL, MSG_RESIZE, 0, NULL);
  278. if (widget->owner != NULL && widget_get_state (WIDGET (widget->owner), WST_ACTIVE))
  279. send_message (widget, NULL, MSG_DRAW, 0, NULL);
  280. }
  281. /* --------------------------------------------------------------------------------------------- */
  282. void
  283. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  284. {
  285. WDialog *h = w->owner;
  286. int color;
  287. if (widget_get_state (w, WST_DISABLED))
  288. color = DISABLED_COLOR;
  289. else if (hotkey)
  290. {
  291. if (focused)
  292. color = h->color[DLG_COLOR_HOT_FOCUS];
  293. else
  294. color = h->color[DLG_COLOR_HOT_NORMAL];
  295. }
  296. else
  297. {
  298. if (focused)
  299. color = h->color[DLG_COLOR_FOCUS];
  300. else
  301. color = h->color[DLG_COLOR_NORMAL];
  302. }
  303. tty_setcolor (color);
  304. }
  305. /* --------------------------------------------------------------------------------------------- */
  306. void
  307. widget_erase (Widget * w)
  308. {
  309. if (w != NULL)
  310. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. /**
  314. * Check whether widget is active or not.
  315. * @param w the widget
  316. *
  317. * @return TRUE if the widget is active, FALSE otherwise
  318. */
  319. gboolean
  320. widget_is_active (const void *w)
  321. {
  322. return (w == CONST_WIDGET (w)->owner->current->data);
  323. }
  324. /* --------------------------------------------------------------------------------------------- */
  325. void
  326. widget_redraw (Widget * w)
  327. {
  328. if (w != NULL)
  329. {
  330. WDialog *h = w->owner;
  331. if (h != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
  332. w->callback (w, NULL, MSG_DRAW, 0, NULL);
  333. }
  334. }
  335. /* --------------------------------------------------------------------------------------------- */
  336. /**
  337. * Replace widget in the dialog.
  338. *
  339. * @param old_w old widget that need to be replaced
  340. * @param new_w new widget that will replace @old_w
  341. */
  342. void
  343. widget_replace (Widget * old_w, Widget * new_w)
  344. {
  345. WDialog *h = old_w->owner;
  346. gboolean should_focus = FALSE;
  347. GList *holder;
  348. if (h->widgets == NULL)
  349. return;
  350. if (h->current == NULL)
  351. h->current = h->widgets;
  352. /* locate widget position in the list */
  353. if (old_w == h->current->data)
  354. holder = h->current;
  355. else
  356. holder = g_list_find (h->widgets, old_w);
  357. /* if old widget is focused, we should focus the new one... */
  358. if (widget_get_state (old_w, WST_FOCUSED))
  359. should_focus = TRUE;
  360. /* ...but if new widget isn't selectable, we cannot focus it */
  361. if (!widget_get_options (new_w, WOP_SELECTABLE))
  362. should_focus = FALSE;
  363. /* if new widget isn't selectable, select other widget before replace */
  364. if (!should_focus)
  365. {
  366. GList *l;
  367. for (l = dlg_get_widget_next_of (holder);
  368. !widget_get_options (WIDGET (l->data), WOP_SELECTABLE)
  369. && !widget_get_state (WIDGET (l->data), WST_DISABLED); l = dlg_get_widget_next_of (l))
  370. ;
  371. widget_select (WIDGET (l->data));
  372. }
  373. /* replace widget */
  374. new_w->owner = h;
  375. new_w->id = old_w->id;
  376. holder->data = new_w;
  377. send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
  378. send_message (new_w, NULL, MSG_INIT, 0, NULL);
  379. if (should_focus)
  380. widget_select (new_w);
  381. else
  382. widget_redraw (new_w);
  383. }
  384. /* --------------------------------------------------------------------------------------------- */
  385. /**
  386. * Select specified widget in it's owner.
  387. *
  388. * Note: this function (and widget_focus(), which it calls) is a no-op
  389. * if the widget is already selected.
  390. *
  391. * @param w widget to be selected
  392. */
  393. void
  394. widget_select (Widget * w)
  395. {
  396. WDialog *h;
  397. if (!widget_get_options (w, WOP_SELECTABLE))
  398. return;
  399. h = w->owner;
  400. if (h != NULL)
  401. {
  402. if (widget_get_options (w, WOP_TOP_SELECT))
  403. {
  404. GList *l;
  405. l = dlg_find (h, w);
  406. widget_reorder (l, TRUE);
  407. }
  408. widget_focus (w);
  409. }
  410. }
  411. /* --------------------------------------------------------------------------------------------- */
  412. /**
  413. * Set widget at bottom of widget list.
  414. */
  415. void
  416. widget_set_bottom (Widget * w)
  417. {
  418. widget_reorder (dlg_find (w->owner, w), FALSE);
  419. }
  420. /* --------------------------------------------------------------------------------------------- */
  421. /**
  422. * Check whether two widgets are overlapped or not.
  423. * @param a 1st widget
  424. * @param b 2nd widget
  425. *
  426. * @return TRUE if widgets are overlapped, FALSE otherwise.
  427. */
  428. gboolean
  429. widget_overlapped (const Widget * a, const Widget * b)
  430. {
  431. return !((b->x >= a->x + a->cols)
  432. || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
  433. }
  434. /* --------------------------------------------------------------------------------------------- */
  435. /* get mouse pointer location within widget */
  436. Gpm_Event
  437. mouse_get_local (const Gpm_Event * global, const Widget * w)
  438. {
  439. Gpm_Event local;
  440. local.buttons = global->buttons;
  441. #ifdef HAVE_LIBGPM
  442. local.clicks = 0;
  443. local.margin = 0;
  444. local.modifiers = 0;
  445. local.vc = 0;
  446. #endif
  447. local.x = global->x - w->x;
  448. local.y = global->y - w->y;
  449. local.type = global->type;
  450. return local;
  451. }
  452. /* --------------------------------------------------------------------------------------------- */
  453. gboolean
  454. mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
  455. {
  456. return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
  457. && (event->y <= w->y + w->lines);
  458. }
  459. /* --------------------------------------------------------------------------------------------- */