tar: avoid need for base64_init and extra table.
[midnight-commander.git] / lib / widget / gauge.c
blob37dd02e3a7e78b7c4948b4eba33587943165657f
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009-2022
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file gauge.c
32 * \brief Source: WGauge widget (progress indicator)
35 #include <config.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/color.h"
44 #include "lib/skin.h"
45 #include "lib/widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** forward declarations (file scope functions) *************************************************/
55 /*** file scope variables ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 static cb_ret_t
62 gauge_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
64 WGauge *g = GAUGE (w);
65 const int *colors;
67 switch (msg)
69 case MSG_DRAW:
70 colors = widget_get_colors (w);
71 widget_gotoyx (w, 0, 0);
72 if (!g->shown)
74 tty_setcolor (colors[DLG_COLOR_NORMAL]);
75 tty_printf ("%*s", w->rect.cols, "");
77 else
79 int gauge_len;
80 int percentage, columns;
81 int total = g->max;
82 int done = g->current;
84 if (total <= 0 || done < 0)
86 done = 0;
87 total = 100;
89 if (done > total)
90 done = total;
91 while (total > 65535)
93 total /= 256;
94 done /= 256;
97 gauge_len = w->rect.cols - 7; /* 7 positions for percentage */
99 percentage = (200 * done / total + 1) / 2;
100 columns = (2 * gauge_len * done / total + 1) / 2;
101 tty_print_char ('[');
102 if (g->from_left_to_right)
104 tty_setcolor (GAUGE_COLOR);
105 tty_printf ("%*s", columns, "");
106 tty_setcolor (colors[DLG_COLOR_NORMAL]);
107 tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
109 else
111 tty_setcolor (colors[DLG_COLOR_NORMAL]);
112 tty_printf ("%*s", gauge_len - columns, "");
113 tty_setcolor (GAUGE_COLOR);
114 tty_printf ("%*s", columns, "");
115 tty_setcolor (colors[DLG_COLOR_NORMAL]);
116 tty_printf ("] %3d%%", percentage);
119 return MSG_HANDLED;
121 default:
122 return widget_default_callback (w, sender, msg, parm, data);
126 /* --------------------------------------------------------------------------------------------- */
127 /*** public functions ****************************************************************************/
128 /* --------------------------------------------------------------------------------------------- */
130 WGauge *
131 gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
133 WRect r = { y, x, 1, cols };
134 WGauge *g;
135 Widget *w;
137 g = g_new (WGauge, 1);
138 w = WIDGET (g);
139 widget_init (w, &r, gauge_callback, NULL);
141 g->shown = shown;
142 if (max == 0)
143 max = 1; /* I do not like division by zero :) */
144 g->max = max;
145 g->current = current;
146 g->from_left_to_right = TRUE;
148 return g;
151 /* --------------------------------------------------------------------------------------------- */
153 void
154 gauge_set_value (WGauge *g, int max, int current)
156 if (g->current == current && g->max == max)
157 return; /* Do not flicker */
159 if (max == 0)
160 max = 1; /* I do not like division by zero :) */
161 g->current = current;
162 g->max = max;
163 widget_draw (WIDGET (g));
166 /* --------------------------------------------------------------------------------------------- */
168 void
169 gauge_show (WGauge *g, gboolean shown)
171 if (g->shown != shown)
173 g->shown = shown;
174 widget_draw (WIDGET (g));
178 /* --------------------------------------------------------------------------------------------- */