// ProjectTreeView.h : interface of the CProjectTreeView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(_PROJECTTREEVIEW_H_)
#define _PROJECTTREEVIEW_H_

#include <map>
#include "enums.h"
#include "cmString.h"

using namespace std;

class CProjectDoc;
class CXmlBaseElement;

typedef map<HTREEITEM, cmString> TreeItemToIdMap;
typedef map<cmString, CXmlBaseElement*> IdToXmlElementMap; 

class CProjectTreeView : public CTreeView
{
protected: // create from serialization only
	CProjectTreeView();
	DECLARE_DYNCREATE(CProjectTreeView)

// Attributes
public:
	CProjectDoc* GetDocument();

// Operations
public:
	void DisplayItemId(bool display);

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CProjectTreeView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnInitialUpdate(); // called first time after construct
	virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CProjectTreeView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CProjectTreeView)
	afx_msg void OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnMnuAppendClassElement();
	afx_msg void OnUpdateMnuAppendClassElement(CCmdUI* pCmdUI);
	afx_msg void OnMnuAppendMemberElement();
	afx_msg void OnUpdateMnuAppendMemberElement(CCmdUI* pCmdUI);
	afx_msg void OnMnuRemoveElement();
	afx_msg void OnUpdateMnuRemoveElement(CCmdUI* pCmdUI);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnPaint();
	afx_msg void OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT nIDEvent);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

protected:
	void BuildTree();
	void Clear();
	HTREEITEM CreateNode(CXmlBaseElement* xmlElement, HTREEITEM parentNode = TVI_ROOT);
	CXmlBaseElement* GetSelectedItem();
	void AppendNode(enDataType dataType, CXmlBaseElement* parentXmlElement, HTREEITEM parentTreeItem);
	HTREEITEM GetTreeItemById(const cmString& id);
	CXmlBaseElement* GetItem(HTREEITEM hItem);
	void RemoveItem(HTREEITEM item);
	
	

	void UpdateAllStates();
	void UpdateNode(HTREEITEM item);
	cmString FormatNodeLabel(CXmlBaseElement* element);
	void UpdateAllLabels();
	void UpdateNodeLabel(HTREEITEM node);

	TreeItemToIdMap treeItems_;

	CImageList imageList_;
	CImageList stateImageList_;
	bool displayItemId_;

	CRect clientRect_;
	
	// drag & drop support
	//
	bool IsChildOf(HTREEITEM hItem1, HTREEITEM hItem2);
	HTREEITEM MoveTree(HTREEITEM hDest, HTREEITEM hSrc);
	HTREEITEM CopyTree(HTREEITEM hDest, HTREEITEM hSrc);
	void CopyChildren(HTREEITEM hDest, HTREEITEM hSrc);
	BOOL IsItemExpanded(HTREEITEM hItem);
	HTREEITEM HighlightDropTarget(CPoint point);

	bool isDragging_;
	HTREEITEM hDragItem_;
	CImageList* dragImageList_;
	int scrollMargin_;
	int scrollInterval_;
	int delayInterval_;
};

#ifndef _DEBUG  // debug version in ProjectTreeView.cpp
inline CProjectDoc* CProjectTreeView::GetDocument()
   { return (CProjectDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(_PROJECTTREEVIEW_H_)
