button.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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. WGroup *g = w->owner;
  48. WDialog *h = DIALOG (g);
  49. int off = 0;
  50. switch (msg)
  51. {
  52. case MSG_HOTKEY:
  53. /*
  54. * Don't let the default button steal Enter from the current
  55. * button. This is a workaround for the flawed event model
  56. * when hotkeys are sent to all widgets before the key is
  57. * handled by the current widget.
  58. */
  59. if (parm == '\n' && WIDGET (g->current->data) == w)
  60. {
  61. send_message (w, sender, MSG_KEY, ' ', data);
  62. return MSG_HANDLED;
  63. }
  64. if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
  65. {
  66. send_message (w, sender, MSG_KEY, ' ', data);
  67. return MSG_HANDLED;
  68. }
  69. if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
  70. {
  71. send_message (w, sender, MSG_KEY, ' ', data);
  72. return MSG_HANDLED;
  73. }
  74. return MSG_NOT_HANDLED;
  75. case MSG_KEY:
  76. if (parm != ' ' && parm != '\n')
  77. return MSG_NOT_HANDLED;
  78. h->ret_value = b->action;
  79. if (b->callback == NULL || b->callback (b, b->action) != 0)
  80. dlg_stop (h);
  81. return MSG_HANDLED;
  82. case MSG_CURSOR:
  83. switch (b->flags)
  84. {
  85. case DEFPUSH_BUTTON:
  86. off = 3;
  87. break;
  88. case NORMAL_BUTTON:
  89. off = 2;
  90. break;
  91. case NARROW_BUTTON:
  92. off = 1;
  93. break;
  94. case HIDDEN_BUTTON:
  95. default:
  96. off = 0;
  97. break;
  98. }
  99. widget_gotoyx (w, 0, b->hotpos + off);
  100. return MSG_HANDLED;
  101. case MSG_DRAW:
  102. {
  103. gboolean focused;
  104. focused = widget_get_state (w, WST_FOCUSED);
  105. widget_selectcolor (w, focused, FALSE);
  106. widget_gotoyx (w, 0, 0);
  107. switch (b->flags)
  108. {
  109. case DEFPUSH_BUTTON:
  110. tty_print_string ("[< ");
  111. break;
  112. case NORMAL_BUTTON:
  113. tty_print_string ("[ ");
  114. break;
  115. case NARROW_BUTTON:
  116. tty_print_string ("[");
  117. break;
  118. case HIDDEN_BUTTON:
  119. default:
  120. return MSG_HANDLED;
  121. }
  122. hotkey_draw (w, b->text, focused);
  123. switch (b->flags)
  124. {
  125. case DEFPUSH_BUTTON:
  126. tty_print_string (" >]");
  127. break;
  128. case NORMAL_BUTTON:
  129. tty_print_string (" ]");
  130. break;
  131. case NARROW_BUTTON:
  132. tty_print_string ("]");
  133. break;
  134. default:
  135. break;
  136. }
  137. return MSG_HANDLED;
  138. }
  139. case MSG_DESTROY:
  140. hotkey_free (b->text);
  141. return MSG_HANDLED;
  142. default:
  143. return widget_default_callback (w, sender, msg, parm, data);
  144. }
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. void
  148. button_mouse_default_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  149. {
  150. (void) event;
  151. switch (msg)
  152. {
  153. case MSG_MOUSE_DOWN:
  154. widget_select (w);
  155. break;
  156. case MSG_MOUSE_CLICK:
  157. send_message (w, NULL, MSG_KEY, ' ', NULL);
  158. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  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 = hotkey_new (text);
  175. widget_init (w, y, x, 1, button_get_len (b), button_default_callback,
  176. button_mouse_default_callback);
  177. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  178. b->callback = callback;
  179. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  180. return b;
  181. }
  182. /* --------------------------------------------------------------------------------------------- */
  183. char *
  184. button_get_text (const WButton * b)
  185. {
  186. return hotkey_get_text (b->text);
  187. }
  188. /* --------------------------------------------------------------------------------------------- */
  189. void
  190. button_set_text (WButton * b, const char *text)
  191. {
  192. Widget *w = WIDGET (b);
  193. hotkey_t hk;
  194. hk = hotkey_new (text);
  195. if (hotkey_equal (b->text, hk))
  196. {
  197. hotkey_free (hk);
  198. return;
  199. }
  200. hotkey_free (b->text);
  201. b->text = hk;
  202. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  203. w->cols = button_get_len (b);
  204. widget_draw (w);
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. int
  208. button_get_len (const WButton * b)
  209. {
  210. int ret = hotkey_width (b->text);
  211. switch (b->flags)
  212. {
  213. case DEFPUSH_BUTTON:
  214. ret += 6;
  215. break;
  216. case NORMAL_BUTTON:
  217. ret += 4;
  218. break;
  219. case NARROW_BUTTON:
  220. ret += 2;
  221. break;
  222. case HIDDEN_BUTTON:
  223. default:
  224. return 0;
  225. }
  226. return ret;
  227. }
  228. /* --------------------------------------------------------------------------------------------- */