2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2020-01-18 19:54:37 +03:00
|
|
|
Copyright (C) 1994-2020
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Authors:
|
|
|
|
Radek Doulik, 1994, 1995
|
|
|
|
Miguel de Icaza, 1994, 1995
|
|
|
|
Jakub Jelinek, 1995
|
|
|
|
Andrej Borsenkow, 1996
|
|
|
|
Norbert Warmuth, 1997
|
2016-01-19 15:05:00 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013, 2016
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2010-11-12 11:03:57 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-12 11:03:57 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file radio.c
|
|
|
|
* \brief Source: WRadui widget (radiobuttons)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h"
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
2020-03-14 11:17:58 +03:00
|
|
|
const global_keymap_t *radio_map = NULL;
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2020-03-14 11:17:58 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
2020-03-14 11:17:58 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static cb_ret_t
|
|
|
|
radio_execute_cmd (WRadio * r, long command)
|
|
|
|
{
|
|
|
|
cb_ret_t ret = MSG_HANDLED;
|
|
|
|
Widget *w = WIDGET (r);
|
|
|
|
|
|
|
|
switch (command)
|
|
|
|
{
|
|
|
|
case CK_Up:
|
|
|
|
case CK_Top:
|
|
|
|
if (r->pos == 0)
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
|
|
|
if (command == CK_Top)
|
|
|
|
r->pos = 0;
|
|
|
|
else
|
|
|
|
r->pos--;
|
|
|
|
widget_draw (w);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
case CK_Down:
|
|
|
|
case CK_Bottom:
|
|
|
|
if (r->pos == r->count - 1)
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
|
|
|
if (command == CK_Bottom)
|
|
|
|
r->pos = r->count - 1;
|
|
|
|
else
|
|
|
|
r->pos++;
|
|
|
|
widget_draw (w);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
case CK_Select:
|
|
|
|
r->sel = r->pos;
|
|
|
|
widget_set_state (w, WST_FOCUSED, TRUE); /* Also draws the widget */
|
|
|
|
send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = MSG_NOT_HANDLED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Return MSG_HANDLED if we want a redraw */
|
|
|
|
static cb_ret_t
|
|
|
|
radio_key (WRadio * r, int key)
|
|
|
|
{
|
|
|
|
long command;
|
|
|
|
|
|
|
|
command = widget_lookup_key (WIDGET (r), key);
|
|
|
|
if (command == CK_IgnoreKey)
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
return radio_execute_cmd (r, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
static cb_ret_t
|
2012-06-26 11:52:21 +04:00
|
|
|
radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2012-10-11 12:25:07 +04:00
|
|
|
WRadio *r = RADIO (w);
|
2010-11-12 11:03:57 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_HOTKEY:
|
2016-11-24 11:25:56 +03:00
|
|
|
for (i = 0; i < r->count; i++)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-11-24 11:25:56 +03:00
|
|
|
if (r->texts[i].hotkey != NULL)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-11-24 11:25:56 +03:00
|
|
|
int c;
|
|
|
|
|
|
|
|
c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
|
|
|
|
if (c != parm)
|
|
|
|
continue;
|
|
|
|
r->pos = i;
|
|
|
|
|
|
|
|
/* Take action */
|
2020-03-14 11:17:58 +03:00
|
|
|
send_message (w, sender, MSG_ACTION, CK_Select, data);
|
2016-11-24 11:25:56 +03:00
|
|
|
return MSG_HANDLED;
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_KEY:
|
2020-03-14 11:17:58 +03:00
|
|
|
return radio_key (r, parm);
|
2016-11-24 11:25:56 +03:00
|
|
|
|
2020-03-14 11:17:58 +03:00
|
|
|
case MSG_ACTION:
|
|
|
|
return radio_execute_cmd (r, parm);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_CURSOR:
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (r, r->pos, 1);
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DRAW:
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-06-13 10:16:45 +03:00
|
|
|
gboolean focused;
|
|
|
|
|
|
|
|
focused = widget_get_state (w, WST_FOCUSED);
|
|
|
|
|
|
|
|
for (i = 0; i < r->count; i++)
|
|
|
|
{
|
|
|
|
widget_selectcolor (w, i == r->pos && focused, FALSE);
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (w, i, 0);
|
2016-06-13 10:16:45 +03:00
|
|
|
tty_draw_hline (w->y + i, w->x, ' ', w->cols);
|
|
|
|
tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
|
|
|
|
hotkey_draw (w, r->texts[i], i == r->pos && focused);
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2016-06-13 10:16:45 +03:00
|
|
|
return MSG_HANDLED;
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DESTROY:
|
2010-11-12 11:03:57 +03:00
|
|
|
for (i = 0; i < r->count; i++)
|
2019-12-14 11:50:04 +03:00
|
|
|
hotkey_free (r->texts[i]);
|
2010-11-12 11:03:57 +03:00
|
|
|
g_free (r->texts);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
2012-09-28 15:05:43 +04:00
|
|
|
return widget_default_callback (w, sender, msg, parm, data);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-01-19 15:05:00 +03:00
|
|
|
static void
|
|
|
|
radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-01-19 15:05:00 +03:00
|
|
|
switch (msg)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-01-19 15:05:00 +03:00
|
|
|
case MSG_MOUSE_DOWN:
|
|
|
|
RADIO (w)->pos = event->y;
|
2016-08-09 09:35:27 +03:00
|
|
|
widget_select (w);
|
2016-01-19 15:05:00 +03:00
|
|
|
break;
|
2011-03-21 16:43:56 +03:00
|
|
|
|
2016-01-19 15:05:00 +03:00
|
|
|
case MSG_MOUSE_CLICK:
|
|
|
|
RADIO (w)->pos = event->y;
|
2020-03-14 11:17:58 +03:00
|
|
|
send_message (w, NULL, MSG_ACTION, CK_Select, NULL);
|
2016-01-19 15:05:00 +03:00
|
|
|
send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
|
|
|
|
break;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2016-01-19 15:05:00 +03:00
|
|
|
default:
|
|
|
|
break;
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
WRadio *
|
|
|
|
radio_new (int y, int x, int count, const char **texts)
|
|
|
|
{
|
|
|
|
WRadio *r;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w;
|
2010-11-12 11:03:57 +03:00
|
|
|
int i, wmax = 0;
|
|
|
|
|
|
|
|
r = g_new (WRadio, 1);
|
2012-06-20 15:09:44 +04:00
|
|
|
w = WIDGET (r);
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/* Compute the longest string */
|
|
|
|
r->texts = g_new (hotkey_t, count);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2012-06-20 15:09:44 +04:00
|
|
|
int width;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2019-12-14 11:50:04 +03:00
|
|
|
r->texts[i] = hotkey_new (texts[i]);
|
2012-06-20 15:09:44 +04:00
|
|
|
width = hotkey_width (r->texts[i]);
|
2016-04-07 10:52:04 +03:00
|
|
|
wmax = MAX (width, wmax);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 4 is width of "(*) " */
|
2016-03-06 09:44:16 +03:00
|
|
|
widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
|
2016-06-07 15:03:20 +03:00
|
|
|
w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
|
2020-03-14 11:17:58 +03:00
|
|
|
w->keymap = radio_map;
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
r->pos = 0;
|
|
|
|
r->sel = 0;
|
|
|
|
r->count = count;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|