check.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 check.c
  26. * \brief Source: WCheck widget (checkbutton)
  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/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. check_callback (Widget * w, widget_msg_t msg, int parm)
  41. {
  42. WCheck *c = (WCheck *) w;
  43. Dlg_head *h = c->widget.owner;
  44. switch (msg)
  45. {
  46. case WIDGET_HOTKEY:
  47. if (c->text.hotkey != NULL)
  48. {
  49. if (g_ascii_tolower ((gchar) c->text.hotkey[0]) == parm)
  50. {
  51. check_callback (w, WIDGET_KEY, ' '); /* make action */
  52. return MSG_HANDLED;
  53. }
  54. }
  55. return MSG_NOT_HANDLED;
  56. case WIDGET_KEY:
  57. if (parm != ' ')
  58. return MSG_NOT_HANDLED;
  59. c->state ^= C_BOOL;
  60. c->state ^= C_CHANGE;
  61. h->callback (h, w, DLG_ACTION, 0, NULL);
  62. check_callback (w, WIDGET_FOCUS, ' ');
  63. return MSG_HANDLED;
  64. case WIDGET_CURSOR:
  65. widget_move (&c->widget, 0, 1);
  66. return MSG_HANDLED;
  67. case WIDGET_FOCUS:
  68. case WIDGET_UNFOCUS:
  69. case WIDGET_DRAW:
  70. widget_selectcolor (w, msg == WIDGET_FOCUS, FALSE);
  71. widget_move (&c->widget, 0, 0);
  72. tty_print_string ((c->state & C_BOOL) ? "[x] " : "[ ] ");
  73. hotkey_draw (w, c->text, msg == WIDGET_FOCUS);
  74. return MSG_HANDLED;
  75. case WIDGET_DESTROY:
  76. release_hotkey (c->text);
  77. return MSG_HANDLED;
  78. default:
  79. return default_proc (msg, parm);
  80. }
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. static int
  84. check_event (Gpm_Event * event, void *data)
  85. {
  86. Widget *w = (Widget *) data;
  87. if (!mouse_global_in_widget (event, w))
  88. return MOU_UNHANDLED;
  89. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  90. {
  91. dlg_select_widget (w);
  92. if ((event->type & GPM_UP) != 0)
  93. {
  94. check_callback (w, WIDGET_KEY, ' ');
  95. check_callback (w, WIDGET_FOCUS, 0);
  96. w->owner->callback (w->owner, w, DLG_POST_KEY, ' ', NULL);
  97. }
  98. }
  99. return MOU_NORMAL;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. /*** public functions ****************************************************************************/
  103. /* --------------------------------------------------------------------------------------------- */
  104. WCheck *
  105. check_new (int y, int x, int state, const char *text)
  106. {
  107. WCheck *c;
  108. c = g_new (WCheck, 1);
  109. c->text = parse_hotkey (text);
  110. init_widget (&c->widget, y, x, 1, 4 + hotkey_width (c->text), check_callback, check_event);
  111. /* 4 is width of "[X] " */
  112. c->state = state ? C_BOOL : 0;
  113. widget_want_hotkey (c->widget, TRUE);
  114. return c;
  115. }