#include "txmlobject.h"
#include "exceptions/exception.h"

#include "tk/gui/gui.h"

TXmlObject::t_ds_map TXmlObject::dsMap;
TXmlObject::t_name_map TXmlObject::nameMap;

const char* TXmlObject::type="TXmlObject";

const char* TXmlObject::getType() const
{
	return type;
}

bool TXmlObject::hasParent(const char* className) const
{
	return className==TObject::type;
}

void TXmlObject::registerClass(const char* className,t_xml_factory method)
{
	if (dsMap.find(className)!=dsMap.end())
	{
		throw new Exception(type,"Failed to register class %s deserialization handler ! Already registed !",className);
	}
	dsMap[className]=method;
	nameMap[className]=className;
}

TXmlObject* TXmlObject::getXmlObject(const TiXmlElement& element)
{
	t_ds_map::iterator it=dsMap.find(element.Value());
	if (it==dsMap.end()) return 0;
	TXmlObject* xmlObject=it->second();

	xmlObject->doDeserialize(element);

	return xmlObject;
}

const char* TXmlObject::getClassUid(const string& name)
{
	t_name_map::iterator it=nameMap.find(name);
	if (it==nameMap.end()) return 0;

	return it->second;
}

#define enregister(className) registerClass(className::type,className::newObject)

void TXmlObject::registerAll()
{
	enregister(TkPlaceLayout);
	enregister(TkGridLayout);
	
	enregister(TkPlaceConstraints);
	enregister(TkGridConstraints);

	enregister(TkButton);
	enregister(TkCheckButton);
	enregister(TkEntry);
	enregister(TkFrame);
	enregister(TkLabel);
	enregister(TkListbox);
	enregister(TkRadioButton);
	enregister(TkSpinBox);
	enregister(TkScrollBar);
	enregister(TkText);
	
	enregister(TkTopLevel);

	//designer classes
	enregister(MyVarDef);
	enregister(MyModuleType);
	
	//vars
	enregister(TclBoolVar);
	enregister(TclIntVar);
	enregister(TclDoubleVar);
	enregister(TclStringVar);
	enregister(TclListVar);

	enregister(TkOptionDef);

	//options
	enregister(TkBoolOption);
	enregister(TkColorOption);
	enregister(TkDoubleOption);
	enregister(TkIntOption);
	enregister(TkStringOption);
	enregister(TkListOption);

	enregister(TkDestroyProtocolOption);
	enregister(TkRectOption);

	//enum options
	enregister(Tk3StateOption);
	enregister(TkAnchorOption);
	enregister(TkCompoundOption);
	enregister(TkJustifyOption);
	enregister(TkOrientOption);
	enregister(TkReliefOption);
	enregister(TkStateOption);
	enregister(TkValidationTypeOption);
	enregister(TkWinStateOption);
	enregister(TkWrapOption);
}
