Ticket #2598: u7z: Improve handling of missing p7zip binaries.
[midnight-commander.git] / lib / widget / radio.c
blob78dbfd652ad0d186c32ee8887da7b3998a641bae
1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
8 1995 Jakub Jelinek
9 1996 Andrej Borsenkow
10 1997 Norbert Warmuth
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 /** \file radio.c
30 * \brief Source: WRadui widget (radiobuttons)
33 #include <config.h>
35 #include <stdlib.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/mouse.h"
41 #include "lib/widget.h"
43 /*** global variables ****************************************************************************/
45 /*** file scope macro definitions ****************************************************************/
47 /*** file scope type declarations ****************************************************************/
49 /*** file scope variables ************************************************************************/
51 /*** file scope functions ************************************************************************/
53 static cb_ret_t
54 radio_callback (Widget * w, widget_msg_t msg, int parm)
56 WRadio *r = (WRadio *) w;
57 int i;
58 Dlg_head *h = r->widget.owner;
60 switch (msg)
62 case WIDGET_HOTKEY:
64 for (i = 0; i < r->count; i++)
66 if (r->texts[i].hotkey != NULL)
68 int c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
70 if (c != parm)
71 continue;
72 r->pos = i;
74 /* Take action */
75 radio_callback (w, WIDGET_KEY, ' ');
76 return MSG_HANDLED;
80 return MSG_NOT_HANDLED;
82 case WIDGET_KEY:
83 switch (parm)
85 case ' ':
86 r->sel = r->pos;
87 h->callback (h, w, DLG_ACTION, 0, NULL);
88 radio_callback (w, WIDGET_FOCUS, ' ');
89 return MSG_HANDLED;
91 case KEY_UP:
92 case KEY_LEFT:
93 if (r->pos > 0)
95 r->pos--;
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 return MSG_HANDLED;
108 return MSG_NOT_HANDLED;
110 case WIDGET_CURSOR:
111 h->callback (h, w, DLG_ACTION, 0, NULL);
112 radio_callback (w, WIDGET_FOCUS, ' ');
113 widget_move (&r->widget, r->pos, 1);
114 return MSG_HANDLED;
116 case WIDGET_UNFOCUS:
117 case WIDGET_FOCUS:
118 case WIDGET_DRAW:
119 for (i = 0; i < r->count; i++)
121 const gboolean focused = (i == r->pos && msg == WIDGET_FOCUS);
123 widget_selectcolor (w, focused, FALSE);
124 widget_move (&r->widget, i, 0);
125 tty_draw_hline (r->widget.y + i, r->widget.x, ' ', r->widget.cols);
126 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
127 hotkey_draw (w, r->texts[i], focused);
129 return MSG_HANDLED;
131 case WIDGET_DESTROY:
132 for (i = 0; i < r->count; i++)
133 release_hotkey (r->texts[i]);
134 g_free (r->texts);
135 return MSG_HANDLED;
137 default:
138 return default_proc (msg, parm);
142 /* --------------------------------------------------------------------------------------------- */
144 static int
145 radio_event (Gpm_Event * event, void *data)
147 WRadio *r = data;
148 Widget *w = data;
150 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
152 Dlg_head *h = r->widget.owner;
154 r->pos = event->y - 1;
155 dlg_select_widget (r);
156 if ((event->type & GPM_UP) != 0)
158 radio_callback (w, WIDGET_KEY, ' ');
159 h->callback (h, w, DLG_POST_KEY, ' ', NULL);
160 return MOU_NORMAL;
163 return MOU_NORMAL;
166 /* --------------------------------------------------------------------------------------------- */
167 /*** public functions ****************************************************************************/
168 /* --------------------------------------------------------------------------------------------- */
170 WRadio *
171 radio_new (int y, int x, int count, const char **texts)
173 WRadio *r;
174 int i, wmax = 0;
176 r = g_new (WRadio, 1);
177 /* Compute the longest string */
178 r->texts = g_new (hotkey_t, count);
180 for (i = 0; i < count; i++)
182 int w;
184 r->texts[i] = parse_hotkey (texts[i]);
185 w = hotkey_width (r->texts[i]);
186 wmax = max (w, wmax);
189 init_widget (&r->widget, y, x, count, 4 + wmax, radio_callback, radio_event);
190 /* 4 is width of "(*) " */
191 r->state = 1;
192 r->pos = 0;
193 r->sel = 0;
194 r->count = count;
195 widget_want_hotkey (r->widget, TRUE);
197 return r;
200 /* --------------------------------------------------------------------------------------------- */