check.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, 2013
  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, 2013
  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 * sender, widget_msg_t msg, int parm, void *data)
  41. {
  42. WCheck *c = CHECK (w);
  43. switch (msg)
  44. {
  45. case MSG_HOTKEY:
  46. if (c->text.hotkey != NULL)
  47. {
  48. if (g_ascii_tolower ((gchar) c->text.hotkey[0]) == parm)
  49. {
  50. /* make action */
  51. send_message (w, sender, MSG_KEY, ' ', data);
  52. return MSG_HANDLED;
  53. }
  54. }
  55. return MSG_NOT_HANDLED;
  56. case MSG_KEY:
  57. if (parm != ' ')
  58. return MSG_NOT_HANDLED;
  59. c->state ^= C_BOOL;
  60. c->state ^= C_CHANGE;
  61. send_message (WIDGET (w)->owner, w, MSG_ACTION, 0, NULL);
  62. send_message (w, sender, MSG_FOCUS, ' ', data);
  63. return MSG_HANDLED;
  64. case MSG_CURSOR:
  65. widget_move (c, 0, 1);
  66. return MSG_HANDLED;
  67. case MSG_FOCUS:
  68. case MSG_UNFOCUS:
  69. case MSG_DRAW:
  70. widget_selectcolor (w, msg == MSG_FOCUS, FALSE);
  71. widget_move (c, 0, 0);
  72. tty_print_string ((c->state & C_BOOL) ? "[x] " : "[ ] ");
  73. hotkey_draw (w, c->text, msg == MSG_FOCUS);
  74. return MSG_HANDLED;
  75. case MSG_DESTROY:
  76. release_hotkey (c->text);
  77. return MSG_HANDLED;
  78. default:
  79. return widget_default_callback (w, sender, msg, parm, data);
  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. send_message (w, NULL, MSG_KEY, ' ', NULL);
  95. send_message (w, NULL, MSG_FOCUS, 0, NULL);
  96. send_message (w->owner, w, MSG_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. Widget *w;
  109. c = g_new (WCheck, 1);
  110. w = WIDGET (c);
  111. c->text = parse_hotkey (text);
  112. widget_init (w, y, x, 1, 4 + hotkey_width (c->text), check_callback, check_event);
  113. /* 4 is width of "[X] " */
  114. c->state = state ? C_BOOL : 0;
  115. widget_want_hotkey (w, TRUE);
  116. return c;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */