button.c 7.3 KB

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