(query_dialog): add horizontal line.
[midnight-commander.git] / lib / widget / radio.c
blob548236382dff0010d51fd45cc4a83edb83b8dbf5
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;
61 Dlg_head *h = w->owner;
63 switch (msg)
65 case WIDGET_HOTKEY:
67 for (i = 0; i < r->count; i++)
69 if (r->texts[i].hotkey != NULL)
71 int c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
73 if (c != parm)
74 continue;
75 r->pos = i;
77 /* Take action */
78 send_message (w, sender, WIDGET_KEY, ' ', data);
79 return MSG_HANDLED;
83 return MSG_NOT_HANDLED;
85 case WIDGET_KEY:
86 switch (parm)
88 case ' ':
89 r->sel = r->pos;
90 h->callback (h, w, DLG_ACTION, 0, NULL);
91 send_message (w, sender, WIDGET_FOCUS, ' ', data);
92 return MSG_HANDLED;
94 case KEY_UP:
95 case KEY_LEFT:
96 if (r->pos > 0)
98 r->pos--;
99 return MSG_HANDLED;
101 return MSG_NOT_HANDLED;
103 case KEY_DOWN:
104 case KEY_RIGHT:
105 if (r->count - 1 > r->pos)
107 r->pos++;
108 return MSG_HANDLED;
111 return MSG_NOT_HANDLED;
113 case WIDGET_CURSOR:
114 h->callback (h, w, DLG_ACTION, 0, NULL);
115 send_message (w, sender, WIDGET_FOCUS, ' ', data);
116 widget_move (r, r->pos, 1);
117 return MSG_HANDLED;
119 case WIDGET_UNFOCUS:
120 case WIDGET_FOCUS:
121 case WIDGET_DRAW:
122 for (i = 0; i < r->count; i++)
124 const gboolean focused = (i == r->pos && msg == WIDGET_FOCUS);
126 widget_selectcolor (w, focused, FALSE);
127 widget_move (r, 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], focused);
132 return MSG_HANDLED;
134 case WIDGET_DESTROY:
135 for (i = 0; i < r->count; i++)
136 release_hotkey (r->texts[i]);
137 g_free (r->texts);
138 return MSG_HANDLED;
140 default:
141 return default_widget_callback (sender, msg, parm, data);
145 /* --------------------------------------------------------------------------------------------- */
147 static int
148 radio_event (Gpm_Event * event, void *data)
150 Widget *w = WIDGET (data);
152 if (!mouse_global_in_widget (event, w))
153 return MOU_UNHANDLED;
155 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
157 WRadio *r = (WRadio *) data;
158 Gpm_Event local;
160 local = mouse_get_local (event, w);
162 r->pos = local.y - 1;
163 dlg_select_widget (w);
164 if ((event->type & GPM_UP) != 0)
166 radio_callback (w, NULL, WIDGET_KEY, ' ', NULL);
167 w->owner->callback (w->owner, w, DLG_POST_KEY, ' ', NULL);
171 return MOU_NORMAL;
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
178 WRadio *
179 radio_new (int y, int x, int count, const char **texts)
181 WRadio *r;
182 Widget *w;
183 int i, wmax = 0;
185 r = g_new (WRadio, 1);
186 w = WIDGET (r);
188 /* Compute the longest string */
189 r->texts = g_new (hotkey_t, count);
191 for (i = 0; i < count; i++)
193 int width;
195 r->texts[i] = parse_hotkey (texts[i]);
196 width = hotkey_width (r->texts[i]);
197 wmax = max (width, wmax);
200 init_widget (w, y, x, count, 4 + wmax, radio_callback, radio_event);
201 /* 4 is width of "(*) " */
202 r->state = 1;
203 r->pos = 0;
204 r->sel = 0;
205 r->count = count;
206 widget_want_hotkey (w, TRUE);
208 return r;
211 /* --------------------------------------------------------------------------------------------- */