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

// This example program derives an object from ZafWindowObject.  The derived
// object acts as a light.  This object	changes its color or, on off state
// depending on its disabled state.  Its colors are saved in the 
// ZafWindowObjects internal palette map table when the light object is  
// constucted.

#include <zaf.hpp>
#include "draw2.hpp"

// ----- LIGHT WINDOW -------------------------------------------------------

LightWin::LightWin(int left, int top, int width, int height) :
	ZafWindow(left, top, width, height)
{
	// Create the button used to change the state of the light.
	ZafButton *button = new ZafButton(1, 4, 18, 1, new ZafStringData("Change Light State"));
	button->SetSendMessageWhenSelected(true);
	button->SetValue(CHANGE_LIGHT_STATE);

	// Add the objects to the window.
	AddGenericObjects(new ZafStringData("Light"));
	Add(button);
	light = new Light(7, 1, 6, 2);
	Add(light);

	// Center the Window.
	zafWindowManager->Center(this);
}

ZafEventType LightWin::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch(ccode)
	{
	case CHANGE_LIGHT_STATE:
		// Turn the light on or off.
		if (!light->Disabled())
			// Turn it off.
			light->SetDisabled(true);
		else
			// Turn it on.
			light->SetDisabled(false);
		// Redisplay to show the new state.
		light->Redisplay();
		break;
	default:
		ccode = ZafWindow::Event(event);
		break;
	}
	return(ccode);

}

// ----- Light Object -------------------------------------------------------

Light::Light(int left, int top, int width, int height) :
	ZafWindowObject(left, top, width, height)
{

	// Create a ZafPaletteData.
	SetUserPaletteData(new ZafPaletteData());

	// If disabled attribute is set to true the light is in the off state.
	// If disabled attribute is set	to false the light is in the on state.
	// Create the palette to be used when the light is off.
	ZafPaletteStruct drawPalette = userPaletteData->GetPalette(ZAF_PM_FOREGROUND,
		ZAF_PM_DISABLED);
	drawPalette.colorForeground = drawPalette.colorBackground = ZAF_CLR_GREEN;

	// Add the palette to the palette table.
	userPaletteData->AddPalette(ZAF_PM_FOREGROUND, ZAF_PM_DISABLED, drawPalette);
	
	// Create the palette to be used when the light is on.
	drawPalette = userPaletteData->GetPalette(ZAF_PM_FOREGROUND, ZAF_PM_ENABLED);
	drawPalette.colorForeground = ZAF_CLR_GREEN;
	drawPalette.colorBackground = ZAF_CLR_LIGHTGREEN;

	// Add the palette to the palette table.
	userPaletteData->AddPalette(ZAF_PM_FOREGROUND, ZAF_PM_ENABLED, drawPalette);

	// Turn off the OSDraw so Draw() function will be called.
	SetOSDraw(false);
	// Set light's state to off, and make it noncurrent.
	SetDisabled(true);
	SetNoncurrent(true);
}

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType Light::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	ZafDisplay *display = Display();
	ZafRegionStruct drawRegion = BeginDraw();

	// Set the palette the light should be drawn with.
	display->SetPalette(LogicalPalette(ZAF_PM_FOREGROUND, PaletteState()));

	// Get the vertical or horizontal offset according to the cell aspect ratio.
	// Using this offset gives us a perfect circle instead of an ellipse.
	int offset = ((drawRegion.bottom - drawRegion.top) - 
		(drawRegion.right - drawRegion.left)) >> 1;

	// The Light is an ellipse drawn 1 pixel inside of the object's region.
	if (offset < 0)
	{
		// The offset is horizontal.
		display->Ellipse(drawRegion.left + 1 - offset, drawRegion.top + 1,
		 drawRegion.right - 1 + offset, drawRegion.bottom - 1, 
		 0, 360, 2, true);
	}
	else
	{
		// The offset is vertical.
		display->Ellipse(drawRegion.left + 1, drawRegion.top + 1 + offset,
		 drawRegion.right - 1, drawRegion.bottom - 1 - offset, 
		 0, 360, 2, true);
	}

	EndDraw();
	return (ccode);
}
#endif

// ----- Main() -------------------------------------------------------------

int ZafApplication::Main(void)
{
	// Ensure main() is linked properly.
	LinkMain();

	// Create and add the StopLight window.
	zafWindowManager->Add(new LightWin(0, 0, 20, 6));

	// Get the user input.
	Control();

	// Return success.
	return (0);
}

