/*
 ----------------------------------------------------------------
 |    Perlin Production Corporation, February-April 1993 (c).	|##
 |   WINDOW PACKAGE Source Code for SuperSoft C 1.2.3 compiler.	|##
 |    V e r s i o n  3.2 , part 15. LAST CHANGES: 01.12.1993.	|##
 ----------------------------------------------------------------##
  #################################################################

			LIST OF FUNCTIONS:
	menu
*/

#include "stdio.h"
#include "conio.h"
#include "wios.h"
#include "stddef.h"

/*	This function gets a responce from current menu and returns
 *	an int value, that means:
 *       1 .. count  - number of menu that was chosen
 *	 0 	     - menu must be closed, because <ESC> button was pulled.
 */


int menu(wn)
WINDOW *wn;
{
	int choice,pred,count,leave;
	char foregr,symbol,*option;
	register char i;

	count=strlen(wn->hot_k)-1;
	if (wn->t_hgt < count ) count=wn->t_hgt;

	/* Puts menu text */

	wrt_esc(wn->bgrnd);
	for ( pred=wn->t_ys,i=0 ; i<=count ; i++,pred++)
	{
		choice=wn->t_lgt;
		goto_xy(wn->t_xs,pred);
		for ( option=wn->wtxt[i] ; *option && choice-- ; )
		{
			putchar (*option++);
		}
	}

	/* Make your choice */

	leave=FALSE;
	pred=choice=wn->item;
	foregr = ( wn->bgrnd == NORM_VID ) ? REV_VID : NORM_VID;

	FOREVER
	{
	 wrt_video(wn->t_xs,wn->t_ys+pred,wn->wtxt[pred],wn->bgrnd,wn->t_lgt);
	 wrt_video(wn->t_xs,wn->t_ys+choice,wn->wtxt[choice],foregr,wn->t_lgt);
	 pred=choice;
	 if ( wn->helpfun ) (*(wn->helpfun))(choice);
	 do
	 {
	    while (!(symbol=toupper(ki()))); /* Wait here: scan faster */
	    switch (symbol)
	    {
		 case UP	: { choice--;break; }
		 case DOWN	: { choice++;break; }
		 case ENTER	: { leave=TRUE; break; }
		 case HOME	: { choice=0; break; }
		 case STR	: { choice=count; break; }
		 case ESC	: { choice=NO_CHOICE; leave=TRUE; break; }
		 case LEFT	: { choice=PREV_MENU; leave=TRUE; break; }
		 case RIGHT	: { choice=POST_MENU; leave=TRUE; break; }
		 default:
		 {
		    for (i=0 ; i<= count ; i++)
			if ( ( wn->hot_k[i] ) == symbol )
			{
				wrt_video(wn->t_xs,wn->t_ys+pred,
					wn->wtxt[pred],wn->bgrnd,wn->t_lgt);
				wrt_video(wn->t_xs,wn->t_ys+i,
					wn->wtxt[i],foregr,wn->t_lgt);
				choice=i;
				leave=TRUE;
			}
		    break;
		 }
	    }
	    if (choice > NO_CHOICE) wn->item=choice;
	    if (leave)
	    {
		if (wn->helpfun) (*(wn->helpfun))(wn->item);
		return(choice);
	    }
	 }
	 while (pred == choice);
	 if (choice > count) choice=0;
	 if (choice < 0 )    choice=count;
	}
}
