// AsBinaryWildCardCell.cpp: implementation for the CAsBinaryWildCard class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "AsBinaryWildCardsCell.h"
#include "DrawUtils.h"
#include "resource.h"

#define BIT_SIZE			16
#define CHK_LEFT_BORDER		6
#define CHK_TOP_BORDER		2

CAsBinaryWildCardsCell::CAsBinaryWildCardsCell():
	readOnly_(false),
	bitCount_(8)
{
}

CAsBinaryWildCardsCell::~CAsBinaryWildCardsCell()
{}

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

	CRect tmp;

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

	if (tmp.Width() < 2 * CHK_LEFT_BORDER + BIT_SIZE)
		tmp.right = tmp.left + 2 * CHK_LEFT_BORDER + BIT_SIZE;
	
	// calculate draw area for each bit
	//
	bitRect_.clear();

	tmp.left = drawArea.left;
	tmp.top = drawArea.top;
	tmp.bottom = drawArea.bottom;
	for (int i = 0; i < bitCount_; i++) {
		tmp.right = tmp.left + BIT_SIZE;

		bitRect_.push_back(tmp);

		tmp.OffsetRect(BIT_SIZE + 1, 0);
	}	
	
	CAsBaseCell::SetDrawArea(tmp);
}

CSize CAsBinaryWildCardsCell::GetMinSize()
{
	CSize minSize;

	minSize.cx = 2 * CHK_LEFT_BORDER + (BIT_SIZE + 1) * bitCount_;
	minSize.cy = 2 * CHK_TOP_BORDER + BIT_SIZE;

	return minSize;
}

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

	CAsBaseCell::Draw(pDC);

	CFont* font;

	font = fontInfo_.CreateFont();
	
	for (int i = 0; i < bitRect_.size(); i++) {
		DrawUtils::DrawGradient(pDC, bitRect_[i], RGB(255,0,0), RGB(100,100,100), .5);
	}

	fontInfo_.ReleaseFont();
}

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

bool CAsBinaryWildCardsCell::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 CAsBinaryWildCardsCell::KillFocus(bool storeData, bool redraw)
{
	CAsBaseCell::KillFocus(storeData, redraw);
}

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

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

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

void CAsBinaryWildCardsCell::SetBitCount(long bitCount)
{
	bitCount_ = bitCount;
}

long CAsBinaryWildCardsCell::GetBitCount() const
{
	return bitCount_;
}

void CAsBinaryWildCardsCell::SetBitStatus(long bitIndex, enBitStatus status, bool redraw)
{
	if (bitIndex < 0 || bitIndex >= bitCount_)
		return;

	bitStatus_[bitIndex] = status;

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

CAsBinaryWildCardsCell::enBitStatus CAsBinaryWildCardsCell::GetBitStatus(long bitIndex) const
{
	if (bitIndex < 0 || bitIndex >= bitCount_)
		return enBitStatus_Unchecked;

	return bitStatus_[bitIndex];
}

void CAsBinaryWildCardsCell::SetBinaryString(const cmString& checkedText, bool redraw)
{

}

cmString CAsBinaryWildCardsCell::GetBinaryString() const
{
	return binaryString_;
}

void CAsBinaryWildCardsCell::SetValue(long value, bool redraw)
{

}

long CAsBinaryWildCardsCell::GetValue() const
{
	return value_;
}
