// BuildFileView.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"

#include "MegatronDoc.h"

#include "BuildFileView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBuildFileView

IMPLEMENT_DYNCREATE(CBuildFileView, CView)

CBuildFileView::CBuildFileView()
{
}

CBuildFileView::~CBuildFileView()
{
}


BEGIN_MESSAGE_MAP(CBuildFileView, CView)
	//{{AFX_MSG_MAP(CBuildFileView)
	ON_WM_RBUTTONUP()
	ON_WM_SIZE()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBuildFileView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CBuildFileView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CBuildFileView message handlers

int CBuildFileView::AddItem(CFileNode *pNode,HTREEITEM hParent)
{
	int count = 0;
	if (pNode)
	{
		count++;
		HTREEITEM hItem = m_wndTreeFiles.InsertItem(pNode->strName,hParent);
		m_wndTreeFiles.SetItemData(hItem,(DWORD)pNode);
		POSITION pos = pNode->listChildren.GetHeadPosition();
		while (pos)
		{
			CFileNode *pChild = (CFileNode *)pNode->listChildren.GetNext(pos);
			count += AddItem(pChild,hItem);
		}
	}
	return count;
}

void CBuildFileView::OnInitialUpdate() 
{
	CRect rc;
	GetClientRect(&rc);
	rc.top=rc.left=0;
	rc.right=rc.bottom=200;

	m_wndTreeFiles.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | TVS_HASBUTTONS | TVS_FULLROWSELECT | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS,rc,this,NULL);
	m_wndTreeFiles.ShowWindow(SW_SHOW);

	CView::OnInitialUpdate();

	CDocument *pDoc = GetDocument();
	if (pDoc)
	{
		CMegatronDoc *pMegaDoc = (CMegatronDoc *)pDoc;
		CFileNode *pNode = pMegaDoc->m_pFileNode;
		int count = AddItem(pNode);
		/*POSITION pos = pMegaDoc->m_mapBuildFiles.GetStartPosition();
		while (pos)
		{
			CString strKey;
			LPVOID lpValue;
			pMegaDoc->m_mapBuildFiles.GetNextAssoc(pos,strKey,lpValue);
			int	index = m_wndListFiles.AddString(strKey);
			m_wndListFiles.SetItemData(index,(DWORD)lpValue);
		}*/
	}
}

BOOL CBuildFileView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	switch (HIWORD(wParam))
	{
	case TVN_SELCHANGED:
		{
			NMTREEVIEW *pNm = (NMTREEVIEW *)lParam;
			/*int nCount = m_wndListFiles.GetSelCount();
			int *items = new int[nCount];
			int err = m_wndListFiles.GetSelItems(nCount,items);
			CPtrList list;
			for (int x=0;x<nCount;x++)
			{
				DWORD data = m_wndListFiles.GetItemData(items[x]);
				list.AddHead((LPVOID)data);
			}
			delete items;

			CDocument *pDoc = GetDocument();
			pDoc->UpdateAllViews(this,UPDATE_BUILDFILES,&list);*/
			break;
		}
	}
	
	return CView::OnCommand(wParam, lParam);
}

void CBuildFileView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	CMenu* pMenu = new CMenu();
	pMenu->LoadMenu(IDR_POPUP_MENUS);
	CMenu *pSubMenu = pMenu->GetSubMenu(0);

	ClientToScreen(&point);

	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,point.x, point.y, this, NULL);
	delete pMenu;

	CView::OnRButtonUp(nFlags, point);
}

void CBuildFileView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	if (m_wndTreeFiles.GetSafeHwnd())
	{
		m_wndTreeFiles.MoveWindow(0,0,cx,cy);
	}
}

BOOL CBuildFileView::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	NMHDR *pHdr = (NMHDR *)lParam;
	switch (pHdr->code)
	{
	case TVN_SELCHANGED:
		{
			NMTREEVIEW *pNM = (NMTREEVIEW *)pHdr;
			CFileNode *pNode = (CFileNode *)pNM->itemNew.lParam;
			CPtrList list;
			list.AddHead((LPVOID)pNode->pFile);
			CDocument *pDoc = GetDocument();
			pDoc->UpdateAllViews(this,UPDATE_BUILDFILES,&list);
			break;
		}
	default:
		{
		}
	}
	
	return CView::OnNotify(wParam, lParam, pResult);
}

void CBuildFileView::OnRButtonDown(UINT nFlags, CPoint point) 
{
/*	CMenu* pMenu = new CMenu();
	pMenu->LoadMenu(IDR_POPUP_MENUS);
	CMenu *pSubMenu = pMenu->GetSubMenu(0);

	ClientToScreen(&point);

	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,point.x, point.y, this, NULL);
	delete pMenu;
*/	
	CView::OnRButtonDown(nFlags, point);
}
