#ifndef _TCL_VAR_H_
#define _TCL_VAR_H_

#include "../../ptr.h"
#include "../../txmlobject.h"

#include "../tcl_interp.h"
#include "../tcl_guid.h"

#include <string>
#include <list>

using namespace std;

class TclVar;

/**
 * \brief Base class for variable listeners 
 *
 * \date 01-11-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TclVarListener:public TObject
{
public:

	/**
	 * \brief Called whenever a variable is changed 
	 *
	 * \param variable pointer to changing variable
	 */
	virtual void valueUpdated(TclVar* variable)=0;
};

/**
 * \brief Encapsulates variable in Tcl shell
 *
 * \date 01-11-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TclVar:public TXmlObject
{
	typedef list<Ptr<TclVarListener> > t_listeners;
	t_listeners listeners;

	static char* catchVar(ClientData clientData, Tcl_Interp* tcl,CONST84 char* name1, CONST84 char* name2, int flags);
	void catchWrite(const char* name1,const char* name2);
	bool eventsEnabled;
protected:
	TclGuid guid;
	Tcl_Obj* objName;
	Ptr<TclInterp> interp;

	virtual void doSerialize(TiXmlElement& element) const=0;
	virtual void doDeserialize(const TiXmlElement& element)=0;
public:
	static const char* type;
	const char* getType() const;

	/**
	 * \param interp interpret
	 */
	TclVar(Ptr<TclInterp>& interp);
	~TclVar();

	const string& toString() const;
	
	/**
	 * \brief Add variable change listener 
	 * \param listener listener
	 */
	void addListener(TclVarListener* listener);

	/**
	 * \brief Enable or disable event generation 
	 * \param value if true events will be enabled
	 */
	void setEventsEnabled(bool value);


	/**
	 * \return true if events are enabled
	 */
	bool isEventsEnabled() const;
};

#endif/*_TCL_VAR_H_*/
