#include "tk_place_constraints.h"
#include "../../exceptions/exceptions.h"

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

void TkPlaceConstraints::doSerialize(TiXmlElement& element) const
{
	element.SetDoubleAttribute("width",width);
	element.SetDoubleAttribute("height",height);
	element.SetDoubleAttribute("x",x);
	element.SetDoubleAttribute("y",y);
	element.SetAttribute("relSize",relSize?"1":"0");
	element.SetAttribute("relPosition",relPosition?"1":"0");
}
	
void TkPlaceConstraints::doDeserialize(const TiXmlElement& element)
{
	if (strcmp(element.Value(),type)!=0) throw new ExceptionType(type,element.Value());
	
	element.Attribute("width",&width);
	element.Attribute("height",&height);
	element.Attribute("x",&x);
	element.Attribute("y",&y);

	relSize=element.Attribute("relSize")[0]=='1';
	relPosition=element.Attribute("relPosition")[0]=='1';
}

string& TkPlaceConstraints::toString() const
{
	char pos_buf[255];
	if (relPosition)
		sprintf(pos_buf,"-relx %f -rely %f ",x,y);
	else
		sprintf(pos_buf,"-x %f -y %f ",x,y);
	char siz_buf[255];
	if (relSize)
		sprintf(siz_buf,"-relwidth %f -relheight %f",width,height);
	else
		sprintf(siz_buf,"-width %f -height %f",width,height);

	string& res=*(new string(pos_buf));
	res.append(siz_buf);

	return res;
}

TObject* TkPlaceConstraints::clone()
{
	TkPlaceConstraints* pc=new TkPlaceConstraints();

	pc->width=width;
	pc->height=height;
	pc->x=x;
	pc->y=y;

	pc->relPosition=relPosition;
	pc->relSize=relSize;

	return pc;
}


