2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
|
2013-06-24 11:40:53 +04:00
|
|
|
2004, 2005, 2006, 2007, 2009, 2010, 2011, 2013
|
2011-10-15 14:56:47 +04:00
|
|
|
The 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);
|
2012-09-28 11:18:45 +04:00
|
|
|
WDialog *h = w->owner;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case MSG_INIT:
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
/* We don't want to get the focus */
|
|
|
|
case MSG_FOCUS:
|
2010-11-12 11:03:57 +03:00
|
|
|
return MSG_NOT_HANDLED;
|
|
|
|
|
2012-12-24 13:36:29 +04:00
|
|
|
case MSG_DRAW:
|
2012-06-20 15:09:44 +04:00
|
|
|
widget_move (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;
|
|
|
|
long total = g->max;
|
|
|
|
long done = g->current;
|
|
|
|
|
|
|
|
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);
|
|
|
|
tty_printf ("%*s", (int) columns, "");
|
|
|
|
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]);
|
2012-12-24 13:36:29 +04:00
|
|
|
tty_printf ("] %3d%%", 100 * columns / gauge_len, 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);
|
2012-12-24 13:36:29 +04:00
|
|
|
widget_want_cursor (w, FALSE);
|
|
|
|
widget_want_hotkey (w, FALSE);
|
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;
|
2013-02-11 15:35:00 +04:00
|
|
|
widget_redraw (WIDGET (g));
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
gauge_show (WGauge * g, gboolean shown)
|
|
|
|
{
|
|
|
|
if (g->shown != shown)
|
|
|
|
{
|
|
|
|
g->shown = shown;
|
2013-02-11 15:35:00 +04:00
|
|
|
widget_redraw (WIDGET (g));
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|