radio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file radio.c
  26. * \brief Source: WRadui widget (radiobuttons)
  27. */
  28. #include <config.h>
  29. #include <stdlib.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/mouse.h"
  33. #include "lib/widget.h"
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /*** file scope functions ************************************************************************/
  39. static cb_ret_t
  40. radio_callback (Widget * w, widget_msg_t msg, int parm)
  41. {
  42. WRadio *r = (WRadio *) w;
  43. int i;
  44. Dlg_head *h = r->widget.owner;
  45. switch (msg)
  46. {
  47. case WIDGET_HOTKEY:
  48. {
  49. for (i = 0; i < r->count; i++)
  50. {
  51. if (r->texts[i].hotkey != NULL)
  52. {
  53. int c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
  54. if (c != parm)
  55. continue;
  56. r->pos = i;
  57. /* Take action */
  58. radio_callback (w, WIDGET_KEY, ' ');
  59. return MSG_HANDLED;
  60. }
  61. }
  62. }
  63. return MSG_NOT_HANDLED;
  64. case WIDGET_KEY:
  65. switch (parm)
  66. {
  67. case ' ':
  68. r->sel = r->pos;
  69. h->callback (h, w, DLG_ACTION, 0, NULL);
  70. radio_callback (w, WIDGET_FOCUS, ' ');
  71. return MSG_HANDLED;
  72. case KEY_UP:
  73. case KEY_LEFT:
  74. if (r->pos > 0)
  75. {
  76. r->pos--;
  77. return MSG_HANDLED;
  78. }
  79. return MSG_NOT_HANDLED;
  80. case KEY_DOWN:
  81. case KEY_RIGHT:
  82. if (r->count - 1 > r->pos)
  83. {
  84. r->pos++;
  85. return MSG_HANDLED;
  86. }
  87. }
  88. return MSG_NOT_HANDLED;
  89. case WIDGET_CURSOR:
  90. h->callback (h, w, DLG_ACTION, 0, NULL);
  91. radio_callback (w, WIDGET_FOCUS, ' ');
  92. widget_move (&r->widget, r->pos, 1);
  93. return MSG_HANDLED;
  94. case WIDGET_UNFOCUS:
  95. case WIDGET_FOCUS:
  96. case WIDGET_DRAW:
  97. for (i = 0; i < r->count; i++)
  98. {
  99. const gboolean focused = (i == r->pos && msg == WIDGET_FOCUS);
  100. widget_selectcolor (w, focused, FALSE);
  101. widget_move (&r->widget, i, 0);
  102. tty_draw_hline (r->widget.y + i, r->widget.x, ' ', r->widget.cols);
  103. tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
  104. hotkey_draw (w, r->texts[i], focused);
  105. }
  106. return MSG_HANDLED;
  107. case WIDGET_DESTROY:
  108. for (i = 0; i < r->count; i++)
  109. release_hotkey (r->texts[i]);
  110. g_free (r->texts);
  111. return MSG_HANDLED;
  112. default:
  113. return default_proc (msg, parm);
  114. }
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. static int
  118. radio_event (Gpm_Event * event, void *data)
  119. {
  120. WRadio *r = data;
  121. Widget *w = data;
  122. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  123. {
  124. Dlg_head *h = r->widget.owner;
  125. r->pos = event->y - 1;
  126. dlg_select_widget (r);
  127. if ((event->type & GPM_UP) != 0)
  128. {
  129. radio_callback (w, WIDGET_KEY, ' ');
  130. h->callback (h, w, DLG_POST_KEY, ' ', NULL);
  131. return MOU_NORMAL;
  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. int i, wmax = 0;
  144. r = g_new (WRadio, 1);
  145. /* Compute the longest string */
  146. r->texts = g_new (hotkey_t, count);
  147. for (i = 0; i < count; i++)
  148. {
  149. int w;
  150. r->texts[i] = parse_hotkey (texts[i]);
  151. w = hotkey_width (r->texts[i]);
  152. wmax = max (w, wmax);
  153. }
  154. init_widget (&r->widget, y, x, count, 4 + wmax, radio_callback, radio_event);
  155. /* 4 is width of "(*) " */
  156. r->state = 1;
  157. r->pos = 0;
  158. r->sel = 0;
  159. r->count = count;
  160. widget_want_hotkey (r->widget, TRUE);
  161. return r;
  162. }
  163. /* --------------------------------------------------------------------------------------------- */