2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2021-01-01 14:21:28 +03:00
|
|
|
Copyright (C) 1994-2021
|
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:04:05 +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 buttonbar.c
|
|
|
|
* \brief Source: WButtonBar widget
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h"
|
|
|
|
#include "lib/tty/key.h" /* XCTRL and ALT macros */
|
|
|
|
#include "lib/skin.h"
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/util.h"
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* calculate positions of buttons; width is never less than 7 */
|
|
|
|
static void
|
|
|
|
buttonbar_init_button_positions (WButtonBar * bb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
if (COLS < BUTTONBAR_LABELS_NUM * 7)
|
|
|
|
{
|
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
{
|
|
|
|
if (pos + 7 <= COLS)
|
|
|
|
pos += 7;
|
|
|
|
|
|
|
|
bb->labels[i].end_coord = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Distribute the extra width in a way that the middle vertical line
|
|
|
|
(between F5 and F6) aligns with the two panels. The extra width
|
|
|
|
is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
|
|
|
|
int dv, md;
|
|
|
|
|
|
|
|
dv = COLS / BUTTONBAR_LABELS_NUM;
|
|
|
|
md = COLS % BUTTONBAR_LABELS_NUM;
|
|
|
|
|
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
|
|
|
|
{
|
|
|
|
pos += dv;
|
|
|
|
if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
bb->labels[i].end_coord = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
{
|
|
|
|
pos += dv;
|
|
|
|
if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
bb->labels[i].end_coord = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* return width of one button */
|
|
|
|
static int
|
|
|
|
buttonbar_get_button_width (const WButtonBar * bb, int i)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
return bb->labels[0].end_coord;
|
|
|
|
return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
if (bb->labels[i].end_coord > x)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_label_text (WButtonBar * bb, int idx, const char *text)
|
|
|
|
{
|
|
|
|
g_free (bb->labels[idx - 1].text);
|
|
|
|
bb->labels[idx - 1].text = g_strdup (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* returns TRUE if a function has been called, FALSE otherwise. */
|
|
|
|
static gboolean
|
|
|
|
buttonbar_call (WButtonBar * bb, int i)
|
|
|
|
{
|
|
|
|
cb_ret_t ret = MSG_NOT_HANDLED;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w = WIDGET (bb);
|
2016-02-14 20:40:22 +03:00
|
|
|
Widget *target;
|
|
|
|
|
2011-02-20 18:23:32 +03:00
|
|
|
if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
|
2017-05-13 12:57:58 +03:00
|
|
|
{
|
|
|
|
target = (bb->labels[i].receiver != NULL) ? bb->labels[i].receiver : WIDGET (w->owner);
|
2016-02-14 20:40:22 +03:00
|
|
|
ret = send_message (target, w, MSG_ACTION, bb->labels[i].command, NULL);
|
2017-05-13 12:57:58 +03:00
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static cb_ret_t
|
2012-06-26 11:52:21 +04:00
|
|
|
buttonbar_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
|
|
|
WButtonBar *bb = BUTTONBAR (w);
|
2010-11-12 11:03:57 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_HOTKEY:
|
2010-11-12 11:03:57 +03:00
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
|
|
|
|
return MSG_HANDLED;
|
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DRAW:
|
2010-11-12 11:03:57 +03:00
|
|
|
if (bb->visible)
|
|
|
|
{
|
|
|
|
buttonbar_init_button_positions (bb);
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (w, 0, 0);
|
2010-11-12 11:03:57 +03:00
|
|
|
tty_setcolor (DEFAULT_COLOR);
|
2012-06-20 15:09:44 +04:00
|
|
|
tty_printf ("%-*s", w->cols, "");
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (w, 0, 0);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
{
|
|
|
|
int width;
|
2013-10-15 10:34:04 +04:00
|
|
|
const char *text;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
width = buttonbar_get_button_width (bb, i);
|
|
|
|
if (width <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
tty_setcolor (BUTTONBAR_HOTKEY_COLOR);
|
|
|
|
tty_printf ("%2d", i + 1);
|
|
|
|
|
|
|
|
tty_setcolor (BUTTONBAR_BUTTON_COLOR);
|
|
|
|
text = (bb->labels[i].text != NULL) ? bb->labels[i].text : "";
|
|
|
|
tty_print_string (str_fit_to_term (text, width - 2, J_LEFT_FIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DESTROY:
|
2010-11-12 11:03:57 +03:00
|
|
|
for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
|
|
|
|
g_free (bb->labels[i].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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-01-19 15:04:05 +03:00
|
|
|
static void
|
|
|
|
buttonbar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-01-19 15:04:05 +03:00
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case MSG_MOUSE_CLICK:
|
|
|
|
{
|
|
|
|
WButtonBar *bb = BUTTONBAR (w);
|
|
|
|
int button;
|
2011-03-21 16:43:56 +03:00
|
|
|
|
2016-01-19 15:04:05 +03:00
|
|
|
button = buttonbar_get_button_by_x_coord (bb, event->x);
|
|
|
|
if (button >= 0)
|
|
|
|
buttonbar_call (bb, button);
|
|
|
|
break;
|
|
|
|
}
|
2011-03-21 16:43:56 +03:00
|
|
|
|
2016-01-19 15:04:05 +03:00
|
|
|
default:
|
|
|
|
break;
|
2011-03-21 16:43:56 +03:00
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
WButtonBar *
|
|
|
|
buttonbar_new (gboolean visible)
|
|
|
|
{
|
|
|
|
WButtonBar *bb;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
bb = g_new0 (WButtonBar, 1);
|
2012-06-20 15:09:44 +04:00
|
|
|
w = WIDGET (bb);
|
2016-03-06 09:44:16 +03:00
|
|
|
widget_init (w, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_mouse_callback);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-06-20 15:09:44 +04:00
|
|
|
w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
|
2010-11-12 11:03:57 +03:00
|
|
|
bb->visible = visible;
|
2012-06-20 15:09:44 +04:00
|
|
|
widget_want_hotkey (w, TRUE);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2016-02-20 19:02:05 +03:00
|
|
|
buttonbar_set_label (WButtonBar * bb, int idx, const char *text, const global_keymap_t * keymap,
|
|
|
|
Widget * receiver)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
|
|
|
|
{
|
2015-11-21 18:15:11 +03:00
|
|
|
long command = CK_IgnoreKey;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (keymap != NULL)
|
2010-11-18 11:43:23 +03:00
|
|
|
command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if ((text == NULL) || (text[0] == '\0'))
|
|
|
|
set_label_text (bb, idx, "");
|
|
|
|
else
|
|
|
|
set_label_text (bb, idx, text);
|
|
|
|
|
|
|
|
bb->labels[idx - 1].command = command;
|
2012-06-20 15:09:44 +04:00
|
|
|
bb->labels[idx - 1].receiver = WIDGET (receiver);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Find ButtonBar widget in the dialog */
|
|
|
|
WButtonBar *
|
2012-09-28 11:18:45 +04:00
|
|
|
find_buttonbar (const WDialog * h)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2016-09-29 11:16:17 +03:00
|
|
|
return BUTTONBAR (widget_find_by_type (CONST_WIDGET (h), buttonbar_callback));
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|