// ClassInfoDlg_Gui.cpp : implementation file
//

#include "stdafx.h"
#include "XmlClassInfo.h"
#include "ClassInfoDlg_Gui.h"
#include "Enums.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_Gui dialog


CClassInfoDlg_Gui::CClassInfoDlg_Gui(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CClassInfoDlg_Gui::IDD, pParent),
	lastSelectedChild_(NULL),
	classInfo_(NULL),
	initialized_(false)
{
	//{{AFX_DATA_INIT(CClassInfoDlg_Gui)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CClassInfoDlg_Gui::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassInfoDlg_Gui)
	DDX_Control(pDX, IDC_CBO_CLASS_RENDERING_MODE, cboRenderingMode_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClassInfoDlg_Gui, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CClassInfoDlg_Gui)
	ON_CBN_SELCHANGE(IDC_CBO_CLASS_RENDERING_MODE, OnSelChangeCboClassRenderingMode)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassInfoDlg_Gui message handlers

BOOL CClassInfoDlg_Gui::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	guiNoneDlg_.Create(IDD_CLASSINFO_GUI_NONE, this);
	guiFlatListDlg_.Create(IDD_CLASSINFO_GUI_FLATLIST, this);	
	guiCatListDlg_.Create(IDD_CLASSINFO_GUI_CATLIST, this);
	guiGridDlg_.Create(IDD_CLASSINFO_GUI_GRID, this);
	guiCustomFormDlg_.Create(IDD_CLASSINFO_GUI_CUSTOMFORM, this);

	UpdateChildSize(guiNoneDlg_);
	UpdateChildSize(guiFlatListDlg_);
	UpdateChildSize(guiCatListDlg_);
	UpdateChildSize(guiGridDlg_);
	UpdateChildSize(guiCustomFormDlg_);

	// initialize combo box
	//
	COMBOBOXEXITEM item;

	memset(&item, 0, sizeof(COMBOBOXEXITEM));
	
	item.mask = CBEIF_TEXT | CBEIF_LPARAM;
	item.pszText = _T("None");
	item.lParam = (DWORD)enRenderingMode_None;
	item.iItem = 0;
	cboRenderingMode_.InsertItem(&item);
		
	item.pszText = _T("Flat List");
	item.lParam = (DWORD)enRenderingMode_FlatList;
	item.iItem = 1;
	cboRenderingMode_.InsertItem(&item);

	item.pszText = _T("Category List");
	item.lParam = (DWORD)enRenderingMode_CategoryList;
	item.iItem = 2;
	cboRenderingMode_.InsertItem(&item);

	item.pszText = _T("Grid");
	item.lParam = (DWORD)enRenderingMode_Grid;
	item.iItem = 3;
	cboRenderingMode_.InsertItem(&item);

	item.pszText = _T("Custom Dialog");
	item.lParam = (DWORD)enRenderingMode_CustomDialog;
	item.iItem = 4;
	cboRenderingMode_.InsertItem(&item);

	initialized_ = true;

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

void CClassInfoDlg_Gui::UpdateChildSize(CWnd& child)
{
	CRect clientRect;
	CRect cboRect;
	CRect childRect;

	GetClientRect(clientRect);
	
	cboRenderingMode_.GetWindowRect(cboRect);
	ScreenToClient(cboRect);

	childRect.left = 0;
	childRect.right = clientRect.Width();
	childRect.top = cboRect.bottom + 5;
	childRect.bottom = clientRect.bottom;

	child.SetWindowPos(	&CWnd::wndTop, 
						childRect.left, 
						childRect.top, 
						childRect.Width(),
						childRect.Height(),
						SWP_NOREDRAW);
}

void CClassInfoDlg_Gui::OnSelChangeCboClassRenderingMode() 
{
	CWnd* selChild;
	enRenderingMode renderingMode;
	enRenderingMode previousRenderingMode;

	renderingMode = (enRenderingMode)cboRenderingMode_.GetItemData(cboRenderingMode_.GetCurSel());
	previousRenderingMode = classInfo_->GetRenderingMode();

	switch(renderingMode) {
		case enRenderingMode_None:
			selChild = &guiNoneDlg_;
			break;

		case enRenderingMode_FlatList:
			selChild = &guiFlatListDlg_;
			break;

		case enRenderingMode_CategoryList:
			selChild = &guiCatListDlg_;
			break;

		case enRenderingMode_Grid:
			if (classInfo_->GetCardinalityInfo()->GetCardinality() != enCardinality_Array) {
				int ret;
				
				ret = MessageBox(_T("Grid Mode requires a cardinality set to \"Array\"."), _T("Warning"), MB_ICONWARNING | MB_OKCANCEL);
				if (ret != IDOK) {
					// restore previous rendering mode on combo box
					//
					for (int i = 0; i < cboRenderingMode_.GetCount(); i++) {
						if (cboRenderingMode_.GetItemData(i) == classInfo_->GetRenderingMode()) {
							cboRenderingMode_.SetCurSel(i);
							OnSelChangeCboClassRenderingMode();
							break;
						}
					}
					return;
				}

				classInfo_->GetCardinalityInfo()->SetCardinality(enCardinality_Array);
				UpdateAllViews(enUpdateViewReason_ItemChanged);
			}		
			selChild = &guiGridDlg_;
			break;

		case enRenderingMode_CustomDialog:
			selChild = &guiCustomFormDlg_;
			break;
	}

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

	selChild->ShowWindow(SW_SHOW);
	lastSelectedChild_ = selChild;
}

void CClassInfoDlg_Gui::StoreData()
{
	enRenderingMode renderingMode;
	int curSel;

	curSel = cboRenderingMode_.GetCurSel();
	if (curSel == CB_ERR)
		renderingMode = enRenderingMode_None;
	else 
		renderingMode = (enRenderingMode)cboRenderingMode_.GetItemData(curSel);

	guiNoneDlg_.StoreData();
	guiFlatListDlg_.StoreData();
	guiCatListDlg_.StoreData();
	guiGridDlg_.StoreData();
	guiCustomFormDlg_.StoreData();

	classInfo_->SetRenderingMode(renderingMode);
}

void CClassInfoDlg_Gui::SetData(CXmlBaseElement* element)
{
	classInfo_ = dynamic_cast<CXmlClassInfo*>(element);
	assert(classInfo_);
	if (classInfo_ == NULL)
		return;

	guiNoneDlg_.SetData(element);
	guiFlatListDlg_.SetData(element);
	guiCatListDlg_.SetData(element);
	guiGridDlg_.SetData(element);
	guiCustomFormDlg_.SetData(element);

	// select rendering mode in combo box
	//
	for (int i = 0; i < cboRenderingMode_.GetCount(); i++) {
		if (cboRenderingMode_.GetItemData(i) == classInfo_->GetRenderingMode()) {
			cboRenderingMode_.SetCurSel(i);
			OnSelChangeCboClassRenderingMode();
			break;
		}
	}
}

void CClassInfoDlg_Gui::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if (initialized_) {
		UpdateChildSize(guiNoneDlg_);
		UpdateChildSize(guiFlatListDlg_);
		UpdateChildSize(guiCatListDlg_);
		UpdateChildSize(guiGridDlg_);
		UpdateChildSize(guiCustomFormDlg_);	
	
	}
	
}

void CClassInfoDlg_Gui::OnOK()
{}

void CClassInfoDlg_Gui::OnCancel()
{}

void CClassInfoDlg_Gui::OnDataUpdate()
{
	guiNoneDlg_.OnDataUpdate();
	guiFlatListDlg_.OnDataUpdate();
	guiCatListDlg_.OnDataUpdate();
	guiGridDlg_.OnDataUpdate();
	guiCustomFormDlg_.OnDataUpdate();
	
}






