/*   *   *  ****  *     *  *     *  ****  *   *
     **  *  *     *     *  **   **  *     **  *
     * * *  ***   *  *  *  * * * *  ***   * * *
     *  **  *     * * * *  *  *  *  *     *  **
     *   *  ****   *   *   *     *  ****  *   *
*/

#include	"equs.c"
#include	"data.c"



mendisp (dm) 
	struct	men   *dm;
{	char	i;
	struct	ung	U;  /* unget structure */
	
	dm->MENlastDisp = 0;
	U.ungPlace = 0;
	/* start display */
	curpos (dm->MENheadRow,dm->MENheadCol);
	if (intCheck (dm,U)) goto retrout;
	putz (dm->MENhead);
	for (i = 0; i < dm->MENnumChoice; i++)
		{ dm->MENlastDisp = i;
		  menItemDisp (dm,i);
		  if (intCheck (dm,U)) goto retrout;
		}

    retrout:
	for (i = U.ungPlace; i > 0; i--)
		  ungtch (U.ungString [i-1]);
	putz (homeseq);
}   /* end of men disp */




/* move cursor block around menu, return any chars beside up 
	and down chars */

char	menControl (dm) 
	struct	men   *dm;
{	char	i,oldact,result;

	oldact = dm->MENactChoice; /* for undoing inverse */
	result = gtstch();
	while ((result == UPCUR) | (result == DOWNCUR))
		{ if (result == UPCUR)
			{ if (--(dm->MENactChoice) == 0xFF)
				dm->MENactChoice = dm->MENnumChoice - 1;
			}
		else
			{ if (++(dm->MENactChoice) == dm->MENnumChoice)
				dm->MENactChoice = 0;
			} /* downchar */
		result = gtstch();
		}  /* move active choice */
	if (oldact != dm->MENactChoice)
		{ menItemDisp (dm, oldact); /* take out inverse */		
		  menItemDisp (dm, dm->MENactChoice); /* take out inverse */
		}
	putz (homeseq);
	return (result);

}   /* end of men disp */







/* this routine provides video control for standard
   submenu., and returns the user hit char (CR or ESC) */
char	subControl (menNum)
	char	menNum;
{	char	holdact,holdef,result;

	curpos (HEADR3,HEADC3B);
	putz (head3bptr);

	mens [menNum].MENactChoice = mens [menNum].MENdefChoice [0];
	menItemDisp  (mens [menNum],mens [menNum].MENdefChoice [0]);

	while (((result = menControl (mens [menNum])) != CR) & (result != ESC))
		;  /* end of while */
	holdact = mens [menNum].MENactChoice;
	mens [menNum].MENactChoice = NOACT;
	if (result == CR)
		{   /* if chosen get level */
		holdef = mens [menNum].MENdefChoice [0];
		mens [menNum].MENdefChoice [0] = holdact;
		if (holdef != holdact) 
			menItemDisp (mens [menNum],holdef);
		}
	menItemDisp (mens [menNum],holdact);
	
	return (result);


}  /* end of subcontrol  */





menFrame ()
{  char	i,k;
	if (dealer) k = 1;  
	else k = 3;  /* for skiiped spaces */
	
	for (i = 0; i < (mainmen.MENnumChoice + k); i++)
		{ curpos ((MAINROW+i),LINECOL);
		  ptch (pipe);
		}
	curpos ((mainmen.MENheadRow - 1),2);
	repchar	(dash,76);
	curpos ((mainmen.MENchRow [OsignOff] + 1),2);
	repchar	(dash,76);

}  /* men frame */





menclear (dm) 
	struct	men   *dm;
{	char	i;
	
	/* start display */
	curpos (dm->MENheadRow,dm->MENheadCol);
	putz (clrtoeolseq);
	for (i = 0; i <= dm->MENlastDisp; i++)
		{ curpos (dm->MENchRow [i],dm->MENchCol [i]);
		  putz (clrtoeolseq);		  
		}

}   /* end of men disp */




/* interrupt menu by returning TRUE if input is in int string */
char intCheck (dm,U)
	struct	men	*dm;
	struct  ung	*U;
{	char	result;

	if (result = gtstch())
		{ U->ungString [U->ungPlace] = result;
		  U->ungPlace++;
		  if (findplace (result,dm->MENintString))
			return (TRUE);
		}
	return (FALSE);

}	/* interrupt check */






menItemDisp (dm,item)  
	char	item;
	struct	men   *dm;
{	char	inver,under;
	curpos (dm->MENchRow [item],dm->MENchCol [item]);
	if (item == dm->MENactChoice) inver = TRUE;
	else	inver = FALSE;
	if (dm->MENsingleDef)
		{ if (dm->MENdefChoice [0] == item)  under = TRUE;
		  else under = FALSE;
		}  /* only one default value */
	else
		{ under = dm-> MENdefChoice [item];
		}  /* many defaults possible */
	attrPutz (dm->MENchoice [item],under,inver);
	putz (homeseq);
}



/* clear prompt from screen */
clrPrompt (num)
	char	num;
{ 	int	i;
	for (i = (num -1); i >= 0; i--)
		{ curpos ((PROMPTLINE - i),1);
		  putz (clrtoeolseq);
		}
	putz (homeseq);
}





/* puts prompt and gets back char */
doPrompt (num,strarray)
	char	num;
	struct	promarray  *strarray;
{	dosPrompt (num,strarray,PROMPTLINE);
}


/* do prompt with selctable start line */
doSPrompt (num,strarray,line)
	char	num;
	struct	promarray  *strarray;
	int	line;
{	char	incol;
	int	i;

	for (i = (num-1); i >= 0; i--)
		{ curpos ((line - i),PROMCOL);
		  putz (clrtoeolseq);
		  putz (strarray->PAptr [i]);
		}
}	/* prompt */





char	conPrompt (stptr)
	char	*stptr;
{
	return (conSprompt (stptr,PROMPTLINE));	
}




/* con prompt with selectable line */
char	conSprompt (stptr,line)
	char	*stptr;
	int	line;
{ 	char	res2;
	while (1)
		{ res2 = toUpper (inpPrompt (line,strlen (stptr)));
		  if (res2 == 'Y')
			{ ptch ('Y');
			  return (TRUE);
			}
		  if (res2 == 'N') 
			{ ptch ('N');
			  return (FALSE);
			}
		  ptch (BELL);
		}
}   /* conPrompt */




/* put prompt and wait for a keystroke to continue */
keyprompt (ptr)
	char	*ptr;
{
	clrPrompt (3);
	PA.PAptr [0] = ms52ptr;
	PA.PAptr [1] = ptr;
	ptch (BELL);
	doPrompt (2,PA);
	dummy = inpPrompt (PROMPTLINE,strlen (ms52ptr));
	clrPrompt (2);
}  /* keyprompt */






/* actual controls inptut of prompt character */
char	inpPrompt (row,col)
	char	row,col;
{	int	i;
	char	result;
	while (1)
		{
		curpos (row,1);
		invstring (">>>");
		curpos (row,col + PROMCOL + 1);
		for (i = 0; i < 800; i++)
			if ((result = gtstch()) != '\0') goto gotResponse;
		curpos (row,1);
		putz (normseq);
		putz ("   ");
		curpos (row,col + PROMCOL + 1);
		for (i = 0; i < 350; i++)
			if ((result = gtstch()) != '\0') goto gotResponse;
		}  /* flashing loop */
   gotResponse:
	curpos (row,1);
	putz (normseq);
	putz ("   ");
	curpos (row,col + PROMCOL + 1);
	return (result);
}  /* end of inp prompt */



attrPutz (str,under,inver)
	char	*str,under,inver;
{
	if (inver | under)
		{
		if (isFox)
			{
			repchar (CTRLF,(strlen (str)+1));
			putz (normseq);
			repchar (BS,(strlen (str) + 1));
			if (inver) putz (inverseseq);
			else putz (normseq);
			if (under) ptch ('>');
			else ptch (' ');
			putz (str);
			} /* isFox */
		else 
			{
			if (inver) putz (inverseseq);
			if (under) putz (stunderseq);
			ptch (' ');
			putz (str);
			putz (normseq);
			}  /* 5000  */
		}  /* put an attribute */
	else  
		{ putz (normseq);
		  ptch (' ');
		  putz (str);
		}
}   /* attr Putz */




invString (string)
	char	*string;
{
if (isFox) 
	{ repchar (CTRLF,strlen (string));
	  putz (normseq);
	  repchar (BS,strlen (string));
	}
putz (inverseseq);
putz (string);
putz (normseq);
}  /* inv string */






curpos (r,c)
	char	r,c;
{
	ptch (ESC); ptch('Y');
	ptch (r + 0x20); ptch (c + 0x20);
}


clearsc()
{ putz (clearseq);
}
