// CfgComboBox.cpp : implementation file
//
#include "stdafx.h"
#include "CfgComboBox.h"
#include "MemDC.h"
#include "DrawUtils.h"
#include "resource.h"
#include "GuiDefs.h"

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

CCfgComboBox::CCfgComboBox():
	hwndList_(NULL),
	droppedDown_(false),
	dropAreaHeight_(100),
	basicRect_(0,0,0,0)
{
	WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();
    //HINSTANCE hInst = AfxGetResourceHandle();

    if (!(::GetClassInfo(hInst, L"AsComboBox", &wndcls)))
    {
        // otherwise we need to register a new class
        wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc      = ::DefWindowProc;
        wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
        wndcls.hInstance        = hInst;
        wndcls.hIcon            = NULL;
        wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
        wndcls.lpszMenuName     = NULL;
        wndcls.lpszClassName    = L"AsComboBox";

        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return;
        }
    }
}

CCfgComboBox::~CCfgComboBox()
{
}


BEGIN_MESSAGE_MAP(CCfgComboBox, CComboBox)
	//{{AFX_MSG_MAP(CCfgComboBox)
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
	ON_WM_CTLCOLOR()
	ON_WM_PAINT()
	//ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAsComboBox message handlers

void CCfgComboBox::OnDropdown() 
{
	droppedDown_ = true;
}

void CCfgComboBox::OnCloseup() 
{
	droppedDown_ = false;
}

void CCfgComboBox::DrawComboButton(CDC* pDC, CRect clientRect)
{
	CSize bmpSize;
	CRect bmpRect;

	bmpSize = DrawUtils::GetBitmapSize(IDB_COMBO_BOX_NORMAL);

	bmpRect = clientRect;
	bmpRect.left = bmpRect.right - bmpSize.cx - 2;
	bmpRect.top = (bmpRect.Height() - bmpSize.cy) / 2;

	DrawUtils::DrawBitmap(pDC, IDB_COMBO_BOX_NORMAL, bmpRect.TopLeft(), DrawUtils::enBmp_AlignLeft);

	clientRect.DeflateRect(4,0,bmpRect.Width() + 2, 0);
	
	if (IsWindowEnabled())
		DrawUtils::DrawString(pDC, stringGetSelString(), clientRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, DIALOG_EDITBOX_FORE_COLOR, GetFont());
	else
		DrawUtils::DrawString(pDC, stringGetSelString(), clientRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, ::GetSysColor(COLOR_3DSHADOW), GetFont());
}

BOOL CCfgComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	BOOL ret;

	dwStyle |= (WS_CHILD | CBS_HASSTRINGS | CBS_NOINTEGRALHEIGHT | WS_VSCROLL);
	
	ret = CWnd::Create(L"ComboBox", L"Algostar Combo Box", dwStyle, rect, pParentWnd, nID, NULL);
	
	return ret;	
}


HBRUSH CCfgComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
	
	if (nCtlColor == CTLCOLOR_LISTBOX) {
		//CRect rc;

		//GetWindowRect(rc);
		
		//dropAreaHeight_ = GetItemHeight(0) * sortKeyToValueMap_.size();
		//pWnd->SetWindowPos(&CWnd::wndTop, 0, 0, rc.Width(), dropAreaHeight_, SWP_SHOWWINDOW | SWP_NOMOVE); 
		pDC->SetTextColor(DIALOG_EDITBOX_FORE_COLOR);
	}
	
	return hbr;
}

void CCfgComboBox::Clear()
{
	sortKeyToValueMap_.clear();
	valueToSortKeyMap_.clear();

	ResetContent();
}

void CCfgComboBox::AddItem(const cmString& s)
{
	AddItem(s, (long)sortKeyToValueMap_.size());
}

void CCfgComboBox::AddItem(const cmString& s, double sortKey)
{
	wchar_t buf[100];

	wsprintf(buf, L"%10.10f", sortKey);

	AddItem(s, buf);
}

void CCfgComboBox::AddItem(const cmString& s, int sortKey)
{
	wchar_t buf[100];

	wsprintf(buf, L"%010d", sortKey);

	AddItem(s, buf);
}

void CCfgComboBox::AddItem(const cmString& s, const cmString& sortKey)
{
	sortKeyToValueMap_[sortKey] = s;
	valueToSortKeyMap_[s] = sortKey;

	BuildList();
}

void CCfgComboBox::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rc;

	GetClientRect(rc);

	DrawBackground(&dc, rc);
	DrawComboButton(&dc, rc);
}

/*void CAsComboBox::OnSelchange() 
{
	RedrawWindow();	
}
*/
void CCfgComboBox::DrawBackground(CDC* pDC, CRect clientRect)
{
	// draw rect
	//
	CBrush h;
	
	if (IsWindowEnabled())
		h.CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
	else
		h.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));

	clientRect.DeflateRect(0, 0, 1, 1);
	
	pDC->FillRect(clientRect, &h);
	
	DrawUtils::DrawRect(pDC, clientRect, DIALOG_EDITBOX_BORDER_COLOR);
}

void CCfgComboBox::BuildList()
{
	CString selString;
	map<cmString, cmString>::iterator it;

	if (GetCurSel() != CB_ERR) {
		GetLBText(GetCurSel(), selString);
	}

	ResetContent();

	for (it = sortKeyToValueMap_.begin(); it != sortKeyToValueMap_.end(); it++) {
		AddString(it->second);
	}
}

void CCfgComboBox::SelectItem(double sortKey)
{
	wchar_t buf[100];

	wsprintf(buf, L"%10.10f", sortKey);

	SelectItem(buf);
}

void CCfgComboBox::SelectItem(int sortKey)
{
	wchar_t buf[100];

	wsprintf(buf, L"%010d", sortKey);

	SelectItem(buf);
}

void CCfgComboBox::SelectItem(const cmString& sortKey)
{
	map<cmString, cmString>::iterator it;

	it = sortKeyToValueMap_.find(sortKey);
	if (it != sortKeyToValueMap_.end())
		SelectString(-1, it->second);
}

bool CCfgComboBox::GetSelKey(double& sortKey)
{
	bool ret;
	cmString s;

	ret = GetSelKey(s);
	//sortKey = atof(s.c_str());
	sortKey = s;

	return ret;
}

bool CCfgComboBox::GetSelKey(int& sortKey)
{
	bool ret;
	cmString s;

	ret = GetSelKey(s);
	sortKey = s;

	return ret;
}

bool CCfgComboBox::GetSelKey(cmString& sortKey)
{
	if (GetCurSel() == CB_ERR)
		return false;

	cmString tmp;
	map<cmString, cmString>::iterator it;

	GetLBText(GetCurSel(), tmp);
	
	it = valueToSortKeyMap_.find(tmp);
	if (it != valueToSortKeyMap_.end()) {
		sortKey = it->second;	
		return true;
	}
	
	return false;
}

cmString CCfgComboBox::stringGetSelString()
{
	if (GetCurSel() == CB_ERR)
		return _T("");

	cmString tmp;

	GetLBText(GetCurSel(), tmp);
	
	return tmp;
}

void CCfgComboBox::Draw(CDC* pDC, CRect clientRect)
{
	DrawBackground(pDC, clientRect);
	DrawComboButton(pDC, clientRect);
}
