Added locale setting before calling dpkg to fix "link to" parsing on non-C locales
[midnight-commander.git] / lib / widget / gauge.c
blob8d65e148624901ad94b00baeb6b3a097280077a2
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
8 1995 Jakub Jelinek
9 1996 Andrej Borsenkow
10 1997 Norbert Warmuth
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.
29 /** \file gauge.c
30 * \brief Source: WGauge widget (progress indicator)
33 #include <config.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/color.h"
42 #include "lib/skin.h"
43 #include "lib/widget.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /* Currently width is hardcoded here for text mode */
50 #define gauge_len 47
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
58 static cb_ret_t
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)
65 return MSG_HANDLED;
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]);
75 if (!g->shown)
76 tty_printf ("%*s", gauge_len, "");
77 else
79 int percentage, columns;
80 long total = g->max;
81 long done = g->current;
83 if (total <= 0 || done < 0)
85 done = 0;
86 total = 100;
88 if (done > total)
89 done = total;
90 while (total > 65535)
92 total /= 256;
93 done /= 256;
95 percentage = (200 * done / total + 1) / 2;
96 columns = (2 * (gauge_len - 7) * done / total + 1) / 2;
97 tty_print_char ('[');
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);
105 else
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);
115 return MSG_HANDLED;
118 return default_proc (msg, parm);
121 /* --------------------------------------------------------------------------------------------- */
122 /*** public functions ****************************************************************************/
123 /* --------------------------------------------------------------------------------------------- */
125 WGauge *
126 gauge_new (int y, int x, gboolean shown, int max, int current)
128 WGauge *g;
130 g = g_new (WGauge, 1);
131 init_widget (&g->widget, y, x, 1, gauge_len, gauge_callback, NULL);
133 g->shown = shown;
134 if (max == 0)
135 max = 1; /* I do not like division by zero :) */
136 g->max = max;
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);
143 return g;
146 /* --------------------------------------------------------------------------------------------- */
148 void
149 gauge_set_value (WGauge * g, int max, int current)
151 if (g->current == current && g->max == max)
152 return; /* Do not flicker */
154 if (max == 0)
155 max = 1; /* I do not like division by zero :) */
156 g->current = current;
157 g->max = max;
158 gauge_callback ((Widget *) g, WIDGET_DRAW, 0);
161 /* --------------------------------------------------------------------------------------------- */
163 void
164 gauge_show (WGauge * g, gboolean shown)
166 if (g->shown != shown)
168 g->shown = shown;
169 gauge_callback ((Widget *) g, WIDGET_DRAW, 0);
173 /* --------------------------------------------------------------------------------------------- */