#include "StdAfx.h"
#include "verticalscrollbar.h"

VerticalScrollBar::VerticalScrollBar(void)
{
	pos = 0;
	page = 0.1;
}

VerticalScrollBar::~VerticalScrollBar(void)
{
}

int VerticalScrollBar::Create(HWND parent)
{
	SkinnableWindow::Create(parent);
	linedown.SetLocation(0,size.Height-linedown.size.Height);
	linedown.Create(hWnd);
	lineup.SetLocation(0,0);
	lineup.Create(hWnd);

	// set up the cursor location
	scrollarea.Set(*this);
	scrollarea.SetLocation(0,lineup.size.Height);
	scrollarea.SetSize(size.Width,size.Height-lineup.size.Height-linedown.size.Height);
	scrollarea.normal.borderwidth = 0;
	scrollarea.pressed.borderwidth = 0;
	scrollarea.selected.borderwidth = 0;
	scrollarea.mouseover.borderwidth = 0;
	scrollarea.Create(hWnd);
	cursor.SetLocation(0,0);
	SetPage(0.1);
	cursor.Create(scrollarea.hWnd);
	cursor.SetMessageSink(hWnd);
	SetPos(0);

	return 1;
}

void VerticalScrollBar::Load(const char * b)
{
	_bstr_t base = b;
	SkinnableWindow::Load(base);
	lineup.Load(base+"::lineup");
	linedown.Load(base+"::linedown");
	cursor.Load(base+"::cursor");
}

void VerticalScrollBar::OnButtonMove(HWND h)
{
	if(cursor.hWnd==h){
		if(scrollarea.size.Height-cursor.size.Height>0)
			pos = cursor.location.Y/(float)(scrollarea.size.Height-cursor.size.Height);
		else
			pos = 0;
		SafeSendMessage(hWndParent,WM_COMMAND,MAKEWPARAM(0,ID_SCROLLEVENT),(LPARAM)hWnd);
	}
}

void VerticalScrollBar::SetPos(float y)
{
	if(y<0) y = 0;
	if(y>1.0) y = 1.0;
	cursor.SetLocation(cursor.location.X,(scrollarea.size.Height-cursor.size.Height)*y);
	InvalidateRgn(scrollarea.hWnd,NULL,FALSE);
	pos = y;
	SafeSendMessage(hWndParent,WM_COMMAND,MAKEWPARAM(0,ID_SCROLLEVENT),(LPARAM)hWnd);
}

float VerticalScrollBar::GetPos(){
	return pos;
}

// sets page & cursor height as a percentage of scrollable length
void VerticalScrollBar::SetPage(float y){
	if(y<0.05) y = 0.05;
	if(y>1.0) y = 1.0;
	page = y;
	cursor.SetSize(cursor.size.Width,scrollarea.size.Height*y);
	cursor.SetMoveableRect(0,0,1,scrollarea.size.Height-cursor.size.Height+1);
}

void VerticalScrollBar::SetLine(float y)
{
	if(y<0.05) y = 0.05;
	if(y>1.0) y = 1.0;
	line = y;
}

void VerticalScrollBar::OnButton(HWND h)
{
	if(scrollarea.hWnd==h){
		// determine if this is a page up or page down
		if(env.mouse.Client.y<cursor.location.Y)
			PageUp();
		else
			PageDown();
	} else if(linedown.hWnd==h)
		LineDown();
	else if(lineup.hWnd==h)
		LineUp();
}

void VerticalScrollBar::PageUp()
{
	SetPos(GetPos()-page);	
}

void VerticalScrollBar::PageDown()
{
	SetPos(GetPos()+page);
}

void VerticalScrollBar::LineUp()
{
	SetPos(GetPos()-line);	
}

void VerticalScrollBar::LineDown()
{
	SetPos(GetPos()+line);
}

void VerticalScrollBar::Show(){
	BringWindowToTop(hWnd);
	SkinnableWindow::Show();
}

void VerticalScrollBar::OnSize(int cx, int cy)
{
	SkinnableButton::OnSize(cx,cy);
	linedown.SetLocation(0,size.Height-linedown.size.Height);
	scrollarea.SetLocation(0,lineup.size.Height);
	scrollarea.SetSize(size.Width,size.Height-lineup.size.Height-linedown.size.Height);
	SetPage(page);
	SetPos(pos);
}

void VerticalScrollBar::OnKeyDown(WPARAM wParam, LPARAM lParam)
{

	switch (wParam) 
	{ 
	case VK_UP: 

		// Process the UP ARROW key. 
		LineDown();
		break; 

	case VK_DOWN: 

		// Process the DOWN ARROW key. 
		LineUp();
		break; 

	case VK_PRIOR:
		PageDown();
		break;

	case VK_NEXT:
		PageUp();
		break;

	}

	SkinnableWindow::OnKeyDown(wParam,lParam);
}