check.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2014
  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
  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/tty/mouse.h"
  32. #include "lib/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /*** file scope functions ************************************************************************/
  38. static cb_ret_t
  39. check_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  40. {
  41. WCheck *c = CHECK (w);
  42. switch (msg)
  43. {
  44. case MSG_HOTKEY:
  45. if (c->text.hotkey != NULL)
  46. {
  47. if (g_ascii_tolower ((gchar) c->text.hotkey[0]) == parm)
  48. {
  49. /* make action */
  50. send_message (w, sender, MSG_KEY, ' ', data);
  51. return MSG_HANDLED;
  52. }
  53. }
  54. return MSG_NOT_HANDLED;
  55. case MSG_KEY:
  56. if (parm != ' ')
  57. return MSG_NOT_HANDLED;
  58. c->state ^= C_BOOL;
  59. c->state ^= C_CHANGE;
  60. send_message (WIDGET (w)->owner, w, MSG_ACTION, 0, NULL);
  61. send_message (w, sender, MSG_FOCUS, ' ', data);
  62. return MSG_HANDLED;
  63. case MSG_CURSOR:
  64. widget_move (c, 0, 1);
  65. return MSG_HANDLED;
  66. case MSG_FOCUS:
  67. case MSG_UNFOCUS:
  68. case MSG_DRAW:
  69. widget_selectcolor (w, msg == MSG_FOCUS, FALSE);
  70. widget_move (c, 0, 0);
  71. tty_print_string ((c->state & C_BOOL) ? "[x] " : "[ ] ");
  72. hotkey_draw (w, c->text, msg == MSG_FOCUS);
  73. return MSG_HANDLED;
  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 int
  83. check_event (Gpm_Event * event, void *data)
  84. {
  85. Widget *w = WIDGET (data);
  86. if (!mouse_global_in_widget (event, w))
  87. return MOU_UNHANDLED;
  88. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  89. {
  90. dlg_select_widget (w);
  91. if ((event->type & GPM_UP) != 0)
  92. {
  93. send_message (w, NULL, MSG_KEY, ' ', NULL);
  94. send_message (w, NULL, MSG_FOCUS, 0, NULL);
  95. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  96. }
  97. }
  98. return MOU_NORMAL;
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. /*** public functions ****************************************************************************/
  102. /* --------------------------------------------------------------------------------------------- */
  103. WCheck *
  104. check_new (int y, int x, int state, const char *text)
  105. {
  106. WCheck *c;
  107. Widget *w;
  108. c = g_new (WCheck, 1);
  109. w = WIDGET (c);
  110. c->text = parse_hotkey (text);
  111. widget_init (w, y, x, 1, 4 + hotkey_width (c->text), check_callback, check_event);
  112. /* 4 is width of "[X] " */
  113. c->state = state ? C_BOOL : 0;
  114. widget_want_hotkey (w, TRUE);
  115. return c;
  116. }
  117. /* --------------------------------------------------------------------------------------------- */