#ifndef _TK_TEXT_H_
#define _TK_TEXT_H_

#include "tk_base.h"

/**
 * \brief Represent "text" widget
 * \date 01-12-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TkText:public TkBase,public IStateable,public IXScroll,public IYScroll
{
	char* value;
protected:
	void doSerialize(TiXmlElement& element) const;
	void doDeserialize(const TiXmlElement& element);
public:
	static TXmlObject* newObject()
	{
		return new TkText(TclInterp::defaultInterp);
	}

	const static Ptr<TkOptionDef> OPT_AUTOSEPARATORS ;
	const static Ptr<TkOptionDef> OPT_BACKGROUND ;
	const static Ptr<TkOptionDef> OPT_BORDERWIDTH ;
	const static Ptr<TkOptionDef> OPT_CURSOR ;
	const static Ptr<TkOptionDef> OPT_FONT ;
	const static Ptr<TkOptionDef> OPT_FOREGROUND ;
	const static Ptr<TkOptionDef> OPT_HEIGHT ;
	const static Ptr<TkOptionDef> OPT_INSERTOFFTIME ;
	const static Ptr<TkOptionDef> OPT_INSERTONTIME ;
	const static Ptr<TkOptionDef> OPT_INSERTWIDTH ;
	const static Ptr<TkOptionDef> OPT_MAXUNDO ;
	const static Ptr<TkOptionDef> OPT_PADX ;
	const static Ptr<TkOptionDef> OPT_PADY ;
	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_SETGRID ;
	const static Ptr<TkOptionDef> OPT_STATE ;
	const static Ptr<TkOptionDef> OPT_SPACING1 ;
	const static Ptr<TkOptionDef> OPT_SPACING2 ;
	const static Ptr<TkOptionDef> OPT_SPACING3 ;
	const static Ptr<TkOptionDef> OPT_TABS ;
	const static Ptr<TkOptionDef> OPT_TAKEFOCUS ;
	const static Ptr<TkOptionDef> OPT_UNDO ;
	const static Ptr<TkOptionDef> OPT_WIDTH ;
	const static Ptr<TkOptionDef> OPT_WRAP ;
	const static Ptr<TkOptionDef> OPT_XSCROLLCOMMAND;
	const static Ptr<TkOptionDef> OPT_YSCROLLCOMMAND;


	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();

	TkText(Ptr<TclInterp> interp):TkBase(interp)
	{
	}

	/**
	 * \return number of lines in text
	 */
	inline int getLastLine() const
	{
		int line,chr;
		interp->eval(string(getUid())+" index end");
		sscanf(interp->getStrResult(),"%d.%d",&line,&chr);
		return line;
	}

	/**
	 * \param index index of line
	 * \return content of line with index index, lines are indexed from 0
	 */
	const string& getLine(int index) const
	{
		char buf[128];
		sprintf(buf," get %d.0 %d.end",index,index);
		interp->eval(string(getUid())+buf);
		return *(new string(interp->getStrResult()));
	}

	const string& getText() const
	{
		interp->eval(string(getUid())+" get 1.0 end");
		return *(new string(interp->getStrResult()));
	}

	/**
	 * Delete all text
	 */
	void clearAll()
	{
		interp->eval(string(getUid())+" delete 1.0 end");
	}

	void setText(const string& text)
	{
		clearAll();
		string txt=text;
		string::size_type pos;
		do
		{
			pos=txt.find("\n",0);
			if (pos!=string::npos)
			{
				txt.replace(pos,1,"\\n");
			}
		} while (pos!=string::npos);
		interp->eval(string(getUid())+" insert 1.0 \""+txt.c_str()+"\"");
	}

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

	void create();

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

#endif/*_TK_TEXT_H_*/
