1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "progress_bar.h"
22 #include <assert.h>
24 void
25 progress_bar_paint(const struct progress_bar *p)
26 {
27 assert(p != NULL);
29 mvwhline(p->window.w, 0, 0, ACS_HLINE, p->window.cols);
31 if (p->max > 0) {
32 assert(p->width < p->window.cols);
34 if (p->width > 0)
35 whline(p->window.w, '=', p->width);
37 mvwaddch(p->window.w, 0, p->width, 'O');
38 }
40 wnoutrefresh(p->window.w);
41 }
43 static bool
44 progress_bar_calc(struct progress_bar *p)
45 {
46 unsigned old_width;
48 if (p->max == 0)
49 return false;
51 old_width = p->width;
52 p->width = (p->window.cols * p->current) / (p->max + 1);
53 assert(p->width < p->window.cols);
55 return p->width != old_width;
56 }
58 void
59 progress_bar_resize(struct progress_bar *p, unsigned width, int y, int x)
60 {
61 assert(p != NULL);
63 p->window.cols = width;
64 wresize(p->window.w, 1, width);
65 mvwin(p->window.w, y, x);
67 progress_bar_calc(p);
68 }
70 bool
71 progress_bar_set(struct progress_bar *p, unsigned current, unsigned max)
72 {
73 bool modified;
75 assert(p != NULL);
77 if (current > max)
78 current = max;
80 modified = (max == 0) != (p->max == 0);
82 p->max = max;
83 p->current = current;
85 return progress_bar_calc(p) || modified;
86 }