#include "tk_listbox.h"

const Ptr<TkOptionDef> TkListbox::OPT_ACTIVESTYLE;
const Ptr<TkOptionDef> TkListbox::OPT_BACKGROUND=new TkOptionDef("backgorund",TkColorOption::type);
const Ptr<TkOptionDef> TkListbox::OPT_BORDERWIDTH=new TkOptionDef("borderwidth",TkIntOption::type); ;
const Ptr<TkOptionDef> TkListbox::OPT_CURSOR ;
const Ptr<TkOptionDef> TkListbox::OPT_DISABLEDFOREGROUND ;
const Ptr<TkOptionDef> TkListbox::OPT_FONT ;
const Ptr<TkOptionDef> TkListbox::OPT_FOREGROUND ;
const Ptr<TkOptionDef> TkListbox::OPT_HEIGHT ;
const Ptr<TkOptionDef> TkListbox::OPT_HIGHLIGHTBACKGROUND ;
const Ptr<TkOptionDef> TkListbox::OPT_HIGHLIGHTCOLOR ;
const Ptr<TkOptionDef> TkListbox::OPT_HIGHLIGHTTHICKNESS ;
const Ptr<TkOptionDef> TkListbox::OPT_RELIEF=new TkOptionDef("relief",TkReliefOption::type);
const Ptr<TkOptionDef> TkListbox::OPT_SELECTBACKGROUND;
const Ptr<TkOptionDef> TkListbox::OPT_SELECTBORDERWIDTH;
const Ptr<TkOptionDef> TkListbox::OPT_SELECTFOREGROUND;
const Ptr<TkOptionDef> TkListbox::OPT_SELECTMODE;
const Ptr<TkOptionDef> TkListbox::OPT_SETGRID;
const Ptr<TkOptionDef> TkListbox::OPT_STATE=new TkOptionDef("state",TkStateOption::type);
const Ptr<TkOptionDef> TkListbox::OPT_TAKEFOCUS;
const Ptr<TkOptionDef> TkListbox::OPT_WIDTH;
const Ptr<TkOptionDef> TkListbox::OPT_XSCROLLCOMMAND=new TkOptionDef("xscrollcommand",TkStringOption::type,false);
const Ptr<TkOptionDef> TkListbox::OPT_YSCROLLCOMMAND=new TkOptionDef("yscrollcommand",TkStringOption::type,false);
const Ptr<TkOptionDef> TkListbox::OPT_LISTVARIABLE=new TkOptionDef("listvariable",TclListVar::type);

const Ptr<TkOptionDef> TkListbox::optAll[]=
{
	OPT_ACTIVESTYLE,
	OPT_BACKGROUND,
	OPT_BORDERWIDTH ,
	OPT_CURSOR ,
	OPT_DISABLEDFOREGROUND ,
	OPT_FONT ,
	OPT_FOREGROUND ,
	OPT_HEIGHT ,
	OPT_HIGHLIGHTBACKGROUND ,
	OPT_HIGHLIGHTCOLOR ,
	OPT_HIGHLIGHTTHICKNESS ,
	OPT_RELIEF ,
	OPT_SELECTBACKGROUND,
	OPT_SELECTBORDERWIDTH,
	OPT_SELECTFOREGROUND,
	OPT_SELECTMODE,
	OPT_SETGRID,
	OPT_STATE,
	OPT_TAKEFOCUS,
	OPT_WIDTH,
	OPT_XSCROLLCOMMAND,
	OPT_YSCROLLCOMMAND,
	OPT_LISTVARIABLE
};

const char* TkListbox::type="TkListbox";
const char* TkListbox::tkClass="listbox";

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

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

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


void TkListbox::doSerialize(TiXmlElement& element) const
{
	TkBase::doSerialize(element);
	
	element.SetAttribute("valuesCount",defaultVar->size());
	
	int i=0;
	char buf[128];
	for(TclListVar::const_iterator it=defaultVar->begin();it!=defaultVar->end();it++)
	{
		sprintf(buf,"value%d",i);
		element.SetAttribute(buf,(*it).c_str());
		i++;
	}
}

void TkListbox::doDeserialize(const TiXmlElement& element)
{
	TkBase::doDeserialize(element);
	
	int valuesCount;
	char buf[128];
	element.Attribute("valuesCount",&valuesCount);
	for(int i=0;i<valuesCount;i++)
	{
		sprintf(buf,"value%d",i);
		defaultVar->push_back(element.Attribute(buf));
	}
}

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

	baseClone(obj);

	return obj;
}

TkListbox::TkListbox(Ptr<TclInterp>& interp):TkBase(interp)
{
	defaultVar=new TclListVar(interp);
	setVariable(defaultVar);
}


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

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

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

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

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