// MemberInfoDlg_DataFormat.cpp : implementation file
//

#include "stdafx.h"
#include "XmlMemberInfo.h"
#include "memberinfodlg_dataformat.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_DataFormat dialog


CMemberInfoDlg_DataFormat::CMemberInfoDlg_DataFormat(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CMemberInfoDlg_DataFormat::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMemberInfoDlg_DataFormat)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

void CMemberInfoDlg_DataFormat::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemberInfoDlg_DataFormat)
	DDX_Control(pDX, IDC_LBL_DECIMAL_PLACES, lblDecimalPlaces_);
	DDX_Control(pDX, IDC_LBL_DIGIT_NUMBER, lblDigitNumber_);
	DDX_Control(pDX, IDC_LBL_MAXCHAR, lblMaxChar_);
	DDX_Control(pDX, IDC_SPIN_MAX_CHAR, spinMaxChar_);
	DDX_Control(pDX, IDC_SPIN_DIGIT_NUMBER, spinDigitNumber_);
	DDX_Control(pDX, IDC_SPIN_DECIMAL_PLACES, spinDecimalPlaces_);
	DDX_Control(pDX, IDC_TXT_MAX_CHAR, txtMaxChar_);
	DDX_Control(pDX, IDC_TXT_DIGIT_NUMBER, txtDigitNumber_);
	DDX_Control(pDX, IDC_TXT_DECIMAL_PLACES, txtDecimalPlaces_);
	DDX_Control(pDX, IDC_CBO_DATATYPE, cboDataType_);
	DDX_Control(pDX, IDC_CHK_MULTILINE, chkMultilineString_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMemberInfoDlg_DataFormat, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CMemberInfoDlg_DataFormat)
	ON_CBN_SELCHANGE(IDC_CBO_DATATYPE, OnSelChangeCboDatatype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_DataFormat message handlers
void CMemberInfoDlg_DataFormat::StoreData()
{
	CString tmp;

	txtDecimalPlaces_.GetWindowText(tmp);
	memberInfo_->SetDecimalPlaces(_wtoi((const TCHAR*)tmp));

	txtDigitNumber_.GetWindowText(tmp);
	memberInfo_->SetDigitPlaces(_wtoi((const TCHAR*)tmp));

	txtMaxChar_.GetWindowText(tmp);
	memberInfo_->SetMaxChar(_wtoi((const TCHAR*)tmp));

	memberInfo_->SetDataType((enDataType)cboDataType_.GetItemData(cboDataType_.GetCurSel()));
}

void CMemberInfoDlg_DataFormat::SetData(CXmlBaseElement* element)
{
	CString tmp;

	memberInfo_ = dynamic_cast<CXmlMemberInfo*>(element);
	
	if (memberInfo_) {
		tmp.Format(_T("%d"), memberInfo_->GetMaxChar());
		txtMaxChar_.SetWindowText(tmp);

		tmp.Format(_T("%d"), memberInfo_->GetDecimalPlaces());
		txtDecimalPlaces_.SetWindowText(tmp);

		tmp.Format(_T("%d"), memberInfo_->GetDigitPlaces());
		txtDigitNumber_.SetWindowText(tmp);

		cboDataType_.SelectString(-1, CXmlBaseElement::DataType2String(memberInfo_->GetDataType()));
		
		UpdateFields(memberInfo_->GetDataType());
	} else {
		txtMaxChar_.SetWindowText(_T(""));
		txtDecimalPlaces_.SetWindowText(_T(""));
		txtDigitNumber_.SetWindowText(_T(""));
		cboDataType_.SetCurSel(CB_ERR);
	}
}

void CMemberInfoDlg_DataFormat::OnSelChangeCboDatatype() 
{
	int curSel;
	enDataType selDataType;

	curSel = cboDataType_.GetCurSel();
	selDataType = (enDataType)cboDataType_.GetItemData(curSel);
	
	UpdateFields(selDataType);

	StoreData();
	UpdateAllViews(enUpdateViewReason_ItemChanged);
}

void CMemberInfoDlg_DataFormat::UpdateFields(enDataType dataType)
{
	bool maxCharEnabled = false;
	bool digitNumberEnabled = false;
	bool decimalPlacesEnabled = false;
	bool multilineStringEnabled = false;

	switch(dataType) {
		case enDataType_Long:
			digitNumberEnabled = true;			
			break;

		case enDataType_Double:
			digitNumberEnabled = true;
			decimalPlacesEnabled = true;
			break;

		case enDataType_String:
			maxCharEnabled = true;
			multilineStringEnabled = true;
			break;

		case enDataType_Date:
			// data format not supported
			//
			break;

		case enDataType_Boolean:
			break;
	}

	txtMaxChar_.EnableWindow(maxCharEnabled);
	lblMaxChar_.EnableWindow(maxCharEnabled);

	txtDecimalPlaces_.EnableWindow(decimalPlacesEnabled);
	lblDecimalPlaces_.EnableWindow(decimalPlacesEnabled);

	txtDigitNumber_.EnableWindow(digitNumberEnabled);
	lblDigitNumber_.EnableWindow(digitNumberEnabled);

	chkMultilineString_.EnableWindow(multilineStringEnabled);
} 

BOOL CMemberInfoDlg_DataFormat::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	int insertionPoint;

	insertionPoint = cboDataType_.AddString(CXmlBaseElement::DataType2String(enDataType_String));
	cboDataType_.SetItemData(insertionPoint, (DWORD) enDataType_String);

	insertionPoint = cboDataType_.AddString(CXmlBaseElement::DataType2String(enDataType_Long));
	cboDataType_.SetItemData(insertionPoint, (DWORD) enDataType_Long);

	insertionPoint = cboDataType_.AddString(CXmlBaseElement::DataType2String(enDataType_Double));
	cboDataType_.SetItemData(insertionPoint, (DWORD) enDataType_Double);

	insertionPoint = cboDataType_.AddString(CXmlBaseElement::DataType2String(enDataType_Boolean));
	cboDataType_.SetItemData(insertionPoint, (DWORD) enDataType_Boolean);

	UpdateWindowSize();
	
	spinDecimalPlaces_.SetRange(0, 20);
	spinDigitNumber_.SetRange(1, 20);
	spinMaxChar_.SetRange(1, 2048);

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

void CMemberInfoDlg_DataFormat::UpdateWindowSize()
{
	
}

void CMemberInfoDlg_DataFormat::OnDataUpdate()
{
	
}