#ifndef _TK_PHOTO_H_
#define _TK_PHOTO_H_

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

#include "../../tcl_interp.h"
#include "../../../txmlobject.h"
#include "../../../ptr.h"
#include "../../../exceptions/exceptionio.h"


/**
 * \brief Photo represent loaded image in shell.
 * \date 01-12-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo Photo can be as well accessed like widget. See if it can be inherited from TkBase.
 *
 * \bug 
 *
 */
class TkPhoto:public TXmlObject
{
	Tk_PhotoHandle handler;
	string imageName;
	string fileName;

	Ptr<TclInterp> interp;
protected:
	void doSerialize(TiXmlElement& element) const;
	void doDeserialize(const TiXmlElement& element);
public:
	static TXmlObject* newObject()
	{
		return new TkPhoto(TclInterp::defaultInterp);
	}

	static const char* type;
	const char* getType() const
	{
		return type;
	}
	TkPhoto(Ptr<TclInterp>& interp)
	{
		this->interp=interp;
	}
	
	/**
	 * ctor
	 * NOTE: Not all image types are supported. You can even register your
	 * own image type handler. Base Tk distribution comes with PPM/PGM and GIF formats.
	 * Loading unsuported image type would lead to blank image.
	 * For details see description of "photo" command in the Tk manual pages.
	 * \param interp interpret
	 * \param fileName name of file with image
	 * \return 
	 */
	TkPhoto(Ptr<TclInterp>& interp,const char* fileName)
	{
		this->interp=interp;
		this->fileName=fileName;
		string cmd=string("image create photo -file \"")+fileName+"\"";
		if (interp->eval(cmd)==TCL_ERROR)
		{
			throw new ExceptionIO(fileName,true);
		}
		imageName=interp->getStrResult();
		this->handler=Tk_FindPhoto(interp->getInterp(),imageName.c_str());
	}

	const string& toString() const
	{
		return imageName;
	}

	~TkPhoto()
	{
		this->interp->eval(string("image delete ")+imageName);
	}

	/**
	 * Return size of image
	 * \param width image width
	 * \param height image height
	 */
	void getSize(int* width,int* height) const
	{
		Tk_PhotoGetSize(handler,width,height);
	}


	/**
	 * Resize image to desired size
	 * \param width width
	 * \param height height
	 */
	void setSize(int width,int height)
	{
		Tk_PhotoSetSize(handler,width,height);
	}
};

#endif/*_TK_PHOTO_H_*/

