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

#include <time.h>
#include <z_string.hpp>
#include <z_title.hpp>

const int STRING_OFFSET = 2;

// ----- ZafTitle ----------------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafTitle::Draw(const ZafEventStruct &event, ZafEventType ccode)
{
	// Compute the actual draw region.
	ZafPaletteState state = PaletteState();
	ZafRegionStruct drawRegion = BeginDraw();

	display->SetClipRegion(event.region);

	ZafPaletteStruct palette = LogicalPalette(ZAF_PM_TEXT, state);
	display->SetPalette(palette);

	display->Rectangle(drawRegion, 0, true);
	display->Text(drawRegion.left + display->cellWidth + STRING_OFFSET, drawRegion.top + STRING_OFFSET, drawRegion.right, drawRegion.bottom, Text(),
		-1, ZAF_HZ_LEFT, VtJustify(), hotKeyIndex);

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

ZafEventType ZafTitle::Event(const ZafEventStruct &event)
{
	ZafWindow *parentWindow = DynamicPtrCast(parent, ZafWindow);

	ZafEventType ccode = LogicalEvent(event);

	ZafEventStruct tEvent;

	// Check for zinc events.
	switch (ccode)
	{
	// ----- Create messages -----
	case S_INITIALIZE:
	case S_DEINITIALIZE:
		ccode = ZafWindowObject::Event(event);
		break;

	case S_COMPUTE_SIZE:
		ZafWindowObject::Event(event);
#if defined(ZAF_GRAPHICS_MODE)
		zafRegion.bottom = zafRegion.top + (display->cellHeight - display->preSpace - display->postSpace - 3);
#elif defined(ZAF_TEXT_MODE)
        zafRegion.bottom = zafRegion.top;
#endif
		// Set message value based on parent's state.
		if (parent->parent)
			SetValue(parentWindow->Maximized() || parentWindow->Minimized() ?
				S_MDI_RESTORE : S_MDI_MAXIMIZE);
		else
			SetValue(parentWindow->Maximized() || parentWindow->Minimized() ?
				S_RESTORE : S_MAXIMIZE);
		break;

	case S_REGISTER_OBJECT:
		ccode = ZafWindowObject::Event(event);
		break;

	// ----- Display messages -----
	case S_REDISPLAY_REGION:
	case S_REDISPLAY:
		if (AutomaticUpdate())
			ZafWindowObject::Event(S_REDISPLAY);
		break;

	// ----- Actions -----
	case L_BEGIN_SELECT:
		{
			tEvent = event;
			tEvent.type = S_MOVE_MODE;
			tEvent.rawCode = 0x0001;
			parent->Modify(tEvent);
			windowManager->CaptureMouse(this);
		}
		break;

	case L_END_SELECT:
	case L_CONTINUE_SELECT:
		if (windowManager->MouseEventRoute() == this)
			parent->Modify(event);
		if (ccode == L_END_SELECT)
			windowManager->CaptureMouse(0);
		break;

	case L_DOUBLE_CLICK:
		{
			ZafEventStruct event(Value());
			event.rawCode = 0;
			event.data = parent;
			eventManager->Put(event);
		}
		break;

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

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

// ----- OS Specific Functions ----------------------------------------------

void ZafTitle::OSRegisterObject(void)
{
	ZafWindowObject::OSRegisterObject();
}

ZafError ZafTitle::OSSetText(void)
{
	if (screenID)
		Redisplay();
	return (ZAF_ERROR_NONE);
}

void ZafTitle::OSSize(void)
{
	ZafWindowObject::OSSize();
}

ZafError ZafTitle::OSUpdatePalettes(ZafPaletteType zafTypes, ZafPaletteType osTypes)
{
	return (ZafButton::OSUpdatePalettes(zafTypes, osTypes));
}

