radio.c 6.7 KB

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