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

#include <z_ctype.hpp>
#include <z_prompt.hpp>

// ----- ZafPrompt ----------------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafPrompt::Draw(const ZafEventStruct &event, ZafEventType ccode)
{
	if (transparentBackground && parent && systemObject)
	{
		ZafEventStruct tEvent = event;
		tEvent.region.HzShift(zafRegion.left);
		tEvent.region.VtShift(zafRegion.top);
		parent->Draw(tEvent, ccode);
	}
	// Compute the actual draw region.
	ZafRegionStruct drawRegion = BeginDraw();
	display->SetClipRegion(event.region);

	if (!transparentBackground)
		DrawBackground(drawRegion, ccode);

	display->SetPalette(LogicalPalette(ZAF_PM_TEXT, PaletteState()));
	display->Text(drawRegion, Text(), -1, HzJustify(), ZAF_VT_CENTER, hotKeyIndex, false);

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

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

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

		if (autoSize)
		{
			ZafRegionStruct size;

			display->SetPalette(LogicalPalette(ZAF_PM_TEXT, ZAF_PM_ANY_STATE));
			size = display->TextSize(Text());
#if defined(ZAF_GRAPHICS_MODE)
			if (hzJustify == ZAF_HZ_LEFT)
				zafRegion.right = zafRegion.left + size.Width() +
					(bordered ? 2 * ZAF_BORDER_SIZE : 0);
			else if (hzJustify == ZAF_HZ_RIGHT)
				zafRegion.left = zafRegion.right - size.Width() -
					(bordered ? 2 * ZAF_BORDER_SIZE : 0);
#endif
		}
		}
		break;

	case L_BEGIN_SELECT:
	case L_END_SELECT:
		if (editMode)
			ZafWindowObject::Event(event);
		break;

	case S_HOT_KEY:
		if (hotKeyChar && event.key.value == hotKeyChar)
		{
			if (Next())
				Next()->SetFocus(true);
		}
		else
			ccode = S_UNKNOWN;
		break;

	case S_REDISPLAY_DATA:
		OSSetText();
		windowManager->RepairDamage();
		break;

	case S_UPDATE_OBJECT:
		OSSetText();
		break;

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

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

bool ZafPrompt::SetDisabled(bool setDisabled)
{
	return (ZafWindowObject::SetDisabled(setDisabled));
}

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

ZafEventType ZafPrompt::OSEvent(const ZafEventStruct &)
{
	return (S_ERROR);
}

void ZafPrompt::OSMapPalette(ZafPaletteStruct &palette, ZafPaletteType type, ZafPaletteState state)
{
	ZafWindowObject::OSMapPalette(palette, type, state);
}

ZafError ZafPrompt::OSSetText(void)
{
	if (screenID)
	{
		parent->OSInvalidateRegion(&zafRegion, false, true);
		Event(S_COMPUTE_SIZE);
/* START BLOCK COMMENT
**			ZafRegionStruct tRegion = saveRegion;
**			tRegion.VtShift(-tRegion.top);
**			tRegion.HzShift(-tRegion.left);
**			OSInvalidateRegion(&tRegion);
END BLOCK COMMENT */
//		parent->OSInvalidateRegion(&saveRegion, false, true);
		Redisplay();
	}
	return (ZAF_ERROR_NONE);
}

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

ZafError ZafPrompt::OSUpdatePalettes(ZafPaletteType zafTypes, ZafPaletteType osTypes)
{
	return (ZafWindowObject::OSUpdatePalettes(zafTypes, osTypes));
}

