#include <jaz.h>
#include <keys.h>
#include <jzscreen.h>
extern TWINDOW g_window;

jzmenu(flist , ffore , fback , fnum , fkeys)
char **flist;
int ffore,fback,fnum;
char *fkeys;
{
  TWINDOW *wptr;
  int w,wcol,wdefault;
  int wchr,wscan,wlen,wwidth;
  int wstarty,wstartx;
  int row,col,start,end;

  if ( ! fnum ) return(-1);	/* nothing to list */

  jzgetcur(&row,&col,&start,&end);

  jzsetcur(0x20,0x20);

  wwidth = fnum + 2;
  wlen = 0;
  for (w = 0 ; w < fnum ; w ++) wlen = max(wlen,strlen(flist[w]));

  wlen += 4;	/* space on both sides and allow for box */

  wstartx = (80 - wlen) >> 1;
  wstarty = (24 - wwidth) >> 1;

  wptr = jzopnwnd(0xff,wstarty,wstartx,wlen,wwidth,ffore,fback);

  for (w = 0 ; w < fnum ; w ++)
    jzscrprn(flist[w],wstarty+w+1,wstartx+2,
	     (fback*16|ffore) & 0x7f);

  wdefault = 0;
  do {
    jzscrprn(flist[wdefault],wstarty+wdefault+1,wstartx+2,
	    (ffore*16|fback) & 0x7f);

    wchr = jzinkey(&wscan);

    jzscrprn(flist[wdefault],wstarty+wdefault+1,wstartx+2,
	     (fback*16|ffore) & 0x7f);

    if (strchr(fkeys,wscan)) {
      wdefault = -1;
      wscan = ENTER;
    }

    switch(wscan) {
      case UARR :
	wdefault = --wdefault < 0 ? fnum - 1 : wdefault;
	break;
      case DARR :
	wdefault = ++wdefault == fnum ? 0 : wdefault;
	break;
    }
  } while(wscan != ENTER);

  jzrstwnd(wptr);
  jzdelete(0xff);

  jzsetcur(start,end);

  return(wdefault);

}
