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

/* this program generates a menu and then calls the 
     operation associated with chosen item */

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

#define		WAITCOUNT	500	/* wait before menu display */


main (argc,argv)   
	int	argc;
	char	*argv[];
{	char	secCol,secRow;  /* starting place off menu */
	char	i,result;
	char	holdact,oldact,olddef;		/* previously displayed active element */
	
	dealer = FALSE;
	if (argc > 1)
		if (!strcmp (argv [1],"MNGR")) dealer = TRUE;
	keyinit();
	biginit();
	if (!isFox & isHoriz)
		{ st.isSmall = FALSE;
		  STsize();
		}

	putz (funkseq);
	mainDisp();

	while (1)
		{ oldact = mainmen.MENactChoice; /* for undoing inverse */
		  result = menControl (mainmen);
		  if (oldact != mainmen.MENactChoice)
			{
			  if (findplace ((oldact+1),havemenus))
				menclear (mens [oldact]);
			  if (findplace ((mainmen.MENactChoice + 1),havemenus))
			  	mendisp (mens [(mainmen.MENactChoice)]);
			}
 		  if (result == CR)   /* call sub routine */
			{
			mainmen.MENdefChoice [0] =  mainmen.MENactChoice;
			holdact =  mainmen.MENactChoice;
			mainmen.MENactChoice = 0xFF; /* so won't inverse it */
			menItemDisp (mainmen,holdact); /* take out inverse */		

			if (holdact == Obright) gtBright ();
			else if (holdact == OscrType) GtVideo ();
			else if (holdact == OcursType) Gtcurse ();
			else if (holdact == OscrSize) Gtsize ();
			else if (holdact == OemulType) Gtemulation ();
			else if (holdact == OfuncKeys) defKeys ();
			else if (holdact == Olang) GTlang ();
			else if (holdact == Okeys) Gtkeys ();
			else if (holdact == Oread) readdf();
			else if (holdact == Osave) savedf  ();
			else if (holdact == OcreLang) makeLang ();
			else if (holdact == OcreKey) makeKeyb ();
			else if (holdact == OsignOff) signoff ();

			mainmen.MENactChoice = holdact;
			mainmen.MENdefChoice [0] =  0xFF;  /* no def setting after action */
			menItemDisp (mainmen,mainmen.MENactChoice); /* take out inverse */
			
			curpos (HEADR3,1);
			putz (clrtoeolseq);
			curpos (HEADR3,HEADC3A);
			putz (head3aptr);
			}  /* CR */
		}  /* main menu control loop */
	

	putz (defunkseq);


		

}	/* end of MAIN */

/****   END OF MAIN LOOP  ****/

/****  START OF MAJOR ROUTINES ***/

mainDisp()
{
	clearsc();
	headPrompt ();
	menDisp (mainmen);
	menFrame ();
	if (findplace ((mainmen.MENactChoice + 1),havemenus))
	  	mendisp (mens [(mainmen.MENactChoice)]);
	allDefdisp ();

}  /* end of main disp */

headPrompt ()
{	curpos (HEADR1,HEADC1);
	putz (head1ptr);
	putz (vString);
	curpos (HEADR2,HEADC2);
	putz (head2ptr);
	curpos (HEADR3,HEADC3A);
	putz (head3aptr);
	curpos (mainmen.MENheadRow,(mainmen.MENheadCol + DEFOFF));
	putz (ms93ptr);
	if (dealer)
		{ curpos (0,3);
		 putz (ms99ptr);
		}
} 


/* display all defaullts */

allDefdisp()
{	int i;
	for (i = 0; i < mainmen.MENnumChoice; i++)
		defItem (i);

}  /* end of disp def */



/* display a single default */
defItem (item)
	char	item;

{	char	*defptr,i;
	char	defstr [20],hold [4];

	defptr = defblkptr;
	if (item == Obright)
		{ if (mens [item].MENdefChoice [0] == '\0')
			{ strcpy (defstr,ms3ptr);
			  numtoasc (st.SCbright,hold);
			  strcat (defstr,hold);
			  defptr = defstr;
			} /* if default is 0 */
		}
	else if (item == OscrType)
		defptr = standDef (item);
	else if (item == OemulType)
		{
		if (mens [item].MENdefChoice [0] != 3)
			defptr = standDef (item);
		}
	else if (item == OscrSize)
		{ if (mens [item].MENdefChoice [0] == 2) /* extra line choice*/
			defptr = ms5ptr;  /* short version Extra (26 x 80) */
		else defptr = standDef (item);
		}
	else if (item == OcursType)
		defptr = standDef (item);
	else if (item == Olang)
		defptr = standDef (item);
	else if (item == Osave)
		defptr = DFfilesub (defstr,saveName);
	else if (item == Oread)
		defptr =DFfilesub (defstr,readName);
	else if (item == OcreLang)
		defptr = DFfilesub (defstr,UfileName);
	else if (item == OcreKey)
		defptr = DFfilesub (defstr,UkeybName);
	else if (item == Okeys)
		defptr = standDef (item);
	else if (item == OfuncKeys)
		{ if (FK.numfuncs != 0) 
			{ strcpy (defstr,ms84ptr);
			  numtoasc (FK.numfuncs,hold);
			  strcat (defstr,hold);
			  defptr = defstr;
			}
		}
	if (defptr == noneptr) defptr = defblkptr;
	curpos (mainmen.MENchRow [item],(mainmen.MENchCol [item] + DEFOFF));
	putz (defptr);
			
}   /* def Item */


numtoasc (num,str)
	char	num,*str;
{ if (num > 99) 
	{ str [0] = '1';
	  num -= 100;
	}
 else (str [0] = ' ');
 str [1] = (num/10) + '0';
 str [2] = num - ((num/10) * 10) + '0';
 str [3] = 0; 
}

/* little subroutine */
char	*DFfilesub (nam,inam)
	char	*nam,*inam;
{	int	k;
	 strcpy (nam,defblkptr);
	  for (k = 0; k < strlen (inam); k++)
	  	nam [k] = inam [k];
	  return (nam);
}


char	*standDef (item)
	char	item;
{	char	*defptr;
	if (mens [item].MENdefChoice == 0xFF)
		defptr = defblkptr; /* display blanks */
	else defptr = mens [item].MENchoice [(mens [item].MENdefChoice[0])];
	return (defptr);
}



createchset()
	{
	printz ("\nThis is create cha set\n");
	dummy = gtch();
	}

signoff()
{ 	char result;
	if (changeMade)
		{ PA.PAptr [0] = ms7ptr;
		  PA.PAptr [1] = ms6ptr;
		  doPrompt (2,PA);
		  ptch (BELL);
		  while (1)
		 	{ result = toUpper (inpPrompt (PROMPTLINE,strlen (ms7ptr)));
			  if (result == 'Y') break;
			  if (result == 'N') 
				{ clrPrompt (2);
				  return;
				}
			  ptch (BELL);
			}  /* while 1 */
		  ptch ('Y');
		}
	/* see if we need to chain to an emulation*/
	putz (defunkseq);
	putz (g4010seq);
	emulcheck();
	curpos (23,0);
	printz ("\n\ngoodbye\n");
	exit (0);
}


	/* chain to emualtions */
emulcheck()
{ char stringer [17];
	if (isFox)
		{
		if (st.emulname[0] != ' ')
			{
			/* erase .COM  */
			dummy = exec (st.emulname);
			strcpy (stringer,"A:");
			strcat (stringer,st.emulname);
			exec (stringer);
			}
		}

}  /* end of emulcheck */
  
abort()
{	ptch (useesc); ptch ('P'); /* de-literalize function keys */
	putz (g4010seq);
	exit(0); 
}



readdf ()
{	readIt = TRUE;
	if (ovlLoad ("CUST3.OVL",OVLSR))
		(*ovlcall) ();   /* true for read */
}


savedf ()
{	readIt = FALSE;
	if (ovlLoad ("CUST3.OVL",OVLSR))
		(*ovlcall) ();   /* false for save */
}

/* DUMMY ROUTINE */

defKeys ()
{	if (ovlLoad ("CUST4.OVL",OVLFK))
		(*ovlcall)();
}


/* overlay loader */

char	ovlLoad (ovlname,ovlnum)
	char	*ovlname,ovlnum;
{	char	ovlstr [15];
	if (ovlIn == ovlnum) return (TRUE);

	doPrompt (1,&ms56ptr);
	curpos (PROMPTLINE-1,PROMCOL); /* in case system sends message */
	if (swapin (ovlname,roottop) == -1)
		{ curpos (PROMPTLINE-1,PROMCOL); /* in case */
		  putz (clrtoeolseq);
		  doPrompt (1,&ms56ptr);
		  strcpy (ovlstr,"A:");
		  strcat (ovlstr,ovlname);
		  if (swapin (ovlstr,roottop) == -1)
			{ clrPrompt (1);
 			  PA.PAptr [0] = ms8ptr;
			  ptch (BELL);
			  doPrompt (1,PA);
			  putz (ovlname);
			  return (FALSE);
			}
		}
	ovlIn = ovlnum;
	clrPrompt (1);
	return (TRUE);

}  /* end of ovl load*/



