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

#define USE_RAW_KEYS
#include <z_keymap.hpp>
#include <z_group.hpp>
#include <z_string.hpp>

// ----- ZafGroup ----------------------------------------------------------

ZafPositionStruct ZafGroup::ConvertToOSPosition(const ZafWindowObject *object,
	const ZafPositionStruct *zafPosition) const
{
	return (ZafWindow::ConvertToOSPosition(object, zafPosition));
}

ZafPositionStruct ZafGroup::ConvertToZafPosition(const ZafWindowObject *object,
	const ZafPositionStruct *osPosition) const
{
	return (ZafWindow::ConvertToZafPosition(object, osPosition));
}

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafGroup::Draw(const ZafEventStruct &event, ZafEventType ccode)
{
//	ZafPaletteState state = PaletteState();
	ZafRegionStruct drawRegion = BeginDraw();

	display->SetClipRegion(event.region);

	DrawBackground(drawRegion, ccode);

/* START BLOCK COMMENT
**		if (bordered)
**			DrawBorder(drawRegion, ccode);
END BLOCK COMMENT */

/* START BLOCK COMMENT
**		display->SetPalette(LogicalPalette(ZAF_PM_TEXT, state));
**		const ZafIChar *text = Text();
**		if (text && text[0])
**			display->Text(3 * display->cellWidth,
**				display->cellHeight / 4, drawRegion.right, drawRegion.bottom, text, -1, ZAF_HZ_LEFT, ZAF_VT_TOP,
**				hotKeyIndex, false);
END BLOCK COMMENT */

	EndDraw();
	return (ccode);
}

ZafEventType ZafGroup::DrawBorder(ZafRegionStruct &, ZafEventType ccode)
{
	ZafPaletteState state = PaletteState();
	display->SetPalette(LogicalPalette(ZAF_PM_TEXT, state));

	ZafPaletteStruct lightShadow = LogicalPalette(ZAF_PM_LIGHT_SHADOW, ZAF_PM_ANY_STATE);
	ZafPaletteStruct darkShadow = LogicalPalette(ZAF_PM_DARK_SHADOW, ZAF_PM_ANY_STATE);
	ZafPaletteStruct whiteShadow = lightShadow;
	ZafPaletteStruct blackShadow = darkShadow;

	// Copy the background colors to the foreground since display->Line() uses 
	// the foreground color.  The "shadow" palettes have different colors in 
	// the foreground and background entries so they can generate two shades
	// of shadow.
	whiteShadow.colorForeground = whiteShadow.colorBackground;
	blackShadow.colorForeground = blackShadow.colorBackground;

	const ZafIChar *text = Text();

	int width = (text && text[0]) ?
		display->TextSize(text).Width() + 2 * display->cellWidth : 0;

	ZafCoordinate polygon[12];
	polygon[10] = 2 * display->cellWidth;
	polygon[0] = polygon[10] + width;
	polygon[1] = polygon[3] = polygon[9] = polygon[11] = display->cellHeight / 2;
	polygon[2] = polygon[4] = zafRegion.Width() - 2;
	polygon[5] = polygon[7] = zafRegion.Height() - 2;
	polygon[6] = polygon[8] = 0;

	display->SetPalette(darkShadow);
	display->Polygon(6, polygon);

	display->SetPalette(whiteShadow);
	display->Line(1, polygon[1] + 1, polygon[10], polygon[1] + 1);
	display->Line(polygon[0], polygon[1] + 1, zafRegion.Width() - 3, polygon[1] + 1);
	display->Line(1, polygon[1] + 1, 1, zafRegion.Height() - 3);
	display->Line(zafRegion.Width() - 1, polygon[1], zafRegion.Width() - 1, zafRegion.Height() - 1);
	display->Line(0, zafRegion.Height() - 1, zafRegion.Width() - 2, zafRegion.Height() - 1);

	return (ccode);
}

ZafEventType ZafGroup::DrawFrame(const ZafEventStruct &event, ZafEventType ccode)
{
	ZafPaletteState state = PaletteState();
	ZafRegionStruct drawRegion = BeginDrawFrame();

	display->SetClipRegion(event.region);

	DrawBackground(drawRegion, ccode);

	if (bordered)
		DrawBorder(drawRegion, ccode);

	display->SetPalette(LogicalPalette(ZAF_PM_TEXT, state));
	const ZafIChar *text = Text();
	if (text && text[0])
		display->Text(3 * display->cellWidth,
			display->cellHeight / 4, drawRegion.right, drawRegion.bottom, text, -1, ZAF_HZ_LEFT, ZAF_VT_TOP,
			hotKeyIndex, false);

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

ZafEventType ZafGroup::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = LogicalEvent(event);
	switch (ccode)
	{
	case S_HOT_KEY:
		if (event.key.value == hotKeyChar)
			SetFocus(true);
		else
			ccode = ZafWindow::Event(event);
		break;

	case L_NEXT:
	case L_PREVIOUS:
		ccode = S_UNKNOWN;
		break;

	case L_UP:
	case L_DOWN:
	case L_LEFT:
	case L_RIGHT:
		{
		ZafWindowObject *curObj = Current();
		ccode = ZafWindow::Event(event);
		if (AutoSelect() && curObj != Current() && Current())
			(Current()->*(Current()->memberUserFunction))(event, L_SELECT);
		}
		break;

	default:
		ccode = ZafWindow::Event(event);
	}

	return (ccode);
}

ZafRegionStruct ZafGroup::MaxRegion(ZafWindowObject *, ZafVtJustify, ZafHzJustify)
{
	// Calculate group client region.
	ZafRegionStruct region;
	region.left = bordered ? ZAF_BORDER_SIZE : 0;
	const ZafIChar *text = Text();
#if defined(ZAF_GRAPHICS_MODE)
	region.top = (bordered || (text && text[0])) ? (3 * display->cellHeight) / 4 : 0;
#else
	region.top = 0;
#endif
	region.right = zafRegion.Width() - 1;
	region.bottom = zafRegion.Height() - 1;
	region.coordinateType = zafRegion.coordinateType;
	if (bordered)
	{
		region.right -= ZAF_BORDER_SIZE;
		region.bottom -= ZAF_BORDER_SIZE;
	}

	return (region);
}

ZafWindowObject *ZafGroup::NotifyFocus(ZafWindowObject *focusObject, bool setFocus)
{
	// Defer to the base class.
	ZafWindowObject *invalidObject = ZafWindow::NotifyFocus(focusObject, setFocus);
	if (setFocus && autoSelect && SelectionType() == ZAF_SINGLE_SELECTION)
		focusObject->SetSelected(true);
	return (invalidObject);
}

bool ZafGroup::SetDisabled(bool setDisabled)
{
	// Defer to the base class.
	return (ZafWindow::SetDisabled(setDisabled));
}

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

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

void ZafGroup::OSRegisterObject(void)
{
	ZafWindow::OSRegisterObject();
}

OSWindowID ZafGroup::OSScreenID(ZafScreenIDType) const
{
	return (screenID);
}

ZafError ZafGroup::OSSetText(void)
{
	return (ZAF_ERROR_NONE);
}

ZafError ZafGroup::OSUpdatePalettes(ZafPaletteType , ZafPaletteType )
{
	if (automaticUpdate)
	{
		Event(S_REDISPLAY);
/* START BLOCK COMMENT
**			SendMessage(screenID, WM_SETREDRAW, (WPARAM)TRUE, 0);
**			parent->Event(ZafEventStruct(S_REDISPLAY_REGION, 0, parent->ConvertToOSRegion(this)));
END BLOCK COMMENT */
	}

	return (ZAF_ERROR_NONE);
}

