// GenerationPrefDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ascfgbuilder.h"
#include "GenerationPrefDlg.h"
#include "GuiDefs.h"
#include "MemDC.h"
#include "DrawUtils.h"
#include "GenerationReportDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGenerationPrefDlg dialog


CGenerationPrefDlg::CGenerationPrefDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGenerationPrefDlg::IDD, pParent),
	initialized_(false),
	cmdDetails_(IDB_DETAILS_HIDE, IDB_DETAILS_HIDE, IDB_DETAILS_HIDE),
	expanded_(false)
{
	//{{AFX_DATA_INIT(CGenerationPrefDlg)
	//}}AFX_DATA_INIT
}


void CGenerationPrefDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGenerationPrefDlg)
	DDX_Control(pDX, IDC_CMD_DETAILS, cmdDetails_);
	DDX_Control(pDX, IDC_CBO_TEST_PROJECT, cboTestProject_);
	DDX_Control(pDX, IDC_CBO_CONFIGURATION, cboConfiguration_);
	DDX_Control(pDX, IDC_CBO_ACTION, cboAction_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGenerationPrefDlg, CDialog)
	//{{AFX_MSG_MAP(CGenerationPrefDlg)
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	ON_CBN_SELCHANGE(IDC_CBO_ACTION, OnSelchangeCboAction)
	ON_BN_CLICKED(IDC_CMD_DETAILS, OnCmdDetails)
	ON_CBN_SELCHANGE(IDC_CBO_CONFIGURATION, OnSelchangeCboConfiguration)
	ON_CBN_SELCHANGE(IDC_CBO_TEST_PROJECT, OnSelchangeCboTestProject)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CGenerationPrefDlg::AddAction(enGeneratorAction actionId)
{
	actionList_.push_back(actionId);
}

void CGenerationPrefDlg::AddConfiguration(enGeneratorConfig configId)
{
	configList_.push_back(configId);
}

void CGenerationPrefDlg::AddTestPrjAction(enTestProjectAction actionId)
{
	testProjectActionList_.push_back(actionId);
}

/////////////////////////////////////////////////////////////////////////////
// CGenerationPrefDlg message handlers

BOOL CGenerationPrefDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	int i;
	
	for (i = 0; i < actionList_.size(); i++)
		cboAction_.AddItem(GeneratorAction2String(actionList_[i]), actionList_[i]);
	cboAction_.SelectItem(actionId_);

	for (i = 0; i < configList_.size(); i++)
		cboConfiguration_.AddItem(GeneratorConfig2String(configList_[i]), configList_[i]);
	cboConfiguration_.SelectItem(configId_);

	for (i = 0; i < testProjectActionList_.size(); i++)
		cboTestProject_.AddItem(TestProjectAction2String(testProjectActionList_[i]), testProjectActionList_[i]);
	cboTestProject_.SelectItem(testProjectActionId_);

	initialized_ = true;
	
	imageList_.Create(16, 16, ILC_COLOR32 | ILC_MASK, 4, 4);
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_CPP));
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_DEVENV));
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_BUILD));
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_ITEM_SELECTION));

	cmdDetails_.AutoSize();

	UpdateWindowSize();

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

cmString CGenerationPrefDlg::GeneratorAction2String(enGeneratorAction actionId)
{
	cmString ret;

	switch (actionId) {
		case enGeneratorAction_None:
			ret = _T("Do not generate");
			break;

		case enGeneratorAction_GenerateOnly:
			ret = _T("Generate only");
			break;

		case enGeneratorAction_GenerateAndOpen:
			ret = _T("Generate code and open project");
			break;

		case enGeneratorAction_GenerateAndCompile:
			ret = _T("Generate and compile project");
			break;
	
		default:
			assert(false);

	}
	
	return ret;
}

cmString CGenerationPrefDlg::GeneratorConfig2String(enGeneratorConfig configId)
{
	cmString ret;

	switch (configId) {
		case enGeneratorConfig_All:			
			ret = _T("All");
			break;

		case enGeneratorConfig_Debug:
			ret = _T("Debug");
			break;

		case enGeneratorConfig_Release:
			ret = _T("Release");
			break;

		case enGeneratorConfig_UnicodeDebug:
			ret = _T("Debug Unicode");
			break;

		case enGeneratorConfig_UnicodeRelease:
			ret = _T("Release Unicode");
			break;

		case enGeneratorConfig_ReleaseMinDependency:
			ret = _T("Release Min Dependency");
			break;

		case enGeneratorConfig_ReleaseMinSize:
			ret = _T("Release Min Size");
			break;

		case enGeneratorConfig_UnicodeReleaseMinDependency:
			ret = _T("Release Unicode Min Dependency");
			break;

		case enGeneratorConfig_UnicodeReleaseMinSize:
			ret = _T("Release Unicode Min Size");
			break;

		default:
			assert(false);
	}

	return ret;
}

cmString CGenerationPrefDlg::TestProjectAction2String(enTestProjectAction actionId)
{
	cmString ret;

	switch(actionId) {
		case enTestProjectAction_None:
			ret = _T("Do not generate");
			break;

		case enTestProjectAction_GenerateOnly:
			ret = _T("Generate only");
			break;

		case enTestProjectAction_GenerateAndOpen:
			ret = _T("Generate and open project (Only debug is currently supported)");
			break;

		default:
			assert(false);
	}

	return ret;
}

enGeneratorAction CGenerationPrefDlg::GetActionId()
{
	enGeneratorAction actionId;

	cboAction_.GetSelKey((int&)actionId);

	return actionId;
}

enGeneratorConfig CGenerationPrefDlg::GetConfigId()
{
	enGeneratorConfig configId;

	cboConfiguration_.GetSelKey((int&)configId);

	return configId;
}

enTestProjectAction CGenerationPrefDlg::GetTestProjectActionId()
{
	enTestProjectAction actionId;

	cboTestProject_.GetSelKey((int&)actionId);

	return actionId;
}

void CGenerationPrefDlg::SetActionId(enGeneratorAction actionId)
{
	actionId_ = actionId;
}

void CGenerationPrefDlg::SetConfigId(enGeneratorConfig configId)
{
	configId_ = configId;
}

void CGenerationPrefDlg::SetTestProjectActionId(enTestProjectAction actionId)
{
	testProjectActionId_ = actionId;
}

void CGenerationPrefDlg::SetTitle(const cmString& title)
{
	title_ = title;
}

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

void CGenerationPrefDlg::UpdateWindowSize()
{
	if (!initialized_)
		return;

	CRect rc;
	CRect cboRect;
	int cboLeft = 120;

	GetClientRect(rc);
	
	cboAction_.GetWindowRect(cboRect);
	ScreenToClient(cboRect);
	cboAction_.SetWindowPos(&CWnd::wndTop, cboLeft, cboRect.top, rc.Width() - BORDER - cboLeft, cboRect.Height(), SWP_SHOWWINDOW);

	cboConfiguration_.GetWindowRect(cboRect);
	ScreenToClient(cboRect);
	cboConfiguration_.SetWindowPos(&CWnd::wndTop, cboLeft, cboRect.top, rc.Width() - BORDER - cboLeft, cboRect.Height(), SWP_SHOWWINDOW);

	cboTestProject_.GetWindowRect(cboRect);
	ScreenToClient(cboRect);
	cboTestProject_.SetWindowPos(&CWnd::wndTop, cboLeft, cboRect.top, rc.Width() - BORDER - cboLeft, cboRect.Height(), SWP_SHOWWINDOW);

	cmdDetails_.SetWindowPos(&CWnd::wndTop, rc.right - 18, rc.top + 2, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);

	if (expanded_)
		rc.bottom = cboRect.bottom + BORDER;
	else
		rc.bottom = 20;
	
	SetWindowPos(&CWnd::wndTop, 0,0, rc.Width(), rc.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);
	RedrawWindow();
}

void CGenerationPrefDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CMemDC memDC(&dc);
	CRect rc;
	CRect titleRect;
	CBrush b(DIALOG_BACK_COLOR);
	GetClientRect(rc);
	int iconLeft;

	memDC.FillRect(rc, &b);
	rc.DeflateRect(0, 0, 1, 1);
	DrawUtils::DrawRect(memDC, rc, RGB(0x56, 0x6d, 0xD9));

	titleRect = rc;
	titleRect.bottom = titleRect.top + 20;
	titleRect.DeflateRect(BORDER, 0, BORDER, 0);
	DrawUtils::DrawString(memDC, title_, titleRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, _T("tahoma"), -12, RGB(0x1C, 0x32, 0x9D), true);

	// action, configuration, test project
	CRect tmp;

	cboAction_.GetWindowRect(tmp);
	ScreenToClient(tmp);
	iconLeft = tmp.left;
	tmp.left = BORDER;
	DrawUtils::DrawString(memDC, _T("Action :"), tmp, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, _T("tahoma"), -11, RGB(0x1C, 0x32, 0x9D), false);

	cboConfiguration_.GetWindowRect(tmp);
	ScreenToClient(tmp);
	tmp.left = BORDER;
	DrawUtils::DrawString(memDC, _T("Configuration :"), tmp, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, _T("tahoma"), -11, RGB(0x1C, 0x32, 0x9D), false);

	cboTestProject_.GetWindowRect(tmp);
	ScreenToClient(tmp);
	tmp.left = BORDER;
	DrawUtils::DrawString(memDC, _T("Test Project :"), tmp, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX, _T("tahoma"), -11, RGB(0x1C, 0x32, 0x9D), false);

	// draw icons
	//
	if (GetActionId() == enGeneratorAction_GenerateOnly)
		imageList_.Draw(memDC, 0, CPoint(iconLeft, 2), ILD_NORMAL);
	
	if (GetActionId() == enGeneratorAction_GenerateAndOpen) {
		imageList_.Draw(memDC, 0, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 3, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 1, CPoint(iconLeft, 2), ILD_NORMAL);
	}

	if (GetActionId() == enGeneratorAction_GenerateAndCompile) {
		imageList_.Draw(memDC, 0, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 3, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 1, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 3, CPoint(iconLeft, 2), ILD_NORMAL);
		iconLeft += 16;
		imageList_.Draw(memDC, 2, CPoint(iconLeft, 2), ILD_NORMAL);
	}
}

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

HBRUSH CGenerationPrefDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	pDC->SetBkMode(TRANSPARENT);
	
	return hbr;
}

void CGenerationPrefDlg::OnSelchangeCboAction() 
{
	RedrawWindow();	

	NotifyChange();
}

void CGenerationPrefDlg::OnCmdDetails() 
{
	expanded_ = !expanded_;

	if (expanded_)
		cmdDetails_.SetBitmaps(IDB_DETAILS_HIDE, IDB_DETAILS_HIDE, IDB_DETAILS_HIDE);
	else
		cmdDetails_.SetBitmaps(IDB_DETAILS_SHOW, IDB_DETAILS_SHOW, IDB_DETAILS_SHOW);
	cmdDetails_.RedrawWindow();

	UpdateWindowSize();

	NotifyChange();
	
	RedrawWindow();
	cboAction_.RedrawWindow();
	cboConfiguration_.RedrawWindow();
	cboTestProject_.RedrawWindow();
}

void CGenerationPrefDlg::NotifyChange()
{
	CGenerationReportDlg* dlg;

	dlg = (CGenerationReportDlg*)GetParent();

	dlg->OnPrefChildChanged();
}

void CGenerationPrefDlg::OnSelchangeCboConfiguration() 
{
	NotifyChange();
}

void CGenerationPrefDlg::OnSelchangeCboTestProject() 
{
	NotifyChange();
}

void CGenerationPrefDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CDialog::OnLButtonDown(nFlags, point);
}

void CGenerationPrefDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	CRect rc;
	
	GetClientRect(rc);
	rc.bottom = 20;

	if (rc.PtInRect(point))
		OnCmdDetails();
	
	CDialog::OnLButtonUp(nFlags, point);
}
