check.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2017
  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 check.c
  25. * \brief Source: WCheck widget (checkbutton)
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h"
  31. #include "lib/widget.h"
  32. /*** global variables ****************************************************************************/
  33. /*** file scope macro definitions ****************************************************************/
  34. /*** file scope type declarations ****************************************************************/
  35. /*** file scope variables ************************************************************************/
  36. /*** file scope functions ************************************************************************/
  37. static cb_ret_t
  38. check_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  39. {
  40. WCheck *c = CHECK (w);
  41. switch (msg)
  42. {
  43. case MSG_HOTKEY:
  44. if (c->text.hotkey != NULL)
  45. {
  46. if (g_ascii_tolower ((gchar) c->text.hotkey[0]) == parm)
  47. {
  48. /* make action */
  49. send_message (w, sender, MSG_KEY, ' ', data);
  50. return MSG_HANDLED;
  51. }
  52. }
  53. return MSG_NOT_HANDLED;
  54. case MSG_KEY:
  55. if (parm != ' ')
  56. return MSG_NOT_HANDLED;
  57. c->state = !c->state;
  58. widget_redraw (w);
  59. send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  60. return MSG_HANDLED;
  61. case MSG_CURSOR:
  62. widget_move (w, 0, 1);
  63. return MSG_HANDLED;
  64. case MSG_DRAW:
  65. {
  66. gboolean focused;
  67. focused = widget_get_state (w, WST_FOCUSED);
  68. widget_selectcolor (w, focused, FALSE);
  69. widget_move (w, 0, 0);
  70. tty_print_string (c->state ? "[x] " : "[ ] ");
  71. hotkey_draw (w, c->text, focused);
  72. return MSG_HANDLED;
  73. }
  74. case MSG_DESTROY:
  75. release_hotkey (c->text);
  76. return MSG_HANDLED;
  77. default:
  78. return widget_default_callback (w, sender, msg, parm, data);
  79. }
  80. }
  81. /* --------------------------------------------------------------------------------------------- */
  82. static void
  83. check_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  84. {
  85. (void) event;
  86. switch (msg)
  87. {
  88. case MSG_MOUSE_DOWN:
  89. widget_select (w);
  90. break;
  91. case MSG_MOUSE_CLICK:
  92. send_message (w, NULL, MSG_KEY, ' ', NULL);
  93. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /*** public functions ****************************************************************************/
  101. /* --------------------------------------------------------------------------------------------- */
  102. WCheck *
  103. check_new (int y, int x, gboolean state, const char *text)
  104. {
  105. WCheck *c;
  106. Widget *w;
  107. c = g_new (WCheck, 1);
  108. w = WIDGET (c);
  109. c->text = parse_hotkey (text);
  110. /* 4 is width of "[X] " */
  111. widget_init (w, y, x, 1, 4 + hotkey_width (c->text), check_callback, check_mouse_callback);
  112. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  113. c->state = state;
  114. return c;
  115. }
  116. /* --------------------------------------------------------------------------------------------- */