// ClassInfoDlg_TreeIcons_Chooser.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "ClassInfoDlg_TreeIcons_Chooser.h"
#include "DrawUtils.h"
#include "XmlProject.h"
#include "ProjectIconsDlg.h"
#include "MemDC.h"
#include "GuiDefs.h"

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

#define BORDER_COLOR				RGB(0x80, 0x80, 0x80)
#define BK_COLOR_TOP_DISABLED		::GetSysColor(COLOR_BTNFACE)
#define BK_COLOR_BOTTOM_DISABLED	::GetSysColor(COLOR_BTNFACE)
#define BK_COLOR_TOP_ENABLED		RGB(0xEC, 0xE8, 0xD5)
#define BK_COLOR_MID1_ENABLED		RGB(0xFF, 0xb5, 0x5C)
#define BK_COLOR_MID2_ENABLED		RGB(0xF1, 0x7B, 0x2C)
#define BK_COLOR_BOTTOM_ENABLED		RGB(0xFF, 0xFF, 0xFF)	



/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_TreeIcons_Chooser dialog


CClassInfoDlg_TreeIcons_Chooser::CClassInfoDlg_TreeIcons_Chooser(CWnd* pParent /*=NULL*/)
	: CDialog(CClassInfoDlg_TreeIcons_Chooser::IDD, pParent),
	enabled_(false),
	project_(NULL),
	initialized_(false),
	hIcon_(NULL)
{
	//{{AFX_DATA_INIT(CClassInfoDlg_TreeIcons_Chooser)
	//}}AFX_DATA_INIT
}

void CClassInfoDlg_TreeIcons_Chooser::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassInfoDlg_TreeIcons_Chooser)
	DDX_Control(pDX, IDC_CMD_CHOOSE, cmdChoose_);
	DDX_Control(pDX, IDC_CHK_ENABLE, chkEnable_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClassInfoDlg_TreeIcons_Chooser, CDialog)
	//{{AFX_MSG_MAP(CClassInfoDlg_TreeIcons_Chooser)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnable)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_CMD_CHOOSE, OnCmdChoose)
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_TreeIcons_Chooser message handlers

void CClassInfoDlg_TreeIcons_Chooser::OnPaint() 
{
	CPaintDC tmpDC(this); // device context for painting
	CMemDC memDC(&tmpDC);

	CRect rc;

	GetClientRect(rc);

	// draw border
	//
	CPen pen(PS_SOLID, 1, BORDER_COLOR);
	CPen* oldPen;

	oldPen = memDC.SelectObject(&pen);
	memDC.Rectangle(rc);
	memDC.SelectObject(&oldPen);

	// draw background
	//
	//CBrush b(enabled_ ? BK_COLOR_ENABLED : BK_COLOR_DISABLED);
	rc.DeflateRect(1,1,1,1);
	//memDC.FillRect(rc, &b);
	if (enabled_)
		//DrawUtils::DrawAppleGradient(&memDC, rc, BK_COLOR_TOP_ENABLED, BK_COLOR_MID1_ENABLED, BK_COLOR_MID2_ENABLED, BK_COLOR_BOTTOM_ENABLED);
		DrawUtils::DrawGradient(&memDC, rc, BK_COLOR_MID1_ENABLED, BK_COLOR_BOTTOM_ENABLED, 1);
	else
		DrawUtils::DrawGradient(&memDC, rc, DIALOG_BACK_COLOR, DIALOG_BACK_COLOR, 1);
	
	// write label
	//
	CRect chkRect;
		
	chkEnable_.GetWindowRect(chkRect);
	ScreenToClient(chkRect);
	lblRect_.left = chkRect.right + 5;
	lblRect_.right = rc.right - 5;
	lblRect_.top = chkRect.top;
	lblRect_.bottom = chkRect.bottom;

	DrawUtils::DrawString(&memDC, label_, lblRect_, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS  | DT_NOPREFIX, _T("tahoma"), -11);
	

	// draw icon preview
	//
	CImageList imageList;
	//HICON hIcon;
	CRect btnRect;
	CRect tmpRect;

	cmdChoose_.GetWindowRect(btnRect);
	ScreenToClient(btnRect);

	//hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(), iconFileName_, IMAGE_ICON, 16, 16, LR_LOADFROMFILE/* | LR_LOADTRANSPARENT*/);
	imageList.Create(16, 16, ILC_COLOR32  | ILC_MASK, 4, 10);
	imageList.Add(hIcon_);	
	imageList.Draw(&memDC, 0, CPoint(chkRect.right, btnRect.top), ILD_NORMAL | ILC_MASK);

	// Draw icon file name
	//
	tmpRect.left = chkRect.right + 30;
	tmpRect.top = btnRect.top;
	tmpRect.right = btnRect.left - 5;
	tmpRect.bottom = btnRect.bottom;

	DrawUtils::DrawString(&memDC, iconFileName_, tmpRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS  | DT_NOPREFIX, _T("tahoma"), -11);
}

void CClassInfoDlg_TreeIcons_Chooser::SetLabel(const cmString& label)
{
	label_ = label;

	if (initialized_)
		chkEnable_.SetWindowText(label);
}

cmString CClassInfoDlg_TreeIcons_Chooser::GetLabel() const
{
	return label_;
}

void CClassInfoDlg_TreeIcons_Chooser::SetIconFileName(const cmString& iconFileName)
{
	iconFileName_ = iconFileName;
}

cmString CClassInfoDlg_TreeIcons_Chooser::GetIconFileName() const
{
	return iconFileName_;
}

void CClassInfoDlg_TreeIcons_Chooser::SetEnabled(bool enabled)
{
	enabled_ = enabled;

	if (chkEnable_.GetSafeHwnd())
		chkEnable_.SetCheck(enabled_);

	UpdateControls();
}

bool CClassInfoDlg_TreeIcons_Chooser::GetEnabled() const
{
	return enabled_;
}

void CClassInfoDlg_TreeIcons_Chooser::OnChkEnable() 
{
	enabled_ = !enabled_;
	
	UpdateControls();

	Invalidate();
}

void CClassInfoDlg_TreeIcons_Chooser::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	UpdateWindowSize();	
}

void CClassInfoDlg_TreeIcons_Chooser::UpdateWindowSize()
{
	if (!GetSafeHwnd() || !cmdChoose_.GetSafeHwnd())
		return;

	CRect rc;
	CRect btnRect;

	GetClientRect(rc);
	cmdChoose_.GetClientRect(btnRect);

	cmdChoose_.SetWindowPos(&CWnd::wndTop, rc.right - btnRect.Width() - 5, rc.bottom - btnRect.Height() - 5, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
}

void CClassInfoDlg_TreeIcons_Chooser::UpdateControls()
{
	cmdChoose_.EnableWindow(enabled_);
}

BOOL CClassInfoDlg_TreeIcons_Chooser::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	UpdateControls();
	UpdateWindowSize();

	chkEnable_.SetWindowText(label_);

	initialized_ = true;

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CClassInfoDlg_TreeIcons_Chooser::OnCmdChoose() 
{
	CProjectIconsDlg dlg;
	int ret;

	dlg.SetProject(project_);
	dlg.SetSelectedIcon(iconFileName_);
	ret = dlg.DoModal();
	if (ret == IDOK) {
		CXmlIcon* icon;

		iconFileName_ = dlg.GetSelectedIcon();
		icon = project_->GetProjectIcons()->GetIcon(iconFileName_);
		if (icon) {
			SetEnabled(true);
			SetIconHandle(icon->GetIconHandle16());
			SetIconFileName(iconFileName_);
		} else {
			SetEnabled(false);
			SetIconHandle(NULL);
			SetIconFileName(_T(""));
		}

		Invalidate();
	}
}

void CClassInfoDlg_TreeIcons_Chooser::SetProject(CXmlProject* project)
{
	project_ = project;
}

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

HBRUSH CClassInfoDlg_TreeIcons_Chooser::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);//
	
	return hbr;
}

void CClassInfoDlg_TreeIcons_Chooser::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if (lblRect_.PtInRect(point)) {
		chkEnable_.SetCheck(!chkEnable_.GetCheck());
		OnChkEnable();
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CClassInfoDlg_TreeIcons_Chooser::SetIconHandle(HICON hIcon)
{
	hIcon_ = hIcon;
}

HICON CClassInfoDlg_TreeIcons_Chooser::GetIconHandle() const
{
	return hIcon_;
}

