button.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2024
  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-2022
  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_close (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. WRect r = { y, x, 1, 1 };
  169. WButton *b;
  170. Widget *w;
  171. b = g_new (WButton, 1);
  172. w = WIDGET (b);
  173. b->action = action;
  174. b->flags = flags;
  175. b->text = hotkey_new (text);
  176. r.cols = button_get_len (b);
  177. widget_init (w, &r, button_default_callback, button_mouse_default_callback);
  178. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  179. b->callback = callback;
  180. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  181. return b;
  182. }
  183. /* --------------------------------------------------------------------------------------------- */
  184. char *
  185. button_get_text (const WButton *b)
  186. {
  187. return hotkey_get_text (b->text);
  188. }
  189. /* --------------------------------------------------------------------------------------------- */
  190. void
  191. button_set_text (WButton *b, const char *text)
  192. {
  193. Widget *w = WIDGET (b);
  194. hotkey_t hk;
  195. hk = hotkey_new (text);
  196. if (hotkey_equal (b->text, hk))
  197. {
  198. hotkey_free (hk);
  199. return;
  200. }
  201. hotkey_free (b->text);
  202. b->text = hk;
  203. b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
  204. w->rect.cols = button_get_len (b);
  205. widget_draw (w);
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. int
  209. button_get_len (const WButton *b)
  210. {
  211. int ret = hotkey_width (b->text);
  212. switch (b->flags)
  213. {
  214. case DEFPUSH_BUTTON:
  215. ret += 6;
  216. break;
  217. case NORMAL_BUTTON:
  218. ret += 4;
  219. break;
  220. case NARROW_BUTTON:
  221. ret += 2;
  222. break;
  223. case HIDDEN_BUTTON:
  224. default:
  225. return 0;
  226. }
  227. return ret;
  228. }
  229. /* --------------------------------------------------------------------------------------------- */