#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS

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

Err WbMmMenuKeyDownHandle (SysNotifyParamType *notifyParamsP)
{
	EventPtr DequedEvent;
	DequedEvent = (EventType*)notifyParamsP->notifyDetailsP;
	if (Swap16 (DequedEvent->eType) == keyDownEvent)		//keyDownEvent 0x0004
	{
		MenuBarType *menuP;
		menuP = MenuGetActiveMenu();
		if ( menuP != NULL && menuP->attr.visible)
		{
			EventType event;
			event.eType = (eventsEnum)keyDownEvent;
			event.penDown = false;
			event.tapCount = 0;
			event.screenX = 0;
			event.screenY = 0;
			event.data.keyDown.chr = vchrJogUp;
			event.data.keyDown.keyCode = 0x0000;
			event.data.keyDown.modifiers = commandKeyMask;

			UInt16 times;
			UInt16 shownItemsCount = 0;

			switch (Swap16(DequedEvent->data.keyDown.chr))
			{
			case chrUpArrow:	//chrUpArrow 0x001E
				notifyParamsP->handled = true;
//				event.data.keyDown.chr = vchrJogUp;
				SysHandleEvent (&event);
				DequedEvent->eType = (eventsEnum)0x0000;
				break;

			case chrDownArrow:	//chrDownArrow 0x001F
				notifyParamsP->handled = true;
				event.data.keyDown.chr = vchrJogDown;
				SysHandleEvent (&event);
				DequedEvent->eType = (eventsEnum)0x0000;
				break;

			case vchrJogLeft:
			case chrLeftArrow:	//chrLeftArrow 0x001C
				notifyParamsP->handled = true;
//				event.data.keyDown.chr = vchrJogUp;
				UInt16 shownMenusCount = 0;
				if (menuP->numMenus != 0)
				{
					for (times = 0; times<menuP->numMenus; times++)
					{
						if (menuP->menus[times].hidden != true)
							shownMenusCount++;
					}
				}
				if (menuP->curItem != -1)
				{
					for (times = menuP->curItem; times > 0; times--)
					{
						if ( (menuP->menus[menuP->curMenu].items[times].hidden != true) &&
							(menuP->menus[menuP->curMenu].items[times].itemStr[0] != MenuSeparatorChar))
							shownItemsCount++;
					}
				}
				else
				{
					shownItemsCount = -1;
				}
				for (times = shownItemsCount + shownMenusCount;times > 0;times--)
					SysHandleEvent (&event);
				event.data.keyDown.chr = vchrJogDown;
					SysHandleEvent (&event);
				DequedEvent->eType = (eventsEnum)0x0000;
				break;

			case vchrJogRight:
			case chrRightArrow:	//chrRightArrow 0x001D
				notifyParamsP->handled = true;
//				event.data.keyDown.chr = vchrJogUp;
				if (menuP->curItem != -1)
				{
					for (times = menuP->curItem; times > 0; times--)
					{
						if ( (menuP->menus[menuP->curMenu].items[times].hidden != true) &&
							(menuP->menus[menuP->curMenu].items[times].itemStr[0] != MenuSeparatorChar))
							shownItemsCount++;
					}
				}
				else
				{
					shownItemsCount = -1;
				}
				for (times = shownItemsCount + 2;times > 0;times--)
					SysHandleEvent (&event);
				event.data.keyDown.chr = vchrJogDown;
					SysHandleEvent (&event);
				DequedEvent->eType = (eventsEnum)0x0000;
				break;

			case chrLineFeed:	//chrLineFeed 0x000A
				notifyParamsP->handled = true;
				DequedEvent->data.keyDown.chr = Swap16 (vchrJogRelease);
										//vchrJogRelease 0x1706
				DequedEvent->data.keyDown.modifiers = Swap16 (commandKeyMask);
				break;

			case chrBackspace:	//chrBackspace 0x0008
				notifyParamsP->handled = true;
				event.data.keyDown.chr = vchrJogBack;
				SysHandleEvent (&event);
				DequedEvent->eType = (eventsEnum)0x0000;
				break;
			}
		}
	}
	return errNone;
}