2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
13 Andrew Borodin <aborodin@vmail.ru>, 2009-2022
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/>.
32 * \brief Source: WRadui widget (radiobuttons)
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 const global_keymap_t
*radio_map
= NULL
;
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** forward declarations (file scope functions) *************************************************/
54 /*** file scope variables ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
61 radio_execute_cmd (WRadio
* r
, long command
)
63 cb_ret_t ret
= MSG_HANDLED
;
64 Widget
*w
= WIDGET (r
);
71 return MSG_NOT_HANDLED
;
73 if (command
== CK_Top
)
82 if (r
->pos
== r
->count
- 1)
83 return MSG_NOT_HANDLED
;
85 if (command
== CK_Bottom
)
86 r
->pos
= r
->count
- 1;
94 widget_set_state (w
, WST_FOCUSED
, TRUE
); /* Also draws the widget */
95 send_message (w
->owner
, w
, MSG_NOTIFY
, 0, NULL
);
99 ret
= MSG_NOT_HANDLED
;
106 /* --------------------------------------------------------------------------------------------- */
108 /* Return MSG_HANDLED if we want a redraw */
110 radio_key (WRadio
* r
, int key
)
114 command
= widget_lookup_key (WIDGET (r
), key
);
115 if (command
== CK_IgnoreKey
)
116 return MSG_NOT_HANDLED
;
117 return radio_execute_cmd (r
, command
);
120 /* --------------------------------------------------------------------------------------------- */
123 radio_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
125 WRadio
*r
= RADIO (w
);
131 for (i
= 0; i
< r
->count
; i
++)
133 if (r
->texts
[i
].hotkey
!= NULL
)
137 c
= g_ascii_tolower ((gchar
) r
->texts
[i
].hotkey
[0]);
143 send_message (w
, sender
, MSG_ACTION
, CK_Select
, data
);
147 return MSG_NOT_HANDLED
;
150 return radio_key (r
, parm
);
153 return radio_execute_cmd (r
, parm
);
156 widget_gotoyx (r
, r
->pos
, 1);
163 focused
= widget_get_state (w
, WST_FOCUSED
);
165 for (i
= 0; i
< r
->count
; i
++)
167 widget_selectcolor (w
, i
== r
->pos
&& focused
, FALSE
);
168 widget_gotoyx (w
, i
, 0);
169 tty_draw_hline (w
->rect
.y
+ i
, w
->rect
.x
, ' ', w
->rect
.cols
);
170 tty_print_string ((r
->sel
== i
) ? "(*) " : "( ) ");
171 hotkey_draw (w
, r
->texts
[i
], i
== r
->pos
&& focused
);
178 for (i
= 0; i
< r
->count
; i
++)
179 hotkey_free (r
->texts
[i
]);
184 return widget_default_callback (w
, sender
, msg
, parm
, data
);
188 /* --------------------------------------------------------------------------------------------- */
191 radio_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
196 RADIO (w
)->pos
= event
->y
;
200 case MSG_MOUSE_CLICK
:
201 RADIO (w
)->pos
= event
->y
;
202 send_message (w
, NULL
, MSG_ACTION
, CK_Select
, NULL
);
203 send_message (w
->owner
, w
, MSG_POST_KEY
, ' ', NULL
);
211 /* --------------------------------------------------------------------------------------------- */
212 /*** public functions ****************************************************************************/
213 /* --------------------------------------------------------------------------------------------- */
216 radio_new (int y
, int x
, int count
, const char **texts
)
218 WRect r0
= { y
, x
, count
, 1 };
223 r
= g_new (WRadio
, 1);
226 /* Compute the longest string */
227 r
->texts
= g_new (hotkey_t
, count
);
229 for (i
= 0; i
< count
; i
++)
233 r
->texts
[i
] = hotkey_new (texts
[i
]);
234 width
= hotkey_width (r
->texts
[i
]);
235 wmax
= MAX (width
, wmax
);
238 /* 4 is width of "(*) " */
240 widget_init (w
, &r0
, radio_callback
, radio_mouse_callback
);
241 w
->options
|= WOP_SELECTABLE
| WOP_WANT_CURSOR
| WOP_WANT_HOTKEY
;
242 w
->keymap
= radio_map
;
251 /* --------------------------------------------------------------------------------------------- */