radio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Widget *w = (Widget *) data;
  121. if (!mouse_global_in_widget (event, w))
  122. return MOU_UNHANDLED;
  123. if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
  124. {
  125. WRadio *r = (WRadio *) data;
  126. Gpm_Event local;
  127. local = mouse_get_local (event, w);
  128. r->pos = local.y - 1;
  129. dlg_select_widget (w);
  130. if ((event->type & GPM_UP) != 0)
  131. {
  132. radio_callback (w, WIDGET_KEY, ' ');
  133. w->owner->callback (w->owner, w, DLG_POST_KEY, ' ', NULL);
  134. }
  135. }
  136. return MOU_NORMAL;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. /*** public functions ****************************************************************************/
  140. /* --------------------------------------------------------------------------------------------- */
  141. WRadio *
  142. radio_new (int y, int x, int count, const char **texts)
  143. {
  144. WRadio *r;
  145. int i, wmax = 0;
  146. r = g_new (WRadio, 1);
  147. /* Compute the longest string */
  148. r->texts = g_new (hotkey_t, count);
  149. for (i = 0; i < count; i++)
  150. {
  151. int w;
  152. r->texts[i] = parse_hotkey (texts[i]);
  153. w = hotkey_width (r->texts[i]);
  154. wmax = max (w, wmax);
  155. }
  156. init_widget (&r->widget, 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 (r->widget, TRUE);
  163. return r;
  164. }
  165. /* --------------------------------------------------------------------------------------------- */