radio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 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/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. radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  40. {
  41. WRadio *r = RADIO (w);
  42. int i;
  43. switch (msg)
  44. {
  45. case MSG_HOTKEY:
  46. {
  47. for (i = 0; i < r->count; i++)
  48. {
  49. if (r->texts[i].hotkey != NULL)
  50. {
  51. int c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
  52. if (c != parm)
  53. continue;
  54. r->pos = i;
  55. /* Take action */
  56. send_message (w, sender, MSG_KEY, ' ', data);
  57. return MSG_HANDLED;
  58. }
  59. }
  60. }
  61. return MSG_NOT_HANDLED;
  62. case MSG_KEY:
  63. switch (parm)
  64. {
  65. case ' ':
  66. r->sel = r->pos;
  67. send_message (w->owner, w, MSG_ACTION, 0, NULL);
  68. send_message (w, sender, MSG_FOCUS, ' ', data);
  69. return MSG_HANDLED;
  70. case KEY_UP:
  71. case KEY_LEFT:
  72. if (r->pos > 0)
  73. {
  74. r->pos--;
  75. return MSG_HANDLED;
  76. }
  77. return MSG_NOT_HANDLED;
  78. case KEY_DOWN:
  79. case KEY_RIGHT:
  80. if (r->count - 1 > r->pos)
  81. {
  82. r->pos++;
  83. return MSG_HANDLED;
  84. }
  85. }
  86. return MSG_NOT_HANDLED;
  87. case MSG_CURSOR:
  88. send_message (w->owner, w, MSG_ACTION, 0, NULL);
  89. send_message (w, sender, MSG_FOCUS, ' ', data);
  90. widget_move (r, r->pos, 1);
  91. return MSG_HANDLED;
  92. case MSG_UNFOCUS:
  93. case MSG_FOCUS:
  94. case MSG_DRAW:
  95. for (i = 0; i < r->count; i++)
  96. {
  97. const gboolean focused = (i == r->pos && msg == MSG_FOCUS);
  98. widget_selectcolor (w, focused, FALSE);
  99. widget_move (r, 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], focused);
  103. }
  104. return MSG_HANDLED;
  105. case MSG_DESTROY:
  106. for (i = 0; i < r->count; i++)
  107. release_hotkey (r->texts[i]);
  108. g_free (r->texts);
  109. return MSG_HANDLED;
  110. default:
  111. return widget_default_callback (w, sender, msg, parm, data);
  112. }
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. static int
  116. radio_event (Gpm_Event * event, void *data)
  117. {
  118. Widget *w = WIDGET (data);
  119. if (!mouse_global_in_widget (event, w))
  120. return MOU_UNHANDLED;
  121. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  122. {
  123. WRadio *r = RADIO (data);
  124. Gpm_Event local;
  125. local = mouse_get_local (event, w);
  126. r->pos = local.y - 1;
  127. dlg_select_widget (w);
  128. if ((event->type & GPM_UP) != 0)
  129. {
  130. radio_callback (w, NULL, MSG_KEY, ' ', NULL);
  131. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  132. }
  133. }
  134. return MOU_NORMAL;
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. /*** public functions ****************************************************************************/
  138. /* --------------------------------------------------------------------------------------------- */
  139. WRadio *
  140. radio_new (int y, int x, int count, const char **texts)
  141. {
  142. WRadio *r;
  143. Widget *w;
  144. int i, wmax = 0;
  145. r = g_new (WRadio, 1);
  146. w = WIDGET (r);
  147. /* Compute the longest string */
  148. r->texts = g_new (hotkey_t, count);
  149. for (i = 0; i < count; i++)
  150. {
  151. int width;
  152. r->texts[i] = parse_hotkey (texts[i]);
  153. width = hotkey_width (r->texts[i]);
  154. wmax = max (width, wmax);
  155. }
  156. widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_event);
  157. /* 4 is width of "(*) " */
  158. r->state = 1;
  159. r->pos = 0;
  160. r->sel = 0;
  161. r->count = count;
  162. widget_want_hotkey (w, TRUE);
  163. return r;
  164. }
  165. /* --------------------------------------------------------------------------------------------- */