// COPYRIGHT (C) 1990-1999.  All Rights Reserved. - IMAGE.CPP
// Zinc Software Incorporated.  Pleasant Grove, Utah  USA
// May be freely copied, used and distributed.

#include "image.hpp"
#include <stdio.h>

ImageControlWin::ImageControlWin(ZafIChar *name) :
	ZafWindow(name, *zafObjectPersistence)
{
	// Get a pointer to the image and initialize it with a default image.
	ZafImage *image = DynamicPtrCast(GetObject(ZAF_ITEXT("WallpaperImage")),
		ZafImage);
	image->SetPathName(ZAF_ITEXT("image"));
}

ZafEventType ImageControlWin::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch(ccode)
	{
	case OPEN_NEW_IMAGE:
		{
		// Create a ZafFileDialog to get an image file.
		ZafFileDialog openDialog(5, 5, 66, 10);

		openDialog.SetFilter(ZafImage::defaultFilterName);
		
		// Get the response.
		ZafEventType request = openDialog.GetFile(ZAF_FILE_DIALOG_OPEN);
		if (request == S_DLG_OK)
		{
			// Get the directory and file name from the dialog.
			ZafIChar imageFileName[ZAF_MAXPATHLEN];
			openDialog.FullPath(imageFileName, ZAF_MAXPATHLEN);

			// Get a pointer to the image on this windown and set the new image.
			ZafImage *image = DynamicPtrCast(GetObject(ZAF_ITEXT("WallpaperImage")),
				ZafImage);
			image->SetPathName(imageFileName);
			image->Redisplay();

			// Load the other two image windows from the designer and
			// set the image on them.
			ZafWindow *win = new ZafWindow(ZAF_ITEXT("WallpaperTiled"),
				*zafObjectPersistence);
			image = DynamicPtrCast(win->GetObject(ZAF_ITEXT("WallpaperImage")),
				ZafImage);
			image->SetPathName(imageFileName);

			ZafWindow *win1 = new ZafScrolledWindow(ZAF_ITEXT("TwoImageWindow"),
				*zafObjectPersistence);
			image = DynamicPtrCast(win1->GetObject(ZAF_ITEXT("ImageClip")),
				ZafImage);
			image->SetPathName(imageFileName);
			image = DynamicPtrCast(win1->GetObject(ZAF_ITEXT("ImageAutosize")),
				ZafImage);
			image->SetPathName(imageFileName);

			// Add the windows to the window manager.
			windowManager->Add(win);
			windowManager->Add(win1);
	   		}
		}
		break;

	case N_SIZE:
		ccode = ZafWindow::Event(event);
		Redisplay();
		break;

	default:
		ccode = ZafWindow::Event(event);
		break;
	}
	return(ccode);

}
