2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2017
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * \brief Source: WGauge widget (progress indicator)
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/color.h"
45 #include "lib/widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
58 gauge_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
60 WGauge
*g
= GAUGE (w
);
61 WDialog
*h
= w
->owner
;
66 widget_move (w
, 0, 0);
69 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
70 tty_printf ("%*s", w
->cols
, "");
75 int percentage
, columns
;
77 int done
= g
->current
;
79 if (total
<= 0 || done
< 0)
92 gauge_len
= w
->cols
- 7; /* 7 positions for percentage */
94 percentage
= (200 * done
/ total
+ 1) / 2;
95 columns
= (2 * gauge_len
* done
/ total
+ 1) / 2;
97 if (g
->from_left_to_right
)
99 tty_setcolor (GAUGE_COLOR
);
100 tty_printf ("%*s", columns
, "");
101 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
102 tty_printf ("%*s] %3d%%", gauge_len
- columns
, "", percentage
);
106 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
107 tty_printf ("%*s", gauge_len
- columns
, "");
108 tty_setcolor (GAUGE_COLOR
);
109 tty_printf ("%*s", columns
, "");
110 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
111 tty_printf ("] %3d%%", percentage
);
117 return widget_default_callback (w
, sender
, msg
, parm
, data
);
121 /* --------------------------------------------------------------------------------------------- */
122 /*** public functions ****************************************************************************/
123 /* --------------------------------------------------------------------------------------------- */
126 gauge_new (int y
, int x
, int cols
, gboolean shown
, int max
, int current
)
131 g
= g_new (WGauge
, 1);
133 widget_init (w
, y
, x
, 1, cols
, gauge_callback
, NULL
);
137 max
= 1; /* I do not like division by zero :) */
139 g
->current
= current
;
140 g
->from_left_to_right
= TRUE
;
145 /* --------------------------------------------------------------------------------------------- */
148 gauge_set_value (WGauge
* g
, int max
, int current
)
150 if (g
->current
== current
&& g
->max
== max
)
151 return; /* Do not flicker */
154 max
= 1; /* I do not like division by zero :) */
155 g
->current
= current
;
157 widget_redraw (WIDGET (g
));
160 /* --------------------------------------------------------------------------------------------- */
163 gauge_show (WGauge
* g
, gboolean shown
)
165 if (g
->shown
!= shown
)
168 widget_redraw (WIDGET (g
));
172 /* --------------------------------------------------------------------------------------------- */