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