#include "tk_button.h"
#include "tk_checkbutton.h"
#include "tk_entry.h"
#include "tk_frame.h"
#include "tk_label.h"
#include "tk_listbox.h"
#include "tk_radiobutton.h"
#include "tk_spinbox.h"
#include "tk_text.h"

#include "tk_container.h"

Ptr<TclStringVar>& TkContainer::getRadioGroupByName(const string& name)
{
	t_groups::iterator it=radioGroups.find(name);
	if (it==radioGroups.end())
	{
		Ptr<TclStringVar> var=new TclStringVar(interp);
		radioGroups[name]=var;
		return radioGroups[name];
	}
	else
	{
        return radioGroups[name];
	}
}

void TkContainer::doLayout()
{
	if (created)
	{
		for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
		{
			manager->manageLayout((TclInterp*)interp,this,*it,(TObject*)(*it)->getConstraints());
			(*it)->displayed=true;
		}
		displayed=true;
	}
}

void TkContainer::addChild(TkBase* object)
{
	if (object->parent!=0)
	{
		/// fujtajblik
	}
	childs.push_back(Ptr<TkBase>(object));
	object->parent=this;
	bool eEnabled=object->isEventsEnabled();
	object->setEventsEnabled(false);
	if (created) object->create();
	if (displayed)
	{	
		manager->manageLayout(interp,this,object,constraints);
		object->displayed=true;
	}
	object->setEventsEnabled(eEnabled);
}

void TkContainer::addChild(TkBase* object,TObject* constraints)
{
	if (object->parent!=0)
	{
		/// fujtajblik
	}
	childs.push_back(Ptr<TkBase>(object));
	object->parent=this;
	object->getConstraints()=constraints;
	if (created) object->create();
	if (displayed)
	{	
		manager->manageLayout(interp,this,object,constraints);
		object->displayed=true;
	}
}

TkBase* TkContainer::getChildByCoord(int rootX,int rootY)
{
	Tk_Window wnd=Tk_CoordsToWindow(rootX,rootY,getWindow());
	if (wnd==0) return 0;
	for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
	{
		if ((*it)->getWindow()==wnd) return *it;
	}
	return 0;
}

TkBase* TkContainer::getChildByName(const string& name)
{
	for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
	{
		if ((*it)->getName()==name) return *it;
	}
	return 0;
}

void TkContainer::create()
{
	TkBase::create();
	for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
	{
		(*it)->create();
	}
	if (!manager.isNull()) doLayout();
}

void TkContainer::removeAll()
{
	if (displayed&&!manager.isNull())
	{
		for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
		{
			manager->removeComponent(interp,this,*it);
		}
	}
	childs.clear();
}

void TkContainer::removeChild(TkBase* child)
{
	for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
	{
		if (it->asObject()==child)
		{
			if (displayed&&!manager.isNull())
			{
				manager->removeComponent(interp,this,*it);
			}
			childs.erase(it);
			return;
		}
	}
}

TkLayout* TkContainer::getLayoutManager()
{
	return manager;
}


void TkContainer::setLayoutManager(TkLayout* manager)
{
	if (!this->manager.isNull()) removeAll();
	this->manager=manager;
	doLayout();
}


void TkContainer::doSerialize(TiXmlElement& element) const
{
	TkBase::doSerialize(element);
	TiXmlElement eChilds("childs");
	for(t_child_vector::const_iterator it=childs.begin();it!=childs.end();it++)
	{
		eChilds.InsertEndChild((*it)->serialize());
	}
	element.InsertEndChild(eChilds);
}

void TkContainer::doDeserialize(const TiXmlElement& element)
{
	TkBase::doDeserialize(element);
	const TiXmlElement* eChilds=element.FirstChildElement("childs");
	const TiXmlElement* child=eChilds->FirstChildElement();
	while(child!=0)
	{
		TXmlObject* xmlObject=TXmlObject::getXmlObject(*child);
		addChild((TkBase*)xmlObject);
		child=child->NextSiblingElement();
	}
}

void TkContainer::baseClone(TkBase* obj)
{
	TkBase::baseClone(obj);
	
	TkContainer* cnt=(TkContainer*)obj;
	for(t_child_vector::iterator it=childs.begin();it!=childs.end();it++)
	{
		Ptr<TkBase> clonedChild=(*it)->clone();
		cnt->addChild(clonedChild);
	}
	cnt->manager=manager;
}
