widget-common.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2014
  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. /*** file scope functions ************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. /*** public functions ****************************************************************************/
  43. /* --------------------------------------------------------------------------------------------- */
  44. struct hotkey_t
  45. parse_hotkey (const char *text)
  46. {
  47. hotkey_t result;
  48. const char *cp, *p;
  49. if (text == NULL)
  50. text = "";
  51. /* search for '&', that is not on the of text */
  52. cp = strchr (text, '&');
  53. if (cp != NULL && cp[1] != '\0')
  54. {
  55. result.start = g_strndup (text, cp - text);
  56. /* skip '&' */
  57. cp++;
  58. p = str_cget_next_char (cp);
  59. result.hotkey = g_strndup (cp, p - cp);
  60. cp = p;
  61. result.end = g_strdup (cp);
  62. }
  63. else
  64. {
  65. result.start = g_strdup (text);
  66. result.hotkey = NULL;
  67. result.end = NULL;
  68. }
  69. return result;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. void
  73. release_hotkey (const hotkey_t hotkey)
  74. {
  75. g_free (hotkey.start);
  76. g_free (hotkey.hotkey);
  77. g_free (hotkey.end);
  78. }
  79. /* --------------------------------------------------------------------------------------------- */
  80. int
  81. hotkey_width (const hotkey_t hotkey)
  82. {
  83. int result;
  84. result = str_term_width1 (hotkey.start);
  85. result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
  86. result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
  87. return result;
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. void
  91. hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
  92. {
  93. widget_selectcolor (w, focused, FALSE);
  94. tty_print_string (hotkey.start);
  95. if (hotkey.hotkey != NULL)
  96. {
  97. widget_selectcolor (w, focused, TRUE);
  98. tty_print_string (hotkey.hotkey);
  99. widget_selectcolor (w, focused, FALSE);
  100. }
  101. if (hotkey.end != NULL)
  102. tty_print_string (hotkey.end);
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. void
  106. widget_init (Widget * w, int y, int x, int lines, int cols,
  107. widget_cb_fn callback, mouse_h mouse_handler)
  108. {
  109. w->x = x;
  110. w->y = y;
  111. w->cols = cols;
  112. w->lines = lines;
  113. w->pos_flags = WPOS_KEEP_DEFAULT;
  114. w->callback = callback;
  115. w->mouse = mouse_handler;
  116. w->set_options = widget_default_set_options_callback;
  117. w->owner = NULL;
  118. /* Almost all widgets want to put the cursor in a suitable place */
  119. w->options = W_WANT_CURSOR;
  120. }
  121. /* --------------------------------------------------------------------------------------------- */
  122. /* Default callback for widgets */
  123. cb_ret_t
  124. widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  125. {
  126. (void) w;
  127. (void) sender;
  128. (void) parm;
  129. (void) data;
  130. switch (msg)
  131. {
  132. case MSG_INIT:
  133. case MSG_FOCUS:
  134. case MSG_UNFOCUS:
  135. case MSG_DRAW:
  136. case MSG_DESTROY:
  137. case MSG_CURSOR:
  138. case MSG_IDLE:
  139. return MSG_HANDLED;
  140. default:
  141. return MSG_NOT_HANDLED;
  142. }
  143. }
  144. /* --------------------------------------------------------------------------------------------- */
  145. /**
  146. * Callback for applying new options to widget.
  147. *
  148. * @param w widget
  149. * @param options options set
  150. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  151. */
  152. void
  153. widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable)
  154. {
  155. if (enable)
  156. w->options |= options;
  157. else
  158. w->options &= ~options;
  159. if (w->owner != NULL && (options & W_DISABLED) != 0)
  160. send_message (w, NULL, MSG_DRAW, 0, NULL);
  161. }
  162. /* --------------------------------------------------------------------------------------------- */
  163. /**
  164. * Apply new options to widget.
  165. *
  166. * @param w widget
  167. * @param options options set
  168. * @param enable TRUE if specified options should be added, FALSE if options should be removed
  169. */
  170. void
  171. widget_set_options (Widget * w, widget_options_t options, gboolean enable)
  172. {
  173. w->set_options (w, options, enable);
  174. }
  175. /* --------------------------------------------------------------------------------------------- */
  176. void
  177. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  178. {
  179. widget->x = x;
  180. widget->y = y;
  181. widget->cols = cols;
  182. widget->lines = lines;
  183. send_message (widget, NULL, MSG_RESIZE, 0, NULL);
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. void
  187. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  188. {
  189. WDialog *h = w->owner;
  190. int color;
  191. if ((w->options & W_DISABLED) != 0)
  192. color = DISABLED_COLOR;
  193. else if (hotkey)
  194. {
  195. if (focused)
  196. color = h->color[DLG_COLOR_HOT_FOCUS];
  197. else
  198. color = h->color[DLG_COLOR_HOT_NORMAL];
  199. }
  200. else
  201. {
  202. if (focused)
  203. color = h->color[DLG_COLOR_FOCUS];
  204. else
  205. color = h->color[DLG_COLOR_NORMAL];
  206. }
  207. tty_setcolor (color);
  208. }
  209. /* --------------------------------------------------------------------------------------------- */
  210. void
  211. widget_erase (Widget * w)
  212. {
  213. if (w != NULL)
  214. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. /**
  218. * Check whether widget is active or not.
  219. * @param w the widget
  220. *
  221. * @return TRUE if the widget is active, FALSE otherwise
  222. */
  223. gboolean
  224. widget_is_active (const void *w)
  225. {
  226. return (w == WIDGET (w)->owner->current->data);
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. void
  230. widget_redraw (Widget * w)
  231. {
  232. if (w != NULL)
  233. {
  234. WDialog *h = w->owner;
  235. if (h != NULL && h->state == DLG_ACTIVE)
  236. w->callback (w, NULL, MSG_DRAW, 0, NULL);
  237. }
  238. }
  239. /* --------------------------------------------------------------------------------------------- */
  240. /**
  241. * Replace widget in the dialog.
  242. *
  243. * @param old_w old widget that need to be replaced
  244. * @param new_w new widget that will replace @old_w
  245. */
  246. void
  247. widget_replace (Widget * old_w, Widget * new_w)
  248. {
  249. WDialog *h = old_w->owner;
  250. gboolean should_focus = FALSE;
  251. if (h->widgets == NULL)
  252. return;
  253. if (h->current == NULL)
  254. h->current = h->widgets;
  255. if (old_w == h->current->data)
  256. should_focus = TRUE;
  257. new_w->owner = h;
  258. new_w->id = old_w->id;
  259. if (should_focus)
  260. h->current->data = new_w;
  261. else
  262. g_list_find (h->widgets, old_w)->data = new_w;
  263. send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
  264. send_message (new_w, NULL, MSG_INIT, 0, NULL);
  265. if (should_focus)
  266. dlg_select_widget (new_w);
  267. widget_redraw (new_w);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. /**
  271. * Check whether two widgets are overlapped or not.
  272. * @param a 1st widget
  273. * @param b 2nd widget
  274. *
  275. * @return TRUE if widgets are overlapped, FALSE otherwise.
  276. */
  277. gboolean
  278. widget_overlapped (const Widget * a, const Widget * b)
  279. {
  280. return !((b->x >= a->x + a->cols)
  281. || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
  282. }
  283. /* --------------------------------------------------------------------------------------------- */
  284. /* get mouse pointer location within widget */
  285. Gpm_Event
  286. mouse_get_local (const Gpm_Event * global, const Widget * w)
  287. {
  288. Gpm_Event local;
  289. local.buttons = global->buttons;
  290. local.x = global->x - w->x;
  291. local.y = global->y - w->y;
  292. local.type = global->type;
  293. return local;
  294. }
  295. /* --------------------------------------------------------------------------------------------- */
  296. gboolean
  297. mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
  298. {
  299. return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
  300. && (event->y <= w->y + w->lines);
  301. }
  302. /* --------------------------------------------------------------------------------------------- */