2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2016-01-01 09:53:15 +03:00
|
|
|
Copyright (C) 1994-2016
|
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
|
2015-11-26 01:30:56 +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 button.c
|
|
|
|
* \brief Source: WButton widget
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h"
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static cb_ret_t
|
2012-06-26 11:52:21 +04:00
|
|
|
button_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
|
|
|
WButton *b = BUTTON (w);
|
2012-09-28 15:05:43 +04:00
|
|
|
WDialog *h = w->owner;
|
2010-11-12 11:03:57 +03:00
|
|
|
int off = 0;
|
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_HOTKEY:
|
2010-11-12 11:03:57 +03:00
|
|
|
/*
|
|
|
|
* Don't let the default button steal Enter from the current
|
|
|
|
* button. This is a workaround for the flawed event model
|
|
|
|
* when hotkeys are sent to all widgets before the key is
|
|
|
|
* handled by the current widget.
|
|
|
|
*/
|
2012-06-20 15:09:44 +04:00
|
|
|
if (parm == '\n' && WIDGET (h->current->data) == WIDGET (b))
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
send_message (w, sender, MSG_KEY, ' ', data);
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
send_message (w, sender, MSG_KEY, ' ', data);
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
send_message (w, sender, MSG_KEY, ' ', data);
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_KEY:
|
2010-11-12 11:03:57 +03:00
|
|
|
if (parm != ' ' && parm != '\n')
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
2012-11-22 13:55:38 +04:00
|
|
|
h->ret_value = b->action;
|
|
|
|
if (b->callback == NULL || b->callback (b, b->action) != 0)
|
2010-11-12 11:03:57 +03:00
|
|
|
dlg_stop (h);
|
2012-11-22 13:55:38 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_CURSOR:
|
2010-11-12 11:03:57 +03:00
|
|
|
switch (b->flags)
|
|
|
|
{
|
|
|
|
case DEFPUSH_BUTTON:
|
|
|
|
off = 3;
|
|
|
|
break;
|
|
|
|
case NORMAL_BUTTON:
|
|
|
|
off = 2;
|
|
|
|
break;
|
|
|
|
case NARROW_BUTTON:
|
|
|
|
off = 1;
|
|
|
|
break;
|
|
|
|
case HIDDEN_BUTTON:
|
|
|
|
default:
|
|
|
|
off = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-20 15:09:44 +04:00
|
|
|
widget_move (w, 0, b->hotpos + off);
|
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-12 20:43:21 +03:00
|
|
|
gboolean focused;
|
|
|
|
|
|
|
|
focused = widget_get_state (w, WST_FOCUSED);
|
|
|
|
|
|
|
|
widget_selectcolor (w, focused, FALSE);
|
|
|
|
widget_move (w, 0, 0);
|
|
|
|
|
|
|
|
switch (b->flags)
|
|
|
|
{
|
|
|
|
case DEFPUSH_BUTTON:
|
|
|
|
tty_print_string ("[< ");
|
|
|
|
break;
|
|
|
|
case NORMAL_BUTTON:
|
|
|
|
tty_print_string ("[ ");
|
|
|
|
break;
|
|
|
|
case NARROW_BUTTON:
|
|
|
|
tty_print_string ("[");
|
|
|
|
break;
|
|
|
|
case HIDDEN_BUTTON:
|
|
|
|
default:
|
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
hotkey_draw (w, b->text, focused);
|
|
|
|
|
|
|
|
switch (b->flags)
|
|
|
|
{
|
|
|
|
case DEFPUSH_BUTTON:
|
|
|
|
tty_print_string (" >]");
|
|
|
|
break;
|
|
|
|
case NORMAL_BUTTON:
|
|
|
|
tty_print_string (" ]");
|
|
|
|
break;
|
|
|
|
case NARROW_BUTTON:
|
|
|
|
tty_print_string ("]");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2016-06-12 20:43:21 +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
|
|
|
release_hotkey (b->text);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2015-11-26 01:30:56 +03:00
|
|
|
static void
|
|
|
|
button_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2015-11-26 01:30:56 +03:00
|
|
|
(void) event;
|
2011-03-21 16:43:56 +03:00
|
|
|
|
2015-11-26 01:30:56 +03:00
|
|
|
switch (msg)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2015-11-26 01:30:56 +03:00
|
|
|
case MSG_MOUSE_DOWN:
|
2016-08-09 09:35:27 +03:00
|
|
|
widget_select (w);
|
2015-11-26 01:30:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_MOUSE_CLICK:
|
|
|
|
send_message (w, NULL, MSG_KEY, ' ', NULL);
|
|
|
|
send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
|
|
|
|
break;
|
2011-03-21 16:43:56 +03:00
|
|
|
|
2015-11-26 01:30:56 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
WButton *
|
|
|
|
button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
|
|
|
|
{
|
|
|
|
WButton *b;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
b = g_new (WButton, 1);
|
2012-06-20 15:09:44 +04:00
|
|
|
w = WIDGET (b);
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
b->action = action;
|
|
|
|
b->flags = flags;
|
|
|
|
b->text = parse_hotkey (text);
|
2016-03-06 09:44:16 +03:00
|
|
|
widget_init (w, y, x, 1, button_get_len (b), button_callback, button_mouse_callback);
|
2016-06-07 15:03:20 +03:00
|
|
|
w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
|
2010-11-12 11:03:57 +03:00
|
|
|
b->callback = callback;
|
|
|
|
b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2015-11-13 17:11:29 +03:00
|
|
|
char *
|
2010-11-12 11:03:57 +03:00
|
|
|
button_get_text (const WButton * b)
|
|
|
|
{
|
|
|
|
if (b->text.hotkey != NULL)
|
|
|
|
return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
|
|
|
|
return g_strdup (b->text.start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
button_set_text (WButton * b, const char *text)
|
|
|
|
{
|
2012-09-18 14:35:38 +04:00
|
|
|
Widget *w = WIDGET (b);
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
release_hotkey (b->text);
|
|
|
|
b->text = parse_hotkey (text);
|
2013-04-23 15:56:43 +04:00
|
|
|
b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
|
2012-09-18 14:35:38 +04:00
|
|
|
w->cols = button_get_len (b);
|
2013-02-11 15:35:00 +04:00
|
|
|
widget_redraw (w);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
button_get_len (const WButton * b)
|
|
|
|
{
|
|
|
|
int ret = hotkey_width (b->text);
|
|
|
|
|
|
|
|
switch (b->flags)
|
|
|
|
{
|
|
|
|
case DEFPUSH_BUTTON:
|
|
|
|
ret += 6;
|
|
|
|
break;
|
|
|
|
case NORMAL_BUTTON:
|
|
|
|
ret += 4;
|
|
|
|
break;
|
|
|
|
case NARROW_BUTTON:
|
|
|
|
ret += 2;
|
|
|
|
break;
|
|
|
|
case HIDDEN_BUTTON:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|