// MemberInfoDlg_Global.cpp : implementation file
//

#include "stdafx.h"
#include "MemberInfoDlg_Global.h"
#include "XmlMemberInfo.h"
#include "ProjectItemView.h"
#include "ProjectDoc.h"
#include "GuiDefs.h"
#include "MemDC.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_Global dialog


CMemberInfoDlg_Global::CMemberInfoDlg_Global(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CMemberInfoDlg_Global::IDD, pParent),
	memberInfo_(NULL),
	initialized_(false)
{
	//{{AFX_DATA_INIT(CMemberInfoDlg_Global)
	//}}AFX_DATA_INIT
}


void CMemberInfoDlg_Global::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemberInfoDlg_Global)
	DDX_Control(pDX, IDC_CBO_PERSISTANCE, cboPersistence_);
	DDX_Control(pDX, IDC_TXT_MEMBER_CAPTION, txtMemberCaption_);
	DDX_Control(pDX, IDC_TXT_MEMBERNAME, txtMemberName_);
	DDX_Control(pDX, IDC_TXT_MEMBERDESCRIPTION, txtMemberDescription_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMemberInfoDlg_Global, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CMemberInfoDlg_Global)
	ON_EN_KILLFOCUS(IDC_TXT_MEMBERNAME, OnKillFocusTxtMembername)
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_Global message handlers
void CMemberInfoDlg_Global::StoreData()
{
	if (memberInfo_ == NULL)
		return;

	cmString tmp;

	txtMemberName_.GetWindowText(tmp);
	memberInfo_->SetName(tmp);

	txtMemberCaption_.GetWindowText(tmp);
	memberInfo_->SetCaption(tmp);

	txtMemberDescription_.GetWindowText(tmp);
	memberInfo_->SetDescription(tmp);

	enPersistenceMode p;

	cboPersistence_.GetSelKey((int&)p);
	memberInfo_->SetPersistenceMode(p);
}

void CMemberInfoDlg_Global::SetData(CXmlBaseElement* element)
{
	memberInfo_ = dynamic_cast<CXmlMemberInfo*>(element);;

	if (memberInfo_) {
		txtMemberName_.SetWindowText(memberInfo_->GetName());
		txtMemberCaption_.SetWindowText(memberInfo_->GetCaption());
		txtMemberDescription_.SetWindowText(memberInfo_->GetDescription());
		cboPersistence_.SelectItem(memberInfo_->GetPersistenceMode());
	} else {
		txtMemberName_.SetWindowText(_T(""));
		txtMemberCaption_.SetWindowText(_T(""));
		txtMemberDescription_.SetWindowText(_T(""));
		cboPersistence_.SelectItem(enPersistenceMode_AsChildNode);
	}

	txtMemberName_.RedrawWindow();
	txtMemberCaption_.RedrawWindow();
	txtMemberDescription_.RedrawWindow();
}

void CMemberInfoDlg_Global::OnKillFocusTxtMembername() 
{
	cmString newMemberName;

	txtMemberName_.GetWindowText(newMemberName);
	
	// TODO: validate member name
	//
	
	if (memberInfo_) {
		memberInfo_->SetName(newMemberName);

		UpdateAllViews(enUpdateViewReason_ItemNameChanged);
	}
}

void CMemberInfoDlg_Global::OnDataUpdate()
{
	txtMemberName_.SetWindowText(memberInfo_->GetName());
}

BOOL CMemberInfoDlg_Global::OnInitDialog() 
{
	CPropertyEditorDlg::OnInitDialog();
	
	cboPersistence_.AddItem(_T("No persistance (this class member will not be saved)."), enPersistenceMode_None);
	cboPersistence_.AddItem(_T("Xml Child node persistance"), enPersistenceMode_AsChildNode);
	cboPersistence_.AddItem(_T("Xml Attribute"), enPersistenceMode_AsAttribute);
	cboPersistence_.AddItem(_T("#text Node"), enPersistenceMode_AsTextNode);
	cboPersistence_.AddItem(_T("CDATA"), enPersistenceMode_AsCDATA);

	cboPersistence_.SelectItem(enPersistenceMode_AsChildNode);	

	initialized_ = true;

	UpdateWindowSize();

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

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

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

	CRect rc;
	CRect txtRect;
	int itemWidth;

	GetClientRect(rc);

	txtMemberName_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);
	itemWidth = rc.Width() - BORDER - txtRect.left;
	txtMemberName_.SetWindowPos(&CWnd::wndTop, 0, 0, itemWidth, txtRect.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);

	txtMemberCaption_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);
	itemWidth = rc.Width() - BORDER - txtRect.left;
	txtMemberCaption_.SetWindowPos(&CWnd::wndTop, 0, 0, itemWidth, txtRect.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);

	txtMemberDescription_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);
	itemWidth = rc.Width() - BORDER - txtRect.left;
	txtMemberDescription_.SetWindowPos(&CWnd::wndTop, 0, 0, itemWidth, txtRect.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);

	cboPersistence_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);
	itemWidth = rc.Width() - BORDER - txtRect.left;
	cboPersistence_.SetWindowPos(&CWnd::wndTop, 0, 0, itemWidth, txtRect.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);
}

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

	GetClientRect(rc);

	memDC.FillSolidRect(rc, DIALOG_BACK_COLOR);
}

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

HBRUSH CMemberInfoDlg_Global::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	if (nCtlColor == CTLCOLOR_STATIC) {
		static CBrush b;

		if(b.GetSafeHandle() == NULL)
			b.CreateSolidBrush(DIALOG_BACK_COLOR);

		pDC->SetBkMode(OPAQUE);
		pDC->SetBkColor(DIALOG_BACK_COLOR);
		return b;

	}
	
	return hbr;
}

