// ObjManFrame.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ObjManFrame.h"
#include "ObjTreeView.h"
#include "ObjListView.h"
#include "ObjectManager.h"
#include "Refresher.h"
#include <GameOS\ToolOS.hpp>

/////////////////////////////////////////////////////////////////////////////
// CObjManFrame

//IMPLEMENT_DYNCREATE(CObjManFrame, CFrameWnd)

CObjManFrame::CObjManFrame(ObjectManager *objman,CWnd *parent)
{
ObjMan=objman;
TreePane=NULL;
ListPane=NULL;
	m_ImageList.Create(16,16,ILC_COLOR8 + ILC_MASK,ICONCOUNT,0);

	// NOTE: the ordering here must exactly match the enum values in 
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_BLANK));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_MECH));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_VEHICLE));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_AIRPLANE));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_BUILDING));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_NONCOM));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_CULTURAL));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_NAVPOINT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_CAMERASHIP));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_DROPZONE));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_LATTICE));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_PATH));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_NODE));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_MISSION));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_GROUP));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_BOUNDS));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_WATER));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_POINTLIGHT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_AMBIENTLIGHT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_SPOTLIGHT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_INFINITELIGHT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_LOOKUPLIGHT));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_FXGEN));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_FLAG));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_TEAM));
BOOL res=LoadFrame(IDR_OBJMAN,WS_OVERLAPPED|WS_THICKFRAME|WS_CAPTION|WS_MAXIMIZEBOX|WS_MINIMIZEBOX|WS_SYSMENU,parent);
Verify(res);



}

CObjManFrame::~CObjManFrame()
{
}


BEGIN_MESSAGE_MAP(CObjManFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CObjManFrame)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
	ON_COMMAND(ID_EDIT_GROUP, OnEditGroup)
	ON_COMMAND(ID_EDIT_UNGOUP, OnEditUngoup)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CObjManFrame message handlers

int CObjManFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
m_splitter.CreateStatic(this,1,2);
m_splitter.CreateView(0,0,RUNTIME_CLASS(CObjTreeView),CSize(200,1024),NULL);
m_splitter.CreateView(0,1,RUNTIME_CLASS(CObjListView),CSize(200,1024),NULL);

TreePane=(CObjTreeView *)m_splitter.GetPane(0,0);
ListPane=(CObjListView *)m_splitter.GetPane(0,1);
TreePane->ObjMan=ListPane->ObjMan=ObjMan;
TreePane->MyList=ListPane;
ListPane->MyTree=TreePane;
TreePane->SetImageList(&m_ImageList);
ListPane->SetImageList(&m_ImageList);

RecalcLayout();

	return 0;
}

void CObjManFrame::Refresh(bool expandlist)
{
if(TreePane) 
{
TreePane->Refresh((GUIObjectList *)ObjMan,expandlist);
if(!expandlist) RefreshList();
}

}

void CObjManFrame::OnClose() 
{
	ShowWindow(SW_HIDE);
//	CFrameWnd::OnClose();
}

void CObjManFrame::SaveToReg()
{
	int tdat;
	CRect rct;
	CString wintitle;
	GetWindowText(wintitle);

	GetWindowRect(&rct);
	gos_SaveDataToRegistry((char *)(LPCSTR)(wintitle+"Rect"),&rct,sizeof(rct));
	tdat=IsWindowVisible();	gos_SaveDataToRegistry((char *)(LPCSTR)(wintitle+"Visible"),&tdat,sizeof(tdat));
}

void CObjManFrame::LoadFromReg()
{
	int tdat;
	unsigned long size;
	CRect rct;
	CString wintitle;
	GetWindowText(wintitle);
	
	size=sizeof(rct);
	gos_LoadDataFromRegistry((char *)(LPCSTR)(wintitle+"Rect"),&rct,&size);
	if(size) SetWindowPos(NULL,rct.left,rct.top,rct.Width(),rct.Height(),SWP_NOZORDER);
	
	size=sizeof(tdat);
	gos_LoadDataFromRegistry((char *)(LPCSTR)(wintitle+"Visible"),&tdat,&size);

	if(size && tdat==0) 
		ShowWindow(SW_HIDE);
	else
		ShowWindow(SW_SHOW);
	
}

void CObjManFrame::UpdateSelState()
{
if(ListPane) ListPane->UpdateSelState();
}

void CObjManFrame::RefreshList()
{
	if(TreePane && ListPane) ListPane->Refresh(TreePane->GetSelList());
}

void CObjManFrame::OnEditCopy() 
{
	if(!ObjMan->CopySelection())
		MessageBox("Copy Operation Failed","Copy Fail",MB_OK);
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditCut() 
{
	if(!ObjMan->CutSelection())
		MessageBox("Cut Operation Failed","Cut Fail",MB_OK);
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditDelete() 
{
	ObjMan->DeleteSelection();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditGroup() 
{
	ObjMan->GroupSelected();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditPaste() 
{
	ObjMan->PasteClipBoard();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditRedo() 
{
	ObjMan->Redo();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditUndo() 
{
	ObjMan->Undo();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnEditUngoup() 
{
	ObjMan->UnGroupSelected();
	Refresher::Instance->WorldRefresh();
}

void CObjManFrame::OnUpdateEditRedo(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(ObjMan->CanRedo());
}

void CObjManFrame::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(ObjMan->CanUndo());
}
