/*
 ----------------------------------------------------------------
 |    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 20. LAST CHANGES: 01.12.1993.	|##
 ----------------------------------------------------------------##
  #################################################################

			LIST OF FUNCTIONS:
	menu_bar
	mb_item
*/

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

int menu_bar(wn)
WINDOW *wn;
{
	char		foregr,symbol;
	int		choice,pred,count,bls,tsize,leave;
	register int	i;
	
	count=strlen(wn->hot_k);
	if (wn->t_lgt < (count << 1))
	{
		werrno=WENOTP;
		return(NO_CHOICE);
	}

	for (i=0,tsize=0; i < count ; i++)
	{
		tsize=max( tsize , strlen(wn->wtxt[i]) );
/*
		tsize=(tsize>strlen(wn->wtxt[i])) ? tsize: strlen(wn->wtxt[i]);
*/
	}
	bls=tsize*count;
	if (bls < wn->t_lgt - count) bls=tsize+(wn->t_lgt - bls)/count;
	else
	{
		tsize=(wn->t_lgt - count)/count;
		bls=tsize+1;
	}
	for ( i=0 ; i < count ; i++)
		wrt_video(wn->t_xs+i*bls,wn->t_ys,wn->wtxt[i],wn->bgrnd,tsize);

	/* Make your choice */

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

	FOREVER
	{
	 wrt_video(wn->t_xs+pred*bls,wn->t_ys,wn->wtxt[pred],wn->bgrnd,tsize);
	 wrt_video(wn->t_xs+choice*bls,wn->t_ys,wn->wtxt[choice],foregr,tsize);
	 pred=choice;
	 if ( wn->helpfun ) (*(wn->helpfun))(choice);
	 do
	 {
	    while ( !(symbol=toupper(ki())));
	    switch (symbol)
	    {
		case LEFT	: { choice--; break; }
		case RIGHT	: { choice++; break; }
		case HOME	: { choice=0; break; }
		case STR	: { choice=count-1; break; }
		case ENTER	:
		case DOWN	: { leave=TRUE; break; }
		case UP		: { choice=PREV_MENU; leave=TRUE; break; }
		case ESC	: { choice=NO_CHOICE; leave=TRUE; break; }
		default		:
		{
		  for (i=0 ; i < count ; i++)
		    if ( wn->hot_k[i] == symbol )
		    {
			wrt_video(wn->t_xs+pred*bls,wn->t_ys,wn->wtxt[pred],
							wn->bgrnd,tsize);
			wrt_video(wn->t_xs+i*bls,wn->t_ys,wn->wtxt[i],
							foregr,tsize);
			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-1;
	}
}


/* Fixup Previous Menu Bar Item. */

int	mb_item(code,wn)
int	code;
WINDOW	*wn;
{
	int	count;		/* Last item number */
	int	pred,tsize,bls,i;
	char	foregr;

	pred=wn->item;
	switch (code)
	{
		case	PREV_MENU:	{ wn->item--   ; break; }
		case	POST_MENU:	{ wn->item++   ; break; }
		default	:		{ return(wn->item); }
	}
	count=strlen(wn->hot_k);

	if ( wn->item >= count ) wn->item=0;
	else if ( wn->item < 0 ) wn->item=count-1;

	for (i=0,tsize=0; i < count ; i++)
	{
		tsize=max( tsize , strlen(wn->wtxt[i]) );
	}
	bls=tsize*count;
	if (bls < wn->t_lgt - count) bls=tsize+(wn->t_lgt - bls)/count;
	else
	{
		tsize=(wn->t_lgt - count)/count;
		bls=tsize+1;
	}
	foregr = (wn->bgrnd == REV_VID) ? NORM_VID : REV_VID;
	wrt_video(wn->t_xs+pred*bls,wn->t_ys,
				wn->wtxt[pred],wn->bgrnd,tsize);
	wrt_video(wn->t_xs+wn->item*bls,wn->t_ys,
				wn->wtxt[wn->item],foregr,tsize);
	return(wn->item);
}
