92b3dd088464b5f8bb9e756b70c49a6d7fc84ef1
[midnight-commander.git] / lib / widget / radio.c
blob92b3dd088464b5f8bb9e756b70c49a6d7fc84ef1
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file radio.c
33 * \brief Source: WRadui widget (radiobuttons)
36 #include <config.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 static cb_ret_t
57 radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
59 WRadio *r = (WRadio *) w;
60 int i;
62 switch (msg)
64 case MSG_HOTKEY:
66 for (i = 0; i < r->count; i++)
68 if (r->texts[i].hotkey != NULL)
70 int c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
72 if (c != parm)
73 continue;
74 r->pos = i;
76 /* Take action */
77 send_message (w, sender, MSG_KEY, ' ', data);
78 return MSG_HANDLED;
82 return MSG_NOT_HANDLED;
84 case MSG_KEY:
85 switch (parm)
87 case ' ':
88 r->sel = r->pos;
89 send_message (w->owner, w, MSG_ACTION, 0, NULL);
90 send_message (w, sender, MSG_FOCUS, ' ', data);
91 return MSG_HANDLED;
93 case KEY_UP:
94 case KEY_LEFT:
95 if (r->pos > 0)
97 r->pos--;
98 return MSG_HANDLED;
100 return MSG_NOT_HANDLED;
102 case KEY_DOWN:
103 case KEY_RIGHT:
104 if (r->count - 1 > r->pos)
106 r->pos++;
107 return MSG_HANDLED;
110 return MSG_NOT_HANDLED;
112 case MSG_CURSOR:
113 send_message (w->owner, w, MSG_ACTION, 0, NULL);
114 send_message (w, sender, MSG_FOCUS, ' ', data);
115 widget_move (r, r->pos, 1);
116 return MSG_HANDLED;
118 case MSG_UNFOCUS:
119 case MSG_FOCUS:
120 case MSG_DRAW:
121 for (i = 0; i < r->count; i++)
123 const gboolean focused = (i == r->pos && msg == MSG_FOCUS);
125 widget_selectcolor (w, focused, FALSE);
126 widget_move (r, i, 0);
127 tty_draw_hline (w->y + i, w->x, ' ', w->cols);
128 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
129 hotkey_draw (w, r->texts[i], focused);
131 return MSG_HANDLED;
133 case MSG_DESTROY:
134 for (i = 0; i < r->count; i++)
135 release_hotkey (r->texts[i]);
136 g_free (r->texts);
137 return MSG_HANDLED;
139 default:
140 return widget_default_callback (w, sender, msg, parm, data);
144 /* --------------------------------------------------------------------------------------------- */
146 static int
147 radio_event (Gpm_Event * event, void *data)
149 Widget *w = WIDGET (data);
151 if (!mouse_global_in_widget (event, w))
152 return MOU_UNHANDLED;
154 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
156 WRadio *r = (WRadio *) data;
157 Gpm_Event local;
159 local = mouse_get_local (event, w);
161 r->pos = local.y - 1;
162 dlg_select_widget (w);
163 if ((event->type & GPM_UP) != 0)
165 radio_callback (w, NULL, MSG_KEY, ' ', NULL);
166 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
170 return MOU_NORMAL;
173 /* --------------------------------------------------------------------------------------------- */
174 /*** public functions ****************************************************************************/
175 /* --------------------------------------------------------------------------------------------- */
177 WRadio *
178 radio_new (int y, int x, int count, const char **texts)
180 WRadio *r;
181 Widget *w;
182 int i, wmax = 0;
184 r = g_new (WRadio, 1);
185 w = WIDGET (r);
187 /* Compute the longest string */
188 r->texts = g_new (hotkey_t, count);
190 for (i = 0; i < count; i++)
192 int width;
194 r->texts[i] = parse_hotkey (texts[i]);
195 width = hotkey_width (r->texts[i]);
196 wmax = max (width, wmax);
199 init_widget (w, y, x, count, 4 + wmax, radio_callback, radio_event);
200 /* 4 is width of "(*) " */
201 r->state = 1;
202 r->pos = 0;
203 r->sel = 0;
204 r->count = count;
205 widget_want_hotkey (w, TRUE);
207 return r;
210 /* --------------------------------------------------------------------------------------------- */