summaryrefslogtreecommitdiffstats
path: root/lib/widget/gauge.c
blob: 5eebb11c89d071d7924db5cda0cf4e41398ebf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
   Widgets for the Midnight Commander

   Copyright (C) 1994-2023
   Free Software Foundation, Inc.

   Authors:
   Radek Doulik, 1994, 1995
   Miguel de Icaza, 1994, 1995
   Jakub Jelinek, 1995
   Andrej Borsenkow, 1996
   Norbert Warmuth, 1997
   Andrew Borodin <aborodin@vmail.ru>, 2009-2022

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file gauge.c
 *  \brief Source: WGauge widget (progress indicator)
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>

#include "lib/global.h"

#include "lib/tty/tty.h"
#include "lib/tty/color.h"
#include "lib/skin.h"
#include "lib/widget.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static cb_ret_t
gauge_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
{
    WGauge *g = GAUGE (w);
    const int *colors;

    switch (msg)
    {
    case MSG_DRAW:
        colors = widget_get_colors (w);
        widget_gotoyx (w, 0, 0);
        if (!g->shown)
        {
            tty_setcolor (colors[DLG_COLOR_NORMAL]);
            tty_printf ("%*s", w->rect.cols, "");
        }
        else
        {
            int gauge_len;
            int percentage, columns;
            int total = g->max;
            int done = g->current;

            if (total <= 0 || done < 0)
            {
                done = 0;
                total = 100;
            }
            if (done > total)
                done = total;
            while (total > 65535)
            {
                total /= 256;
                done /= 256;
            }

            gauge_len = w->rect.cols - 7;       /* 7 positions for percentage */

            percentage = (200 * done / total + 1) / 2;
            columns = (2 * gauge_len * done / total + 1) / 2;
            tty_print_char ('[');
            if (g->from_left_to_right)
            {
                tty_setcolor (GAUGE_COLOR);
                tty_printf ("%*s", columns, "");
                tty_setcolor (colors[DLG_COLOR_NORMAL]);
                tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
            }
            else
            {
                tty_setcolor (colors[DLG_COLOR_NORMAL]);
                tty_printf ("%*s", gauge_len - columns, "");
                tty_setcolor (GAUGE_COLOR);
                tty_printf ("%*s", columns, "");
                tty_setcolor (colors[DLG_COLOR_NORMAL]);
                tty_printf ("] %3d%%", percentage);
            }
        }
        return MSG_HANDLED;

    default:
        return widget_default_callback (w, sender, msg, parm, data);
    }
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

WGauge *
gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
{
    WRect r = { y, x, 1, cols };
    WGauge *g;
    Widget *w;

    g = g_new (WGauge, 1);
    w = WIDGET (g);
    widget_init (w, &r, gauge_callback, NULL);

    g->shown = shown;
    if (max == 0)
        max = 1;                /* I do not like division by zero :) */
    g->max = max;
    g->current = current;
    g->from_left_to_right = TRUE;

    return g;
}

/* --------------------------------------------------------------------------------------------- */

void
gauge_set_value (WGauge * g, int max, int current)
{
    if (g->current == current && g->max == max)
        return;                 /* Do not flicker */

    if (max == 0)
        max = 1;                /* I do not like division by zero :) */
    g->current = current;
    g->max = max;
    widget_draw (WIDGET (g));
}

/* --------------------------------------------------------------------------------------------- */

void
gauge_show (WGauge * g, gboolean shown)
{
    if (g->shown != shown)
    {
        g->shown = shown;
        widget_draw (WIDGET (g));
    }
}

/* --------------------------------------------------------------------------------------------- */