// MainDialogSettings.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "MainDialogSettings.h"
#include "enums.h"
#include "XmlMainDialog.h"
#include "XmlProject.h"
#include "DrawUtils.h"
#include "ProjectIconsDlg.h"
#include "GuiDefs.h"
#include "MemDC.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings dialog


CMainDialogSettings::CMainDialogSettings(CWnd* pParent /*=NULL*/)
	: CBaseDialog(CMainDialogSettings::IDD, pParent),
	mainDialogSettings_(NULL),
	project_(NULL),
	initialized_(false),
	lastSelChild_(NULL)
{
	//{{AFX_DATA_INIT(CMainDialogSettings)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CMainDialogSettings::~CMainDialogSettings()
{

}

void CMainDialogSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDialogSettings)
	DDX_Control(pDX, IDOK, btnOk_);
	DDX_Control(pDX, IDCANCEL, btnCancel_);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMainDialogSettings, CBaseDialog)
	//{{AFX_MSG_MAP(CMainDialogSettings)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTabProperties)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings message handlers

BOOL CMainDialogSettings::OnInitDialog() 
{
	CBaseDialog::OnInitDialog();
	
		font_.CreateFont(-11, 
					 0,										/* width */ 
					 0,										/* escapement */
					 0,										/* orientation */
					 FW_BOLD,		/* weight*/
					 0,										/* italic*/
					 0,										/* underline */
					 0,										/* strikeout */
					 ANSI_CHARSET,							/* charset */
					 OUT_DEFAULT_PRECIS,					/* out precision */
					 CLIP_DEFAULT_PRECIS,					/* clip precision */
					 DEFAULT_QUALITY,						/* quality */
					 DEFAULT_PITCH,							/* pitch family */
					 _T("tahoma"));	

	tab_.Create(WS_CHILD | WS_VISIBLE, CRect(0,0,0,0), this, IDC_TAB);
	tab_.SetFont(&font_);

	DWORD flags = ETC_FLAT | ETC_SELECTION | ETC_BACKTABS;

	tab_.SetBkgndColor(BACKGROUND_END_COLOR);
	tab_.EnableDraw(BTC_ALL);
	tab_.ModifyStyle(0, WS_CLIPCHILDREN);
	tab_.InsertItem(0, _T("General"));
	tab_.InsertItem(1, _T("Tree"));
	tab_.SetCurSel(0);
	tab_.ClearBackGroundBmp();

	propertyEditors_.push_back(&generalDlg_);
	propertyEditors_.push_back(&treeDlg_);

	lastSelChild_ = &generalDlg_;

	generalDlg_.Create(CMainDialogSettings_General::IDD, this);
	generalDlg_.SetData(project_);

	treeDlg_.Create(CMainDialogSettings_Tree::IDD, this);
	treeDlg_.SetData(project_->GetMainDialog()->GetTreeSettings());


/***

***/	
	
	initialized_ = true;

	UpdateWindowSize();

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

void CMainDialogSettings::SetMainDialogSettings(CXmlMainDialog* mainDialogSettings)
{
	mainDialogSettings_ = mainDialogSettings;
}

void CMainDialogSettings::OnOK() 
{
	generalDlg_.StoreData();
	treeDlg_.StoreData();

	CBaseDialog::OnOK();
}

/*void CMainDialogSettings::OnCmdBrowse() 
{
	CProjectIconsDlg dlg;
	int ret;

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

		Invalidate();
	}

}*/

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

void CMainDialogSettings::DrawBackground(CDC* pDC)
{
	CBaseDialog::DrawBackground(pDC);
	
	CRect rc;

	GetClientRect(rc);
	pDC->FillSolidRect(rc, DIALOG_BACK_COLOR);

/*	if (imageList_.GetImageCount()) {
		CRect rc;
		CRect txtRect;

		txtIcon_.GetWindowRect(txtRect);
		ScreenToClient(txtRect);

		rc.left = txtRect.left - 17; 
		rc.top = txtRect.top + 2;
		rc.right = rc.left + 16;
		rc.bottom = rc.top + 16;

		imageList_.Draw(pDC, 0, rc.TopLeft(), ILD_NORMAL);
	}*/
} 

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

void CMainDialogSettings::OnSize(UINT nType, int cx, int cy) 
{
	CBaseDialog::OnSize(nType, cx, cy);
	
	UpdateWindowSize();
}

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

	DrawBackground(memDC);
}

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

	CRect rc;
	CRect btnRect;
	CRect tabRect;

	GetClientRect(rc);
	btnCancel_.GetWindowRect(btnRect);
	ScreenToClient(btnRect);

	tabRect = rc;
	tabRect.DeflateRect(BORDER, BORDER, BORDER, 2 * BORDER + btnRect.Height());
	tab_.SetWindowPos(&CWnd::wndTop, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);

	btnCancel_.SetWindowPos(&CWnd::wndTop, rc.right - BORDER - btnRect.Width(), rc.bottom - BORDER - btnRect.Height(), 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	btnOk_.SetWindowPos(&CWnd::wndTop, rc.right - 2 * BORDER - 2 * btnRect.Width(), rc.bottom - BORDER - btnRect.Height(), 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);

	UpdateSelChild();
}

void CMainDialogSettings::UpdateSelChild()
{
	CRect tabRc;
	CRect tabItemRc;
	CRect childRc; 
	CPropertyEditorDlg* child;

	if (lastSelChild_)
		lastSelChild_->ShowWindow(SW_HIDE);

	if (propertyEditors_.size() == 0)
		return;

	tab_.GetWindowRect(tabRc);
	tab_.GetItemRect(tab_.GetCurSel(), tabItemRc);
	ScreenToClient(tabRc);

	childRc = tabRc;
	childRc.DeflateRect(5, tabItemRc.Height() + 5 , 5, 5);
	
	child = propertyEditors_[tab_.GetCurSel()];
	child->SetWindowPos(&CWnd::wndTop, childRc.left, childRc.top, childRc.Width(), childRc.Height(), SWP_SHOWWINDOW);
	
	lastSelChild_ = child;
}

void CMainDialogSettings::OnSelchangeTabProperties(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateSelChild();
	
	*pResult = 0;
}