widget-common.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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
  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
  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. init_widget (Widget * w, int y, int x, int lines, int cols,
  108. callback_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->callback = callback;
  115. w->mouse = mouse_handler;
  116. w->owner = NULL;
  117. /* Almost all widgets want to put the cursor in a suitable place */
  118. w->options = W_WANT_CURSOR;
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. /* Default callback for widgets */
  122. cb_ret_t
  123. default_proc (widget_msg_t msg, int parm)
  124. {
  125. (void) parm;
  126. switch (msg)
  127. {
  128. case WIDGET_INIT:
  129. case WIDGET_FOCUS:
  130. case WIDGET_UNFOCUS:
  131. case WIDGET_DRAW:
  132. case WIDGET_DESTROY:
  133. case WIDGET_CURSOR:
  134. case WIDGET_IDLE:
  135. return MSG_HANDLED;
  136. default:
  137. return MSG_NOT_HANDLED;
  138. }
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. void
  142. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  143. {
  144. widget->x = x;
  145. widget->y = y;
  146. widget->cols = cols;
  147. widget->lines = lines;
  148. send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. void
  152. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  153. {
  154. Dlg_head *h = w->owner;
  155. int color;
  156. if ((w->options & W_DISABLED) != 0)
  157. color = DISABLED_COLOR;
  158. else if (hotkey)
  159. {
  160. if (focused)
  161. color = h->color[DLG_COLOR_HOT_FOCUS];
  162. else
  163. color = h->color[DLG_COLOR_HOT_NORMAL];
  164. }
  165. else
  166. {
  167. if (focused)
  168. color = h->color[DLG_COLOR_FOCUS];
  169. else
  170. color = h->color[DLG_COLOR_NORMAL];
  171. }
  172. tty_setcolor (color);
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. void
  176. widget_erase (Widget * w)
  177. {
  178. if (w != NULL)
  179. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. /* get mouse pointer location within widget */
  183. Gpm_Event
  184. mouse_get_local (const Gpm_Event * global, const Widget * w)
  185. {
  186. Gpm_Event local;
  187. local.buttons = global->buttons;
  188. local.x = global->x - w->x;
  189. local.y = global->y - w->y;
  190. local.type = global->type;
  191. return local;
  192. }
  193. /* --------------------------------------------------------------------------------------------- */
  194. gboolean
  195. mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
  196. {
  197. return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
  198. && (event->y <= w->y + w->lines);
  199. }
  200. /* --------------------------------------------------------------------------------------------- */