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
|
2013-06-24 11:40:53 +04:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
|
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 ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static cb_ret_t
|
2012-06-26 11:52:21 +04: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);
|
2016-09-27 13:53:17 +03:00
|
|
|
WDialog *h = DIALOG (w->owner);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case MSG_DRAW:
|
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
|
|
|
{
|
|
|
|
tty_setcolor (h->color[DLG_COLOR_NORMAL]);
|
|
|
|
tty_printf ("%*s", w->cols, "");
|
|
|
|
}
|
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
|
|
|
|
|
|
|
gauge_len = w->cols - 7; /* 7 positions for percentage */
|
|
|
|
|
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, "");
|
2010-11-12 11:03:57 +03:00
|
|
|
tty_setcolor (h->color[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
|
|
|
|
{
|
|
|
|
tty_setcolor (h->color[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, "");
|
|
|
|
tty_setcolor (h->color[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
|
|
|
{
|
|
|
|
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);
|
2013-06-24 11:40:53 +04:00
|
|
|
widget_init (w, y, x, 1, cols, 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
|
|
|
|
gauge_set_value (WGauge * g, int max, int current)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
gauge_show (WGauge * g, gboolean shown)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|