#ifndef _TCL_INTERP_H_
#define _TCL_INTERP_H_

#include <tcl.h>
#include <tk.h>

#include <ostream>
#include <fstream>

#include "../ptr.h"
#include "../tobject.h"
#include "../exceptions/exceptionio.h"

using namespace std;

/**
 * \brief Encapsulates Tcl interpret, manages logging
 *
 * \date 01-11-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TclInterp:public TObject
{
	Tcl_Interp* interp;
	ostream* logstream;
	bool managedLog;

	static int onMenuEvent(ClientData clientData,Tcl_Interp *interp,int objc, Tcl_Obj *CONST objv[]);
	static int onDeleteWindow(ClientData clientData,Tcl_Interp *interp,int objc, Tcl_Obj *CONST objv[]);
public:
	static const char* type;
	const char* getType() const;

	/// Default intepret
	static Ptr<TclInterp> defaultInterp;
	/// If true, each eval command will be logged
	bool logging;


	/**
	 * \brief Returns pointer to Tcl interpret
     *
	 * \return pointer to Tcl interpret
	 */
	inline Tcl_Interp* getInterp() const
	{
		return interp;
	}

	/**
	 * \param interp interpret 
	 * \param logstream reference to logstream
	 */
	TclInterp(Tcl_Interp* interp,ostream& logstream);
	
	/**
	 * \param interp interpret 
	 * \param fileName file for logging
	 */
	TclInterp(Tcl_Interp* interp,const char* fileName);

	~TclInterp();
	
	
	/**
	 * \brief Evaluate script in shell 
	 *
	 * \param s string to evaluate
	 * \return script evaluation return code
	 */
	int eval(const string& s) const;


	/**
	 * \brief Return script evaluation result
	 *
	 * \return pointer to result
	 */
	Tcl_Obj* getObjResult() const;


	/**
	 * \brief Return script evaluation result
	 *
	 * \return pointer to result
	 */
	char* getStrResult() const;
	
	/**
	 * \brief Return script evaluation result
	 *
	 * \return result as integer value
	 */
	int getIntResult() const;

	/**
	 * \brief Test if script evaluation result is NULL
	 *
	 * \return true if script evaluation result is NULL
	 */
	bool isNullResult() const;

	/**
	 * \brief Translates Tcl shell return code in integer format to string
	 *
	 * \param retCode Tcl shell return code value
	 * \return name of return code
	 */
	static string& getCodeName(int retCode);
};


#endif/*_TCL_INTERP_*/
