radio.c 5.6 KB

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