radio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 radio.c
  25. * \brief Source: WRadui widget (radiobuttons)
  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. radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  39. {
  40. WRadio *r = RADIO (w);
  41. int i;
  42. switch (msg)
  43. {
  44. case MSG_HOTKEY:
  45. for (i = 0; i < r->count; i++)
  46. {
  47. if (r->texts[i].hotkey != NULL)
  48. {
  49. int c;
  50. c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
  51. if (c != parm)
  52. continue;
  53. r->pos = i;
  54. /* Take action */
  55. send_message (w, sender, MSG_KEY, ' ', data);
  56. return MSG_HANDLED;
  57. }
  58. }
  59. return MSG_NOT_HANDLED;
  60. case MSG_KEY:
  61. switch (parm)
  62. {
  63. case ' ':
  64. r->sel = r->pos;
  65. widget_set_state (w, WST_FOCUSED, TRUE); /* Also draws the widget. */
  66. send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  67. return MSG_HANDLED;
  68. case KEY_UP:
  69. case KEY_LEFT:
  70. if (r->pos > 0)
  71. {
  72. r->pos--;
  73. widget_redraw (w);
  74. return MSG_HANDLED;
  75. }
  76. return MSG_NOT_HANDLED;
  77. case KEY_DOWN:
  78. case KEY_RIGHT:
  79. if (r->count - 1 > r->pos)
  80. {
  81. r->pos++;
  82. widget_redraw (w);
  83. return MSG_HANDLED;
  84. }
  85. return MSG_NOT_HANDLED;
  86. default:
  87. return MSG_NOT_HANDLED;
  88. }
  89. case MSG_CURSOR:
  90. widget_move (r, r->pos, 1);
  91. return MSG_HANDLED;
  92. case MSG_DRAW:
  93. {
  94. gboolean focused;
  95. focused = widget_get_state (w, WST_FOCUSED);
  96. for (i = 0; i < r->count; i++)
  97. {
  98. widget_selectcolor (w, i == r->pos && focused, FALSE);
  99. widget_move (w, i, 0);
  100. tty_draw_hline (w->y + i, w->x, ' ', w->cols);
  101. tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
  102. hotkey_draw (w, r->texts[i], i == r->pos && focused);
  103. }
  104. return MSG_HANDLED;
  105. }
  106. case MSG_DESTROY:
  107. for (i = 0; i < r->count; i++)
  108. release_hotkey (r->texts[i]);
  109. g_free (r->texts);
  110. return MSG_HANDLED;
  111. default:
  112. return widget_default_callback (w, sender, msg, parm, data);
  113. }
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. static void
  117. radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  118. {
  119. switch (msg)
  120. {
  121. case MSG_MOUSE_DOWN:
  122. RADIO (w)->pos = event->y;
  123. widget_select (w);
  124. break;
  125. case MSG_MOUSE_CLICK:
  126. RADIO (w)->pos = event->y;
  127. send_message (w, NULL, MSG_KEY, ' ', NULL);
  128. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. /*** public functions ****************************************************************************/
  136. /* --------------------------------------------------------------------------------------------- */
  137. WRadio *
  138. radio_new (int y, int x, int count, const char **texts)
  139. {
  140. WRadio *r;
  141. Widget *w;
  142. int i, wmax = 0;
  143. r = g_new (WRadio, 1);
  144. w = WIDGET (r);
  145. /* Compute the longest string */
  146. r->texts = g_new (hotkey_t, count);
  147. for (i = 0; i < count; i++)
  148. {
  149. int width;
  150. r->texts[i] = parse_hotkey (texts[i]);
  151. width = hotkey_width (r->texts[i]);
  152. wmax = MAX (width, wmax);
  153. }
  154. /* 4 is width of "(*) " */
  155. widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
  156. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  157. r->pos = 0;
  158. r->sel = 0;
  159. r->count = count;
  160. return r;
  161. }
  162. /* --------------------------------------------------------------------------------------------- */