#include "StdAfx.h"
#include "CheckBox.h"

CheckBox::CheckBox()
{
	type = CheckBoxType; 
	checked = 0;
}

CheckBox::~CheckBox()
{

}

void CheckBox::Load(const char * b)
{
	_bstr_t base = b;
	SkinnableWindow::Load(base);
	Checked.Load(base+"::checked");
	Unchecked.Load(base+"::unchecked");
	Disabled.Load(base+"::disabled");
}

int CheckBox::Create(HWND parent)
{

	if(checked)
		Set(Checked);
	else
		Set(Unchecked);

	return SkinnableWindow::Create(parent);
}

void CheckBox::OnLButtonUp()
{
	Toggle();
	if(GetFocus()==hWnd){ // there was a button click!
		if(hWndParent)
			SafeSendMessage(hWndParent,WM_COMMAND,MAKEWPARAM(0,ID_LEFTBUTTON),(LPARAM)hWnd);
	}
}

void CheckBox::SetValue(int b)
{
	checked = b;
	if(checked)
		Set(Checked);
	else
		Set(Unchecked);

	InvalidateRect(hWnd,NULL,TRUE);
	UpdateWindow(hWnd);
}

void CheckBox::Toggle()
{
	checked = !checked;
	if(checked)
		Set(Checked);
	else
		Set(Unchecked);

	InvalidateRect(hWnd,NULL,TRUE);
	UpdateWindow(hWnd);
}

void CheckBox::Disable()
{
	SkinnableWindow::Disable();
	Set(Disabled);
	InvalidateRect(hWnd,NULL,TRUE);
	UpdateWindow(hWnd);
}

void CheckBox::Enable()
{
	SkinnableWindow::Enable();
	SetValue(checked);
}

void CheckBox::OnCarriageReturn(HWND h)
{
	Toggle();
	if(GetFocus()==hWnd){ // there was a button click!
		if(hWndParent)
			SafeSendMessage(hWndParent,WM_COMMAND,MAKEWPARAM(0,ID_LEFTBUTTON),(LPARAM)hWnd);
	}	
}