check.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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_draw (w);
  59. send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  60. return MSG_HANDLED;
  61. case MSG_CURSOR:
  62. widget_gotoyx (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_gotoyx (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. hotkey_free (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 = hotkey_new (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. /* --------------------------------------------------------------------------------------------- */
  117. void
  118. check_set_text (WCheck * check, const char *text)
  119. {
  120. Widget *w = WIDGET (check);
  121. hotkey_t hk;
  122. hk = hotkey_new (text);
  123. if (hotkey_equal (check->text, hk))
  124. {
  125. hotkey_free (hk);
  126. return;
  127. }
  128. hotkey_free (check->text);
  129. check->text = hk;
  130. if (check->text.start[0] == '\0' && check->text.hotkey == NULL && check->text.end == NULL)
  131. w->cols = 3; /* "[ ]" */
  132. else
  133. w->cols = 4 + hotkey_width (check->text); /* "[ ] text" */
  134. widget_draw (w);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */