2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
12 Andrej Borsenkow, 1996
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 * \brief Source: WGauge widget (progress indicator)
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.h"
46 #include "lib/widget.h"
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
59 gauge_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
61 WGauge
*g
= GAUGE (w
);
62 WDialog
*h
= w
->owner
;
69 /* We don't want to get the focus */
71 return MSG_NOT_HANDLED
;
74 widget_move (w
, 0, 0);
77 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
78 tty_printf ("%*s", w
->cols
, "");
83 int percentage
, columns
;
85 long done
= g
->current
;
87 if (total
<= 0 || done
< 0)
100 gauge_len
= w
->cols
- 7; /* 7 positions for percentage */
102 percentage
= (200 * done
/ total
+ 1) / 2;
103 columns
= (2 * gauge_len
* done
/ total
+ 1) / 2;
104 tty_print_char ('[');
105 if (g
->from_left_to_right
)
107 tty_setcolor (GAUGE_COLOR
);
108 tty_printf ("%*s", (int) columns
, "");
109 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
110 tty_printf ("%*s] %3d%%", gauge_len
- columns
, "", percentage
);
114 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
115 tty_printf ("%*s", gauge_len
- columns
, "");
116 tty_setcolor (GAUGE_COLOR
);
117 tty_printf ("%*s", columns
, "");
118 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
119 tty_printf ("] %3d%%", 100 * columns
/ gauge_len
, percentage
);
125 return widget_default_callback (w
, sender
, msg
, parm
, data
);
129 /* --------------------------------------------------------------------------------------------- */
130 /*** public functions ****************************************************************************/
131 /* --------------------------------------------------------------------------------------------- */
134 gauge_new (int y
, int x
, int cols
, gboolean shown
, int max
, int current
)
139 g
= g_new (WGauge
, 1);
141 init_widget (w
, y
, x
, 1, cols
, gauge_callback
, NULL
);
142 widget_want_cursor (w
, FALSE
);
143 widget_want_hotkey (w
, FALSE
);
147 max
= 1; /* I do not like division by zero :) */
149 g
->current
= current
;
150 g
->from_left_to_right
= TRUE
;
155 /* --------------------------------------------------------------------------------------------- */
158 gauge_set_value (WGauge
* g
, int max
, int current
)
160 if (g
->current
== current
&& g
->max
== max
)
161 return; /* Do not flicker */
164 max
= 1; /* I do not like division by zero :) */
165 g
->current
= current
;
167 gauge_callback (WIDGET (g
), NULL
, MSG_DRAW
, 0, NULL
);
170 /* --------------------------------------------------------------------------------------------- */
173 gauge_show (WGauge
* g
, gboolean shown
)
175 if (g
->shown
!= shown
)
178 gauge_callback (WIDGET (g
), NULL
, MSG_DRAW
, 0, NULL
);
182 /* --------------------------------------------------------------------------------------------- */