src/filemanager/filegui.c: fix coding style.
[midnight-commander.git] / lib / widget / radio.c
blob0230231169670fdacddbff8a80bd48d18373613c
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2019
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013, 2016
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file radio.c
32 * \brief Source: WRadui widget (radiobuttons)
35 #include <config.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
54 static cb_ret_t
55 radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
57 WRadio *r = RADIO (w);
58 int i;
60 switch (msg)
62 case MSG_HOTKEY:
63 for (i = 0; i < r->count; i++)
65 if (r->texts[i].hotkey != NULL)
67 int c;
69 c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
70 if (c != parm)
71 continue;
72 r->pos = i;
74 /* Take action */
75 send_message (w, sender, MSG_KEY, ' ', data);
76 return MSG_HANDLED;
79 return MSG_NOT_HANDLED;
81 case MSG_KEY:
82 switch (parm)
84 case ' ':
85 r->sel = r->pos;
86 widget_set_state (w, WST_FOCUSED, TRUE); /* Also draws the widget. */
87 send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
88 return MSG_HANDLED;
90 case KEY_UP:
91 case KEY_LEFT:
92 if (r->pos > 0)
94 r->pos--;
95 widget_redraw (w);
96 return MSG_HANDLED;
98 return MSG_NOT_HANDLED;
100 case KEY_DOWN:
101 case KEY_RIGHT:
102 if (r->count - 1 > r->pos)
104 r->pos++;
105 widget_redraw (w);
106 return MSG_HANDLED;
108 return MSG_NOT_HANDLED;
110 default:
111 return MSG_NOT_HANDLED;
114 case MSG_CURSOR:
115 widget_move (r, r->pos, 1);
116 return MSG_HANDLED;
118 case MSG_DRAW:
120 gboolean focused;
122 focused = widget_get_state (w, WST_FOCUSED);
124 for (i = 0; i < r->count; i++)
126 widget_selectcolor (w, i == r->pos && focused, FALSE);
127 widget_move (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);
133 return MSG_HANDLED;
136 case MSG_DESTROY:
137 for (i = 0; i < r->count; i++)
138 release_hotkey (r->texts[i]);
139 g_free (r->texts);
140 return MSG_HANDLED;
142 default:
143 return widget_default_callback (w, sender, msg, parm, data);
147 /* --------------------------------------------------------------------------------------------- */
149 static void
150 radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
152 switch (msg)
154 case MSG_MOUSE_DOWN:
155 RADIO (w)->pos = event->y;
156 widget_select (w);
157 break;
159 case MSG_MOUSE_CLICK:
160 RADIO (w)->pos = event->y;
161 send_message (w, NULL, MSG_KEY, ' ', NULL);
162 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
163 break;
165 default:
166 break;
170 /* --------------------------------------------------------------------------------------------- */
171 /*** public functions ****************************************************************************/
172 /* --------------------------------------------------------------------------------------------- */
174 WRadio *
175 radio_new (int y, int x, int count, const char **texts)
177 WRadio *r;
178 Widget *w;
179 int i, wmax = 0;
181 r = g_new (WRadio, 1);
182 w = WIDGET (r);
184 /* Compute the longest string */
185 r->texts = g_new (hotkey_t, count);
187 for (i = 0; i < count; i++)
189 int width;
191 r->texts[i] = parse_hotkey (texts[i]);
192 width = hotkey_width (r->texts[i]);
193 wmax = MAX (width, wmax);
196 /* 4 is width of "(*) " */
197 widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
198 w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
199 r->pos = 0;
200 r->sel = 0;
201 r->count = count;
203 return r;
206 /* --------------------------------------------------------------------------------------------- */