uprava merge
[midnight-commander/osp/novacja3.git] / lib / widget / radio.c
blob7656cbf33c347bb515a1f152001d9f2d0a127a34
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_msg_t msg, int parm)
59 WRadio *r = (WRadio *) w;
60 int i;
61 Dlg_head *h = r->widget.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 radio_callback (w, WIDGET_KEY, ' ');
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 radio_callback (w, WIDGET_FOCUS, ' ');
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 radio_callback (w, WIDGET_FOCUS, ' ');
116 widget_move (&r->widget, 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->widget, i, 0);
128 tty_draw_hline (r->widget.y + i, r->widget.x, ' ', r->widget.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_proc (msg, parm);
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, WIDGET_KEY, ' ');
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 int i, wmax = 0;
184 r = g_new (WRadio, 1);
185 /* Compute the longest string */
186 r->texts = g_new (hotkey_t, count);
188 for (i = 0; i < count; i++)
190 int w;
192 r->texts[i] = parse_hotkey (texts[i]);
193 w = hotkey_width (r->texts[i]);
194 wmax = max (w, wmax);
197 init_widget (&r->widget, y, x, count, 4 + wmax, radio_callback, radio_event);
198 /* 4 is width of "(*) " */
199 r->state = 1;
200 r->pos = 0;
201 r->sel = 0;
202 r->count = count;
203 widget_want_hotkey (r->widget, TRUE);
205 return r;
208 /* --------------------------------------------------------------------------------------------- */