#include "tk_text.h"

const Ptr<TkOptionDef> TkText::OPT_AUTOSEPARATORS=0;
const Ptr<TkOptionDef> TkText::OPT_BACKGROUND=new TkOptionDef("background",TkColorOption::type);
const Ptr<TkOptionDef> TkText::OPT_BORDERWIDTH=new TkOptionDef("borderwidth",TkIntOption::type);
const Ptr<TkOptionDef> TkText::OPT_CURSOR=0;
const Ptr<TkOptionDef> TkText::OPT_FONT=0;
const Ptr<TkOptionDef> TkText::OPT_FOREGROUND=new TkOptionDef("foreground",TkColorOption::type);
const Ptr<TkOptionDef> TkText::OPT_HEIGHT=new TkOptionDef("height",TkIntOption::type);
const Ptr<TkOptionDef> TkText::OPT_INSERTOFFTIME=0;
const Ptr<TkOptionDef> TkText::OPT_INSERTONTIME=0;
const Ptr<TkOptionDef> TkText::OPT_INSERTWIDTH=0;
const Ptr<TkOptionDef> TkText::OPT_MAXUNDO=0;
const Ptr<TkOptionDef> TkText::OPT_PADX=new TkOptionDef("padx",TkIntOption::type);
const Ptr<TkOptionDef> TkText::OPT_PADY=new TkOptionDef("pady",TkIntOption::type);
const Ptr<TkOptionDef> TkText::OPT_RELIEF=new TkOptionDef("relief",TkReliefOption::type);
const Ptr<TkOptionDef> TkText::OPT_SELECTBACKGROUND=new TkOptionDef("selectbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkText::OPT_SELECTBORDERWIDTH=new TkOptionDef("selectborderwidth",TkColorOption::type);
const Ptr<TkOptionDef> TkText::OPT_SELECTFOREGROUND=new TkOptionDef("selectforeground",TkColorOption::type);
const Ptr<TkOptionDef> TkText::OPT_SETGRID=0;
const Ptr<TkOptionDef> TkText::OPT_SPACING1=0;
const Ptr<TkOptionDef> TkText::OPT_SPACING2=0;
const Ptr<TkOptionDef> TkText::OPT_SPACING3=0;
const Ptr<TkOptionDef> TkText::OPT_STATE=new TkOptionDef("state",TkStateOption::type);
const Ptr<TkOptionDef> TkText::OPT_TABS=0;
const Ptr<TkOptionDef> TkText::OPT_TAKEFOCUS=0;
const Ptr<TkOptionDef> TkText::OPT_UNDO=0;
const Ptr<TkOptionDef> TkText::OPT_WIDTH=new TkOptionDef("width",TkIntOption::type);
const Ptr<TkOptionDef> TkText::OPT_WRAP=new TkOptionDef("wrap",TkWrapOption::type);
const Ptr<TkOptionDef> TkText::OPT_XSCROLLCOMMAND=new TkOptionDef("xscrollcommand",TkStringOption::type);
const Ptr<TkOptionDef> TkText::OPT_YSCROLLCOMMAND=new TkOptionDef("yscrollcommand",TkStringOption::type);

const Ptr<TkOptionDef> TkText::optAll[]=
{
	TkText::OPT_AUTOSEPARATORS,
	TkText::OPT_BACKGROUND,
	TkText::OPT_BORDERWIDTH,
	TkText::OPT_CURSOR,
	TkText::OPT_FONT,
	TkText::OPT_FOREGROUND,
	TkText::OPT_HEIGHT,
	TkText::OPT_INSERTOFFTIME,
	TkText::OPT_INSERTONTIME,
	TkText::OPT_INSERTWIDTH,
	TkText::OPT_MAXUNDO,
	TkText::OPT_PADX,
	TkText::OPT_PADY,
	TkText::OPT_RELIEF,
	TkText::OPT_SELECTBACKGROUND,
	TkText::OPT_SELECTBORDERWIDTH,
	TkText::OPT_SELECTFOREGROUND,
	TkText::OPT_SETGRID,
	TkText::OPT_SPACING1,
	TkText::OPT_SPACING2,
	TkText::OPT_SPACING3,
	TkText::OPT_TABS,
	TkText::OPT_TAKEFOCUS,
	TkText::OPT_UNDO,
	TkText::OPT_WIDTH,
	TkText::OPT_WRAP,
	TkText::OPT_XSCROLLCOMMAND,
	TkText::OPT_YSCROLLCOMMAND
};

const char* TkText::type="TkText";
const char* TkText::tkClass="text";

const Ptr<TkOptionDef>* TkText::getOptionAll() const
{
	return TkText::optAll;
}

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

const char* TkText::getTkClass() const
{
	return TkText::tkClass;
}

void TkText::doSerialize(TiXmlElement& element) const
{
	TkBase::doSerialize(element);

	element.SetAttribute("text",getText().c_str());
}

void TkText::doDeserialize(const TiXmlElement& element)
{
	TkBase::doDeserialize(element);

	setText(element.Attribute("text"));
}

TObject* TkText::clone()
{
	TkBase* obj=new TkText(interp);

	baseClone(obj);

	return obj;
}

void TkText::setEnabled(bool value)
{
	if (value)
		setOption(TkText::OPT_STATE,new TkStateOption(TkStateOption::normal));
	else
		setOption(TkText::OPT_STATE,new TkStateOption(TkStateOption::disabled));	
}

bool TkText::isEnabled() const
{
	const string enabled=getStrOption(TkText::OPT_STATE);
	if (enabled=="disabled") return false;
	return true;
}

void TkText::create()
{
	TkBase::create();
	createXBar(interp,this,OPT_XSCROLLCOMMAND);
	createYBar(interp,this,OPT_YSCROLLCOMMAND);
}

void TkText::setBackgroundColor(TkColorOption* color)
{
	setOption(OPT_BACKGROUND,color);
}

Ptr<TkColorOption>& TkText::getBackgroundColor() const
{
	TkColorOption* colorOption=new TkColorOption();
	
	colorOption->fromObject(getOption(OPT_BACKGROUND));
	return *(new Ptr<TkColorOption>(colorOption));
}
