2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 1994-2024
|
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
|
2022-05-01 10:43:33 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009-2022
|
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 gauge.c
|
|
|
|
* \brief Source: WGauge widget (progress indicator)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h"
|
|
|
|
#include "lib/tty/color.h"
|
|
|
|
#include "lib/skin.h"
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
static cb_ret_t
|
2024-06-01 21:12:14 +03:00
|
|
|
gauge_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
|
|
|
WGauge *g = GAUGE (w);
|
2019-11-17 17:58:52 +03:00
|
|
|
const int *colors;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case MSG_DRAW:
|
2019-11-17 17:58:52 +03:00
|
|
|
colors = widget_get_colors (w);
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (w, 0, 0);
|
2010-11-12 11:03:57 +03:00
|
|
|
if (!g->shown)
|
2012-12-24 13:36:29 +04:00
|
|
|
{
|
2019-11-17 17:58:52 +03:00
|
|
|
tty_setcolor (colors[DLG_COLOR_NORMAL]);
|
2022-05-01 10:43:33 +03:00
|
|
|
tty_printf ("%*s", w->rect.cols, "");
|
2012-12-24 13:36:29 +04:00
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
else
|
|
|
|
{
|
2012-12-24 13:36:29 +04:00
|
|
|
int gauge_len;
|
2010-11-12 11:03:57 +03:00
|
|
|
int percentage, columns;
|
2015-11-15 20:03:48 +03:00
|
|
|
int total = g->max;
|
|
|
|
int done = g->current;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (total <= 0 || done < 0)
|
|
|
|
{
|
|
|
|
done = 0;
|
|
|
|
total = 100;
|
|
|
|
}
|
|
|
|
if (done > total)
|
|
|
|
done = total;
|
|
|
|
while (total > 65535)
|
|
|
|
{
|
|
|
|
total /= 256;
|
|
|
|
done /= 256;
|
|
|
|
}
|
2012-12-24 13:36:29 +04:00
|
|
|
|
2022-05-01 10:43:33 +03:00
|
|
|
gauge_len = w->rect.cols - 7; /* 7 positions for percentage */
|
2012-12-24 13:36:29 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
percentage = (200 * done / total + 1) / 2;
|
2012-12-24 13:36:29 +04:00
|
|
|
columns = (2 * gauge_len * done / total + 1) / 2;
|
2010-11-12 11:03:57 +03:00
|
|
|
tty_print_char ('[');
|
|
|
|
if (g->from_left_to_right)
|
|
|
|
{
|
|
|
|
tty_setcolor (GAUGE_COLOR);
|
2015-08-25 13:14:13 +03:00
|
|
|
tty_printf ("%*s", columns, "");
|
2019-11-17 17:58:52 +03:00
|
|
|
tty_setcolor (colors[DLG_COLOR_NORMAL]);
|
2012-12-24 13:36:29 +04:00
|
|
|
tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-17 17:58:52 +03:00
|
|
|
tty_setcolor (colors[DLG_COLOR_NORMAL]);
|
2012-12-24 13:36:29 +04:00
|
|
|
tty_printf ("%*s", gauge_len - columns, "");
|
2010-11-12 11:03:57 +03:00
|
|
|
tty_setcolor (GAUGE_COLOR);
|
|
|
|
tty_printf ("%*s", columns, "");
|
2019-11-17 17:58:52 +03:00
|
|
|
tty_setcolor (colors[DLG_COLOR_NORMAL]);
|
2015-08-25 13:14:13 +03:00
|
|
|
tty_printf ("] %3d%%", percentage);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
default:
|
|
|
|
return widget_default_callback (w, sender, msg, parm, data);
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
WGauge *
|
2012-12-24 13:36:29 +04:00
|
|
|
gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2022-05-21 16:11:06 +03:00
|
|
|
WRect r = { y, x, 1, cols };
|
2010-11-12 11:03:57 +03:00
|
|
|
WGauge *g;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
g = g_new (WGauge, 1);
|
2012-06-20 15:09:44 +04:00
|
|
|
w = WIDGET (g);
|
2022-05-21 16:11:06 +03:00
|
|
|
widget_init (w, &r, gauge_callback, NULL);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
g->shown = shown;
|
|
|
|
if (max == 0)
|
|
|
|
max = 1; /* I do not like division by zero :) */
|
|
|
|
g->max = max;
|
|
|
|
g->current = current;
|
|
|
|
g->from_left_to_right = TRUE;
|
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
gauge_set_value (WGauge *g, int max, int current)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
if (g->current == current && g->max == max)
|
|
|
|
return; /* Do not flicker */
|
|
|
|
|
|
|
|
if (max == 0)
|
|
|
|
max = 1; /* I do not like division by zero :) */
|
|
|
|
g->current = current;
|
|
|
|
g->max = max;
|
2019-10-18 15:44:57 +03:00
|
|
|
widget_draw (WIDGET (g));
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
gauge_show (WGauge *g, gboolean shown)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
if (g->shown != shown)
|
|
|
|
{
|
|
|
|
g->shown = shown;
|
2019-10-18 15:44:57 +03:00
|
|
|
widget_draw (WIDGET (g));
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|