// PeeperView.cpp : implementation of the CPeeperView class
//

#include "stdafx.h"
#include "Peeper.h"

#include "PeeperDoc.h"
#include "PeeperView.h"
#include "SearchDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPeeperView

IMPLEMENT_DYNCREATE(CPeeperView, CTreeView)

BEGIN_MESSAGE_MAP(CPeeperView, CTreeView)
//{{AFX_MSG_MAP(CPeeperView)
ON_WM_CREATE()
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
ON_COMMAND(ID_EDIT_SEARCH, OnEditSearch)
	ON_COMMAND(ID_EDIT_FINDNEXT, OnEditFindnext)
	ON_UPDATE_COMMAND_UI(ID_EDIT_FINDNEXT, OnUpdateEditFindnext)
	//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPeeperView construction/destruction

CPeeperView::CPeeperView()
{
	LastFind=NULL;
	m_dwDefaultStyle |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS;
}

CPeeperView::~CPeeperView()
{
}

BOOL CPeeperView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPeeperView drawing

void CPeeperView::OnDraw(CDC* pDC)
{
	CPeeperDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CPeeperView printing

BOOL CPeeperView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPeeperView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPeeperView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPeeperView diagnostics

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

void CPeeperView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CPeeperDoc* CPeeperView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPeeperDoc)));
	return (CPeeperDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPeeperView message handlers

int CPeeperView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	
	return 0;
}



void CPeeperView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	CPeeperDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	if(pSender!=this)
	{
		GetTreeCtrl().DeleteAllItems();	
		HTREEITEM itm=GetTreeCtrl().InsertItem(pDoc->GetDspPath(),TVI_ROOT);
		GetTreeCtrl().SetItemData(itm,(DWORD)&(pDoc->FileTree));
		pDoc->FileTree.FillTree(&GetTreeCtrl(),itm,0,1);	
	}	
}

void CPeeperView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	HTREEITEM itm=pNMTreeView->itemNew.hItem;
	HTREEITEM child;
	
	child=GetTreeCtrl().GetChildItem(itm);
	
	do
	{
		if(!GetTreeCtrl().ItemHasChildren(child))
		{
			HNode *node=(HNode *)GetTreeCtrl().GetItemData(child);
			ASSERT(node!=NULL);
			node->FillTree(&GetTreeCtrl(),child,0,1);
		}
	}
	while((child=GetTreeCtrl().GetNextSiblingItem(child))!=NULL);
	
	
	*pResult = 0;
}

void CPeeperView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CPeeperDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM itm=pNMTreeView->itemNew.hItem;
	pDoc->UpdateAllViews(this,GetTreeCtrl().GetItemData(itm));
	
	*pResult = 0;
}

void CPeeperView::OnEditSearch() 
{
	CPeeperDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);	
	
	CSearchDlg dlg;
	
	dlg.m_FileName=SearchString;
	if(dlg.DoModal()==IDOK)
	{
		SearchString=dlg.m_FileName;
		OnEditFindnext();
	}
	
	
}



void CPeeperView::OnEditFindnext() 
{
	CPeeperDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);	
	

		LastFind=pDoc->FileTree.Find(SearchString,LastFind);
		if(LastFind) 
		{
			LastFind->DrillDownTo(&GetTreeCtrl());
			GetTreeCtrl().EnsureVisible(LastFind->GetItem());
			GetTreeCtrl().SelectItem(LastFind->GetItem());
		}
		else
		{
			MessageBox("Not Found","No More Results",MB_OK);
		}
	
}

void CPeeperView::OnUpdateEditFindnext(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable((SearchString!="")?TRUE:FALSE);
}
