Ticket #2076: make copy/move/delete progress dialog wider.
[midnight-commander.git] / lib / widget / gauge.c
blob317aed68938f5108cdc53656133f2962a1e01be5
1 /*
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.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
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/>.
32 /** \file gauge.c
33 * \brief Source: WGauge widget (progress indicator)
36 #include <config.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.h"
45 #include "lib/skin.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 ************************************************************************/
58 static cb_ret_t
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;
64 switch (msg)
66 case MSG_INIT:
67 return MSG_HANDLED;
69 /* We don't want to get the focus */
70 case MSG_FOCUS:
71 return MSG_NOT_HANDLED;
73 case MSG_DRAW:
74 widget_move (w, 0, 0);
75 if (!g->shown)
77 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
78 tty_printf ("%*s", w->cols, "");
80 else
82 int gauge_len;
83 int percentage, columns;
84 long total = g->max;
85 long done = g->current;
87 if (total <= 0 || done < 0)
89 done = 0;
90 total = 100;
92 if (done > total)
93 done = total;
94 while (total > 65535)
96 total /= 256;
97 done /= 256;
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);
112 else
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);
122 return MSG_HANDLED;
124 default:
125 return widget_default_callback (w, sender, msg, parm, data);
129 /* --------------------------------------------------------------------------------------------- */
130 /*** public functions ****************************************************************************/
131 /* --------------------------------------------------------------------------------------------- */
133 WGauge *
134 gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
136 WGauge *g;
137 Widget *w;
139 g = g_new (WGauge, 1);
140 w = WIDGET (g);
141 init_widget (w, y, x, 1, cols, gauge_callback, NULL);
142 widget_want_cursor (w, FALSE);
143 widget_want_hotkey (w, FALSE);
145 g->shown = shown;
146 if (max == 0)
147 max = 1; /* I do not like division by zero :) */
148 g->max = max;
149 g->current = current;
150 g->from_left_to_right = TRUE;
152 return g;
155 /* --------------------------------------------------------------------------------------------- */
157 void
158 gauge_set_value (WGauge * g, int max, int current)
160 if (g->current == current && g->max == max)
161 return; /* Do not flicker */
163 if (max == 0)
164 max = 1; /* I do not like division by zero :) */
165 g->current = current;
166 g->max = max;
167 gauge_callback (WIDGET (g), NULL, MSG_DRAW, 0, NULL);
170 /* --------------------------------------------------------------------------------------------- */
172 void
173 gauge_show (WGauge * g, gboolean shown)
175 if (g->shown != shown)
177 g->shown = shown;
178 gauge_callback (WIDGET (g), NULL, MSG_DRAW, 0, NULL);
182 /* --------------------------------------------------------------------------------------------- */