#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS

#include <PalmOS.h>
#include "T-Suite.h"

Err WbMnUpDownKeyEventPreHandle (SysNotifyParamType *notifyParamsP)
{
	EventType *event = (EventType*)notifyParamsP->notifyDetailsP;

// is keyDownEvent
	if (event->eType != Swap16 (keyDownEvent))
		return errNone;

// is UpDownKeyEvent
	if ((event->data.keyDown.chr != Swap16 (chrPageUp)) && (event->data.keyDown.chr != Swap16 (chrPageDown)))
		return errNone;

// get Feature Value and Set Temp Feature Value
	UInt32 ftrValue;
	FtrGet (appFileCreator, ftr_appStatus, &ftrValue);
	TSuitePreferenceType *prefsP = (TSuitePreferenceType*)ftrValue;

	TSuitePreferenceType prefs;
		MemMove (&prefs, prefsP, sizeof (TSuitePreferenceType));

// check UpDownPush.
	if ((event->data.keyDown.chr == Swap16 (chrPageUp)) || (event->data.keyDown.chr == Swap16 (chrPageDown)))
	{
		if ((Swap16 (event->data.keyDown.modifiers) & autoRepeatKeyMask) != autoRepeatKeyMask)
		{
			prefs.UpDownPushDetected = IsOn;

			if (!prefsP->UpDownPushDetected)
				DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));
		}
	}
	else return errNone;

	// if UpDownPush was not detected. change FiveWayEvent to nilEvent.
	if (!prefs.UpDownPushDetected)
	{
		MemSet (event, sizeof (EventType), 0);
		return errNone;
	}

// check UpDownPush.
	MenuBarType *menuP;

	menuP = MenuGetActiveMenu();

	if (menuP != NULL && menuP->attr.visible)
		return errNone;

	if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
		return errNone;

	UInt32 Key;

	if ((Swap16 (event->data.keyDown.modifiers) & autoRepeatKeyMask) == autoRepeatKeyMask)
		return errNone;

	// if PageUp->Down press, then Menu Open.
	if (event->data.keyDown.chr == Swap16 (chrPageUp))
	{
		UInt32 prvTimeOut = TimGetTicks () + (SysTicksPerSecond () / 3);

		Boolean Depressed = false;

		do
		{
			Key = KeyCurrentState ();
			switch (Key)
			{
			case keyBitPageDown:
				EvtFlushKeyQueue ();
				EvtEnqueueKey (vchrMenu, 0, commandKeyMask);
				goto SelectUpDownHandled;
			case keyBitPageUp:
				if (Depressed)
					return errNone;
				break;
			case NULL:
				Depressed = true;
				break;
			default:
				return errNone;
			}
			
//			if (Key == keyBitPageDown)
//			{
//				EvtFlushKeyQueue ();
//				EvtEnqueueKey (vchrMenu, 0, commandKeyMask);
//				goto SelectUpDownHandled;
//			}
		}while (prvTimeOut > TimGetTicks ());

		return errNone;
	}

	// if PageDown->Up press, then Toolbar Open.
	if (event->data.keyDown.chr == Swap16 (chrPageDown))
	{
		UInt32 prvTimeOut = TimGetTicks () + (SysTicksPerSecond () / 3);

		Boolean Depressed = false;

		do
		{
			Key = KeyCurrentState ();
			switch (Key)
			{
			case keyBitPageUp:
				EvtFlushKeyQueue ();
				EvtEnqueueKey (vchrCommand, 0, commandKeyMask);
				goto SelectUpDownHandled;
			case keyBitPageDown:
				if (Depressed)
					return errNone;
				break;
			case NULL:
				Depressed = true;
				break;
			default:
				return errNone;
			}

//			if (Key == keyBitPageUp)
//			{
//				EvtFlushKeyQueue ();
//				EvtEnqueueKey (vchrCommand, 0, commandKeyMask);
//				goto SelectUpDownHandled;
//			}
		}while (prvTimeOut > TimGetTicks ());

		return errNone;
	}

	return errNone;

SelectUpDownHandled:
	MemSet (event, sizeof (EventType), 0);
	prefs.UpDownPushDetected = IsOff;
	DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));

	return errNone;
}