Drop old mouse API and use the new one.
[midnight-commander.git] / lib / widget / radio.c
blob7198b7e085cd7fb9b21f3fc2274b78a6a4c31d3f
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
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:
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 send_message (w, sender, MSG_KEY, ' ', data);
76 return MSG_HANDLED;
80 return MSG_NOT_HANDLED;
82 case MSG_KEY:
83 switch (parm)
85 case ' ':
86 r->sel = r->pos;
87 send_message (w, sender, MSG_FOCUS, ' ', data);
88 send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
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;
107 default:
108 return MSG_NOT_HANDLED;
111 case MSG_CURSOR:
112 send_message (w, sender, MSG_FOCUS, ' ', data);
113 widget_move (r, r->pos, 1);
114 send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
115 return MSG_HANDLED;
117 case MSG_UNFOCUS:
118 case MSG_FOCUS:
119 case MSG_DRAW:
120 for (i = 0; i < r->count; i++)
122 const gboolean focused = (i == r->pos && msg == MSG_FOCUS);
124 widget_selectcolor (w, focused, FALSE);
125 widget_move (r, i, 0);
126 tty_draw_hline (w->y + i, w->x, ' ', w->cols);
127 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
128 hotkey_draw (w, r->texts[i], focused);
130 return MSG_HANDLED;
132 case MSG_DESTROY:
133 for (i = 0; i < r->count; i++)
134 release_hotkey (r->texts[i]);
135 g_free (r->texts);
136 return MSG_HANDLED;
138 default:
139 return widget_default_callback (w, sender, msg, parm, data);
143 /* --------------------------------------------------------------------------------------------- */
145 static void
146 radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
148 switch (msg)
150 case MSG_MOUSE_DOWN:
151 RADIO (w)->pos = event->y;
152 dlg_select_widget (w);
153 break;
155 case MSG_MOUSE_CLICK:
156 RADIO (w)->pos = event->y;
157 send_message (w, NULL, MSG_KEY, ' ', NULL);
158 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
159 break;
161 default:
162 break;
166 /* --------------------------------------------------------------------------------------------- */
167 /*** public functions ****************************************************************************/
168 /* --------------------------------------------------------------------------------------------- */
170 WRadio *
171 radio_new (int y, int x, int count, const char **texts)
173 WRadio *r;
174 Widget *w;
175 int i, wmax = 0;
177 r = g_new (WRadio, 1);
178 w = WIDGET (r);
180 /* Compute the longest string */
181 r->texts = g_new (hotkey_t, count);
183 for (i = 0; i < count; i++)
185 int width;
187 r->texts[i] = parse_hotkey (texts[i]);
188 width = hotkey_width (r->texts[i]);
189 wmax = max (width, wmax);
192 /* 4 is width of "(*) " */
193 widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
194 r->state = 1;
195 r->pos = 0;
196 r->sel = 0;
197 r->count = count;
198 widget_want_hotkey (w, TRUE);
200 return r;
203 /* --------------------------------------------------------------------------------------------- */