Unification of widget and dialog callback functions.
[midnight-commander.git] / lib / widget / gauge.c
blobb1728b16b2236a8103bf684ac606a4bcaba52253
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 /* Currently width is hardcoded here for text mode */
53 #define gauge_len 47
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 /*** file scope functions ************************************************************************/
61 static cb_ret_t
62 gauge_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
64 WGauge *g = (WGauge *) w;
65 Dlg_head *h = w->owner;
67 if (msg == WIDGET_INIT)
68 return MSG_HANDLED;
70 /* We don't want to get the focus */
71 if (msg == WIDGET_FOCUS)
72 return MSG_NOT_HANDLED;
74 if (msg == WIDGET_DRAW)
76 widget_move (w, 0, 0);
77 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
78 if (!g->shown)
79 tty_printf ("%*s", gauge_len, "");
80 else
82 int percentage, columns;
83 long total = g->max;
84 long 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;
98 percentage = (200 * done / total + 1) / 2;
99 columns = (2 * (gauge_len - 7) * done / total + 1) / 2;
100 tty_print_char ('[');
101 if (g->from_left_to_right)
103 tty_setcolor (GAUGE_COLOR);
104 tty_printf ("%*s", (int) columns, "");
105 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
106 tty_printf ("%*s] %3d%%", (int) (gauge_len - 7 - columns), "", (int) percentage);
108 else
110 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
111 tty_printf ("%*s", gauge_len - columns - 7, "");
112 tty_setcolor (GAUGE_COLOR);
113 tty_printf ("%*s", columns, "");
114 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
115 tty_printf ("] %3d%%", 100 * columns / (gauge_len - 7), percentage);
118 return MSG_HANDLED;
121 return default_widget_callback (sender, msg, parm, data);
124 /* --------------------------------------------------------------------------------------------- */
125 /*** public functions ****************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
128 WGauge *
129 gauge_new (int y, int x, gboolean shown, int max, int current)
131 WGauge *g;
132 Widget *w;
134 g = g_new (WGauge, 1);
135 w = WIDGET (g);
136 init_widget (w, y, x, 1, gauge_len, gauge_callback, NULL);
138 g->shown = shown;
139 if (max == 0)
140 max = 1; /* I do not like division by zero :) */
141 g->max = max;
142 g->current = current;
143 g->from_left_to_right = TRUE;
145 widget_want_cursor (w, FALSE);
146 widget_want_hotkey (w, FALSE);
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 gauge_callback (WIDGET (g), NULL, WIDGET_DRAW, 0, NULL);
166 /* --------------------------------------------------------------------------------------------- */
168 void
169 gauge_show (WGauge * g, gboolean shown)
171 if (g->shown != shown)
173 g->shown = shown;
174 gauge_callback (WIDGET (g), NULL, WIDGET_DRAW, 0, NULL);
178 /* --------------------------------------------------------------------------------------------- */