// GUIObjectList.cpp: implementation of the GUIObjectList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MW4GameEd2.h"
#include "GUIObjectList.h"
#include "DrawInfo.h"
#include "GroupProps.h"
#include "ObjPanelDlg.h"
#include "TNPanel.h"
#include "UndoManager.h"

#include<Stuff\Stuff.hpp>
#include"EdGUIObject.h"


#define STARTSIZE 256

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

GUIObjectList::GUIObjectList(CString name):EdGUIObject(name)
{
	ObjectCount=0;
	ArrayMax=STARTSIZE;
	ObjData=new EdGUIObject *[ArrayMax];
	LineCount=0;
	MyItem=NULL;
	LastFoundAt=0;
	ExistEmpty=false;
}

GUIObjectList::~GUIObjectList()
{
	DeleteAllObjects();
	delete ObjData;
}

int GUIObjectList::FindObjectIdx(EdGUIObject *gobj)
{
	int idx;
	
	for(idx=0;idx<GetObjectCount();idx++) 
		if(gobj==GetObject(idx)) 
			return idx;
		
		return -1;
}

int GUIObjectList::FindObjectIdxByName(CString &objname)	
{
	int idx;
	if(LastFoundAt>=GetObjectCount()) LastFoundAt=0;
	
	for(idx=LastFoundAt;idx<GetObjectCount();idx++) 
		if(!GetObject(idx)->GetName().CompareNoCase(objname)) 
			return LastFoundAt=idx;
		
		for(idx=0;idx<LastFoundAt;idx++) 
			if(!GetObject(idx)->GetName().CompareNoCase(objname)) 
				return LastFoundAt=idx;
			
			LastFoundAt=0;
			return -1;
}

void GUIObjectList::RemoveObject(EdGUIObject *gobj)
{
	
	if(gobj) RemoveObject(FindObjectIdx(gobj));
}

void GUIObjectList::RemoveObject(int idx)
{
	Verify(idx>=0 && idx<ObjectCount);
	Verify(ObjData[idx]->Parent!=NULL);
	ObjData[idx]->RemoveFromMap();
	ObjData[idx]->Parent=NULL;
	ObjData[idx]->RemoveRefernce();
	
	for(int i=idx;i<ObjectCount-1;i++) ObjData[i]=ObjData[i+1];
	ObjectCount--;
	
}

void GUIObjectList::DeleteObject(int idx)
{
	Verify(idx>=0 && idx<ObjectCount);
	EdGUIObject *gobj=ObjData[idx];
	RemoveObject(idx);
	delete gobj;
}


EdGUIObject *GUIObjectList::GetObject(int idx)
{
	Verify(idx<ObjectCount);
	if(idx>=ObjectCount) return NULL;
	
	return ObjData[idx];
}

void GUIObjectList::GrowArray()	//Grow Internal Array by 50% and copy data
{
	EdGUIObject **newArray;
	ArrayMax+=ArrayMax/2;
	newArray=new EdGUIObject *[ArrayMax];
	
	for(int i=0;i<ObjectCount;i++) newArray[i]=ObjData[i];
	delete ObjData;
	ObjData=newArray;
}

void GUIObjectList::AddObject(EdGUIObject *gobj)
{
	if(!gobj) return;
	Verify(gobj->Parent!=this); //Prevent a double add
	if(ObjectCount>=ArrayMax) GrowArray();
	ObjData[ObjectCount++]=gobj;
	gobj->AddToMap();
	gobj->Parent=this;
	gobj->AddRefernce();
}

void GUIObjectList::DeleteAllObjects()
{
	for(int i=0;i<ObjectCount;i++) 
	{
		ObjData[i]->RemoveRefernce();
		delete ObjData[i];
		ObjData[i]=NULL;
	}
	ObjectCount=0;
}

bool GUIObjectList::IsModified()
{
	if(EdGUIObject::IsModified()) return true;
	for(int i=0;i<GetObjectCount();i++)
		if(GetObject(i)->IsModified()) { Modify(); return true;}
		
		return false;
}

void GUIObjectList::ClearModifyFlags()
{
	EdGUIObject::ClearModifyFlags();
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->ClearModifyFlags();
}


void GUIObjectList::Dissociate()
{
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->Dissociate();
	
	EdGUIObject::Dissociate();
}

void GUIObjectList::DrawAllObjects(DrawInfo &dinf,bool drawTags, unsigned long tag_flags)
{
	if(IsHidden()) return;
	int i;
	EdGUIObject *gobj;
	
	for(i=0;i<GetObjectCount();i++)
	{
		gobj=GetObject(i);
		gobj->Draw(dinf);
		if(drawTags) gobj->DrawTag(dinf,tag_flags);
	}
	
}

void GUIObjectList::DrawSelectedObjects(DrawInfo &dinf,bool drawTags, unsigned long tag_flags)
{
	if(IsHidden()) return;
	EdGUIObject::DrawSelectedObjects(dinf,drawTags,tag_flags);
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->DrawSelectedObjects(dinf,drawTags,tag_flags);
}

void GUIObjectList::DrawUnSelectedObjects(DrawInfo &dinf,bool drawTags, unsigned long tag_flags)
{
	if(IsHidden()) return;
	EdGUIObject::DrawUnSelectedObjects(dinf,drawTags,tag_flags);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->DrawUnSelectedObjects(dinf,drawTags,tag_flags);
}

void GUIObjectList::Draw(DrawInfo &dinf)
{
	SetPen(dinf);
	if(IsHidden()) return;
	for(int i=0;i<GetObjectCount();i++) GetObject(i)->Draw(dinf);
	EdGUIObject::Draw(dinf);
	
}

void GUIObjectList::DrawTileViolation(DrawInfo &dinf)
{
	if(IsHidden()) return;
	for(int i=0;i<GetObjectCount();i++) GetObject(i)->DrawTileViolation(dinf);

}

void GUIObjectList::DrawTagInternal(DrawInfo &dinf, unsigned long tag_flags)
{	
	Point3D tl,br,tr,bl;
	GetExtents(&tl,&br);
	tr.x=br.x;
	tr.y=0.0f;
	tr.z=tl.z;
	
	bl.x=tl.x;
	bl.y=0.0f;
	bl.z=br.z;
	
	dinf.GetDC()->MoveTo(dinf.vinf.DataToScreen(tl));
	dinf.GetDC()->LineTo(dinf.vinf.DataToScreen(tr));
	dinf.GetDC()->LineTo(dinf.vinf.DataToScreen(br));
	dinf.GetDC()->LineTo(dinf.vinf.DataToScreen(bl));
	dinf.GetDC()->LineTo(dinf.vinf.DataToScreen(tl));
	
	for(int i=0;i<GetObjectCount();i++) GetObject(i)->DrawTag(dinf,tag_flags);
	
	CorrectGroupPosition(br);
	EdGUIObject::DrawTag(dinf,tag_flags);
	
}

void GUIObjectList::DrawTag(DrawInfo &dinf, unsigned long tag_flags)
{
	if(IsHidden() || GetObjectCount()==0) return;
	DrawTagInternal(dinf,tag_flags);
}

void GUIObjectList::CorrectGroupPosition(Point3D &pnt)
{
	SetPos(pnt); 
}

void GUIObjectList::InsertObjectAt(int idx,EdGUIObject *gobj)
{
	Verify(idx<=GetObjectCount());
	AddObject(gobj);
	
	int i;
	
	for(i=GetObjectCount();i>idx;i--)
		ObjData[i]=ObjData[i-1];
	
	ObjData[idx]=gobj;	
}

void GUIObjectList::Select()		
{
	if(GetObjectCount()==0) 
	{
		EdGUIObject::Select();
	}
	else
	{
		for(int i=0;i<GetObjectCount();i++) GetObject(i)->Select();
		CorrectSelectionState();
	}
}	

void GUIObjectList::DeSelect()		
{
	if(GetObjectCount()==0) 
	{
		EdGUIObject::DeSelect();
	}
	else
	{
		for(int i=0;i<GetObjectCount();i++) GetObject(i)->DeSelect();
		CorrectSelectionState();
	}
}	

void GUIObjectList::ToggleSelect() 
{
	for(int i=0;i<GetObjectCount();i++) GetObject(i)->ToggleSelect();
	CorrectSelectionState();
}	

int GUIObjectList::DeSelect(Point3D &pnt)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->DeSelect(pnt);
	selcount+=CorrectSelectionState();
	return selcount;
}

int GUIObjectList::SelectionDepth(Point3D &pnt)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->SelectionDepth(pnt);
	selcount+=EdGUIObject::SelectionDepth(pnt);
	return selcount;
}

int GUIObjectList::SelectionCount() 
{
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->SelectionCount();
	if(IsSelected()) selcount++;
	return selcount;
}

int GUIObjectList::ObjectDepth(Point3D &pnt)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->ObjectDepth(pnt);
	return selcount;
}

int GUIObjectList::Select(Point3D &pnt)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount() && selcount==0;i++) selcount+=GetObject(i)->Select(pnt);
	selcount+=CorrectSelectionState();
	return selcount;
}

int GUIObjectList::ToggleSelect(Point3D &pnt)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->ToggleSelect(pnt);
	selcount+=CorrectSelectionState();
	return selcount;
}

int GUIObjectList::Select(Point3D &topleft,Point3D &bottomright)	
{
	if(IsHidden()) return 0;
	int selcount=0;
	for(int i=0;i<GetObjectCount();i++) selcount+=GetObject(i)->Select(topleft,bottomright);
	selcount+=CorrectSelectionState();
	return selcount;
}

EdGUIObject *GUIObjectList::FindFirstSelected() 
{ 
	if(IsSelected()) return this;
	EdGUIObject *retobj;
	for(int i=0;i<GetObjectCount();i++) 
	{
		retobj=GetObject(i)->FindFirstSelected();
		if(retobj) return retobj;
	}
	return NULL;
}


bool GUIObjectList::AllSelected()
{
	for(int i=0;i<GetObjectCount();i++) 
		if( !GetObject(i)->IsSelected()) return false;
		
		return true;
}

int GUIObjectList::CorrectSelectionState()
{
	if(GetObjectCount()>0) 
	{
		Selected=AllSelected();
	}
	
	return EdGUIObject::IsSelected()?1:0;
}

EdGUIObject *GUIObjectList::Clone()
{
	GUIObjectList *obj=new GUIObjectList;
	obj->Copy(*this);
	return obj;
}

void GUIObjectList::Copy(GUIObjectList &obj)
{
	EdGUIObject::Copy(obj);
	
	DeleteAllObjects();
	
	if(ObjData) delete ObjData;
	
	ObjectCount=obj.ObjectCount;
	ArrayMax=obj.ArrayMax;
	
	ObjData=new EdGUIObject *[ArrayMax];
	
	for(int i=0;i<GetObjectCount();i++)
	{
		ObjData[i]=obj.GetObject(i)->Clone();
		ObjData[i]->Parent=this;
		ObjData[i]->AddRefernce();
	}
}


HTREEITEM GUIObjectList::AddToBranch(CTreeCtrl *tree,HTREEITEM itm)
{
	if(IsHidden()) return 0;
	
	
	MyItem=tree->InsertItem(GetName(),itm);
	tree->SetItemData(MyItem,(unsigned long)(void *)this);
	tree->SetItemImage(MyItem,GetUnSelIcon(),GetSelIcon());
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->AddToBranch(tree,MyItem);
	
	return MyItem;
}

void GUIObjectList::OffsetSelection(Point3D &vct)
{
	if(IsHidden()) return;
	if(IsSelected()) 
	{
		EdGUIObject::OffsetBy(vct);
		//		return;
	}
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->OffsetSelection(vct);
}

void GUIObjectList::RotateSelection(UnitQuaternion &rot)
{
	if(IsHidden()) return ;
	if(IsSelected()) 
	{
		EdGUIObject::RotateBy(rot);
		//		return;
	}
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->RotateSelection(rot);
	
}

void GUIObjectList::SavePosIfSelected(UndoCommand **cmd)
{
	if(IsHidden()) return;
	EdGUIObject::SavePosIfSelected(cmd);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SavePosIfSelected(cmd);
}

void GUIObjectList::SaveRotIfSelected(UndoCommand **cmd)
{
	if(IsHidden()) return;
	EdGUIObject::SaveRotIfSelected(cmd);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SaveRotIfSelected(cmd);
}

void GUIObjectList::SetSnapToGround(bool snap,bool preservepos) 
{
	EdGUIObject::SetSnapToGround(snap,preservepos);
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SetSnapToGround(snap,preservepos);
}

void GUIObjectList::SetHoverElevation(float elev) 
{
	EdGUIObject::SetHoverElevation(elev);
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SetHoverElevation(elev);
}

void GUIObjectList::RotateAround(Point3D &pnt,UnitQuaternion &vct)
{
	if(IsHidden()) return ;
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->RotateAround(pnt,vct);
}

void GUIObjectList::RotateSelectionAround(Point3D &pnt,UnitQuaternion &vct)
{
	if(IsHidden()) return ;
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->RotateSelectionAround(pnt,vct);
}


bool GUIObjectList::InTree(EdGUIObject *gobj)
{
	if(EdGUIObject::InTree(gobj))
		return true;
	else
		for(int i=0;i<GetObjectCount();i++)
			if(GetObject(i)->InTree(gobj)) return true;
			
			return false;
}

bool GUIObjectList::GetSelectionExtents(Point3D *tl,Point3D *br)
{
	if(GetObjectCount()==0 || !ContainsSelection()) return false;
	
	bool foundsel=false;
	EdGUIObject *gobj;	
	
	Point3D tl2,br2;
	for(int i=0;i<GetObjectCount();i++)
	{
		gobj=GetObject(i);	
		if(gobj->IsSelected())
		{
			gobj->GetExtents(&tl2,&br2);
			if(foundsel==false) 
			{
				*tl=tl2; 
				*br=br2; 
				foundsel=true;
			} 
			else
			{
				tl->x=tl->x<tl2.x?tl->x:tl2.x;
				tl->z=tl->z<tl2.z?tl->z:tl2.z;
				
				br->x=br->x>br2.x?br->x:br2.x;
				br->z=br->z>br2.z?br->z:br2.z;
			}
		}
		else
		{	
			if(gobj->GetSelectionExtents(&tl2,&br2))
				if(foundsel==false) 
				{
					*tl=tl2; 
					*br=br2; 
					foundsel=true;
				} 
				else
				{
					tl->x=tl->x<tl2.x?tl->x:tl2.x;
					tl->z=tl->z<tl2.z?tl->z:tl2.z;
					
					br->x=br->x>br2.x?br->x:br2.x;
					br->z=br->z>br2.z?br->z:br2.z;
				}
				
		}
	}
	
	return true;
}

void GUIObjectList::GetExtents(Point3D *tl,Point3D *br)
{
	if(GetObjectCount()==0) 
	{
		*tl=*br=GetPos();
		return;
	}
	
	
	GetObject(0)->GetExtents(tl,br);
	Point3D tl2,br2;
	
	for(int i=1;i<GetObjectCount();i++)
	{
		GetObject(i)->GetExtents(&tl2,&br2);
		tl->x=tl->x<tl2.x?tl->x:tl2.x;
		tl->z=tl->z<tl2.z?tl->z:tl2.z;
		
		br->x=br->x>br2.x?br->x:br2.x;
		br->z=br->z>br2.z?br->z:br2.z;
	}
}


EdGUIObject *GUIObjectList::AddNodeIfSelected(Point3D &pnt,bool insert)
{
	if(IsHidden()) return false;
	EdGUIObject *ret=NULL,*tmp;
	for(int i=0;i<GetObjectCount();i++)
		if(tmp=GetObject(i)->AddNodeIfSelected(pnt,insert)) ret=tmp;
		
		return ret;
}

EdGUIObject *GUIObjectList::GetObjAtPoint(Point3D &pnt) 
{
	if(IsHidden()) return NULL;
	EdGUIObject *gobj=NULL;
	for(int i=0;i<GetObjectCount() && gobj==NULL;i++)
		gobj=GetObject(i)->GetObjAtPoint(pnt);
	return gobj;
}	

bool GUIObjectList::ContainsSelection()
{
	for(int i=0;i<GetObjectCount();i++)
	{
		Verify(GetObject(i)!=NULL);
		if(GetObject(i)->ContainsSelection() ||
			GetObject(i)->IsSelected() ) 
			return true;
	}	
	return false;
}

int GUIObjectList::GroupSelected(UndoCommand **cmd)
{
	if(IsHidden()) return 0;
	if(IsSelected()) return 0;
	
	int grptot=0;
	
	if(HoldsSelected())
	{
		GUIObjectList *newlst=new GUIObjectList();
		AddObject(newlst);
		AddCommand *acmd=new AddCommand(newlst);
		if(*cmd==NULL) *cmd=acmd; else (*cmd)->Attach(acmd);
		
		EdGUIObject *gobj;
		bool found=true;
		
		while(found)
		{
			found=false;
			for(int i=0;i<GetObjectCount() && !found;i++)
			{
				gobj=GetObject(i);
				if(gobj->IsSelected())
				{
					gobj->SaveGroup(cmd);
					RemoveObject(i);
					newlst->AddObject(gobj);
					found=true;
					grptot++;
				}
			}
		}
		
		newlst->Select();
	}
	else
	{
		for(int i=0;i<GetObjectCount();i++)
			grptot+=GetObject(i)->GroupSelected(cmd);
	}
	
	return grptot;
}

void GUIObjectList::PruneEmpty(UndoCommand **cmd)
{
	bool restart=true;
	
	while(restart)
	{
		restart=false;
		for(int i=0;i<GetObjectCount() && restart==false;i++)
		{
			if(GetObject(i)->IsEmpty())
			{
				DeleteCommand *newcom=new DeleteCommand(GetObject(i));
				RemoveObject(i);
				Modify();
				if(*cmd==NULL) *cmd=newcom; else (*cmd)->Attach(newcom);
				restart=true;
			}
		}
		
	}
	
}

bool GUIObjectList::UnGroupSelected(UndoCommand **cmd)
{
	if(IsHidden() || GetObjectCount()==0) return false;
	bool ret=false;
	
	int i;
	int ocount=GetObjectCount();
	EdGUIObject **objary=new EdGUIObject *[ocount];
	for(i=0;i<ocount;i++) objary[i]=GetObject(i);
	
	
	for(i=0;i<ocount;i++)
	{
		ret|=objary[i]->UnGroupSelected(cmd);
	}
	
	delete objary;
	return ret|EdGUIObject::UnGroupSelected(cmd);
}

bool GUIObjectList::HoldsSelected()
{
	for(int i=0;i<GetObjectCount();i++)
		if(GetObject(i)->IsSelected()) return true;
		
		return false;
}

/*
void GUIObjectList::SetHide(bool val)
{
EdGUIObject::SetHide(bool val);
for(int i=0;i<GetObjectCount();i++)
GetObject(i)->SetHide(val);
}

*/


bool GUIObjectList::IsMultiZone()
{
	for(int i=0;i<GetObjectCount();i++)
		if(GetObject(i)->IsMultiZone()) return true;
		
		return false;
}

bool GUIObjectList::IsMultiTile()		
{
	for(int i=0;i<GetObjectCount();i++)
		if(GetObject(i)->IsMultiTile()) return true;
		
		return false;
}

void GUIObjectList::SelectMultiZone()	
{
	//	DeSelect();
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SelectMultiZone();
	
}

void GUIObjectList::SelectMultiTile()	
{
	//	DeSelect();
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SelectMultiTile();
}

void GUIObjectList::Freeze()
{
	EdGUIObject::Freeze();
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->Freeze();
	
}

void GUIObjectList::UnFreeze() 
{
	EdGUIObject::UnFreeze();
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->UnFreeze();
}

bool GUIObjectList::SelectNext(Point3D &pnt,bool found)
{
	
	EdGUIObject *gobj;
	for(int i=0;i<GetObjectCount();i++)
	{
		gobj=GetObject(i);
		if(!found)
		{
			found=gobj->SelectNext(pnt,found);
		}
		else
		{
			if(gobj->SelectNext(pnt,found)) return true;
		}
		
	}
	
	return false;
}

EdGUIObject *GUIObjectList::SelectClosestInternal(Point3D &pnt,Scalar *crat)
{
	EdGUIObject *sel=NULL,*newsel;
	EdGUIObject *gobj;
	for(int i=0;i<GetObjectCount();i++)
	{
		gobj=GetObject(i);
		newsel=gobj->SelectClosestInternal(pnt,crat);
		if(newsel) sel=newsel;
		
	}
	
	return sel;
}


void GUIObjectList::SaveTextInfo(NotationFile *not_file)
{
	Page *page=not_file->SetPage((char *)(LPCSTR)GetName());
	NotationFile mem_not;
	
	page->SetEntry("Type","Group");
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SaveTextInfo(&mem_not);
	
	page->SetEntry("Members",&mem_not);
	
}

void GUIObjectList::UpdateVisibility(DWORD visflag) 
{
	int i;
	bool hflag=true;
	EdGUIObject* object;
	for (i=0;i<GetObjectCount();++i)
		  {
		object = GetObject(i);
		object->UpdateVisibility(visflag);
		if(!object->IsHidden()) hflag=false;
	}
	
	EdGUIObject::SetHide(hflag);
}

void GUIObjectList::MoveToGroup(EdGUIObject *gobj)
{
	if(gobj==this) return;
	Verify(gobj->Parent!=NULL);
	gobj->Parent->RemoveObject(gobj);
	AddObject(gobj);
}



void GUIObjectList::AddPanel(CObjPanelDlg *pnl,UndoCommand **cmd)
{
	pnl->AddPanel(new CTNPanel(this,cmd));
	pnl->AddPanel(new CGroupProps(this,cmd));
}

GUIObjectList *GUIObjectList::PlaceInGroup(EdGUIObject *gobj)
{
	if(this != gobj->Parent) return NULL;
	Verify(gobj->Parent!=NULL);
	RemoveObject(gobj);
	GUIObjectList *olist=new GUIObjectList;
	
	AddObject(olist);
	olist->AddObject(gobj);
	return olist;
}

void GUIObjectList::SortChildren()
{
	for(int i=0;i<GetObjectCount();i++)	
		GetObject(i)->SortChildren();
	
	qsort(ObjData,GetObjectCount(),sizeof(EdGUIObject *),CompareGUIObjs);
	
}

bool GUIObjectList::DeleteSelected(UndoCommand **delcom)
{
	
	bool doagain=true,ret=false;
	EdGUIObject *gobj;
	DeleteCommand *newcom;
	while(doagain)
	{
		doagain=false;
		
		for(int i=0;i<GetObjectCount() && doagain==false;i++)
		{
			gobj=GetObject(i);
			
			
			if(gobj->IsSelected())
			{
				newcom=new DeleteCommand(gobj);
				RemoveObject(i);
				Modify();
				doagain=true;
				ret=true;
				if(*delcom==NULL) *delcom=newcom;
				else (*delcom)->Attach(newcom);
			}
			else
				if(gobj->ContainsSelection())
				{
					gobj->DeleteSelected(delcom);
				}
				
		}
		
	}
	
	return ret;
}

bool GUIObjectList::DeleteSelected()
{
	
	bool doagain=true,ret=false;
	EdGUIObject *gobj;
	while(doagain)
	{
		doagain=false;
		
		for(int i=0;i<GetObjectCount() && doagain==false;i++)
		{
			gobj=GetObject(i);
			
			
			if(gobj->IsSelected())
			{
				RemoveObject(i);
				Modify();
				doagain=true;
				ret=true;
			}
			else
				if(gobj->ContainsSelection())
				{
					gobj->DeleteSelected();
				}
				
		}
		
	}
	
	return ret;
}


void GUIObjectList::OffsetBy( Point3D &pnt)
{
	if(IsHidden()) return;
	EdGUIObject::OffsetBy(pnt);
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->OffsetBy(pnt);
	
}

void GUIObjectList::RotateBy(UnitQuaternion &rot)
{
	if(IsHidden()) return;
	EdGUIObject::RotateBy(rot);
	
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->RotateBy(rot);
}


void GUIObjectList::RemoveFromMap()
{
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->RemoveFromMap();
	
	EdGUIObject::RemoveFromMap();
}

void GUIObjectList::AddToMap()
{
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->AddToMap();
	
	EdGUIObject::AddToMap();
}

void GUIObjectList::CopySelectionToList(GUIObjectList *list)
{
	if(IsSelected())
	{
		EdGUIObject::CopySelectionToList(list);
	}
	else
	{
		for(int i=0;i<GetObjectCount();i++)
			GetObject(i)->CopySelectionToList(list);
	}
}

void GUIObjectList::SyncData()
{
	EdGUIObject::SyncData();
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SyncData();
}

void GUIObjectList::SnapObjectToGround(bool overrideflag)
{
	EdGUIObject::SnapObjectToGround(overrideflag);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SnapObjectToGround(overrideflag);
	
}

void GUIObjectList::GetMultiPropsIfSelected(CObjProps *props) 
{
	EdGUIObject::GetMultiPropsIfSelected(props);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->GetMultiPropsIfSelected(props);
}

void GUIObjectList::SetMultiPropsIfSelected(CObjProps *props,UndoCommand **cmd) 
{
	EdGUIObject::SetMultiPropsIfSelected(props,cmd);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SetMultiPropsIfSelected(props,cmd);
	
}

void GUIObjectList::GetMultiProps(CObjProps *props)
{
	EdGUIObject::GetMultiProps(props);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->GetMultiProps(props);
}

void GUIObjectList::SetMultiProps(CObjProps *props,UndoCommand **cmd)
{
	EdGUIObject::SetMultiProps(props,cmd);
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->SetMultiProps(props,cmd);
}

void GUIObjectList::DefaultExtraData()
{
	EdGUIObject::DefaultExtraData();
	for(int i=0;i<GetObjectCount();i++)
		GetObject(i)->DefaultExtraData();
	
}

EdGUIObject *GUIObjectList::GetObjectByName(CString name)
{
	EdGUIObject *obj=EdGUIObject::GetObjectByName(name);
	if(obj) return obj;

	for(int i=0;i<GetObjectCount();i++)
	{
		obj=GetObject(i)->GetObjectByName(name);
		if(obj) return obj;
	}
return NULL;
}

