#include "ProgressBar.h"

ProgressBar::ProgressBar()
{
	// load in our canned bitmap
	BRect r;
	
	r.Set(0, 0, DATA_WIDTH - 1, DATA_HEIGHT - 1);
	fStartBmap = new BBitmap(r, B_RGB_32_BIT);
	fStartBmap->SetBits((char*)start, DATA_WIDTH*DATA_HEIGHT*3, 0, B_RGB_32_BIT);
	
	// load in our canned bitmap
	r.Set(0, 0, DATA_WIDTH - 1, DATA_HEIGHT - 1);
	fMidBmap = new BBitmap(r, B_RGB_32_BIT);
	fMidBmap->SetBits((char*)middle, DATA_WIDTH*DATA_HEIGHT*3, 0, B_RGB_32_BIT);

	// load in our canned bitmap
	r.Set(0, 0, END_WIDTH - 1, DATA_HEIGHT - 1);
	fEndBmap = new BBitmap(r, B_RGB_32_BIT);
	fEndBmap->SetBits((char*)end, END_WIDTH*DATA_HEIGHT*3, 0, B_RGB_32_BIT);
}

ProgressBar::~ProgressBar()
{
	delete fStartBmap;
	delete fMidBmap;
	delete fEndBmap;
}

void	ProgressBar::Draw(BView *view, BRect theRect, float fraction)
{
	BRect b = theRect;
	
	// draw frame
	view->SetPenSize(1.0);
	view->SetHighColor(0,0,0);
	
	view->StrokeRect(b);
	b.InsetBy(1,1);
	
	// now draw the bar
	int barWidth = (int)((b.Width()+1.0) * fraction);
	int barFilled = 0;
	
	BRect br = b;
	br.right = br.left + barWidth - 1;
	
	// do empty area
	BRect dst;
	dst = b;
	dst.left = br.right+1;
	
	int diff = (int)(dst.right - dst.left);
	if (diff >= 1) {
		view->SetHighColor(100,100,100);
		view->StrokeLine(dst.LeftTop(), dst.LeftBottom());
	}
	if (diff >= 2) {
		view->SetHighColor(240,240,240);
		view->StrokeLine(dst.RightTop(),dst.RightBottom());
	}
	if (diff >= 3) {
		view->SetHighColor(100,100,100);
		view->StrokeLine(BPoint(dst.left+1, dst.top), BPoint(dst.right -1, dst.top));
		
		view->SetHighColor(240,240,240);
		view->StrokeLine(BPoint(dst.left+1, dst.bottom), BPoint(dst.right - 1, dst.bottom));
		
		dst.InsetBy(1,1);
		view->SetHighColor(225,225,225);
		view->FillRect(dst);
		
		view->SetHighColor(180,180,180);
		view->StrokeLine(BPoint(dst.left, dst.top), BPoint(dst.left, dst.bottom));
		
	}
	view->SetHighColor(0,0,0);
	
	if (barWidth <= 0)
		return;
	
	
	// do filled area
	dst = br;
	
	dst.left = br.right - END_WIDTH + 1;
	dst.left = max_c(dst.left,br.left);
	
	BRect src;
	src.Set(0,0,END_WIDTH-1,DATA_HEIGHT-1);
	view->DrawBitmap(fEndBmap,src,dst);
	
	if (dst.left <= br.left)
		return;
			
	dst.right = dst.left-1;
	dst.left = br.left + DATA_WIDTH;
	dst.left = max_c(dst.left,br.left);
	
	src.Set(0,0,DATA_WIDTH-1,DATA_HEIGHT-1);
		
	view->DrawBitmap(fMidBmap,src,dst);
	
	if (dst.left <= br.left)
		return;
	dst.right = dst.left-1;
	dst.left = br.left;
	view->DrawBitmap(fStartBmap,src,dst);
}

const int ProgressBar::DATA_HEIGHT = 10;
const int ProgressBar::DATA_WIDTH = 2;
const int ProgressBar::END_WIDTH = 3;

const unsigned char ProgressBar::start[] = {
0x63,0x63,0xce,0x63,0x63,0xce,0x63,0x63,0xce,0x9c,0x9c,0xff,0x63,0x63,0xce,0xce,
0xce,0xff,0x63,0x63,0xce,0xff,0xff,0xff,0x63,0x63,0xce,0xff,0xff,0xff,0x63,0x63,
0xce,0xff,0xff,0xff,0x63,0x63,0xce,0xce,0xce,0xff,0x63,0x63,0xce,0x9c,0x9c,0xff,
0x63,0x63,0xce,0x63,0x63,0xce,0x63,0x63,0xce,0x31,0x31,0x9c};

const unsigned char ProgressBar::middle[] = {
0x31,0x31,0x9c,0x31,0x31,0x9c,0x63,0x63,0xce,0x63,0x63,0xce,0x9c,0x9c,0xff,0x9c,
0x9c,0xff,0xce,0xce,0xff,0xce,0xce,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xce,0xce,
0xff,0xce,0xce,0xff,0x9c,0x9c,0xff,0x9c,0x9c,0xff,0x63,0x63,0xce,0x63,0x63,0xce,
0x31,0x31,0x9c,0x31,0x31,0x9c,0x00,0x00,0x52,0x00,0x00,0x52};

const unsigned char ProgressBar::end[] = {
0x31,0x31,0x9c,0x31,0x31,0x9c,0x31,0x31,0x9c,0x63,0x63,0xce,0x63,0x63,0xce,0x00,
0x00,0x52,0x9c,0x9c,0xff,0x31,0x31,0x9c,0x00,0x00,0x52,0xce,0xce,0xff,0x31,0x31,
0x9c,0x00,0x00,0x52,0xce,0xce,0xff,0x31,0x31,0x9c,0x00,0x00,0x52,0xce,0xce,0xff,
0x31,0x31,0x9c,0x00,0x00,0x52,0x9c,0x9c,0xff,0x31,0x31,0x9c,0x00,0x00,0x52,0x63,
0x63,0xce,0x31,0x31,0x9c,0x00,0x00,0x52,0x31,0x31,0x9c,0x31,0x31,0x9c,0x00,0x00,
0x52,0x00,0x00,0x52,0x00,0x00,0x52,0x00,0x00,0x52};
