void UndoEngine::DealWithTransients(std::map<HeeksObjId,HeeksObj*> &treemap)
{
	std::map<HeeksObj*,std::list<HeeksObj*> >& map = wxGetApp().GetTransients();
	
	std::map<HeeksObj*,std::list<HeeksObj*> >::iterator it;
	std::list<HeeksObj*> needupdate;
	for(it = map.begin(); it!= map.end(); it++)
	{
		TransientObject *tobj = (TransientObject*)(*it).first;

		std::list<HeeksObj*>::iterator it2;
		for(it2 = (*it).second.begin(); it2 != (*it).second.end(); it2++)
		{
			HeeksObj* obj = (HeeksObj*)(*it2);
			std::map<HeeksObjId,HeeksObj*>::iterator it3 = treemap.find(GetHeeksObjId(obj));
			if(it3 == treemap.end())
			{
				HeeksObj* nobj = obj->MakeACopyWithID();
#ifdef MULTIPLE_OWNERS
				nobj->RemoveOwners();
#else
				nobj->m_owner = NULL;
#endif
				needupdate.push_back(nobj);
				treemap[GetHeeksObjId(nobj)] = nobj;
#ifdef MULTIPLE_OWNERS
				tobj->Owner()->Add(nobj,NULL);
#else
				tobj->m_owner->Add(nobj,NULL);
#endif

			}
			else
			{
				needupdate.push_back((*it3).second);
#ifdef MULTIPLE_OWNERS
				tobj->Owner()->Add((*it3).second, NULL);
#else
				tobj->m_owner->Add((*it3).second, NULL);
#endif
			}
		}

#ifdef MULTIPLE_OWNERS
		tobj->Owner()->Remove(tobj);
#else
		tobj->m_owner->Remove(tobj);
#endif
		//delete tobj;
	}

	//Deal with the quick pointer problem
	std::list<HeeksObj*>::iterator it2;
	for(it2 = needupdate.begin(); it2 != needupdate.end(); it2++)
	{
		HeeksObj *obj = *it2;
#ifdef MULTIPLE_OWNERS
		HeeksObj *owner = obj->GetFirstOwner();
		while(owner)
		{
			owner->ReloadPointers();
			owner = obj->GetNextOwner();
		}
#else
		if(obj->m_owner)obj->m_owner->ReloadPointers();
#endif
		obj->ReloadPointers();
	}

	wxGetApp().ClearTransients();	
}
void UndoEngine::GetModificationsRecursive(std::vector<UndoEvent> &ret,ObjList* newtree, ObjList* oldtree)
{
	std::set<HeeksObjId> new_children;
	std::set<HeeksObjId> old_children;
	std::map<HeeksObjId,HeeksObj*> new_children_map;
	std::map<HeeksObjId,HeeksObj*> old_children_map;


	HeeksObj *new_obj = newtree->GetFirstChild();
	while(new_obj)
	{
		HeeksObjId id = GetHeeksObjId(new_obj);
		new_children.insert(id);
		new_children_map[id] = new_obj;
		new_obj = newtree->GetNextChild();
	}

	HeeksObj *old_obj = oldtree->GetFirstChild();
	while(old_obj)
	{
		HeeksObjId id = GetHeeksObjId(old_obj);
		old_children.insert(id);
		old_children_map[id] = old_obj;
		old_obj = oldtree->GetNextChild();
	}

	std::set<HeeksObjId>::iterator it;
	for(it = new_children.begin(); it != new_children.end(); it++)
	{
		HeeksObj* obj = new_children_map[*it];
		m_tree.m_treemap[*it] = obj;
		if(old_children.find(*it) == old_children.end())
		{
			//TODO, this is actually tricky, when an item is added, it may be added in multiple places in the tree
			//we must make sure that multiple pointers get setup to this object, we also must deep copy
			HeeksObj* copy = obj->MakeACopyWithID();
			ret.push_back(UndoEvent(EventTypeAdd,newtree,copy));
			m_oldtree.m_treemap[*it] = copy;
		}
		else
		{
			//TODO: check if item is modified, if it is an objlist, descend
			if(!obj->GetSkipForUndo() && obj->IsDifferent(old_children_map[*it]))
			{
				HeeksObj* copy = obj->MakeACopyWithID();
				ret.push_back(UndoEvent(EventTypeModified,newtree,copy,old_children_map[*it]));
				m_oldtree.m_treemap[*it] = copy;
			}
			else
			{
				if(obj->IsList())
				{
					ObjList* newlist = (ObjList*)obj;
					ObjList* oldlist = (ObjList*)old_children_map[*it];
					if(newlist->DescendForUndo())
					{
						GetModificationsRecursive(ret,newlist,oldlist);
					}
				}
			}
		}
	}

	for(it = old_children.begin(); it != old_children.end(); it++)
	{
		HeeksObj* obj = old_children_map[*it];
		if(new_children.find(*it) == new_children.end())
			ret.push_back(UndoEvent(EventTypeRemove,newtree,obj->MakeACopyWithID()));
		m_oldtree.m_treemap[*it] = obj;
	}

	//DealWithTransients();
	wxGetApp().ClearTransients();
}