widget-common.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Widgets for the Midnight Commander
  2. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
  4. Authors: 1994, 1995 Radek Doulik
  5. 1994, 1995 Miguel de Icaza
  6. 1995 Jakub Jelinek
  7. 1996 Andrej Borsenkow
  8. 1997 Norbert Warmuth
  9. 2009, 2010 Andrew Borodin
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. /** \file widget-common.c
  23. * \brief Source: shared stuff of widgets
  24. */
  25. #include <config.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "lib/global.h"
  29. #include "lib/tty/tty.h"
  30. #include "lib/tty/color.h"
  31. #include "lib/skin.h"
  32. #include "lib/strutil.h"
  33. #include "lib/widget.h"
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** public functions ****************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. struct hotkey_t
  43. parse_hotkey (const char *text)
  44. {
  45. hotkey_t result;
  46. const char *cp, *p;
  47. if (text == NULL)
  48. text = "";
  49. /* search for '&', that is not on the of text */
  50. cp = strchr (text, '&');
  51. if (cp != NULL && cp[1] != '\0')
  52. {
  53. result.start = g_strndup (text, cp - text);
  54. /* skip '&' */
  55. cp++;
  56. p = str_cget_next_char (cp);
  57. result.hotkey = g_strndup (cp, p - cp);
  58. cp = p;
  59. result.end = g_strdup (cp);
  60. }
  61. else
  62. {
  63. result.start = g_strdup (text);
  64. result.hotkey = NULL;
  65. result.end = NULL;
  66. }
  67. return result;
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. void
  71. release_hotkey (const hotkey_t hotkey)
  72. {
  73. g_free (hotkey.start);
  74. g_free (hotkey.hotkey);
  75. g_free (hotkey.end);
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. int
  79. hotkey_width (const hotkey_t hotkey)
  80. {
  81. int result;
  82. result = str_term_width1 (hotkey.start);
  83. result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
  84. result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
  85. return result;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. void
  89. hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
  90. {
  91. widget_selectcolor (w, focused, FALSE);
  92. tty_print_string (hotkey.start);
  93. if (hotkey.hotkey != NULL)
  94. {
  95. widget_selectcolor (w, focused, TRUE);
  96. tty_print_string (hotkey.hotkey);
  97. widget_selectcolor (w, focused, FALSE);
  98. }
  99. if (hotkey.end != NULL)
  100. tty_print_string (hotkey.end);
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. void
  104. init_widget (Widget * w, int y, int x, int lines, int cols,
  105. callback_fn callback, mouse_h mouse_handler)
  106. {
  107. w->x = x;
  108. w->y = y;
  109. w->cols = cols;
  110. w->lines = lines;
  111. w->callback = callback;
  112. w->mouse = mouse_handler;
  113. w->owner = NULL;
  114. /* Almost all widgets want to put the cursor in a suitable place */
  115. w->options = W_WANT_CURSOR;
  116. }
  117. /* --------------------------------------------------------------------------------------------- */
  118. /* Default callback for widgets */
  119. cb_ret_t
  120. default_proc (widget_msg_t msg, int parm)
  121. {
  122. (void) parm;
  123. switch (msg)
  124. {
  125. case WIDGET_INIT:
  126. case WIDGET_FOCUS:
  127. case WIDGET_UNFOCUS:
  128. case WIDGET_DRAW:
  129. case WIDGET_DESTROY:
  130. case WIDGET_CURSOR:
  131. case WIDGET_IDLE:
  132. return MSG_HANDLED;
  133. default:
  134. return MSG_NOT_HANDLED;
  135. }
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. void
  139. widget_set_size (Widget * widget, int y, int x, int lines, int cols)
  140. {
  141. widget->x = x;
  142. widget->y = y;
  143. widget->cols = cols;
  144. widget->lines = lines;
  145. send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. void
  149. widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
  150. {
  151. Dlg_head *h = w->owner;
  152. int color;
  153. if ((w->options & W_DISABLED) != 0)
  154. color = DISABLED_COLOR;
  155. else if (hotkey)
  156. {
  157. if (focused)
  158. color = h->color[DLG_COLOR_HOT_FOCUS];
  159. else
  160. color = h->color[DLG_COLOR_HOT_NORMAL];
  161. }
  162. else
  163. {
  164. if (focused)
  165. color = h->color[DLG_COLOR_FOCUS];
  166. else
  167. color = h->color[DLG_COLOR_NORMAL];
  168. }
  169. tty_setcolor (color);
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. void
  173. widget_erase (Widget * w)
  174. {
  175. if (w != NULL)
  176. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  177. }
  178. /* --------------------------------------------------------------------------------------------- */