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 bool
44 progress_bar_resize(struct progress_bar *p)
45 {
46 unsigned old_width;
48 assert(p != NULL);
50 if (p->max == 0)
51 return false;
53 old_width = p->width;
54 p->width = (p->window.cols * p->current) / (p->max + 1);
55 assert(p->width < p->window.cols);
57 return p->width != old_width;
58 }
60 bool
61 progress_bar_set(struct progress_bar *p, unsigned current, unsigned max)
62 {
63 bool modified;
65 assert(p != NULL);
67 if (current > max)
68 current = max;
70 modified = (max == 0) != (p->max == 0);
72 p->max = max;
73 p->current = current;
75 return progress_bar_resize(p) || modified;
76 }