// AsOptionListWnd.cpp : implementation file
//

#include "stdafx.h"
#include "AsOptionListWnd.h"
#include "MemDC.h"
#include "DrawUtils.h"
#include "ICellListener.h"

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

#define INFO_BAR_HEIGHT 70

/////////////////////////////////////////////////////////////////////////////
// CAsOptionListWnd

CAsOptionListWnd::CAsOptionListWnd():
	borderColor_(::GetSysColor(COLOR_3DSHADOW)),
	bkColor_(::GetSysColor(COLOR_3DFACE)),
	optionList_(NULL),
	fontInfo_(NULL),
	cellListener_(NULL)
{
}

CAsOptionListWnd::~CAsOptionListWnd()
{
	if (optionList_)
		delete optionList_;

	if (fontInfo_)
		delete fontInfo_;
}


BEGIN_MESSAGE_MAP(CAsOptionListWnd, CWnd)
	//{{AFX_MSG_MAP(CAsOptionListWnd)
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CAsOptionListWnd message handlers

int CAsOptionListWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	
	
	fontInfo_ = new CAsFontInfo;
	optionList_ = new CAsOptionList;
	optionList_->Create(CRect(0,0,0,0), this);
	optionList_->RegisterListener(this);
	optionList_->ModifyStyleEx(0, WS_EX_CONTROLPARENT);
	UpdateWindowSize();

	return 0;
}

BOOL CAsOptionListWnd::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

void CAsOptionListWnd::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);
	
	UpdateWindowSize();
}

void CAsOptionListWnd::OnPaint() 
{
	CPaintDC dc(this); 
	CMemDC memDC(&dc);

	DrawBackground(memDC);
	DrawInfo(memDC);
}

void CAsOptionListWnd::DrawBackground(CDC* pDC)
{
	CRect rc;
	CBrush b(bkColor_);

	GetClientRect(rc);
	pDC->FillRect(infoRect_, &b);

	rc.DeflateRect(0,0,1,1);
	DrawUtils::DrawRect(pDC, rc, borderColor_);
	
}

void CAsOptionListWnd::DrawInfo(CDC* pDC)
{
	CRect tmp;
	CRect textRect;
	int captionLineHeight;
	CFont* oldFont;

	oldFont = pDC->SelectObject(fontInfo_->GetFont());
	captionLineHeight = pDC->GetTextExtent(caption_).cy;
	pDC->SelectObject(oldFont);

	tmp = infoRect_;
	tmp.DeflateRect(5, 5, 5, 5);

	textRect = tmp;
	textRect.bottom = textRect.top + captionLineHeight;
	
	DrawUtils::DrawString(pDC, 
						  caption_, 
						  textRect, 
						  DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX, 
						  fontInfo_->GetFontName(), 
						  -fontInfo_->GetFontSize(), 
						  ::GetSysColor(COLOR_BTNTEXT), 
						  true);

	textRect = tmp;
	textRect.top += captionLineHeight + 5;
	DrawUtils::DrawString(pDC, 
						  description_, 
						  textRect, 
						  DT_LEFT | DT_WORDBREAK | DT_NOPREFIX, 
						  fontInfo_->GetFontName(), 
						  -fontInfo_->GetFontSize(), 
						  ::GetSysColor(COLOR_BTNTEXT), 
						  false);
}

void CAsOptionListWnd::UpdateWindowSize()
{
	CRect rc;

	GetClientRect(rc);

	infoRect_ = rc;

	infoRect_.DeflateRect(0, rc.bottom - INFO_BAR_HEIGHT, 0, 0);

	rc.DeflateRect(0,0,0, INFO_BAR_HEIGHT);

	optionList_->SetWindowPos(&CWnd::wndTop, rc.left, rc.top, rc.Width(), rc.Height(), SWP_SHOWWINDOW);

	
}

CAsOptionList* CAsOptionListWnd::GetOptionList()
{
	return optionList_;
}

BOOL CAsOptionListWnd::Create(const RECT& rect, CWnd* pParentWnd, UINT nID) 
{
	BOOL ret;

	ret = CWnd::Create(NULL, _T("AsOptionListWnd"), WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, rect, pParentWnd, nID, NULL);

	if (ret)
		ModifyStyleEx(0, WS_EX_CONTROLPARENT);

	return ret;
}

void CAsOptionListWnd::SetFontInfo(const CAsFontInfo& fontInfo)
{
	*fontInfo_ = fontInfo;

	optionList_->SetFontInfo(fontInfo);
}

void CAsOptionListWnd::SetFontInfo(const cmString& fontName, int fontSize, bool fontBold)
{
	fontInfo_->SetFontName(fontName);
	fontInfo_->SetFontSize(fontSize);
	fontInfo_->SetFontBold(fontBold);

	optionList_->SetFontInfo(*fontInfo_);
}

BOOL CAsOptionListWnd::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg->message == WM_LBUTTONDOWN) {
		CPoint pt(pMsg->pt);
		
		ScreenToClient(&pt);
//		if (infoRect_.PtInRect(pt))
//			optionList_->SetFocus();
	}

	return CWnd::PreTranslateMessage(pMsg);
}


void CAsOptionListWnd::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);
	
//	optionList_->SetFocus();	
}

void CAsOptionListWnd::OnInfoUpdate(const cmString& caption, const cmString& description)
{
	caption_ = caption;
	description_ = description;

	RedrawWindow(infoRect_);
}


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