/* ... and so we go onward (and downward ???) as we attempt to fake out
	the user interface by attempting to emulate menus in not quite real
	time!
	Mark T. Collins			4/25/92   10:22AM  (after a LONG all nighter!)
*/

/* Episode One-  get measure our menu strings and make sure they are
	sufficient for what we want to do, AND get our strings & things
	along the way */

void SetupMenuExtraData (void)  /* this should be called on the open to setup
									our data */
{
word					i;
long					hansize;
handle					menuHandle;
MenuTemplatePtr			theMenu;
MenuItemTemplatePtr		theMenuItem;
itemStructPtr			theItemStruct;


	menuHandle = GetResData(rMenu, p->rID, 1);
	hansize = (GetHandleSize(menuHandle) * 4) + 6L;
	pPtr->extraData = NewHandle(hansize, edUserID, 0x8018, 0L);
	privMen = (privMenuRecPtr) deref(pPtr)->extraData);
	theMenu = (MenuTemplatePtr) deref(menuHandle);
	privMen->menuID = theMenu->menuID;
	privMen->title = deref(GetResData(rPString, theMenu->menuTitleRef, 1);
    privMen->count = 0;

 	for i = 0; theMenu->itemRefArray[i] != 0; i++) {
    	theMenuItem = (MenuItemTemplatePtr) deref(GetResData(rMenuItem,
	    	theMenu->itemRefArray[i], 1));
		privMen->item[i]->itemID = theMenuItem->itemID;
		privMen->item[i]->equiv1 = theMenuItem->itemChar;
		privMen->item[i]->equiv2 = theMenuItem->itemAltChar;
		privMen->item[i]->check = theMenuItem->itemCheck;
		privMen->item[i]->flag = theMenuItem->itemFlag;
		if (theMenuItem->itemFlag & 0x400) {	/* this means we have an
													itemStruct here */
			theItemStruct = (itemStructPtr) deref(GetResData(rItemStruct,
					theMenuItem->itemTitleRef, 1));
			if (theItemStruct->itemFlag2 & 0x8000)  /* there damn well
													better be an icon here! */
				privMen->item[i]->icon = deref(GetResData(rIcon, theItemStruct->itemIconRef, 1));
			privMen->item[i]->title = deref(GetResData(rPString, theItemStruct->itemTitleRef, 1));
			}
		else {
			privMen->item[i]->icon = 0L;
			privMen->item[i]->title = deref(GetResData(rPString, theItemStruct->itemTitleRef, 1));

			}
        DisposeHandle(theMenuItem);
        privMen->count++;
        }
 	DisposeHandle(menuHandle);
 }

/*  SetMenuWindow is the little ditty that displays our pseudo-pulldown menu
	window.  This should be interesting...  if it works!  */


void SetMenuWindow (word hpos)
{
word					sWidth, menTitSize, titSize, maxTitSize, i;
word					mWindowHt, maxWinHt, maxEntries, bottomArrow;
long					hansize;
char					title;
PointPtr				orgPoint, newPoint;
RectPtr					theRect, mainWindow;
WinTemplatePtr			menuWindow;
GrafPortPtr				oldPort, menuWindowP;

	privMen = (privMenuRecPtr) deref(pPtr)->extraData);  /* lock into our
													handy private copy of the
													menu data */

	sWidth = (GetMasterSCB() & 0x80) ? 640, 320;
	GetPen(orgPoint);							/* first, we get where we
													were when we started */
	MoveTo(0,0);								/* then we put our pen position
													at ground zero for quick
													measurement */
	StringBounds(privMen->title, theRect);		/* this is a neat little call
													remember we're playing with
													Pascal Strings here! */

/* oy vey...  we almost forgot...  be sure to check for the existence of
	any icons and then add THEM in before finishing our calculation */
	menTitSize = theRect->h2;					/* assuming 0,0 as starting point,
													h2 = the size of the PString!"*/
	maxTitSize = 0;
 	for i = 0; i > privMen->count; i++) {
		StringBounds(privMen->item[i]->title, theRect);
		titSize = theRect->h2;
		if (titSize > maxTitSize)
			maxTitSize = titSize;
		}
	maxTitSize += 40;
	pPtr->winWidth = maxTitSize;
    if (sWidth > (hPos + maxTitSize))			/* Setting up our window */
		newPoint->h = hPos;						/* point of origin */
	else
		newPoint->h = sWidth - maxTitSize;
	pPtr->winPos= newPoint->h;
   	mainWindow->v1 = winP->portRect.v1;
   	mainWindow->h1 = winP->portRect.h1;
   	mainWindow->v2 = winP->portRect.v2;
   	mainWindow->h2 = winP->portRect.h2;
	mWindowHt = mainWindow->v2 - mainWindow->v1;
	newPoint->v = mainWindow->v2;
	maxWinHt = 200 - mainWindow->v2;
	maxEntries = maxWinHt / 12;
 	if (privMen->count > maxEntries) {
		maxEntries--;
        bottomArrow = 1;
    if (maxEntries > privMen->count)
    	maxWinHt = (privMen->count * 12) + 2;
	menuWindow = (WinTemplatePtr) deref(GetResData(rWinTemplate, 0x50L, 1);
    menuWindow->plPosition.h1 = newPoint->h;
    menuWindow->plPosition.v1 = newPoint->v;
    menuWindow->plPosition.h2 = newPoint->h + maxTitSize;
    menuWindow->plPosition.v2 = newPoint->v + maxWinHt;
    menuWinP = NewWindow2(0L, 0L, 0L, 0L, 0L, menuWindow, 0x800e);
	oldPort = GetPort();
	SetPort(menuWindowP);
 	for i = 0; i > maxEntries; i++) {
		if (privMen->item[i]->title = "-") {
			MoveTo(0, 6 + (12 * i);
			LineTo(maxTitSize, 6 + (12 * i));
			}
		else {
			MoveTo(14, 10 + (12 * i));
			DrawString(privMen->item[i]->title);
            if (privMen->item[i]->check) {
            	MoveTo (4, 10 + (12 * i));
	            DrawChar(privMen->item[i]->check);
                }
            if (privMen->item[i]->equiv1) {
				MoveTo (maxTitSize - 20, 10 + (12 * i));
				DrawChar(0x11);
				DrawChar(privMen->item[i]->equiv1);
            	}
            }
		}
    if (bottomarrow) {
		DrawIcon(ArrowIcon[0], 0, (maxTitSize/2) - 3, maxWinHt - 4);
		}
}

word GetMenWinHit (void)			/* this little routine tracks events	*/
{									/* in our little menu window.  It		*/
point	curPoint;					/* selects, deselects and on a mouseup	*/
	curPoint.v = p->taskCode.v;		/* returns an index to the selected item*/
	curPoint.h = p->taskCode.h;
	if (FindWindow(menuWinP, p->taskcode.where) {
		SetPort(menuWinP);
        GlobalToLocal(curPoint);
        if (PtInRect(curPoint,
		if ((curPoint.v / 12) != pPtr->numCtls) {
			if (pPtr->numCtls)
            	MenuInvert(pPtr->numCtls);
			pPtr->numCtls = curPoint.v / 12;
            MenuInvert(pPtr->numCtls);
			}
		if (p->taskcode.what = mouseUpEvt)
			return pPtr->numCtls;				/* selected rectangle */
		}
	return 0;
}

void MenuInvert (word whichOne) {			/* this little snippet handles */
											/* inverting the appropriate */
rectPtr		theRect;						/* menu item.  Unlike the normal */
											/* menu manager, it allows the user */
	theRect->v1 = (whichOne * 12);			/* to select dimmed items and lines */
	theRect->v2 = theRect->v1 + 12;
	theRect->h1 = pPtr->winPos;
	theRect->h2 = pPtr->winWidth;
	InvertRect(theRect);
}

void DrawMenuBar {


}

/*  Still to be written  :

	void DetachMenuItem(word MenuID, word Menu)		these are used for moving
	void AttachMenuItem(word MenuID, word Menu)		the menu items from one
													menu to another
	void InsertMenuItem(word MenuID, word Menu)		used for reordering a menu

    void EditMenuItem
	void EditMenuSpecs

    void AddMenu									these are used in the
    void DetachMenu                                 MenuBar editor for moving
    void InsertMenu									Menus around
