// ProjectItemView.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "ProjectItemView.h"
#include "ProjectDoc.h"
#include "XmlClassInfo.h"
#include "PropertyEditorDlg.h"
#include "MemDC.h"
#include "DrawUtils.h"
#include "GuiDefs.h"

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



/////////////////////////////////////////////////////////////////////////////
// CProjectItemView

IMPLEMENT_DYNCREATE(CProjectItemView, CView)

CProjectItemView::CProjectItemView()
	: CView(),
	lastClassSelTab_(0),
	lastMemberSelTab_(0),
	selElement_(NULL),
	lastSelChild_(NULL),
	initialized_(false)
{
	//{{AFX_DATA_INIT(CProjectItemView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CProjectItemView::~CProjectItemView()
{
}


BEGIN_MESSAGE_MAP(CProjectItemView, CView)
	//{{AFX_MSG_MAP(CProjectItemView)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_PROPERTIES, OnSelchangeTabProperties)
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProjectItemView diagnostics

#ifdef _DEBUG
void CProjectItemView::AssertValid() const
{
	CView::AssertValid();
}

void CProjectItemView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CProjectItemView message handlers

void CProjectItemView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	if (initialized_)
		return;

	ModifyStyle(0, WS_CLIPCHILDREN);

	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_PROPERTIES);
	tab_.SetFont(&font_);
	if (!classCardinalityDlg_.GetSafeHwnd()) {
		classCardinalityDlg_.Create(classCardinalityDlg_.IDD, this);
		classGlobalDlg_.Create(classGlobalDlg_.IDD, this);
		classGuiDlg_.Create(classGuiDlg_.IDD, this);
		classTreeIconsDlg_.Create(classTreeIconsDlg_.IDD, this);

		memberGlobalDlg_.Create(memberGlobalDlg_.IDD, this);
		memberValidationDlg_.Create(memberValidationDlg_.IDD, this);
		memberDataFormatDlg_.Create(memberDataFormatDlg_.IDD, this);
		memberGuiDlg_.Create(memberGuiDlg_.IDD, this);
	}	

	DWORD flags = ETC_FLAT | ETC_SELECTION | ETC_BACKTABS;


	tab_.SetBkgndColor(BACKGROUND_END_COLOR);
	tab_.EnableDraw(BTC_ALL);
	tab_.ModifyStyle(0, WS_CLIPCHILDREN);

	titleStrip_.SetBitmaps(IDB_STRIP_BODY, IDB_STRIP_TAIL);

	CEnTabCtrl::EnableCustomLook(flags | RGB(0x9D, 0xC0, 0xED), flags);

	initialized_ = true;
}

void CProjectItemView::DisplayClassInfo()
{
	propertyEditors_.clear();
	propertyEditors_.push_back(&classGlobalDlg_);
	propertyEditors_.push_back(&classGuiDlg_);
	propertyEditors_.push_back(&classCardinalityDlg_);
	propertyEditors_.push_back(&classTreeIconsDlg_);

	tab_.DeleteAllItems();
	tab_.InsertItem(0, _T("Global"));
	tab_.InsertItem(1, _T("Gui"));
	tab_.InsertItem(2, _T("Cardinality"));
	tab_.InsertItem(3, _T("Tree View"));
	tab_.SetCurSel(lastClassSelTab_);

	UpdateSelChild();
}

void CProjectItemView::DisplayMemberInfo()
{
	propertyEditors_.clear();
	propertyEditors_.push_back(&memberGlobalDlg_);
	propertyEditors_.push_back(&memberDataFormatDlg_);
	propertyEditors_.push_back(&memberValidationDlg_);
	propertyEditors_.push_back(&memberGuiDlg_);

	tab_.DeleteAllItems();
	tab_.InsertItem(0, _T("Global"));
	tab_.InsertItem(1, _T("Data Format"));
	tab_.InsertItem(2, _T("Validation"));
	tab_.InsertItem(3, _T("Gui"));

	tab_.SetCurSel(lastMemberSelTab_);

	UpdateSelChild();
}

void CProjectItemView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (tab_.GetSafeHwnd()) {
		switch (lHint) {
			case enUpdateViewReason_ItemNameChanged:
			case enUpdateViewReason_ItemChanged:
				UpdateEditors();
				break;

			case enUpdateViewReason_SelectionChanged:
				StoreEditors();
				SetupEditors();
				break;

			case enUpdateViewReason_DocLoaded:
				SetupEditors();	
				break;

			case enUpdateViewReason_BeforeSaveDoc:
				StoreEditors();
				break;
		}
	}
}

CProjectDoc* CProjectItemView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CProjectDoc)));
	return (CProjectDoc*)m_pDocument;
}

void CProjectItemView::OnSelchangeTabProperties(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if (selElement_->GetDataType() == enDataType_Class)
		lastClassSelTab_ = tab_.GetCurSel();
	else
		lastMemberSelTab_ = tab_.GetCurSel();

	UpdateSelChild();
	
	*pResult = 0;
}

void CProjectItemView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if (tab_.GetSafeHwnd()) {
		CRect rc;

		GetClientRect(rc);

		rc.DeflateRect(0, STRIP_HEIGHT, 0, 0);
		titleStrip_.SetDrawArea(CRect(0, 0, rc.right, STRIP_HEIGHT));

		tab_.SetWindowPos(&CWnd::wndTop, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER);
		
		UpdateSelChild();
	}
}

void CProjectItemView::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;
}

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

void CProjectItemView::StoreEditors()
{
	for (int i = 0; i < propertyEditors_.size(); i++)
		propertyEditors_[i]->StoreData();	
}

void CProjectItemView::SetupEditors()
{
	cmString id;
	
	id = GetDocument()->GetSelectedItemId();
	selElement_ = GetDocument()->GetElementById(id);
	
	if (selElement_== NULL) {
		tab_.DeleteAllItems();
		propertyEditors_.clear();
		return;
	}

	if (selElement_->GetDataType() == enDataType_Class)
		DisplayClassInfo();
	else
		DisplayMemberInfo();

	for (int i = 0; i < propertyEditors_.size(); i++)
		propertyEditors_[i]->SetData(selElement_);
}

void CProjectItemView::UpdateEditors()
{
	for (int i = 0; i < propertyEditors_.size(); i++)
		propertyEditors_[i]->OnDataUpdate();
}

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

	DrawBackground(memDC);
}

void CProjectItemView::DrawBackground(CDC* pDC)
{
	titleStrip_.Draw(pDC);
}

void CProjectItemView::OnDraw(CDC* pDC) 
{
	CMemDC memDC(pDC);

	DrawBackground(memDC);
}
