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

#include <zaf.hpp>
#include <z_utils.hpp>
#include "bitmap2.hpp"

// Constructor for the MyBitmap class.
MyBitmap::MyBitmap(int left, int top, int width, int height,
	ZafIChar *tBitmapName) :
	ZafWindowObject(left, top, width, height),
	bitmapData(ZAF_NULLP(ZafBitmapData)), bitmapName(ZAF_NULLP(ZafIChar)),
	borderWidth(0)
{
	// Set the various attributes.
	SetNoncurrent(true);
	SetAutoSize(false);
	if (tBitmapName)
		SetBitmapName(tBitmapName);
	SetOSDraw(false);
	SetBordered(true);
	if (Bordered())
		borderWidth = ZAF_BORDER_SIZE;
}

// Destructor for the MyBitmap.  Cleans up its own allocates data.
MyBitmap::~MyBitmap(void)
{
	// Delete the various members.
	if (bitmapName)
		delete []bitmapName;
	if (bitmapData && bitmapData->Destroyable() && bitmapData->NotifyCount() == 0)
		delete bitmapData;
}

#if defined(ZAF_DEFAULT_DRAW)
// Draw function for the MyBitmap object
ZafEventType MyBitmap::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	ZafRegionStruct region = BeginDraw();

	// Draw the border if the attribute it set.
	if (Bordered())
		DrawBorder(region, ccode);

	// Display the bitmap itself.
	if (bitmapData)
		zafDisplay->Bitmap(region.left, region.top, *bitmapData);

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

// Event handling is done in the Event function.
ZafEventType MyBitmap::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch (ccode)
	{
	case S_COMPUTE_SIZE:
		// Adjust the size of the object in to the size of the
		// bitmap if the autosize is set to true.
		if (AutoSize())
		{
			int height = bitmapData->Height();
			int width = bitmapData->Width();
			zafRegion.bottom = zafRegion.top + height + (2 * borderWidth) - 1;
			zafRegion.right = zafRegion.left + width + (2 * borderWidth) - 1;
		}
		ccode = ZafWindowObject::Event(event);
		break;

	default:
		ccode = ZafWindowObject::Event(event);
	}
	return (ccode);
}

// Function to set the autosize, true or false.
bool MyBitmap::SetAutoSize(bool tAutoSize)
{
	autoSize = tAutoSize;
	return (autoSize);
}

// Load and set a bitmap.  Should only occur
// for a non-animated bitmap.
void MyBitmap::SetBitmapName(ZafIChar *tBitmapName)
{
	if (bitmapName || tBitmapName)
	{
		delete bitmapName;
		bitmapName = strdup(tBitmapName);
		if (bitmapData)
			delete bitmapData;
		bitmapData = new ZafBitmapData(bitmapName, *zafDataPersistence);
	}
	else
	{
		bitmapName = ZAF_NULLP(ZafIChar);
		bitmapData = ZAF_NULLP(ZafBitmapData);
	}
}
