#include <stream.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

#include "GUICINCLUDE:GUIC_Screen.hpp"
#include "GUICINCLUDE:GUIC_Application.hpp"
#include "GUICINCLUDE:GUIC_GGFXPicture.hpp"
#include "GUICINCLUDE:GUIC_Text.hpp"
#include "GUICINCLUDE:GUIC_Number.hpp"
#include "GUICINCLUDE:GUIC_StringType.hpp"
#include "GUICINCLUDE:GUIC_Exceptions.hpp"
#include "GUICINCLUDE:GUIC_BorderProp.hpp"

#include "PictureWindow.hpp"

/*********************************************************************************************************/

PictureWindowC::PictureWindowC		(GUIC_ApplicationC &app, GUIC_ScreenC &screen) : GUIC_WindowC (-1,-1,20,20)
{
	this->app 		= &app;
	this->screen	= &screen;
	this->picture	= 0;
	this->scale		= TRUE;
	this->center 	= TRUE;
	
	rightProp		= new GUIC_BorderPropC (GUIC_Vertical);
	bottomProp	= new GUIC_BorderPropC (GUIC_Horizontal);
	
	add ( rightProp );
	add ( bottomProp );
	
	widthGadget	= 0;
	heightGadget	= 0;
	textGadget		= 0;
	
	app.addPrefs("PictureWindow",	this);

	setCloseGadgetMode	( FALSE );
	setSizeable					(GUIC_RightAndBottomBorder);
	
	setGuideContext("PictureWindow");
	setTitle ( "Picture" );
}
PictureWindowC::~PictureWindowC		(VOID)
{
	cleanUp();
}

/*********************************************************************************************************/

VOID								PictureWindowC::setTextGadget			(GUIC_TextC *t)
{
	textGadget = t;
}
VOID								PictureWindowC::setNumberGadgets	(GUIC_NumberC *n1, GUIC_NumberC *n2)
{
	widthGadget	= n1;
	heightGadget	= n2;
}

VOID								PictureWindowC::setDimensions			(LONG x, LONG y)
{
	width = x;
	height = y;
	
	setPixelWidth (x);
	setPixelHeight (y);
}
VOID								PictureWindowC::showPicture			(STRPTR fileName)
{
	if (picture) 
		{
		remove (picture);
		delete picture; picture=0;
		
		if (widthGadget)		widthGadget	-> set ( 0 );
		if (heightGadget)	heightGadget	-> set ( 0 );
		}
	
	if (strcmp(fileName, "")) // else we should only close the picture
		{
		try
			{
			STRING m = "Loading picture: "; m+=fileName;
	
			if (textGadget) 		textGadget		-> set ( m );
			picture = new GUIC_GGFXPictureC (fileName);
			if (widthGadget)		widthGadget	-> set ( picture->getWidth() );
			if (heightGadget)	heightGadget	-> set ( picture->getHeight() );
		
			m = "Showing picture: "; m+=fileName;
			if (textGadget) 	textGadget -> set ( m );
			if (center)			picture->setCentered(TRUE);
			if (scale) 			picture->setScaled(TRUE);
			add (picture);
			
			rightProp->set ( 0, picture->getHeight(), picture->getVisibleY() );
			bottomProp->set ( 0, picture->getWidth(), picture->getVisibleX() );
			
			}
		catch (GUIC_Exception &e)
			{
			if (picture) { remove(picture); delete picture; picture = 0; }
			STRING s = "Could not load picture: "; s+=fileName;
			if (textGadget) 		textGadget		-> set ( s );
			rightProp->set(0,100,100);
			bottomProp->set(0,100,100);
			}
		}
}

VOID								PictureWindowC::setScaled				(BOOL x)
{
	scale = x;
}
VOID								PictureWindowC::setCentered				(BOOL x)
{
	center = x;
}

STRPTR							PictureWindowC::getClass					(VOID)
{
	return "PictureWindowC";
}
GUIC_GGFXPictureC *	PictureWindowC::getPicture				(VOID)
{
	return picture;
}

BOOL								PictureWindowC::action						(GUIC_EventC &e)
{
	switch (e.id)
		{
		case GUIC_GadgetEvent:
			if (e.gadget == (GUIC_GadgetC *)rightProp)
				{
				if (picture) picture->setOffsetY(rightProp->getTop());
				return TRUE;
				}
			else if (e.gadget == (GUIC_GadgetC *)bottomProp) 
				{
				if (picture) picture->setOffsetX(bottomProp->getTop());
				return TRUE;
				}
			break;
		case GUIC_WindowResize:
			if (picture)
				{
				picture->setOffsets(0,0);
				rightProp->set ( 0, picture->getHeight(), picture->getVisibleY() );
				bottomProp->set ( 0, picture->getWidth(), picture->getVisibleX() );
				}
			return TRUE;
			break;
		case GUIC_OpenWindow:
			return TRUE;
			break;
		case GUIC_CloseWindow: 
			return FALSE;
			break;
		default:
			cerr << "PictureWindow got an event: " << e.id << endl;
		}
	
	return FALSE;
}

/*********************************************************************************************************/

VOID 								PictureWindowC::cleanUp					(VOID)
{	
	if (picture)
		{
		remove (picture);
		delete picture;
		}
		
	delete rightProp;
	delete bottomProp;
}

