// NotationView.cpp : implementation file
//

#include "stdafx.h"
#include "Multitron.h"
#include "NotationView.h"
#include "NotationDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNotationView

IMPLEMENT_DYNCREATE(CNotationView, CView)

CNotationView::CNotationView()
{
}

CNotationView::~CNotationView()
{
}


BEGIN_MESSAGE_MAP(CNotationView, CView)
	//{{AFX_MSG_MAP(CNotationView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNotationView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CNotationView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CNotationView message handlers

BOOL CNotationView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CView::PreCreateWindow(cs);
}

void CNotationView::OnInitialUpdate() 
{
	CRect rc;
	GetClientRect(&rc);
	if (!m_wndTreeCtrl.Create(WS_VISIBLE | WS_CHILD | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_TRACKSELECT | TVS_FULLROWSELECT,rc,this,0))
	{
		return;
	}
	CView::OnInitialUpdate();
	
}

void CNotationView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	CNotationDoc *pDoc = (CNotationDoc *)GetDocument();
	Stuff::Plug *plug = pDoc->m_pNote;
	if (NULL == plug)
		plug = pDoc->m_pPage;

	m_wndTreeCtrl.DeleteAllItems();
	Stuff::NotationFile::PageIterator *pages = pDoc->GetNotationFile()->MakePageIterator();
	Stuff::Page *page;
	while (NULL != (page = pages->ReadAndNext()))
	{
		CString pagename = page->GetName();
		
		HTREEITEM hPage = m_wndTreeCtrl.InsertItem(pagename,TVI_ROOT);
		m_wndTreeCtrl.SetItemData(hPage,(DWORD)page);
		if (page == plug)
			m_wndTreeCtrl.SelectItem(hPage);

		Stuff::Page::NoteIterator *notes = page->MakeNoteIterator();
		Stuff::Note *note;
		while (NULL != (note = notes->ReadAndNext()))
		{
			CString notename = note->GetName();
			const char *entry;
			note->GetEntry(&entry);
			CString str;
			str.Format("%s=%s",notename,entry);
			HTREEITEM hNote = m_wndTreeCtrl.InsertItem(str,hPage);
			m_wndTreeCtrl.SetItemData(hNote,(DWORD)note);
			if (note == plug)
				m_wndTreeCtrl.SelectItem(hNote);
		}
		delete notes;
	
		m_wndTreeCtrl.Expand(hPage,TVE_EXPAND);
	}
	delete pages;
}

