// cmBaseCell.cpp: implementation of the CAsBaseCell class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AsBaseCell.h"
#include "DrawUtils.h"

/*
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
*/

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAsBaseCell::CAsBaseCell():
	backColor_(::GetSysColor(COLOR_WINDOW)),
	foreColor_(::GetSysColor(COLOR_BTNTEXT)),
	parentWnd_(NULL),
	drawArea_(0,0,0,0),
	hasFocus_(false),
	isSelected_(false),
	visible_(true),
	cellListener_(NULL)
{

}

CAsBaseCell::~CAsBaseCell()
{

}

void CAsBaseCell::SetDrawArea(const CRect& drawArea)
{
	drawArea_ = drawArea;
}

CRect CAsBaseCell::GetDrawArea() const
{
	return drawArea_;
}

CSize CAsBaseCell::GetMinSize()
{
	return drawArea_.Size();
}

void CAsBaseCell::Draw(CDC* pDC)
{
	if (!visible_)
		return;

	CBrush b(backColor_);
	CRect tmp;

	pDC->FillRect(drawArea_, &b);

	DrawText(pDC);	
}

void CAsBaseCell::DrawText(CDC* pDC)
{
	if (text_.length()) {
		CRect txtRect;
		CFont* font;

		txtRect = drawArea_;
		//txtRect.DeflateRect(9, 0, 5, 2);
		txtRect.DeflateRect(10, 2, 5, 2);
        font = fontInfo_.CreateFont();
		DrawUtils::DrawcmString(pDC, text_, txtRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX, foreColor_, font);
		fontInfo_.ReleaseFont();
	}
}

bool CAsBaseCell::HitTest(const CPoint& pt)
{
	return drawArea_.PtInRect(pt) == TRUE;
}


void CAsBaseCell::SetVisible(bool visible)
{
	visible_ = visible;
}

bool CAsBaseCell::IsVisible() const
{
	return visible_;
}

void CAsBaseCell::SetText(const cmString& text, bool redraw)
{
	text_ = text;

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

cmString CAsBaseCell::GetText()
{
	return text_;	
}

void CAsBaseCell::SetParentWindow(CWnd* parentWnd)
{
	parentWnd_ = parentWnd;
}

bool CAsBaseCell::ProcessMessage(DWORD msg, DWORD lParam, DWORD wParam, const CPoint& pt, const CRect& parentRect, bool& needRedraw)
{
	bool processed;
	
	processed = false;
	switch (msg) {
		case WM_LBUTTONDOWN:
		{
			BOOL ret;
			bool changed = false;

			ret = drawArea_.PtInRect(pt);
			if (ret) {
				if (!IsSelected()) {
					SetSelection(false);
					needRedraw = true;
				} else if (!HasFocus()) {
					SetFocus(false);
					needRedraw = true;
				}
			} else {
				if (IsSelected()) {
					KillSelection(false);
					if (!IsHidden())
						needRedraw = true;
				} else if (HasFocus()) {
					KillFocus(false);
					if (!IsHidden())
						needRedraw = true;
				}
			}
			
			
			break;
		}
	}

	return processed;
}

void CAsBaseCell::SetFontInfo(const cmString& fontName, int fontSize, bool fontBold)
{
	CAsFontInfo tmp;

	tmp.SetFontName(fontName);
	tmp.SetFontSize(fontSize);
	tmp.SetFontBold(fontBold);

	SetFontInfo(tmp);
}

void CAsBaseCell::SetFontInfo(const CAsFontInfo& fontInfo)
{
	fontInfo_ = fontInfo;	
}

void CAsBaseCell::KillFocus(bool storeData, bool redraw)
{
	hasFocus_ = false;
	if (redraw && parentWnd_)
		parentWnd_->RedrawWindow(drawArea_);
}

void CAsBaseCell::SetFocus(bool redraw)
{
	hasFocus_ = true;

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

bool CAsBaseCell::HasFocus()
{
	return hasFocus_;
}

void CAsBaseCell::SetBackColor(COLORREF backColor)
{
	backColor_ = backColor;
}

COLORREF CAsBaseCell::GetBackColor() const
{
	return backColor_;
}

void CAsBaseCell::SetForeColor(COLORREF foreColor)
{
	foreColor_ = foreColor;
}

COLORREF CAsBaseCell::GetForeColor() const
{
	return foreColor_;
}

void CAsBaseCell::SetSelection(bool redraw)
{
	isSelected_ = true;

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

void CAsBaseCell::KillSelection(bool redraw)
{
	KillFocus(true, redraw);
	isSelected_ = false;

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

bool CAsBaseCell::IsSelected()
{
	return isSelected_;
}

bool CAsBaseCell::IsHidden()
{
	CRect parentRect;
	bool isHidden;

	if (parentWnd_ == NULL)
		return false;

	parentWnd_->GetClientRect(parentRect);
	
	// check if this cell is visible
	//
	isHidden = (drawArea_.bottom < parentRect.top || drawArea_.top > parentRect.bottom);

	return isHidden;
}

void CAsBaseCell::SetCellListener(ICellListener* cellListener)
{
	cellListener_ = cellListener;
}

void CAsBaseCell::SetName(const cmString& name)
{
	name_ = name;
}

cmString CAsBaseCell::GetName() const
{
	return name_;
}
