radio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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. const global_keymap_t *radio_map = NULL;
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. static cb_ret_t
  41. radio_execute_cmd (WRadio * r, long command)
  42. {
  43. cb_ret_t ret = MSG_HANDLED;
  44. Widget *w = WIDGET (r);
  45. switch (command)
  46. {
  47. case CK_Up:
  48. case CK_Top:
  49. if (r->pos == 0)
  50. return MSG_NOT_HANDLED;
  51. if (command == CK_Top)
  52. r->pos = 0;
  53. else
  54. r->pos--;
  55. widget_draw (w);
  56. return MSG_HANDLED;
  57. case CK_Down:
  58. case CK_Bottom:
  59. if (r->pos == r->count - 1)
  60. return MSG_NOT_HANDLED;
  61. if (command == CK_Bottom)
  62. r->pos = r->count - 1;
  63. else
  64. r->pos++;
  65. widget_draw (w);
  66. return MSG_HANDLED;
  67. case CK_Select:
  68. r->sel = r->pos;
  69. widget_set_state (w, WST_FOCUSED, TRUE); /* Also draws the widget */
  70. send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  71. return MSG_HANDLED;
  72. default:
  73. ret = MSG_NOT_HANDLED;
  74. break;
  75. }
  76. return ret;
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. /* Return MSG_HANDLED if we want a redraw */
  80. static cb_ret_t
  81. radio_key (WRadio * r, int key)
  82. {
  83. long command;
  84. command = widget_lookup_key (WIDGET (r), key);
  85. if (command == CK_IgnoreKey)
  86. return MSG_NOT_HANDLED;
  87. return radio_execute_cmd (r, command);
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. static cb_ret_t
  91. radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  92. {
  93. WRadio *r = RADIO (w);
  94. int i;
  95. switch (msg)
  96. {
  97. case MSG_HOTKEY:
  98. for (i = 0; i < r->count; i++)
  99. {
  100. if (r->texts[i].hotkey != NULL)
  101. {
  102. int c;
  103. c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
  104. if (c != parm)
  105. continue;
  106. r->pos = i;
  107. /* Take action */
  108. send_message (w, sender, MSG_ACTION, CK_Select, data);
  109. return MSG_HANDLED;
  110. }
  111. }
  112. return MSG_NOT_HANDLED;
  113. case MSG_KEY:
  114. return radio_key (r, parm);
  115. case MSG_ACTION:
  116. return radio_execute_cmd (r, parm);
  117. case MSG_CURSOR:
  118. widget_gotoyx (r, r->pos, 1);
  119. return MSG_HANDLED;
  120. case MSG_DRAW:
  121. {
  122. gboolean focused;
  123. focused = widget_get_state (w, WST_FOCUSED);
  124. for (i = 0; i < r->count; i++)
  125. {
  126. widget_selectcolor (w, i == r->pos && focused, FALSE);
  127. widget_gotoyx (w, i, 0);
  128. tty_draw_hline (w->y + i, w->x, ' ', w->cols);
  129. tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
  130. hotkey_draw (w, r->texts[i], i == r->pos && focused);
  131. }
  132. return MSG_HANDLED;
  133. }
  134. case MSG_DESTROY:
  135. for (i = 0; i < r->count; i++)
  136. hotkey_free (r->texts[i]);
  137. g_free (r->texts);
  138. return MSG_HANDLED;
  139. default:
  140. return widget_default_callback (w, sender, msg, parm, data);
  141. }
  142. }
  143. /* --------------------------------------------------------------------------------------------- */
  144. static void
  145. radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  146. {
  147. switch (msg)
  148. {
  149. case MSG_MOUSE_DOWN:
  150. RADIO (w)->pos = event->y;
  151. widget_select (w);
  152. break;
  153. case MSG_MOUSE_CLICK:
  154. RADIO (w)->pos = event->y;
  155. send_message (w, NULL, MSG_ACTION, CK_Select, NULL);
  156. send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. /* --------------------------------------------------------------------------------------------- */
  163. /*** public functions ****************************************************************************/
  164. /* --------------------------------------------------------------------------------------------- */
  165. WRadio *
  166. radio_new (int y, int x, int count, const char **texts)
  167. {
  168. WRadio *r;
  169. Widget *w;
  170. int i, wmax = 0;
  171. r = g_new (WRadio, 1);
  172. w = WIDGET (r);
  173. /* Compute the longest string */
  174. r->texts = g_new (hotkey_t, count);
  175. for (i = 0; i < count; i++)
  176. {
  177. int width;
  178. r->texts[i] = hotkey_new (texts[i]);
  179. width = hotkey_width (r->texts[i]);
  180. wmax = MAX (width, wmax);
  181. }
  182. /* 4 is width of "(*) " */
  183. widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
  184. w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
  185. w->keymap = radio_map;
  186. r->pos = 0;
  187. r->sel = 0;
  188. r->count = count;
  189. return r;
  190. }
  191. /* --------------------------------------------------------------------------------------------- */