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

// This example derives an object from ZafWindwoObject and implements 
// a Draw() function that uses many of the drawing primitives available
// in ZAF. When drawing on an object it is important to do the drawing
// in a Draw() function to insure that the object is refreshed at the 
// neccessary times by the OS.

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

// ----- Draw Object -------------------------------------------------------

DrawObject::DrawObject(void) :
	ZafWindowObject(1, 1, 40, 13)
{
	// Turn off the OSDraw so Draw() function will be called.
	SetOSDraw(false);
}

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

	if (ccode == S_REDISPLAY_REGION)
		display->SetClipRegion(event.region);

	// Draw a border around the object.
	DrawBorder(drawRegion, ccode);

	// Draw a rectangle with a wide border.
	// Set the foreground color to green to draw the border.
	display->SetForeground(ZAF_CLR_GREEN);

	// Set the background color to yellow to fill the rectangle.
	display->SetBackground(ZAF_CLR_YELLOW);
	
	display->Rectangle(drawRegion.left + display->cellWidth, 
		drawRegion.top + (display->cellHeight / 2), drawRegion.right - display->cellWidth,
		drawRegion.bottom - (display->cellHeight / 2), 4, true);

	// Set the lineStyle to dotted to draw dashed lines or borders.
	display->SetLineStyle(ZAF_LINE_DOTTED);

	// Draw a filled arc.
	// Set the foreground to black to draw the border.
	display->SetForeground(ZAF_CLR_BLACK);

	// Set the background to light green to fill the arc.
	display->SetBackground(ZAF_CLR_LIGHTGREEN);
	display->Ellipse(drawRegion.right - (20 * display->cellWidth),
		drawRegion.bottom - (6 * display->cellHeight),
		drawRegion.right - (10 * display->cellWidth), drawRegion.bottom -
		(3 * display->cellWidth), 25, 249, 1, true);

	// Draw a line and change the color to draw with.
	display->SetForeground(ZAF_CLR_LIGHTBLUE);
	display->Line(drawRegion.right, drawRegion.top, drawRegion.left,
		drawRegion.bottom, 1);

	// Draw a filled polygon.
	// Create a list of points.
	ZafCoordinate polyPoints [6];
	polyPoints[0] = drawRegion.left + (4 * display->cellWidth);
	polyPoints[1] = drawRegion.bottom - (7 * display->cellHeight);
	polyPoints[2] = drawRegion.left + (5 * display->cellWidth);
	polyPoints[3] = drawRegion.bottom - (3 * display->cellHeight);
	polyPoints[4] = drawRegion.left + (10 * display->cellWidth);
	polyPoints[5] = drawRegion.bottom - (2 * display->cellHeight);

	// Set the background and foreground colors.
	display->SetForeground(ZAF_CLR_RED);
	display->SetBackground(ZAF_CLR_MAGENTA);
	
	// Set the line style to solid lines
	display->SetLineStyle(ZAF_LINE_SOLID);
	display->Polygon(3, polyPoints, 1, true, true);

	// Draw some text.
	// Set the color to draw the text in.
	display->SetForeground(ZAF_CLR_RED);

	// Set the font to use.
	display->SetFont(ZAF_FNT_SYSTEM);
	display->Text(drawRegion.left + (6 * display->cellWidth),
		drawRegion.top + ((12 * display->cellHeight) / 5) , ZAF_ITEXT("This is text."));

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

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

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

	// Create a window with a DrawObject.
	ZafWindow *window = new ZafWindow(0, 0, 42, 15);
	window->AddGenericObjects(new ZafStringData("Draw Object Window"));
	window->Add(new DrawObject());
	
	// Center the window.
	zafWindowManager->Center(window);

	// Add the window to the window manager.
	zafWindowManager->Add(window);

	// Get the user input.
	Control();

	// Return success.
	return (0);
}

