// COPYRIGHT (C) 1990-1999.  All Rights Reserved. - POPUP.CPP
// Zinc Software Incorporated.  Pleasant Grove, Utah  USA
// May be freely copied, used and distributed.

#include <zaf.hpp>
#include "popup.hpp"

PopupWindow::PopupWindow(void) :
	ZafWindow(0, 0, 60, 5), popup(ZAF_NULLP(ZafPopUpMenu))
{
	ZafPositionStruct pos;
	pos.Assign(0, 0, ZAF_PIXEL);
	popup = PopupMenu(pos);
	AddGenericObjects(new ZafStringData("Press the right mouse button in the window."));
	zafWindowManager->Center(this);
}

PopupWindow::~PopupWindow(void)
{
	delete popup;
}

ZafEventType PopupWindow::Beep(ZafWindowObject *, ZafEventStruct &, ZafEventType ccode)
{
	if (ccode == L_SELECT)
		zafEventManager->Put(BEEP_EVENT);
	return (0);
}

ZafEventType PopupWindow::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = LogicalEvent(event);
	switch (ccode)
	{
	case L_BEGIN_ESCAPE:
		{
		if (popup->screenID)
			windowManager->Subtract(popup);
		// Convert the mouse coordinates to a screen position.
		ZafPositionStruct pos = ConvertToScreenPosition(event.position);
		popup->zafRegion.top = pos.line;
		popup->zafRegion.left = pos.column;
		zafWindowManager->Add(popup);
		}
		break;

	case BEEP_EVENT:
		ZafErrorStub::Beep();
		break;

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

ZafPopUpMenu *PopupWindow::PopupMenu(ZafPositionStruct position)
{
	ZafPopUpMenu *menu = new ZafPopUpMenu(position.column, position.line);
	menu->SetCoordinateType(ZAF_PIXEL);

	ZafPopUpItem *item = new ZafPopUpItem(ZAF_ITEXT("Function"));
	item->SetUserFunction(Beep);
	menu->Add(item);

	item = new ZafPopUpItem(ZAF_ITEXT("Send Message"));
	item->SetSendMessageWhenSelected(true);
	item->SetValue(BEEP_EVENT);
	menu->Add(item);

	return (menu);
};

int ZafApplication::Main(void)
{
	LinkMain();

	zafWindowManager->Add(new PopupWindow);

	Control();

	return (0);
}

