button.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 button.c
  23. * \brief Source: WButton widget
  24. */
  25. #include <config.h>
  26. #include <stdlib.h>
  27. #include "lib/global.h"
  28. #include "lib/tty/tty.h"
  29. #include "lib/tty/mouse.h"
  30. #include "lib/strutil.h"
  31. #include "lib/widget.h"
  32. /*** global variables ****************************************************************************/
  33. /*** file scope macro definitions ****************************************************************/
  34. /*** file scope type declarations ****************************************************************/
  35. /*** file scope variables ************************************************************************/
  36. /*** file scope functions ************************************************************************/
  37. static cb_ret_t
  38. button_callback (Widget * w, widget_msg_t msg, int parm)
  39. {
  40. WButton *b = (WButton *) w;
  41. int stop = 0;
  42. int off = 0;
  43. Dlg_head *h = b->widget.owner;
  44. switch (msg)
  45. {
  46. case WIDGET_HOTKEY:
  47. /*
  48. * Don't let the default button steal Enter from the current
  49. * button. This is a workaround for the flawed event model
  50. * when hotkeys are sent to all widgets before the key is
  51. * handled by the current widget.
  52. */
  53. if (parm == '\n' && (Widget *) h->current->data == &b->widget)
  54. {
  55. button_callback (w, WIDGET_KEY, ' ');
  56. return MSG_HANDLED;
  57. }
  58. if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
  59. {
  60. button_callback (w, WIDGET_KEY, ' ');
  61. return MSG_HANDLED;
  62. }
  63. if (b->text.hotkey != NULL &&
  64. g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
  65. {
  66. button_callback (w, WIDGET_KEY, ' ');
  67. return MSG_HANDLED;
  68. }
  69. return MSG_NOT_HANDLED;
  70. case WIDGET_KEY:
  71. if (parm != ' ' && parm != '\n')
  72. return MSG_NOT_HANDLED;
  73. if (b->callback != NULL)
  74. stop = b->callback (b, b->action);
  75. if (b->callback == NULL || stop != 0)
  76. {
  77. h->ret_value = b->action;
  78. dlg_stop (h);
  79. }
  80. return MSG_HANDLED;
  81. case WIDGET_CURSOR:
  82. switch (b->flags)
  83. {
  84. case DEFPUSH_BUTTON:
  85. off = 3;
  86. break;
  87. case NORMAL_BUTTON:
  88. off = 2;
  89. break;
  90. case NARROW_BUTTON:
  91. off = 1;
  92. break;
  93. case HIDDEN_BUTTON:
  94. default:
  95. off = 0;
  96. break;
  97. }
  98. widget_move (&b->widget, 0, b->hotpos + off);
  99. return MSG_HANDLED;
  100. case WIDGET_UNFOCUS:
  101. case WIDGET_FOCUS:
  102. case WIDGET_DRAW:
  103. if (msg == WIDGET_UNFOCUS)
  104. b->selected = FALSE;
  105. else if (msg == WIDGET_FOCUS)
  106. b->selected = TRUE;
  107. widget_selectcolor (w, b->selected, FALSE);
  108. widget_move (w, 0, 0);
  109. switch (b->flags)
  110. {
  111. case DEFPUSH_BUTTON:
  112. tty_print_string ("[< ");
  113. break;
  114. case NORMAL_BUTTON:
  115. tty_print_string ("[ ");
  116. break;
  117. case NARROW_BUTTON:
  118. tty_print_string ("[");
  119. break;
  120. case HIDDEN_BUTTON:
  121. default:
  122. return MSG_HANDLED;
  123. }
  124. hotkey_draw (w, b->text, b->selected);
  125. switch (b->flags)
  126. {
  127. case DEFPUSH_BUTTON:
  128. tty_print_string (" >]");
  129. break;
  130. case NORMAL_BUTTON:
  131. tty_print_string (" ]");
  132. break;
  133. case NARROW_BUTTON:
  134. tty_print_string ("]");
  135. break;
  136. default:
  137. break;
  138. }
  139. return MSG_HANDLED;
  140. case WIDGET_DESTROY:
  141. release_hotkey (b->text);
  142. return MSG_HANDLED;
  143. default:
  144. return default_proc (msg, parm);
  145. }
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. static int
  149. button_event (Gpm_Event * event, void *data)
  150. {
  151. WButton *b = data;
  152. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  153. {
  154. Dlg_head *h = b->widget.owner;
  155. dlg_select_widget (b);
  156. if ((event->type & GPM_UP) != 0)
  157. {
  158. button_callback (&b->widget, WIDGET_KEY, ' ');
  159. h->callback (h, &b->widget, DLG_POST_KEY, ' ', NULL);
  160. return MOU_NORMAL;
  161. }
  162. }
  163. return MOU_NORMAL;
  164. }
  165. /* --------------------------------------------------------------------------------------------- */
  166. /*** public functions ****************************************************************************/
  167. /* --------------------------------------------------------------------------------------------- */
  168. WButton *
  169. button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
  170. {
  171. WButton *b;
  172. b = g_new (WButton, 1);
  173. b->action = action;
  174. b->flags = flags;
  175. b->text = parse_hotkey (text);
  176. init_widget (&b->widget, y, x, 1, button_get_len (b), button_callback, button_event);
  177. b->selected = FALSE;
  178. b->callback = callback;
  179. widget_want_hotkey (b->widget, TRUE);
  180. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  181. return b;
  182. }
  183. /* --------------------------------------------------------------------------------------------- */
  184. const char *
  185. button_get_text (const WButton * b)
  186. {
  187. if (b->text.hotkey != NULL)
  188. return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
  189. return g_strdup (b->text.start);
  190. }
  191. /* --------------------------------------------------------------------------------------------- */
  192. void
  193. button_set_text (WButton * b, const char *text)
  194. {
  195. release_hotkey (b->text);
  196. b->text = parse_hotkey (text);
  197. b->widget.cols = button_get_len (b);
  198. dlg_redraw (b->widget.owner);
  199. }
  200. /* --------------------------------------------------------------------------------------------- */
  201. int
  202. button_get_len (const WButton * b)
  203. {
  204. int ret = hotkey_width (b->text);
  205. switch (b->flags)
  206. {
  207. case DEFPUSH_BUTTON:
  208. ret += 6;
  209. break;
  210. case NORMAL_BUTTON:
  211. ret += 4;
  212. break;
  213. case NARROW_BUTTON:
  214. ret += 2;
  215. break;
  216. case HIDDEN_BUTTON:
  217. default:
  218. return 0;
  219. }
  220. return ret;
  221. }
  222. /* --------------------------------------------------------------------------------------------- */