button.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2019
  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, 2016
  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/strutil.h"
  32. #include "lib/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** public functions ****************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. cb_ret_t
  44. button_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  45. {
  46. WButton *b = BUTTON (w);
  47. WDialog *h = w->owner;
  48. int off = 0;
  49. switch (msg)
  50. {
  51. case MSG_HOTKEY:
  52. /*
  53. * Don't let the default button steal Enter from the current
  54. * button. This is a workaround for the flawed event model
  55. * when hotkeys are sent to all widgets before the key is
  56. * handled by the current widget.
  57. */
  58. if (parm == '\n' && WIDGET (h->current->data) == w)
  59. {
  60. send_message (w, sender, MSG_KEY, ' ', data);
  61. return MSG_HANDLED;
  62. }
  63. if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
  64. {
  65. send_message (w, sender, MSG_KEY, ' ', data);
  66. return MSG_HANDLED;
  67. }
  68. if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
  69. {
  70. send_message (w, sender, MSG_KEY, ' ', data);
  71. return MSG_HANDLED;
  72. }
  73. return MSG_NOT_HANDLED;
  74. case MSG_KEY:
  75. if (parm != ' ' && parm != '\n')
  76. return MSG_NOT_HANDLED;
  77. h->ret_value = b->action;
  78. if (b->callback == NULL || b->callback (b, b->action) != 0)
  79. dlg_stop (h);
  80. return MSG_HANDLED;
  81. case MSG_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_gotoyx (w, 0, b->hotpos + off);
  99. return MSG_HANDLED;
  100. case MSG_DRAW:
  101. {
  102. gboolean focused;
  103. focused = widget_get_state (w, WST_FOCUSED);
  104. widget_selectcolor (w, focused, FALSE);
  105. widget_gotoyx (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, focused);
  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. }
  138. case MSG_DESTROY:
  139. hotkey_free (b->text);
  140. return MSG_HANDLED;
  141. default:
  142. return widget_default_callback (w, sender, msg, parm, data);
  143. }
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. void
  147. button_mouse_default_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  148. {
  149. (void) event;
  150. switch (msg)
  151. {
  152. case MSG_MOUSE_DOWN:
  153. widget_select (w);
  154. break;
  155. case MSG_MOUSE_CLICK:
  156. send_message (w, NULL, MSG_KEY, ' ', NULL);
  157. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  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 = hotkey_new (text);
  174. widget_init (w, y, x, 1, button_get_len (b), button_default_callback,
  175. button_mouse_default_callback);
  176. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  177. b->callback = callback;
  178. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  179. return b;
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. char *
  183. button_get_text (const WButton * b)
  184. {
  185. return hotkey_get_text (b->text);
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. void
  189. button_set_text (WButton * b, const char *text)
  190. {
  191. Widget *w = WIDGET (b);
  192. hotkey_t hk;
  193. hk = hotkey_new (text);
  194. if (hotkey_equal (b->text, hk))
  195. {
  196. hotkey_free (hk);
  197. return;
  198. }
  199. hotkey_free (b->text);
  200. b->text = hk;
  201. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  202. w->cols = button_get_len (b);
  203. widget_draw (w);
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. int
  207. button_get_len (const WButton * b)
  208. {
  209. int ret = hotkey_width (b->text);
  210. switch (b->flags)
  211. {
  212. case DEFPUSH_BUTTON:
  213. ret += 6;
  214. break;
  215. case NORMAL_BUTTON:
  216. ret += 4;
  217. break;
  218. case NARROW_BUTTON:
  219. ret += 2;
  220. break;
  221. case HIDDEN_BUTTON:
  222. default:
  223. return 0;
  224. }
  225. return ret;
  226. }
  227. /* --------------------------------------------------------------------------------------------- */