// AsCheckBoxCell.cpp: implementation for the CAsBaseCell class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "AsCheckBoxCell.h"
#include "DrawUtils.h"
#include "resource.h"
#include "ICellListener.h"

#define CHK_ICON_SIZE		16
#define CHK_LEFT_BORDER		6
#define CHK_TOP_BORDER		2

CAsCheckBoxCell::CAsCheckBoxCell():
	checked_(false),
	iconRect_(0,0,0,0),
	readOnly_(false)
{
}

CAsCheckBoxCell::~CAsCheckBoxCell()
{}

void CAsCheckBoxCell::SetDrawArea(const CRect& drawArea)
{
	if (!visible_)
		return;

	CRect tmp;

	tmp = drawArea;
	
	if (tmp.Height() < 2 * CHK_TOP_BORDER + CHK_ICON_SIZE)
		tmp.bottom = tmp.top + 2 * CHK_TOP_BORDER + CHK_ICON_SIZE;

	if (tmp.Width() < 2 * CHK_LEFT_BORDER + CHK_ICON_SIZE)
		tmp.right = tmp.left + 2 * CHK_LEFT_BORDER + CHK_ICON_SIZE;

	iconRect_.left = tmp.left + CHK_LEFT_BORDER;
	iconRect_.top = tmp.top + (tmp.Height() - CHK_ICON_SIZE) / 2;
	iconRect_.right = iconRect_.left + CHK_ICON_SIZE;
	iconRect_.bottom = iconRect_.top + CHK_ICON_SIZE;
	
	CAsBaseCell::SetDrawArea(tmp);
}

CSize CAsCheckBoxCell::GetMinSize()
{
	CSize minSize;

	minSize.cx = 2 * CHK_LEFT_BORDER + CHK_ICON_SIZE;
	minSize.cy = 2 * CHK_TOP_BORDER + CHK_ICON_SIZE;

	return minSize;
}

void CAsCheckBoxCell::Draw(CDC* pDC)
{
	if (!visible_ || IsHidden())
		return;

	CAsBaseCell::Draw(pDC);

	DrawUtils::DrawIcon(pDC, checked_ ? IDI_CHK_CHECKED : IDI_CHK_UNCHECKED, iconRect_.TopLeft(), CHK_ICON_SIZE);

	CRect txtRect;
	CFont* font;

	txtRect = drawArea_;
	txtRect.DeflateRect(iconRect_.Width() + 2 * CHK_LEFT_BORDER, 0, CHK_LEFT_BORDER, 0);

	font = fontInfo_.CreateFont();
	if (checked_)
		DrawUtils::DrawString(pDC, checkedText_, txtRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE, foreColor_, font); 
	else
		DrawUtils::DrawString(pDC, uncheckedText_, txtRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE, foreColor_, font); 

	fontInfo_.ReleaseFont();
}

bool CAsCheckBoxCell::HitTest(const CPoint& pt)
{
	return CAsBaseCell::HitTest(pt);
}

void CAsCheckBoxCell::SetCheckedText(const cmString& checkedText, bool redraw)
{
	checkedText_ = checkedText;
	if (parentWnd_ && redraw)
		parentWnd_->RedrawWindow(drawArea_);
}

cmString CAsCheckBoxCell::GetCheckedText() const
{
	return checkedText_;
}

void CAsCheckBoxCell::SetUncheckedText(const cmString& uncheckedText, bool redraw)
{
	uncheckedText_ = uncheckedText;

	if (parentWnd_ && redraw)
		parentWnd_->RedrawWindow(drawArea_);
}

cmString CAsCheckBoxCell::GetUncheckedText() const
{
	return uncheckedText_;
}

bool CAsCheckBoxCell::ProcessMessage(DWORD msg, DWORD lParam, DWORD wParam, const CPoint& pt, const CRect& parentRect, bool& needRedraw)
{
	CAsBaseCell::ProcessMessage(msg, lParam, wParam, pt, parentRect, needRedraw);

	if (msg == WM_LBUTTONUP && iconRect_.PtInRect(pt) && !readOnly_) {
		SetChecked(!checked_);
	}

	return false;
}

void CAsCheckBoxCell::KillFocus(bool storeData, bool redraw)
{
	CAsBaseCell::KillFocus(storeData, redraw);
}

void CAsCheckBoxCell::SetFocus(bool redraw)
{
	CAsBaseCell::SetFocus(redraw);
}


void CAsCheckBoxCell::SetChecked(bool checked, bool redraw)
{
	if (checked == checked_)
		return;

	if (cellListener_) {
		bool ret;

		ret = cellListener_->OnValueChanged(name_, checked_, checked);
		if (ret)
			checked_ = checked;
	} else {
		checked_ = checked;
	}
	
	
	if (parentWnd_ && redraw)
		parentWnd_->RedrawWindow(drawArea_);
}

bool CAsCheckBoxCell::GetChecked() const
{
	return checked_;
}

void CAsCheckBoxCell::SetReadOnly(bool readOnly)
{
	readOnly_ = readOnly;
}

bool CAsCheckBoxCell::IsReadOnly() const
{
	return readOnly_;
}