/* Terminality - a portable terminal handling library * Copyright (C) 1998-2002, Emil Mikulic. * This is LGPL - look at COPYING.LIB */ /* Project: Terminality/GUI * File: pbar.cpp * Author: Michal Safranek * Description: Implements the progress bar */ #include const char pbar_rcsid[] = "$Id: pbar.cpp,v 1.6 2002/07/26 01:39:40 darkmoon Exp $"; progressBar::progressBar(int xx, int yy, int ww, int hh) { x = xx; y = yy; w = ww; h = hh; bg = COLOR_PBAR_BG; none = COLOR_PBAR_NONE; low = COLOR_PBAR_LOW; mid = COLOR_PBAR_MID; hi = COLOR_PBAR_HI; perc = COLOR_PBAR_PERC; max = 100; cur = 0; adv = 1; show_perc = w > 3; alt_clr = false; visible = true; fixed_colors = false; #ifndef __DJGPP__ chr_none = BLOCK_1; chr_low = BLOCK_2; chr_mid = BLOCK_3; chr_hi = BLOCK_4; #else chr_none = 176; chr_low = 177; chr_mid = 178; chr_hi = 219; #endif low_mid = 34; mid_hi = 67; } progressBar::progressBar(int xx, int yy, int ww, int hh, color _b, color _n, color _l, color _m, color _h, color _p) { x = xx; y = yy; w = ww; h = hh; bg = _b; low = _l; none = _n; mid = _m; hi = _h; perc = _p; max = 100; cur = 0; adv = 1; show_perc = w > 3; alt_clr = false; visible = true; fixed_colors = false; #ifndef __DJGPP__ chr_none = BLOCK_1; chr_low = BLOCK_2; chr_mid = BLOCK_3; chr_hi = BLOCK_4; #else chr_none = 176; chr_low = 177; chr_mid = 178; chr_hi = 219; #endif low_mid = 34; mid_hi = 67; } progressBar::~progressBar() { } #define GET_PERCENT(a, b) ((int)((float) (100.0 * (float) a) / ((float) b))) // Set color by percent void progressBar::color_by_perc(int c, int m) { color _b, _n, _l, _m, _h, _p; /* Set colors: */ _b = bg == Default?COLOR_PBAR_BG:bg; _n = none == Default?COLOR_PBAR_NONE:none; _l = low == Default?COLOR_PBAR_LOW:low; _m = mid == Default?COLOR_PBAR_MID:mid; _h = hi == Default?COLOR_PBAR_HI:hi; _p = perc == Default?COLOR_PBAR_PERC:perc; if (GET_PERCENT(c, m) >= GET_PERCENT(cur, max)) setcolor(_n, _b); else if ((alt_clr?GET_PERCENT(cur, max):GET_PERCENT(c, m)) >= low_mid) if ((alt_clr?GET_PERCENT(cur, max):GET_PERCENT(c, m)) > mid_hi) setcolor(_h, _b); else setcolor(_m, _b); else setcolor(_l, _b); } // Set char by percent chtype *progressBar::char_by_perc(int c, int m) { if (GET_PERCENT(c, m) >= GET_PERCENT(cur, max)) return &chr_none; else if (GET_PERCENT(c, m) >= low_mid) if (GET_PERCENT(c, m) > mid_hi) return &chr_hi; else return &chr_mid; else return &chr_low; } // Drawing routine void progressBar::draw(void) { int i, j; chtype *elem = NULL; color _b, _p; if(!visible) return; /* Set colors: */ _b = bg == Default?COLOR_PBAR_BG:bg; _p = perc == Default?COLOR_PBAR_PERC:perc; for(i = 0; i < w; i++) { for(j = 0; j < h; j++) { gotoxy(x + i, y + j); color_by_perc(i, w); elem = char_by_perc(i, w); writech(*elem); } } if (show_perc) { gotoxy(x + w, y + h/2); setcolor(_p, _b); printw("%3d%%", GET_PERCENT(cur, max)); } update(); } // Change color scheme void progressBar::change_scheme(void) { if (fixed_colors) return; bg = COLOR_PBAR_BG; none = COLOR_PBAR_NONE; low = COLOR_PBAR_LOW; mid = COLOR_PBAR_MID; hi = COLOR_PBAR_HI; perc = COLOR_PBAR_PERC; } // Advance progress int progressBar::advance(void) { if (cur == max) return 0; cur = cur + adv > max ? max : cur + adv; draw(); return 1; } // Set units per advance int progressBar::setadv(int units) { if (units < 1 || units > max) return 0; adv = units; return 1; } // Set max units int progressBar::setmax(int units) { if (units < 1) return 0; max = units; if (adv > max) adv = max; return 1; } // Set current units int progressBar::setcur(int units) { if (units < 0 || units > max) return 0; cur = units; return 1; } int progressBar::setperc(int f, int s) { if (f < 0 || f > 100 || s < f || s > 100) return 0; low_mid = f; mid_hi = s; return 1; }