#include "tk_grid_constraints.h"
#include "../../exceptions/exceptions.h"

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

const int TkGridConstraints::NORTH=(1<<1);
const int TkGridConstraints::SOUTH=(1<<2);
const int TkGridConstraints::EAST =(1<<3);
const int TkGridConstraints::WEST =(1<<4);

string& TkGridConstraints::toString() const
{
	char pos_buf[256];
	sprintf(pos_buf,"-row %d -column %d -rowspan %d -columnspan %d",row,column,rowspan,columnspan);
		
	string& res=*(new string(pos_buf));

	if (flags)
	{
		res.append(" -sticky ");
		if (flags&NORTH) res.append("n");
		if (flags&EAST)  res.append("e");
		if (flags&WEST)  res.append("w");
		if (flags&SOUTH) res.append("s");
	}

	return res;
}


void TkGridConstraints::doSerialize(TiXmlElement& element) const
{
	element.SetAttribute("row",row);
	element.SetAttribute("column",column);
	element.SetAttribute("rowspan",rowspan);
	element.SetAttribute("columnspan",columnspan);

	element.SetAttribute("flags",flags);
}
	
void TkGridConstraints::doDeserialize(const TiXmlElement& element)
{
	if (strcmp(element.Value(),type)!=0) throw new ExceptionType(type,element.Value());
	
	element.Attribute("row",&row);
	element.Attribute("column",&column);
	element.Attribute("rowspan",&rowspan);
	element.Attribute("columnspan",&columnspan);

	element.Attribute("flags",&flags);
}


TObject* TkGridConstraints::clone()
{
	TkGridConstraints* gc=new TkGridConstraints();

	gc->row=row;
	gc->column=column;
	gc->rowspan=rowspan;
	gc->columnspan=columnspan;

	gc->flags=flags;

	return gc;
}
