//	Zinc Application Framework - D_CLOSE.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_title.hpp>
#include "i_close.hpp"
#include <z_app.hpp>

#if defined (ZAF_WIN95_STYLE)

#define ZAF_CLOSE_BUTTON_INFO
#include "img_def.cpp"

ZafClassID ZafCloseButton::classID = ID_ZAF_CLOSE_BUTTON;
ZafClassNameChar ZAF_FARDATA ZafCloseButton::className[] = ZAF_ITEXT("ZafCloseButton");

// ----- ZafCloseButton ---------------------------------------------------

static ZafStringIDChar ZAF_FARDATA _stringID[] = ZAF_ITEXT("ZAF_NUMID_CLOSE");

char ZAF_FARDATA ZafCloseButton::closeName[] = "close";
char ZAF_FARDATA ZafCloseButton::closeDepressedName[] = "closeDepressed";
char ZAF_FARDATA ZafCloseButton::closeDisabledName[] = "closeDisabled";
ZafBitmapData *ZafCloseButton::closeBitmap = ZAF_NULLP(ZafBitmapData);
ZafBitmapData *ZafCloseButton::closeDepressedBitmap = ZAF_NULLP(ZafBitmapData);
ZafBitmapData *ZafCloseButton::closeDisabledBitmap = ZAF_NULLP(ZafBitmapData);

ZafCloseButton::ZafCloseButton(ZafEventType tValue) :
	ZafButton(0, 0, 0, 0, ZAF_NULLP(ZafIChar))
{
	SetNoncurrent(true);
	SetBordered(true);
	SetSupportObject(true);
	SetRegionType(ZAF_AVAILABLE_REGION);
	SetSendMessageWhenSelected(true);
	SetValue(tValue);
	numberID = ZAF_NUMID_CLOSE;

	// Check the image information.
	ImageAllocate();

	SetDepth(1);
	SetStringID(_stringID);
}

ZafCloseButton::ZafCloseButton(const ZafCloseButton &copy) :
	ZafButton(copy)
{
}

ZafCloseButton::~ZafCloseButton(void)
{
	// Check the image information.
	ImageFree();
}

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafCloseButton::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	// Compute the actual draw region.
	ZafPaletteState state = 0;
	if (RootObject()->Focus())
		state |= ZAF_PM_ACTIVE;
	ZafRegionStruct drawRegion = BeginDraw();

	ZafPaletteStruct palette;
	ZafWindow *parentWindow = DynamicPtrCast(parent, ZafWindow);
	ZafWindowObject *title = parentWindow->Title();
	if (title)
		palette = title->LogicalPalette(ZAF_PM_TEXT, state);
	else
		palette = LogicalPalette(ZAF_PM_TEXT, state);

	display->SetPalette(palette);

	// Draw the bitmap.
	automaticUpdate = false;
	if (Disabled())
		SetBitmapData(closeDisabledBitmap);
	else if (Depressed())
		SetBitmapData(closeDepressedBitmap);
	else
		SetBitmapData(closeBitmap);
	automaticUpdate = true;

	int bitmapWidth = bitmapData->Width();
	int bitmapHeight = bitmapData->Height();
	int fieldHeight = zafRegion.Height();
	int right = bitmapWidth;
	int top = (fieldHeight - bitmapHeight) / 2;
	int bottom = top + bitmapHeight;

	display->Bitmap(0, top, *bitmapData);

	display->Rectangle(0, 0, zafRegion.Width() - 1,	top - 1, 0, true); 
	display->Rectangle(0, bottom, zafRegion.Width() - 1, zafRegion.Height() - 1, 0, true);
	display->Rectangle(right, top, zafRegion.Width() - 1, bottom, 0, true);

	EndDraw();

	return (ccode);
}
#endif

ZafEventType ZafCloseButton::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = LogicalEvent(event);

	// Check for zinc events.
	switch (ccode)
	{
	// ----- Create messages -----
	case S_COMPUTE_SIZE:
		ZafWindowObject::Event(event);
		zafRegion.bottom = zafRegion.top + (display->cellHeight - display->preSpace - display->postSpace - 3);
		zafRegion.left = zafRegion.right - (display->cellHeight - display->preSpace - display->postSpace - 2);
		break;

	// ----- Unknown or error messages -----
	default:
		ccode = ZafButton::Event(event);
		break;
	}

	// Return the control code.
	return (ccode);
}

void ZafCloseButton::ImageAllocate(void)
{
	// closeBitmap
	if (!closeBitmap)
	{
		ZafI18nData::AddStaticModule(ImageFree);
		closeBitmap = new ZafBitmapData(_close_ZafBitmap);
		closeBitmap->staticHandle = true;
		closeBitmap->SetDestroyable(false);

		closeDepressedBitmap = new ZafBitmapData(_closeDepressed_ZafBitmap);
		closeDepressedBitmap->staticHandle = true;
		closeDepressedBitmap->SetDestroyable(false);

		closeDisabledBitmap = new ZafBitmapData(_closeDisabled_ZafBitmap);
		closeDisabledBitmap->SetDestroyable(false);
	}
}

void ZafCloseButton::ImageFree(bool globalRequest)
{
	// Destroy the static images.
	if (globalRequest && closeBitmap)
	{
		closeBitmap->staticHandle = false;
		delete closeBitmap;
		closeBitmap = ZAF_NULLP(ZafBitmapData);
		closeDepressedBitmap->staticHandle = false;
		delete closeDepressedBitmap;
		closeDepressedBitmap = ZAF_NULLP(ZafBitmapData);
		closeDisabledBitmap->staticHandle = false;
		delete closeDisabledBitmap;
		closeDisabledBitmap = ZAF_NULLP(ZafBitmapData);
	}
}

bool ZafCloseButton::IsA(ZafClassID compareID) const
{
	return ((compareID == classID) ? true : ZafButton::IsA(compareID));
}

bool ZafCloseButton::IsA(ZafClassName compareName) const
{
	return (!strcmp(compareName, className) ? true : ZafButton::IsA(compareName));
}

bool ZafCloseButton::SetSupportObject(bool setSupportObject)
{
	// Make sure the attribute has changed.
	if (supportObject != setSupportObject)
		supportObject = setSupportObject;

	// Return the current attribute.
	return (supportObject);
}

#if defined(USE_ZAF_PERSISTENCE)
void ZafCloseButton::Write(ZafObjectPersistence &) 
{
	// Don't allow it to store.
}
#endif

#endif // Win95 style.

