button.c 7.4 KB

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