// MainDialogSettings_General.cpp : implementation file
//

#include "stdafx.h"
#include "ascfgbuilder.h"
#include "MainDialogSettings_General.h"
#include "MemDC.h"
#include "GuiDefs.h"
#include "DrawUtils.h"
#include "ProjectIconsDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings_General dialog


CMainDialogSettings_General::CMainDialogSettings_General(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CMainDialogSettings_General::IDD, pParent),
	settings_(NULL),
	cmdBrowse_(IDB_CMD_BROWSE, IDB_CMD_BROWSE, IDB_CMD_BROWSE),
	initialized_(false)
{
	//{{AFX_DATA_INIT(CMainDialogSettings_General)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMainDialogSettings_General::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDialogSettings_General)
		DDX_Control(pDX, IDC_CMD_BROWSE, cmdBrowse_);
		DDX_Control(pDX, IDC_TXT_ICON, txtIcon_);
		DDX_Control(pDX, IDC_TXT_CAPTION, txtCaption_);
		DDX_Control(pDX, IDC_CBO_INIT_SIZE, cboInitialSize_);
		DDX_Control(pDX, IDC_CBO_SPLITTER_POS, cboSplitterPos_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMainDialogSettings_General, CDialog)
	//{{AFX_MSG_MAP(CMainDialogSettings_General)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_CMD_BROWSE, OnCmdBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings_General message handlers

void CMainDialogSettings_General::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CMemDC memDC(&dc);
	CRect rc;

	GetClientRect(rc);
	memDC.FillSolidRect(rc, DIALOG_BACK_COLOR);
}

void CMainDialogSettings_General::SetData(CXmlBaseElement* element)
{
	project_ = dynamic_cast<CXmlProject*>(element);
	assert(project_);
	if (project_)
		settings_ = project_->GetMainDialog();

	cboInitialSize_.AddItem(_T("640x480"), enMainDlgSize_Small);
	cboInitialSize_.AddItem(_T("800x600"), enMainDlgSize_Medium);
	cboInitialSize_.AddItem(_T("1024x768"), enMainDlgSize_Large);
	cboInitialSize_.SelectItem(settings_->GetInitialSize());
		
	cboSplitterPos_.AddItem(_T("10%"), 10);
	cboSplitterPos_.AddItem(_T("20%"), 20);
	cboSplitterPos_.AddItem(_T("30%"), 30);
	cboSplitterPos_.AddItem(_T("40%"), 40);
	cboSplitterPos_.AddItem(_T("50%"), 50);
	cboSplitterPos_.AddItem(_T("60%"), 60);
	cboSplitterPos_.AddItem(_T("70%"), 70);
	cboSplitterPos_.AddItem(_T("80%"), 80);
	cboSplitterPos_.AddItem(_T("90%"), 90);
	cboSplitterPos_.SelectItem(settings_->GetSplitterPos());

	txtIcon_.SetWindowText(settings_->GetIconFileName());
	txtCaption_.SetWindowText(settings_->GetCaption());

	CRect txtRect;
	CSize bmpSize;

	txtIcon_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);
	bmpSize = DrawUtils::GetBitmapSize(IDB_CMD_BROWSE);

	cmdBrowse_.SetWindowPos(&CWnd::wndTop, txtRect.right + 1, txtRect.top, bmpSize.cx, bmpSize.cy, SWP_SHOWWINDOW);

	imageList_.Create(16, 16, ILC_COLOR32 | ILC_MASK, 4, 4);
	
	LoadIcon();

}

void CMainDialogSettings_General::StoreData()
{
	int selKey;
	cmString tmp;

	cboInitialSize_.GetSelKey(selKey);
	settings_->SetInitialSize((enMainDlgSize)selKey);

	cboSplitterPos_.GetSelKey(selKey);
	settings_->SetSplitterPos(selKey);

	txtIcon_.GetWindowText(tmp);
	settings_->SetIconFileName(tmp);

	txtCaption_.GetWindowText(tmp);
	settings_->SetCaption(tmp);
}

void CMainDialogSettings_General::OnDataUpdate()
{

}

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

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

BOOL CMainDialogSettings_General::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	initialized_ = true;

	UpdateWindowSize();

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

void CMainDialogSettings_General::LoadIcon()
{
	HICON hIcon = NULL; 
	CXmlIcon* icon;

	icon = project_->GetProjectIcons()->GetIcon(settings_->GetIconFileName());
	if (icon) {
		hIcon = icon->GetIconHandle16();
		txtIcon_.SetWindowText(icon->GetFileName());
	}

	imageList_.Remove(0);
	if (hIcon)
		imageList_.Add(hIcon);
}

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


}

void CMainDialogSettings_General::OnCmdBrowse() 
{
	CProjectIconsDlg dlg;
	int ret;

	dlg.SetProject(project_);
	dlg.SetSelectedIcon(settings_->GetIconFileName());
	ret = dlg.DoModal();
	if (ret == IDOK) {
		cmString iconFileName;
		
		iconFileName = dlg.GetSelectedIcon();
		settings_->SetIconFileName(iconFileName);
				
		LoadIcon();

		Invalidate();
	}
}
