1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * \brief Source: WGauge widget (progress indicator)
38 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/color.h"
43 #include "lib/widget.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /* Currently width is hardcoded here for text mode */
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
59 gauge_callback (Widget
* w
, widget_msg_t msg
, int parm
)
61 WGauge
*g
= (WGauge
*) w
;
62 Dlg_head
*h
= g
->widget
.owner
;
64 if (msg
== WIDGET_INIT
)
67 /* We don't want to get the focus */
68 if (msg
== WIDGET_FOCUS
)
69 return MSG_NOT_HANDLED
;
71 if (msg
== WIDGET_DRAW
)
73 widget_move (&g
->widget
, 0, 0);
74 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
76 tty_printf ("%*s", gauge_len
, "");
79 int percentage
, columns
;
81 long done
= g
->current
;
83 if (total
<= 0 || done
< 0)
95 percentage
= (200 * done
/ total
+ 1) / 2;
96 columns
= (2 * (gauge_len
- 7) * done
/ total
+ 1) / 2;
98 if (g
->from_left_to_right
)
100 tty_setcolor (GAUGE_COLOR
);
101 tty_printf ("%*s", (int) columns
, "");
102 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
103 tty_printf ("%*s] %3d%%", (int) (gauge_len
- 7 - columns
), "", (int) percentage
);
107 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
108 tty_printf ("%*s", gauge_len
- columns
- 7, "");
109 tty_setcolor (GAUGE_COLOR
);
110 tty_printf ("%*s", columns
, "");
111 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
112 tty_printf ("] %3d%%", 100 * columns
/ (gauge_len
- 7), percentage
);
118 return default_proc (msg
, parm
);
121 /* --------------------------------------------------------------------------------------------- */
122 /*** public functions ****************************************************************************/
123 /* --------------------------------------------------------------------------------------------- */
126 gauge_new (int y
, int x
, gboolean shown
, int max
, int current
)
130 g
= g_new (WGauge
, 1);
131 init_widget (&g
->widget
, y
, x
, 1, gauge_len
, gauge_callback
, NULL
);
135 max
= 1; /* I do not like division by zero :) */
137 g
->current
= current
;
138 g
->from_left_to_right
= TRUE
;
140 widget_want_cursor (g
->widget
, FALSE
);
141 widget_want_hotkey (g
->widget
, FALSE
);
146 /* --------------------------------------------------------------------------------------------- */
149 gauge_set_value (WGauge
* g
, int max
, int current
)
151 if (g
->current
== current
&& g
->max
== max
)
152 return; /* Do not flicker */
155 max
= 1; /* I do not like division by zero :) */
156 g
->current
= current
;
158 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
161 /* --------------------------------------------------------------------------------------------- */
164 gauge_show (WGauge
* g
, gboolean shown
)
166 if (g
->shown
!= shown
)
169 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
173 /* --------------------------------------------------------------------------------------------- */