// NodeObjectReferenceView1.cpp : implementation file
//

#include "stdafx.h"
#include "contenttrack.h"
#include "NodeObjectReferenceView1.h"
#include "NodeDataValueFileDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNodeObjectReferenceView

IMPLEMENT_DYNCREATE(CNodeObjectReferenceView, CListView)

CNodeObjectReferenceView::CNodeObjectReferenceView()
{
}

CNodeObjectReferenceView::~CNodeObjectReferenceView()
{
}


BEGIN_MESSAGE_MAP(CNodeObjectReferenceView, CListView)
	//{{AFX_MSG_MAP(CNodeObjectReferenceView)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNodeObjectReferenceView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CNodeObjectReferenceView diagnostics

#ifdef _DEBUG
void CNodeObjectReferenceView::AssertValid() const
{
	CListView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CNodeObjectReferenceView message handlers

BOOL CNodeObjectReferenceView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.style |= LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL;	
	return CListView::PreCreateWindow(cs);
}

void CNodeObjectReferenceView::OnInitialUpdate() 
{
	CListCtrl *pCtrl = &GetListCtrl();
	
	pCtrl->InsertColumn(0,"Object references",LVCFMT_LEFT,300);
	CListView::OnInitialUpdate();	
}

void CNodeObjectReferenceView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	CListCtrl *pCtrl = &GetListCtrl();
	pCtrl->DeleteAllItems();
	if (GetDocument()->IsKindOf(RUNTIME_CLASS(CNodeDataValueFileDoc)))
	{
		CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)GetDocument();
		if (pDoc->m_pActiveChild)
		{
			CNodeObject *pNode = pDoc->m_pActiveChild;
			while (pNode && !pNode->IsFile())
				pNode = pNode->GetParentNode();
			if (pNode)
			{
				CNodeDisk *pDisk = ((CNodeDataValueFile *)pNode)->GetNodeDisk();

				int i=0;
				POSITION pos = pDisk->GetFirstReference();
				while (pos)
				{
					CNodeObject *pChild = pDisk->GetNextReference(pos);
					// Find the file node to the disk
					while (pChild && (!pChild->IsFile() ||(pChild->GetClassID() == CLASS_CNODEDATAVALUEFILEPROJECT)))
						pChild = pChild->GetParentNode();
					// Find the node that loaded this file
					while (pChild && !pChild->IsFile())
						pChild = pChild->GetParentNode();
					if (pChild)
					{
						CNodeDisk *pDisk = ((CNodeDataValueFile *)pChild)->GetNodeDisk();
						int index = pCtrl->InsertItem(i,pDisk->GetPath());
						pCtrl->SetItemData(index,(DWORD)pChild);
						i++;
					}
				}
			}
		}
	}
}

void CNodeObjectReferenceView::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if (pNMListView->uNewState & LVIS_SELECTED)
	{
		CNodeObject *pNode = (CNodeObject *)pNMListView->lParam;
		if (pNode)
			GetDocument()->UpdateAllViews(this,(DWORD)pNode,NULL);
	}
	*pResult = 0;
}
