#include "tk_radiobutton.h"
#include "tk_container.h"
#include "image/tk_photo.h"

const Ptr<TkOptionDef> TkRadioButton::OPT_ACTIVEBACKGROUND=new TkOptionDef("activebackground",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_ACTIVEFOREGROUND=new TkOptionDef("activeforeground",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_ANCHOR=new TkOptionDef("anchor",TkAnchorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_BACKGROUND=new TkOptionDef("background",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_BITMAP=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_BORDERWIDTH=new TkOptionDef("borderwidth",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_COMMAND=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_COMPOUND=new TkOptionDef("compound",TkCompoundOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_CURSOR=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_DISABLEDFOREGROUND=new TkOptionDef("disabledforeground",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_FONT=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_FOREGROUND=new TkOptionDef("foreground",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_HEIGHT=new TkOptionDef("height",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_HIGHLIGHTBACKGROUND=new TkOptionDef("highlightbackground",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_HIGHLIGHTCOLOR=new TkOptionDef("highlightcolor",TkColorOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_HIGHLIGHTTHICKNESS=new TkOptionDef("highlightthickness",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_INDICATORON=new TkOptionDef("indicatoron",TkBoolOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_IMAGE=new TkOptionDef("image",TkPhoto::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_JUSTIFY=new TkOptionDef("justify",TkJustifyOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_OVERRELIEF=new TkOptionDef("overrelief",TkReliefOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_PADX=new TkOptionDef("padx",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_PADY=new TkOptionDef("padx",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_RELIEF=new TkOptionDef("relief",TkReliefOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_SELECTIMAGE=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_STATE=new TkOptionDef("state",TkStateOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_TAKEFOCUS=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_TEXT=new TkOptionDef("text",TkStringOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_TEXTVARIABLE=0;
const Ptr<TkOptionDef> TkRadioButton::OPT_UNDERLINE=new TkOptionDef("underline",TkBoolOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_VALUE=new TkOptionDef("value",TkStringOption::type,true,false);
const Ptr<TkOptionDef> TkRadioButton::OPT_VARIABLE=new TkOptionDef("variable",TclStringVar::type,true,false);
const Ptr<TkOptionDef> TkRadioButton::OPT_WIDTH=new TkOptionDef("width",TkIntOption::type);
const Ptr<TkOptionDef> TkRadioButton::OPT_WRAPLENGTH=new TkOptionDef("wraplength",TkWrapOption::type);

const Ptr<TkOptionDef> TkRadioButton::optAll[]=
{
	TkRadioButton::OPT_ACTIVEBACKGROUND,
	TkRadioButton::OPT_ACTIVEFOREGROUND,
	TkRadioButton::OPT_ANCHOR,
	TkRadioButton::OPT_BACKGROUND,
	TkRadioButton::OPT_BITMAP,
	TkRadioButton::OPT_BORDERWIDTH,
	TkRadioButton::OPT_COMMAND,
	TkRadioButton::OPT_COMPOUND,
	TkRadioButton::OPT_CURSOR,
	TkRadioButton::OPT_DISABLEDFOREGROUND,
	TkRadioButton::OPT_FONT,
	TkRadioButton::OPT_FOREGROUND,
	TkRadioButton::OPT_HEIGHT,
	TkRadioButton::OPT_HIGHLIGHTBACKGROUND,
	TkRadioButton::OPT_HIGHLIGHTCOLOR,
	TkRadioButton::OPT_HIGHLIGHTTHICKNESS,
	TkRadioButton::OPT_INDICATORON,
	TkRadioButton::OPT_IMAGE,
	TkRadioButton::OPT_JUSTIFY,
	TkRadioButton::OPT_OVERRELIEF,
	TkRadioButton::OPT_PADX,
	TkRadioButton::OPT_PADY,
	TkRadioButton::OPT_RELIEF,
	TkRadioButton::OPT_SELECTIMAGE,
	TkRadioButton::OPT_STATE,
	TkRadioButton::OPT_TAKEFOCUS,
	TkRadioButton::OPT_TEXT,
	TkRadioButton::OPT_TEXTVARIABLE,
	TkRadioButton::OPT_UNDERLINE,
	TkRadioButton::OPT_VALUE,
	TkRadioButton::OPT_VARIABLE,
	TkRadioButton::OPT_WIDTH,
	TkRadioButton::OPT_WRAPLENGTH
};

const char* TkRadioButton::type="TkRadioButton";
const char* TkRadioButton::tkClass="radiobutton";

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

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

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

void TkRadioButton::doSerialize(TiXmlElement& element) const
{
	TkBase::doSerialize(element);
	
	element.SetAttribute("text",getText().c_str());
	element.SetAttribute("group",group.c_str());
	element.SetAttribute("selected",isSelected()?1:0);
}

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

	setText(element.Attribute("text"));
	
	const char* c_groupName=element.Attribute("group");
	if (c_groupName) setGroup(string(c_groupName));

	int valIsSelected;
	element.Attribute("selected",&valIsSelected);
	if (valIsSelected) setSelected(true);
}

TkRadioButton::TkRadioButton(Ptr<TclInterp>& interp):TkBase(interp)
{
	string value=guid.getId();
	setOption(OPT_VALUE,new TkStringOption(guid.getId()));
	selectedSet=false;
	groupSet=false;
}

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

	baseClone(obj);

	return obj;
}

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

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

void TkRadioButton::setSelected(bool value)
{
	selectedSet=true;
	selected=value;

	if (created)
	{
		interp->eval(string(getUid())+(value?" select":" deselect"));
	}
}

bool TkRadioButton::isSelected() const
{
	const string varName=getStrOption(OPT_VARIABLE);
	
	Tcl_Obj* objName=Tcl_NewStringObj(varName.c_str(),varName.size());
	Tcl_Obj* obj=Tcl_ObjGetVar2(interp->getInterp(),objName,0,TCL_GLOBAL_ONLY);
	const char* value=Tcl_GetString(obj);
	return (guid.getId()==value);
}

const string& TkRadioButton::getGroup() const
{
	return group;
}

void TkRadioButton::setGroup(const string& name)
{
	this->group=name;
	if (created)
	{
		Ptr<TclStringVar>& var=getParent()->getRadioGroupByName(name);
		setOption(OPT_VARIABLE,(TclStringVar*)var.asObject());
	}
	else
	{
		groupSet=true;
	}
}


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

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


