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 /* Currently width is hardcoded here for text mode */
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 /*** file scope functions ************************************************************************/
62 gauge_callback (Widget
* w
, widget_msg_t msg
, int parm
)
64 WGauge
*g
= (WGauge
*) w
;
65 Dlg_head
*h
= g
->widget
.owner
;
67 if (msg
== WIDGET_INIT
)
70 /* We don't want to get the focus */
71 if (msg
== WIDGET_FOCUS
)
72 return MSG_NOT_HANDLED
;
74 if (msg
== WIDGET_DRAW
)
76 widget_move (&g
->widget
, 0, 0);
77 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
79 tty_printf ("%*s", gauge_len
, "");
82 int percentage
, columns
;
84 long done
= g
->current
;
86 if (total
<= 0 || done
< 0)
98 percentage
= (200 * done
/ total
+ 1) / 2;
99 columns
= (2 * (gauge_len
- 7) * done
/ total
+ 1) / 2;
100 tty_print_char ('[');
101 if (g
->from_left_to_right
)
103 tty_setcolor (GAUGE_COLOR
);
104 tty_printf ("%*s", (int) columns
, "");
105 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
106 tty_printf ("%*s] %3d%%", (int) (gauge_len
- 7 - columns
), "", (int) percentage
);
110 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
111 tty_printf ("%*s", gauge_len
- columns
- 7, "");
112 tty_setcolor (GAUGE_COLOR
);
113 tty_printf ("%*s", columns
, "");
114 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
115 tty_printf ("] %3d%%", 100 * columns
/ (gauge_len
- 7), percentage
);
121 return default_proc (msg
, parm
);
124 /* --------------------------------------------------------------------------------------------- */
125 /*** public functions ****************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
129 gauge_new (int y
, int x
, gboolean shown
, int max
, int current
)
133 g
= g_new (WGauge
, 1);
134 init_widget (&g
->widget
, y
, x
, 1, gauge_len
, gauge_callback
, NULL
);
138 max
= 1; /* I do not like division by zero :) */
140 g
->current
= current
;
141 g
->from_left_to_right
= TRUE
;
143 widget_want_cursor (g
->widget
, FALSE
);
144 widget_want_hotkey (g
->widget
, FALSE
);
149 /* --------------------------------------------------------------------------------------------- */
152 gauge_set_value (WGauge
* g
, int max
, int current
)
154 if (g
->current
== current
&& g
->max
== max
)
155 return; /* Do not flicker */
158 max
= 1; /* I do not like division by zero :) */
159 g
->current
= current
;
161 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
164 /* --------------------------------------------------------------------------------------------- */
167 gauge_show (WGauge
* g
, gboolean shown
)
169 if (g
->shown
!= shown
)
172 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
176 /* --------------------------------------------------------------------------------------------- */