#include "tk_checkbutton.h"
#include "image/tk_photo.h"

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

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

const char* TkCheckButton::type="TkCheckButton";
const char* TkCheckButton::tkClass="checkbutton";

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

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

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

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

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

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

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

	baseClone(obj);

	return obj;
}

TkCheckButton::TkCheckButton(Ptr<TclInterp>& interp):TkBase(interp)
{
	defaultVar=new TclBoolVar(interp);
	var=defaultVar;
	setOption(TkCheckButton::OPT_VARIABLE,var);
}

void TkCheckButton::addValueListener(TclVarListener* listener)
{
	var->addListener(listener);
}

void TkCheckButton::setSelected(bool value)
{
	var->set(value);
}

bool TkCheckButton::isSelected() const
{
	return var->get();
}

void TkCheckButton::setText(const string& text)
{
	setOption(TkCheckButton::OPT_TEXT,new TkStringOption(text));
}

const string& TkCheckButton::getText() const
{
	return TkBase::getStrOption(TkCheckButton::OPT_TEXT);
}

void TkCheckButton::setVariable(TclBoolVar* var)
{
	this->var=var;
	setOption(TkCheckButton::OPT_VARIABLE,var);
}

TclVar* TkCheckButton::getDefaultVar()
{
	return defaultVar;
}

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

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

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

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