#include "tk_entry.h"

const Ptr<TkOptionDef> TkEntry::OPT_BACKGROUND=new TkOptionDef("background",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_BORDERWIDTH=new TkOptionDef("borderwidth",TkIntOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_CURSOR=0;
const Ptr<TkOptionDef> TkEntry::OPT_DISABLEDBACKGROUND=new TkOptionDef("disabledbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_DISABLEDFOREGROUND=new TkOptionDef("disabledforeground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_EXPORTSELECTION=0;
const Ptr<TkOptionDef> TkEntry::OPT_FONT=0;
const Ptr<TkOptionDef> TkEntry::OPT_FOREGROUND=new TkOptionDef("foreground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_HIGHLIGHTBACKGROUND=new TkOptionDef("highlightbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_HIGHLIGHTCOLOR=new TkOptionDef("highlightcolor",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_HIGHLIGHTTHICKNESS=new TkOptionDef("highlightthickness",TkIntOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_INSERTBACKGROUND=new TkOptionDef("insertbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_INSERTBORDERWIDTH=0;
const Ptr<TkOptionDef> TkEntry::OPT_INSERTONTIME=0;
const Ptr<TkOptionDef> TkEntry::OPT_INSERTWIDTH=0;
const Ptr<TkOptionDef> TkEntry::OPT_INVALIDCOMMAND=0;
const Ptr<TkOptionDef> TkEntry::OPT_JUSTIFY=new TkOptionDef("justify",TkJustifyOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_READONLYBACKGROUND=new TkOptionDef("readonlybackground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_RELIEF=new TkOptionDef("relief",TkReliefOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_SELECTBACKGROUND=new TkOptionDef("selectbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_SELECTBORDERWIDTH=0;
const Ptr<TkOptionDef> TkEntry::OPT_SELECTFOREGROUND=new TkOptionDef("selectforeground",TkColorOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_SHOW=0;
const Ptr<TkOptionDef> TkEntry::OPT_STATE=new TkOptionDef("state",Tk3StateOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_TAKEFOCUS=0;
const Ptr<TkOptionDef> TkEntry::OPT_TEXTVARIABLE=new TkOptionDef("textvariable",TclStringVar::type,true,false);;
const Ptr<TkOptionDef> TkEntry::OPT_VALIDATE=new TkOptionDef("validate",TkValidationTypeOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_VALIDATECOMMAND=new TkOptionDef("validatecommand",TkStringOption::type,false);
const Ptr<TkOptionDef> TkEntry::OPT_WIDTH=new TkOptionDef("width",TkIntOption::type);
const Ptr<TkOptionDef> TkEntry::OPT_XSCROLLCOMMAND=0;

const Ptr<TkOptionDef> TkEntry::optAll[]=
{
	TkEntry::OPT_BACKGROUND,
	TkEntry::OPT_BORDERWIDTH,
	TkEntry::OPT_CURSOR,
	TkEntry::OPT_DISABLEDBACKGROUND,
	TkEntry::OPT_DISABLEDFOREGROUND,
	TkEntry::OPT_EXPORTSELECTION,
	TkEntry::OPT_FONT,
	TkEntry::OPT_FOREGROUND,
	TkEntry::OPT_HIGHLIGHTBACKGROUND,
	TkEntry::OPT_HIGHLIGHTCOLOR,
	TkEntry::OPT_HIGHLIGHTTHICKNESS,
	TkEntry::OPT_INSERTBACKGROUND,
	TkEntry::OPT_INSERTBORDERWIDTH,
	TkEntry::OPT_INSERTONTIME,
	TkEntry::OPT_INSERTWIDTH,
	TkEntry::OPT_INVALIDCOMMAND,
	TkEntry::OPT_JUSTIFY,
	TkEntry::OPT_READONLYBACKGROUND,
	TkEntry::OPT_RELIEF,
	TkEntry::OPT_SELECTBACKGROUND,
	TkEntry::OPT_SELECTBORDERWIDTH,
	TkEntry::OPT_SELECTFOREGROUND,
	TkEntry::OPT_SHOW,
	TkEntry::OPT_STATE,
	TkEntry::OPT_TAKEFOCUS,
	TkEntry::OPT_TEXTVARIABLE,
	TkEntry::OPT_VALIDATE,
	TkEntry::OPT_VALIDATECOMMAND,
	TkEntry::OPT_WIDTH,
	TkEntry::OPT_XSCROLLCOMMAND
};

const char* TkEntry::type="TkEntry";
const char* TkEntry::tkClass="entry";

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

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

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

void TkEntry::doSerialize(TiXmlElement& element) const
{
	TkBase::doSerialize(element);
	
	element.SetAttribute("text",getText().c_str());
}

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

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

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

	baseClone(obj);

	return obj;
}

TkEntry::TkEntry(Ptr<TclInterp>& interp):TkBase(interp),var(interp)
{
	setOption(TkEntry::OPT_TEXTVARIABLE,&var);
}

TkEntry::TkEntry(Ptr<TclInterp>& interp,const string& text):TkBase(interp),var(interp)
{
	setOption(TkEntry::OPT_TEXTVARIABLE,&var);
	setText(text);
}

void TkEntry::addValueListenter(TclVarListener* listener)
{
	var.addListener(listener);
}

void TkEntry::setText(const string& value)
{
	var.set(value);
}

void TkEntry::setValidationDouble()
{
	setOption(TkEntry::OPT_VALIDATECOMMAND,new TkStringOption(
		string("{expr {[string is double %P] || [string match {[+-]} %P]}}")
	));
	setOption(TkEntry::OPT_VALIDATE,new TkValidationTypeOption(TkValidationTypeOption::E_ALL));
}

void TkEntry::setValidationInt()
{
	setOption(TkEntry::OPT_VALIDATECOMMAND,new TkStringOption(
		string("{expr {[string is integer %P] || [string match {[+-]} %P]}}")
	));
	setOption(TkEntry::OPT_VALIDATE,new TkValidationTypeOption(TkValidationTypeOption::E_ALL));
}

void TkEntry::setValidationNone()
{
	setOption(TkEntry::OPT_VALIDATE,new TkValidationTypeOption(TkValidationTypeOption::E_NONE));
}
	
const string& TkEntry::getText() const
{
	return *(new string(var.get()));
}

double TkEntry::doubleValue() const
{
	return atof(var.get());
}

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

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

void TkEntry::setReadonly()
{
	setOption(TkEntry::OPT_STATE,new Tk3StateOption(Tk3StateOption::readonly));	
}

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

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