#ifndef _TK_LISTBOX_H_
#define _TK_LISTBOX_H_

#include "tk_base.h"

/**
 * \brief Represent "listbox" widget
 *
 * \date 01-12-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TkListbox:public TkBase,public IStateable,public IXScroll,public IYScroll
{
	Ptr<TclListVar> defaultVar;
	Ptr<TclListVar> var;
protected:
	void doSerialize(TiXmlElement& element) const;
	void doDeserialize(const TiXmlElement& element);
public:
	static TXmlObject* newObject()
	{
		return new TkListbox(TclInterp::defaultInterp);
	}

	const static Ptr<TkOptionDef> OPT_ACTIVESTYLE;
	const static Ptr<TkOptionDef> OPT_BACKGROUND;
	const static Ptr<TkOptionDef> OPT_BORDERWIDTH ;
	const static Ptr<TkOptionDef> OPT_CURSOR ;
	const static Ptr<TkOptionDef> OPT_DISABLEDFOREGROUND ;
	const static Ptr<TkOptionDef> OPT_FONT ;
	const static Ptr<TkOptionDef> OPT_FOREGROUND ;
	const static Ptr<TkOptionDef> OPT_HEIGHT ;
	const static Ptr<TkOptionDef> OPT_HIGHLIGHTBACKGROUND ;
	const static Ptr<TkOptionDef> OPT_HIGHLIGHTCOLOR ;
	const static Ptr<TkOptionDef> OPT_HIGHLIGHTTHICKNESS ;
	const static Ptr<TkOptionDef> OPT_RELIEF ;
	const static Ptr<TkOptionDef> OPT_SELECTBACKGROUND;
	const static Ptr<TkOptionDef> OPT_SELECTBORDERWIDTH;
	const static Ptr<TkOptionDef> OPT_SELECTFOREGROUND;
	const static Ptr<TkOptionDef> OPT_SELECTMODE;
	const static Ptr<TkOptionDef> OPT_SETGRID;
	const static Ptr<TkOptionDef> OPT_STATE;
	const static Ptr<TkOptionDef> OPT_TAKEFOCUS;
	const static Ptr<TkOptionDef> OPT_WIDTH;
	const static Ptr<TkOptionDef> OPT_XSCROLLCOMMAND;
	const static Ptr<TkOptionDef> OPT_YSCROLLCOMMAND;
	const static Ptr<TkOptionDef> OPT_LISTVARIABLE;

	const static Ptr<TkOptionDef> optAll[];
	const static char* type;
	const static char* tkClass;

	const Ptr<TkOptionDef>* getOptionAll() const;
	const char* getType() const;
	const char* getTkClass() const;

	TObject* clone();

	TkListbox(Ptr<TclInterp>& interp);

	/**
	 * Synchronize own variable with list, this variable has got value of item
	 * \param variable 
	 */
	void setVariable(TclListVar* variable)
	{
		setOption(TkListbox::OPT_LISTVARIABLE,variable);
		this->var=variable;
	}

	TclListVar* getDefaultVariable()
	{
		return defaultVar;
	}

	/**
	 *
	 * \return cursor position of selected item, items are indexed from top, starting from index 0, -1 means no selected item
	 */
	int getCursor()
	{
		interp->eval(string(getUid())+" curselection");
		if (interp->isNullResult()) return -1;
		return interp->getIntResult();
	}

	/**
	 * Delete range of items from listbox
	 * \param firstIndex index of first item
	 * \param lastIndex index of last item
	 */
	void deleteItem(int firstIndex,int lastIndex)
	{
		char buf[128];
		sprintf(buf,"%d %d",firstIndex,lastIndex);
		interp->eval(string(getUid())+" delete "+buf);
	}

	/**
	 * Delete single item from listbox
	 * \param index index of item
	 */
	void deleteItem(int index)
	{
		deleteItem(index,index);
	}

	/**
	 * \param index index of item
	 * \return item at index index
	 */
	string& get(int index)
	{
		char buf[128];
		sprintf(buf,"%d",index);
		interp->eval(string(getUid())+" get "+buf);
	}

	void setEnabled(bool value);
	bool isEnabled() const;

	void create();

	void setEventsEnabled(bool value)
	{
		TkBase::setEventsEnabled(value);
		var->setEventsEnabled(value);
	}

	void setBackgroundColor(TkColorOption* color);
	Ptr<TkColorOption>& getBackgroundColor() const;
};

#endif/*_TK_LISTBOX_H_*/
