widget-common.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012, 2013
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012, 2013
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file widget-common.c
  26. * \brief Source: shared stuff of widgets
  27. */
  28. #include <config.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "lib/global.h"
  32. #include "lib/tty/tty.h"
  33. #include "lib/tty/color.h"
  34. #include "lib/skin.h"
  35. #include "lib/strutil.h"
  36. #include "lib/widget.h"
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /*** public functions ****************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. struct hotkey_t
  46. parse_hotkey (const char *text)
  47. {
  48. hotkey_t result;
  49. const char *cp, *p;
  50. if (text == NULL)
  51. text = "";
  52. /* search for '&', that is not on the of text */
  53. cp = strchr (text, '&');
  54. if (cp != NULL && cp[1] != '\0')
  55. {
  56. result.start = g_strndup (text, cp - text);
  57. /* skip '&' */
  58. cp++;
  59. p = str_cget_next_char (cp);
  60. result.hotkey = g_strndup (cp, p - cp);
  61. cp = p;
  62. result.end = g_strdup (cp);
  63. }
  64. else
  65. {
  66. result.start = g_strdup (text);
  67. result.hotkey = NULL;
  68. result.end = NULL;
  69. }
  70. return result;
  71. }
  72. /* --------------------------------------------------------------------------------------------- */
  73. void
  74. release_hotkey (const hotkey_t hotkey)
  75. {
  76. g_free (hotkey.start);
  77. g_free (hotkey.hotkey);
  78. g_free (hotkey.end);
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. int
  82. hotkey_width (const hotkey_t hotkey)
  83. {
  84. int result;
  85. result = str_term_width1 (hotkey.start);
  86. result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
  87. result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
  88. return result;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. void
  92. hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
  93. {
  94. widget_selectcolor (w, focused, FALSE);
  95. tty_print_string (hotkey.start);
  96. if (hotkey.hotkey != NULL)
  97. {
  98. widget_selectcolor (w, focused, TRUE);
  99. tty_print_string (hotkey.hotkey);
  100. widget_selectcolor (w, focused, FALSE);
  101. }
  102. if (hotkey.end != NULL)
  103. tty_print_string (hotkey.end);
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. void
  107. widget_init (Widget * w, int y, int x, int lines, int cols,
  108. widget_cb_fn callback, mouse_h mouse_handler)
  109. {
  110. w->x = x;
  111. w->y = y;
  112. w->cols = cols;
  113. w->lines = lines;
  114. w->pos_flags = WPOS_KEEP_DEFAULT;
  115. w->callback = callback;
  116. w->mouse = mouse_handler;
  117. w->set_options = widget_default_set_options_callback;
  118. w->owner = NULL;
  119. /* Almost all widgets want to put the cursor in a suitable place */
  120. w->options = W_WANT_CURSOR;
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. /* Default callback for widgets */
  124. cb_ret_t
  125. widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  126. {
  127. (void) w;
  128. (void) sender;
  129. (void) parm;
  130. (void) data;
  131. switch (msg)
  132. {
  133. case MSG_INIT:
  134. case MSG_FOCUS:
  135. case MSG_UNFOCUS:
  136. case MSG_DRAW:
  137. case MSG_DESTROY:
  138. case MSG_CURSOR:
  139. case MSG_IDLE:
  140. return MSG_HANDLED;
  141. default:
  142. return MSG_NOT_HANDLED;
  143. }
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. /**
  147. * Callback for applying new options to widget.
  148. *
  149. * @param w widget
  150. * @param options options set
  151. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  152. */
  153. void
  154. widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable)
  155. {
  156. if (enable)
  157. w->options |= options;
  158. else
  159. w->options &= ~options;
  160. if (w->owner != NULL && (options & W_DISABLED) != 0)
  161. send_message (w, NULL, MSG_DRAW, 0, NULL);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. /**
  165. * Apply new options to widget.
  166. *
  167. * @param w widget
  168. * @param options options set
  169. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  170. */
  171. void
  172. widget_set_options (Widget * w, widget_options_t options, gboolean enable)
  173. {
  174. w->set_options (w, options, enable);
  175. }
  176. /* --------------------------------------------------------------------------------------------- */
  177. void
  178. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  179. {
  180. widget->x = x;
  181. widget->y = y;
  182. widget->cols = cols;
  183. widget->lines = lines;
  184. send_message (widget, NULL, MSG_RESIZE, 0, NULL);
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. void
  188. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  189. {
  190. WDialog *h = w->owner;
  191. int color;
  192. if ((w->options & W_DISABLED) != 0)
  193. color = DISABLED_COLOR;
  194. else if (hotkey)
  195. {
  196. if (focused)
  197. color = h->color[DLG_COLOR_HOT_FOCUS];
  198. else
  199. color = h->color[DLG_COLOR_HOT_NORMAL];
  200. }
  201. else
  202. {
  203. if (focused)
  204. color = h->color[DLG_COLOR_FOCUS];
  205. else
  206. color = h->color[DLG_COLOR_NORMAL];
  207. }
  208. tty_setcolor (color);
  209. }
  210. /* --------------------------------------------------------------------------------------------- */
  211. void
  212. widget_erase (Widget * w)
  213. {
  214. if (w != NULL)
  215. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. /**
  219. * Check whether widget is active or not.
  220. * @param w the widget
  221. *
  222. * @return TRUE if the widget is active, FALSE otherwise
  223. */
  224. gboolean
  225. widget_is_active (const void *w)
  226. {
  227. return (w == WIDGET (w)->owner->current->data);
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. void
  231. widget_redraw (Widget * w)
  232. {
  233. if (w != NULL)
  234. {
  235. WDialog *h = w->owner;
  236. if (h != NULL && h->state == DLG_ACTIVE)
  237. w->callback (w, NULL, MSG_DRAW, 0, NULL);
  238. }
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. /**
  242. * Replace widget in the dialog.
  243. *
  244. * @param old_w old widget that need to be replaced
  245. * @param new_w new widget that will replace @old_w
  246. */
  247. void
  248. widget_replace (Widget * old_w, Widget * new_w)
  249. {
  250. WDialog *h = old_w->owner;
  251. gboolean should_focus = FALSE;
  252. if (h->widgets == NULL)
  253. return;
  254. if (h->current == NULL)
  255. h->current = h->widgets;
  256. if (old_w == h->current->data)
  257. should_focus = TRUE;
  258. new_w->owner = h;
  259. new_w->id = old_w->id;
  260. if (should_focus)
  261. h->current->data = new_w;
  262. else
  263. g_list_find (h->widgets, old_w)->data = new_w;
  264. send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
  265. send_message (new_w, NULL, MSG_INIT, 0, NULL);
  266. if (should_focus)
  267. dlg_select_widget (new_w);
  268. widget_redraw (new_w);
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. /**
  272. * Check whether two widgets are overlapped or not.
  273. * @param a 1st widget
  274. * @param b 2nd widget
  275. *
  276. * @return TRUE if widgets are overlapped, FALSE otherwise.
  277. */
  278. gboolean
  279. widget_overlapped (const Widget * a, const Widget * b)
  280. {
  281. return !((b->x >= a->x + a->cols)
  282. || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
  283. }
  284. /* --------------------------------------------------------------------------------------------- */
  285. /* get mouse pointer location within widget */
  286. Gpm_Event
  287. mouse_get_local (const Gpm_Event * global, const Widget * w)
  288. {
  289. Gpm_Event local;
  290. local.buttons = global->buttons;
  291. local.x = global->x - w->x;
  292. local.y = global->y - w->y;
  293. local.type = global->type;
  294. return local;
  295. }
  296. /* --------------------------------------------------------------------------------------------- */
  297. gboolean
  298. mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
  299. {
  300. return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
  301. && (event->y <= w->y + w->lines);
  302. }
  303. /* --------------------------------------------------------------------------------------------- */