// NodeDataValueFileView.cpp : implementation file
//

#include "stdafx.h"
#include "ContentTrack.h"
#include "NodeDataValueFileView.h"
#include "NodeDataValueFileDoc.h"
#include "EditKeyType.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNodeDataValueFileView

IMPLEMENT_DYNCREATE(CNodeDataValueFileView, CTreeView)

CNodeDataValueFileView::CNodeDataValueFileView()
{
	m_hBoldItem = NULL;
}

CNodeDataValueFileView::~CNodeDataValueFileView()
{
}


BEGIN_MESSAGE_MAP(CNodeDataValueFileView, CTreeView)
	//{{AFX_MSG_MAP(CNodeDataValueFileView)
	ON_COMMAND(ID_EXPANDALL, OnExpandall)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
	ON_WM_CONTEXTMENU()
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_COMMAND(ID_VIEW_EDITKEY, OnViewEditkey)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNodeDataValueFileView drawing

void CNodeDataValueFileView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CNodeDataValueFileView diagnostics

#ifdef _DEBUG
void CNodeDataValueFileView::AssertValid() const
{
	CTreeView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CNodeDataValueFileView message handlers

void CNodeDataValueFileView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{	
	CTreeCtrl *pCtrl = &this->GetTreeCtrl();
	
	if (lHint)
	{
		pCtrl->SetItemState(m_hBoldItem,0,TVIS_BOLD);
		CNodeObject *pNode = (CNodeObject *)lHint;
		HANDLE handle = pNode->ViewNode(pCtrl,NULL,2);
		HTREEITEM hItem = (HTREEITEM)handle;
		pCtrl->SetItemState(hItem,TVIS_BOLD,TVIS_BOLD);
		m_hBoldItem = hItem;
		pCtrl->EnsureVisible(hItem);
	}
	else
	{
		CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)GetDocument();
		HTREEITEM hItem = (HTREEITEM)pDoc->GetNodeProject()->ViewNode(pCtrl,NULL,2);
		// Expand the root item
		pCtrl->Expand(hItem,TVE_EXPAND);
	}
}

BOOL CNodeDataValueFileView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.style |= TVS_HASLINES | TVS_SHOWSELALWAYS | TVS_DISABLEDRAGDROP | TVS_FULLROWSELECT | TVS_HASBUTTONS; // TVS_LINESATROOT |;	
	return CTreeView::PreCreateWindow(cs);
}

void CNodeDataValueFileView::OnExpandall() 
{
	ExpandAll(GetTreeCtrl().GetSelectedItem());
}

int CNodeDataValueFileView::ExpandAll(HTREEITEM hParent)
{
	CTreeCtrl *pCtrl = &this->GetTreeCtrl();
	if (pCtrl->ItemHasChildren(hParent))
	{
		HTREEITEM hChildItem = pCtrl->GetChildItem(hParent);
		while (hChildItem != NULL)
		{
			ExpandAll(hChildItem);
			hChildItem = pCtrl->GetNextItem(hChildItem, TVGN_NEXT);
		}
	}
	pCtrl->Expand(hParent,TVE_EXPAND);
	return 0;
}

void CNodeDataValueFileView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
/*	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	CTreeCtrl *pCtrl = &GetTreeCtrl();
	HTREEITEM hItem = pNMTreeView->itemNew.hItem;
	CNodeObject *pNode = (CNodeObject *)pCtrl->GetItemData(hItem);
	CNodeObject *pParent = pNode->GetParentNode();
	pNode->ViewNode(pCtrl,pParent?pCtrl->GetParentItem(hItem):NULL,3);
	*pResult = 0;*/
}

void CNodeDataValueFileView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	CTreeCtrl *pCtrl = &GetTreeCtrl();
	HTREEITEM hItem = pNMTreeView->itemNew.hItem;
	CNodeObject *pNode = (CNodeObject *)pCtrl->GetItemData(hItem);
	CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)GetDocument();
	pDoc->m_pActiveChild = pNode;
	pDoc->UpdateAllViews(this);
	*pResult = 0;
}

void CNodeDataValueFileView::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	CTreeCtrl *pCtrl = &GetTreeCtrl();
	HTREEITEM hItem = pNMTreeView->itemNew.hItem;
	CNodeObject *pNode = (CNodeObject *)pCtrl->GetItemData(hItem);
	CNodeObject *pParent = pNode->GetParentNode();
	pNode->ViewNode(pCtrl,pParent?pCtrl->GetParentItem(hItem):NULL,3);
	*pResult = 0;
}
/*
void CNodeDataValueFileView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	CMenu menu;
	//VERIFY(menu.CreateMenu());
	VERIFY(menu.LoadMenu(IDR_CONTENTYPE));
	CMenu *pPopup = menu.GetSubMenu(3);
	VERIFY(pPopup);
	POINT pt = pNMTreeView->ptDrag;
	static int x=1;
	//if (x)
	//	ClientToScreen(&pt);
	//else
	ScreenToClient(&pt);
//	ClientToDoc(pt);
	RECT rc;
	//this->GetTreeCtrl().GetItemRect(pNMTreeView->itemNew.hItem,&rc,true);
	
	*pResult = 0;
}
*/
void CNodeDataValueFileView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	ScreenToClient(&point);
	HTREEITEM hItem = GetTreeCtrl().HitTest(point);
//	CNodeObject *pNode = (CNodeObject *)GetTreeCtrl().GetItemData(hItem);

	CMenu menu;
	VERIFY(menu.LoadMenu(IDR_CONTENTYPE));
	CMenu *pPopup = menu.GetSubMenu(3);
	VERIFY(pPopup);
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd());

}

void CNodeDataValueFileView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	CMenu menu;
	//VERIFY(menu.CreateMenu());
	VERIFY(menu.LoadMenu(IDR_CONTENTYPE));
	CMenu *pPopup = menu.GetSubMenu(3);
	VERIFY(pPopup);

	CTreeCtrl *pCtrl = &GetTreeCtrl();

	HTREEITEM hItem = pCtrl->GetSelectedItem();

	pCtrl->EnsureVisible(hItem);
	RECT rc;
	pCtrl->GetItemRect(hItem,&rc,true);
	ClientToScreen(&rc);	


	//pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,rc.right,rc.top,AfxGetMainWnd());
	
	*pResult = 0;
}

extern CStringMap g_StringMap;

void CNodeDataValueFileView::OnViewEditkey() 
{
	CTreeCtrl *pCtrl = &GetTreeCtrl();

	HTREEITEM hItem = pCtrl->GetSelectedItem();

	if (hItem)
	{
		CNodeObject *pNode = (CNodeObject *)pCtrl->GetItemData(hItem);
		if (pNode->GetClassID() == CLASS_CNODEDATAENTRY)
		{
			CNodeDataEntry *pEntry = (CNodeDataEntry *)pNode;
			CEditKeyType dlg;
			CString str;
			pNode->GetName(str);
			dlg.m_editName = str;
			dlg.m_iCurSel = pEntry->GetKeyValue();
			POSITION lpos = pEntry->GetKeyPos();
			if (IDOK == dlg.DoModal())
			{
				g_StringMap.SetValue(lpos,dlg.m_iCurSel);
				CContentTrackApp *pApp = (CContentTrackApp *)AfxGetApp();
				pApp->UpdateKeys();
				pApp->WritePropFile();
			}
		}
	}
}
