By default, WOP_WANT_HOTKEY option is off.
[midnight-commander.git] / lib / widget / gauge.c
blob61c3a03efb8f719e7d4d5fb62c88db49527279fb
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
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, 2010, 2013
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 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
57 static cb_ret_t
58 gauge_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
60 WGauge *g = GAUGE (w);
61 WDialog *h = w->owner;
63 switch (msg)
65 case MSG_INIT:
66 return MSG_HANDLED;
68 /* We don't want to get the focus */
69 case MSG_FOCUS:
70 return MSG_NOT_HANDLED;
72 case MSG_DRAW:
73 widget_move (w, 0, 0);
74 if (!g->shown)
76 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
77 tty_printf ("%*s", w->cols, "");
79 else
81 int gauge_len;
82 int percentage, columns;
83 int total = g->max;
84 int done = g->current;
86 if (total <= 0 || done < 0)
88 done = 0;
89 total = 100;
91 if (done > total)
92 done = total;
93 while (total > 65535)
95 total /= 256;
96 done /= 256;
99 gauge_len = w->cols - 7; /* 7 positions for percentage */
101 percentage = (200 * done / total + 1) / 2;
102 columns = (2 * gauge_len * done / total + 1) / 2;
103 tty_print_char ('[');
104 if (g->from_left_to_right)
106 tty_setcolor (GAUGE_COLOR);
107 tty_printf ("%*s", columns, "");
108 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
109 tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
111 else
113 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
114 tty_printf ("%*s", gauge_len - columns, "");
115 tty_setcolor (GAUGE_COLOR);
116 tty_printf ("%*s", columns, "");
117 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
118 tty_printf ("] %3d%%", percentage);
121 return MSG_HANDLED;
123 default:
124 return widget_default_callback (w, sender, msg, parm, data);
128 /* --------------------------------------------------------------------------------------------- */
129 /*** public functions ****************************************************************************/
130 /* --------------------------------------------------------------------------------------------- */
132 WGauge *
133 gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
135 WGauge *g;
136 Widget *w;
138 g = g_new (WGauge, 1);
139 w = WIDGET (g);
140 widget_init (w, y, x, 1, cols, gauge_callback, NULL);
142 g->shown = shown;
143 if (max == 0)
144 max = 1; /* I do not like division by zero :) */
145 g->max = max;
146 g->current = current;
147 g->from_left_to_right = TRUE;
149 return g;
152 /* --------------------------------------------------------------------------------------------- */
154 void
155 gauge_set_value (WGauge * g, int max, int current)
157 if (g->current == current && g->max == max)
158 return; /* Do not flicker */
160 if (max == 0)
161 max = 1; /* I do not like division by zero :) */
162 g->current = current;
163 g->max = max;
164 widget_redraw (WIDGET (g));
167 /* --------------------------------------------------------------------------------------------- */
169 void
170 gauge_show (WGauge * g, gboolean shown)
172 if (g->shown != shown)
174 g->shown = shown;
175 widget_redraw (WIDGET (g));
179 /* --------------------------------------------------------------------------------------------- */