// ObjTreeView.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ObjTreeView.h"
#include "ObjListView.h"
#include "GUIObjectList.h"
#include "ObjectManager.h"
#include "Refresher.h"


/////////////////////////////////////////////////////////////////////////////
// CObjTreeView

IMPLEMENT_DYNCREATE(CObjTreeView, CView)

CObjTreeView::CObjTreeView():DropTarget(this)
{
ObjMan=NULL;
}

CObjTreeView::~CObjTreeView()
{
}


BEGIN_MESSAGE_MAP(CObjTreeView, CView)
	//{{AFX_MSG_MAP(CObjTreeView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CObjTreeView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CObjTreeView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CObjTreeView message handlers
void CObjTreeView::Refresh(GUIObjectList *objman,bool expandlist)
{
m_Tree.DeleteAllItems();
objman->SortChildren();
HTREEITEM itm=objman->AddToBranch(&m_Tree,TVI_ROOT);
if(expandlist)
	{	
	m_Tree.SelectItem(itm);
	ExpandAll();
	}
}

void CObjTreeView::ExpandAll(HTREEITEM itm)
{
if(itm==NULL) return;
	m_Tree.Expand(itm,TVE_EXPAND);
HTREEITEM nitm=m_Tree.GetChildItem(itm);
		
while(nitm!=NULL)
	{
	ExpandAll(nitm);
	nitm=m_Tree.GetNextSiblingItem(nitm);
	}

}



GUIObjectList *CObjTreeView::GetSelList()
{
HTREEITEM itm;

itm=m_Tree.GetSelectedItem();
if(itm==NULL) return NULL;
else return (GUIObjectList *)m_Tree.GetItemData(itm);
}


int CObjTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_Tree.Create(TVS_EDITLABELS|TVS_HASBUTTONS|TVS_HASLINES|TVS_SHOWSELALWAYS|TVS_LINESATROOT,CRect(0,0,200,200),this,NULL);
//	m_Tree.SetItemHeight(20);
	m_Tree.ShowWindow(SW_SHOW);
	DropTarget.Register(&m_Tree);

	return 0;
}

void CObjTreeView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if(GetSafeHwnd()!=NULL)
	{
	m_Tree.SetWindowPos(NULL,0,0,cx,cy,SWP_NOZORDER);
	}
		
}

void CObjTreeView::SetImageList(CImageList *ilist)
{
	m_Tree.SetImageList(ilist,LVSIL_NORMAL);
}

BOOL CObjTreeView::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
HRESULT res=CView::OnNotify(wParam, lParam, pResult);
	NMHDR *hdr=(NMHDR *)lParam;
	
	switch(hdr->code)
	{
	case TVN_ENDLABELEDIT:
		{
			NMTVDISPINFO* pTVDispInfo = (NMTVDISPINFO*)hdr;
			
			EdGUIObject *gobj=(EdGUIObject *)pTVDispInfo->item.lParam;
			
			CString newname=CString(pTVDispInfo->item.pszText);
			if(newname!="")
				ObjMan->SetObjectName(gobj,newname);
			
			m_Tree.SetItemText(pTVDispInfo->item.hItem,gobj->GetName());
			Refresher::Instance->OverviewRefresh();
		}
		break;
		
	case TVN_SELCHANGED: 
		{
			MyList->Refresh(GetSelList());
		}
		break;

	case NM_DBLCLK:
		{
			HTREEITEM selitm=m_Tree.GetSelectedItem();
			if(selitm!=NULL)
			{
			EdGUIObject *gobj=(EdGUIObject *)m_Tree.GetItemData(selitm);
			ObjMan->SetProperties(gobj,this);
			m_Tree.SetItemText(selitm,gobj->GetName());
			Refresher::Instance->WorldRefresh();
			}
		}
		break;

	case TVN_BEGINDRAG:
		{
		}
		break;
	}

return res;
}

HTREEITEM CObjTreeView::FindItem(EdGUIObject *gobj,HTREEITEM itm)
{
if(itm==NULL) return NULL;

if(itm!=TVI_ROOT)
{
GUIObjectList *slst=(GUIObjectList *)gobj,*dlst=(GUIObjectList *)m_Tree.GetItemData(itm);
if(dlst==slst)
	 return itm;
}

HTREEITEM ritm,nitm=m_Tree.GetChildItem(itm);
		
while(nitm!=NULL)
	{
	ritm=FindItem(gobj,nitm);
	if(ritm!=NULL) return ritm;
	nitm=m_Tree.GetNextSiblingItem(nitm);
	}

return NULL;
}

bool CObjTreeView::InTree(EdGUIObject *gobj)
{
return (FindItem(gobj)!=NULL);
}


bool CObjTreeView::DrillDownTo(EdGUIObject *gobj)
{
HTREEITEM itm;
itm=FindItem(gobj);
if(itm==NULL) return false;
m_Tree.EnsureVisible(itm);
m_Tree.Select(itm,TVGN_CARET);
return true;
}

void CObjTreeView::ListDropAt(CPoint &pnt)
{
GUIObjectList *sobj=ObjectAtPoint(pnt);
if(sobj && sobj->CanTakeChildren())
{
MyList->AddSelectedToGroup(sobj);
}


}

GUIObjectList *CObjTreeView::ObjectAtPoint(CPoint &pnt)
{
UINT flags;
HTREEITEM taritem;
taritem=m_Tree.HitTest(pnt,&flags);
if(taritem!=NULL)
	{
	return (GUIObjectList *)m_Tree.GetItemData(taritem);
	}

return NULL;
}


bool CObjTreeView::CanListDropAt(CPoint &pnt)
{
GUIObjectList *olist=ObjectAtPoint(pnt);
if(olist!=NULL && olist->CanTakeChildren()) return true;
else return false;
}