widget-common.c 10 KB

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