// MainDialogSettings_Tree.cpp : implementation file
//

#include "stdafx.h"
#include "ascfgbuilder.h"
#include "MainDialogSettings_Tree.h"
#include "XmlMainDialogTree.h"
#include "GuiDefs.h"
#include "MemDC.h"
#include <vector>

using namespace std;

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

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings_Tree dialog


CMainDialogSettings_Tree::CMainDialogSettings_Tree(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CMainDialogSettings_Tree::IDD, pParent),
	settings_(NULL),
	initialized_(false)

{
	//{{AFX_DATA_INIT(CMainDialogSettings_Tree)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMainDialogSettings_Tree::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDialogSettings_Tree)
	DDX_Control(pDX, IDC_TREE_PREVIEW, treePreview_);
	DDX_Control(pDX, IDC_LBL_PREVIEW, lblPreview_);
	DDX_Control(pDX, IDC_LBL_INDENT, lblIndent_);
	DDX_Control(pDX, IDC_INDENT, txtIndent_);
	DDX_Control(pDX, IDC_CHK_SINGLE_NODE_EXPAND, chkSingleNodeExpand_);
	DDX_Control(pDX, IDC_CHK_SELECTION_ALWAYS, chkShowSelectionAlways_);
	DDX_Control(pDX, IDC_CHK_HIDE_ROOT, chkHideRoot_);
	DDX_Control(pDX, IDC_CHK_HAS_LINES, chkHasLines_);
	DDX_Control(pDX, IDC_CHK_HAS_BUTTONS, chkHasButtons_);
	DDX_Control(pDX, IDC_CHK_FULL_ROW_SELECT, chkFullRowSelect_);
	DDX_Control(pDX, IDC_CHK_LINES_AT_ROOT, chkLinesAtRoot_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMainDialogSettings_Tree, CDialog)
	//{{AFX_MSG_MAP(CMainDialogSettings_Tree)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_CHK_SINGLE_NODE_EXPAND, OnChkSingleNodeExpand)
	ON_BN_CLICKED(IDC_CHK_SELECTION_ALWAYS, OnChkSelectionAlways)
	ON_BN_CLICKED(IDC_CHK_LINES_AT_ROOT, OnChkLinesAtRoot)
	ON_BN_CLICKED(IDC_CHK_HIDE_ROOT, OnChkHideRoot)
	ON_BN_CLICKED(IDC_CHK_HAS_LINES, OnChkHasLines)
	ON_BN_CLICKED(IDC_CHK_HAS_BUTTONS, OnChkHasButtons)
	ON_BN_CLICKED(IDC_CHK_FULL_ROW_SELECT, OnChkFullRowSelect)
	ON_EN_CHANGE(IDC_INDENT, OnChangeIndent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDialogSettings_Tree message handlers
void CMainDialogSettings_Tree::SetData(CXmlBaseElement* element)
{
	settings_ = dynamic_cast<CXmlMainDialogTree*>(element);
	assert(settings_);

	if(settings_) {
		chkFullRowSelect_.SetCheck(settings_->GetFullRowSelect());
		chkHasButtons_.SetCheck(settings_->GetHasButtons());
		chkHasLines_.SetCheck(settings_->GetHasLines());
		chkHideRoot_.SetCheck(settings_->GetHideRootNode());
		chkShowSelectionAlways_.SetCheck(settings_->GetShowSelectionAlways());
		chkSingleNodeExpand_.SetCheck(settings_->GetSingleNodeExpand());
		chkLinesAtRoot_.SetCheck(settings_->GetLinesAtRoot());

		cmString tmp;

		tmp.Format(_T("%d"), settings_->GetIndent());
		txtIndent_.SetWindowText(tmp);
	}

	BuildPreview();
	UpdatePreview();
}

void CMainDialogSettings_Tree::StoreData()
{
	settings_->SetFullRowSelect(chkFullRowSelect_.GetCheck() == TRUE);
	settings_->SetHasButtons(chkHasButtons_.GetCheck() == TRUE);
	settings_->SetHasLines(chkHasLines_.GetCheck() == TRUE);
	settings_->SetHideRootNode(chkHideRoot_.GetCheck() == TRUE);
	settings_->SetShowSelectionAlways(chkShowSelectionAlways_.GetCheck() == TRUE);
	settings_->SetSingleNodeExpand(chkSingleNodeExpand_.GetCheck() == TRUE);
	settings_->SetLinesAtRoot(chkLinesAtRoot_.GetCheck() == TRUE);

	cmString tmp;

	txtIndent_.GetWindowText(tmp);
	settings_->SetIndent(tmp);
}

void CMainDialogSettings_Tree::OnDataUpdate()
{

}

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

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

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

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

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

	vector<CWnd*> wndList;
	CRect rc;
	CRect chkRect;
	CRect lblRect;
	int top;
	int maxWidth = 0;

	GetClientRect(rc);

	wndList.push_back(&chkHideRoot_);
	wndList.push_back(&chkSingleNodeExpand_);
	wndList.push_back(&chkShowSelectionAlways_);
	wndList.push_back(&chkHasLines_);
	wndList.push_back(&chkHasButtons_);
	wndList.push_back(&chkFullRowSelect_);
	wndList.push_back(&chkLinesAtRoot_);

	top = BORDER;
	for (int i = 0; i < wndList.size(); i++) {
		wndList[i]->SetWindowPos(&CWnd::wndTop, BORDER, top, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
		wndList[i]->GetWindowRect(chkRect);

		maxWidth = max(maxWidth, chkRect.Width());

		top += chkRect.Height() + BORDER;
	}

	maxWidth += BORDER;

	lblIndent_.SetWindowPos(&CWnd::wndTop, BORDER, top, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	lblIndent_.GetWindowRect(lblRect);
	txtIndent_.SetWindowPos(&CWnd::wndTop, 2 * BORDER + lblRect.Width(), top,  0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);

	lblPreview_.SetWindowPos(&CWnd::wndTop, maxWidth, BORDER, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	lblPreview_.GetWindowRect(lblRect);

	treePreview_.SetWindowPos(&CWnd::wndTop, maxWidth, 2 * BORDER + lblRect.Height(), rc.Width() - BORDER - maxWidth, rc.Height() - 3 * BORDER - lblRect.Height(), SWP_SHOWWINDOW);
}

BOOL CMainDialogSettings_Tree::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	initialized_ = true;
	
	imageList_.Create(16, 16, ILC_COLOR32 | ILC_MASK, 4, 4);
	imageList_.Add(AfxGetApp()->LoadIcon(IDI_CLASS));
	treePreview_.SetImageList(&imageList_, TVSIL_NORMAL);
	
	UpdateWindowSize();

	BuildPreview();
	UpdatePreview();

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

void CMainDialogSettings_Tree::OnChkSingleNodeExpand() 
{
	UpdatePreview();
	
}

void CMainDialogSettings_Tree::OnChkSelectionAlways() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChkLinesAtRoot() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChkHideRoot() 
{
	BuildPreview();
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChkHasLines() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChkHasButtons() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChkFullRowSelect() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::OnChangeIndent() 
{
	UpdatePreview();
}

void CMainDialogSettings_Tree::UpdatePreview()
{
	DWORD stylesToAdd = 0;
	DWORD stylesToRemove = 0;
	cmString indent;
	
	if (chkFullRowSelect_.GetCheck())
		stylesToAdd |= TVS_FULLROWSELECT;
	else
		stylesToRemove |= TVS_FULLROWSELECT;

	if (chkHasButtons_.GetCheck())
		stylesToAdd |= TVS_HASBUTTONS;
	else
		stylesToRemove |= TVS_HASBUTTONS;

	if (chkHasLines_.GetCheck())
		stylesToAdd |= TVS_HASLINES;
	else
		stylesToRemove |= TVS_HASLINES;

	if (chkLinesAtRoot_.GetCheck())
		stylesToAdd |= TVS_LINESATROOT;
	else
		stylesToRemove |= TVS_LINESATROOT;

	if (chkShowSelectionAlways_.GetCheck())
		stylesToAdd |= TVS_SHOWSELALWAYS;
	else
		stylesToRemove |= TVS_SHOWSELALWAYS;

	if (chkSingleNodeExpand_.GetCheck())
		stylesToAdd |= TVS_SINGLEEXPAND;
	else
		stylesToRemove |= TVS_SINGLEEXPAND;

	treePreview_.ModifyStyle(stylesToRemove, stylesToAdd);

	txtIndent_.GetWindowText(indent);
	treePreview_.SetIndent((int)indent);
}

void CMainDialogSettings_Tree::BuildPreview()
{
	HTREEITEM root;
	HTREEITEM lev1;
	HTREEITEM lev2;
	cmString itemName;

	treePreview_.DeleteAllItems();

	if (chkHideRoot_.GetCheck())
		root = TVI_ROOT;
	else
		root = treePreview_.InsertItem(_T("Root"), 0, 0, TVI_ROOT);

	for (int i = 0; i < 4; i++) {
		itemName.Format(_T("Item %d"), i + 1);
		lev1 = treePreview_.InsertItem(itemName, 0, 0, root);
		for (int j = 0; j < 4; j++) {
			itemName.Format(_T("Item %d.%d"), i + 1, j + 1);
			lev2 = treePreview_.InsertItem(itemName, 0, 0, lev1);
			for (int k = 0; k < 4; k++) {
				itemName.Format(_T("Item %d.%d.%d"), i + 1, j + 1, k + 1);
				treePreview_.InsertItem(itemName, 0, 0, lev2);
			}
		} 
	}
}
