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

#include <z_plldn.hpp>
#include <z_string.hpp>

// ----- ZafPullDownItem -------------------------------------------------

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

	display->SetClipRegion(event.region);

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

	--drawRegion;
	display->Rectangle(drawRegion, 0, true);
	display->Text(drawRegion.left, drawRegion.top, drawRegion.right, drawRegion.bottom, Text(), -1, HzJustify(), VtJustify(), hotKeyIndex, false);

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

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

	// Check for zinc events.
	switch (ccode)
	{
	case S_INITIALIZE:
#if defined(ZAF_GRAPHICS_MODE)
		menu.SetBordered(true);
#elif defined(ZAF_TEXT_MODE)
		menu.SetBordered(false);
#endif
		menu.SetTemporary(true);
		menu.SetDestroyable(false);
		ZafButton::Event(event);
		break;

	case S_COMPUTE_SIZE:
		ccode = ZafWindowObject::Event(event);
		display->SetFont(LogicalPalette(ZAF_PM_TEXT, ZAF_PM_ANY_STATE).font);
		zafRegion.right = zafRegion.left + display->TextSize(Text()).Width() + 2 * display->cellWidth - 1;
//		zafRegion.bottom = zafRegion.top + display->cellHeight - 2 * (display->preSpace + display->postSpace);
		break;

	case S_NON_CURRENT:
		// Make sure menu.current doesn't get "stuck" in a focus state.
		if (menu.Current())
			menu.Current()->Event(event);
		ccode = ZafButton::Event(event);
		break;

	case L_VIEW:
	case L_CONTINUE_SELECT:
		{
			// We only get L_CONTINUE_SELECT if the pull-down menu sends it,
			// which it won't do if it isn't tracking the mouse, so we can
			// assume we're allowed to have focus, etc.
			SetFocus(true);

			// Need to close some pop-up menus?
			ZafWindowObject *rootObject = RootObject();
			if	(windowManager->First() != rootObject)
			{
				ZafWindow *popup = DynamicPtrCast(windowManager->First(), ZafWindow);
				while(popup && popup != rootObject && popup != &menu)
					if (popup->Temporary())
					{
						windowManager->Event(ZafEventStruct(S_CLOSE_TEMPORARY));
						popup = DynamicPtrCast(windowManager->First(), ZafWindow);
					}
				if (menu.First() && windowManager->Index(&menu) == -1)
					Event(ZafEventStruct(L_DOWN));
			}
		}
		break;

	case L_BEGIN_SELECT:
		ccode = ZafWindowObject::Event(event);
		if (ccode == S_UNKNOWN)
			break;

		// Continue to L_SELECT.
	case L_SELECT:
		if (userFunction)
		{
			ZafEventStruct uEvent;
			uEvent = event;
			ccode = (*userFunction)(this, uEvent, L_SELECT);
			break;
		}
		else if (!menu.First() && SendMessageWhenSelected())
		{
			eventManager->Put(ZafEventStruct(Value()), Q_BEGIN);
			break;
		}

		// Continue to L_DOWN.
	case L_DOWN:
		if (menu.First() && windowManager->Index(&menu) == -1)
		{
	 		menu.zafRegion.coordinateType = ZAF_PIXEL;
			ZafPositionStruct origin;
			origin.column = zafRegion.left;
			origin.line = zafRegion.bottom;
			origin.coordinateType = zafRegion.coordinateType;

			// Since origin is relative to the parent's coordinates, call
			// parent's ConvertToOSPosition.
			origin = parent->ConvertToOSPosition(this, &origin);
			menu.zafRegion.left = origin.column;
			menu.zafRegion.top = origin.line + 1;
			if (menu.Current())
				menu.Current()->SetFocus(true);
//				menu.Current()->SetFocus(false);
			windowManager->Add(&menu);
		}
		break;

	// ----- Miscellaneous messages -----
	case S_ADD_OBJECT:
		Add(event.windowObject);
		break;

	case S_SUBTRACT_OBJECT:
		Subtract(event.windowObject);
		break;

	default:
		ccode = ZafButton::Event(event);
		break;
	}

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

bool ZafPullDownItem::SetDisabled(bool setDisabled)
{
	return (ZafButton::SetDisabled(setDisabled));
}

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

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

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

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

ZafError ZafPullDownItem::OSSetText(void)
{
	if (screenID && parent)
	{
		parent->Event(S_COMPUTE_SIZE);
		parent->Redisplay();
	}
	return (ZAF_ERROR_NONE);
}

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

ZafError ZafPullDownItem::OSUpdatePalettes(ZafPaletteType zafTypes, 
 ZafPaletteType osTypes)
{
	// Defer to the base class.
	ZafError tError = ZafButton::OSUpdatePalettes(zafTypes, osTypes);
	return (tError);
}

