#include "tx.h" 
#include <ctype.h> 
extern int ExpandNames(char *, char *, int) ; 
extern void DrawFrame(int,int,int,int,int) ; 
 
 
int PrintTextMenu(int MenuNum) 
{ 
  int row, col , width = 0, height, i ; 
  int bottom, left = 0; 
  int previous=0, current = 0 ; 
  char *TextInWindow, *TextInLine, *ExpMenu , **mp ; 
  char *Entry[MAX_ENTRIES] ; 
  
  ExpMenu = malloc(NumEntries[MenuNum]*MAX_WIDTH);
  mp = menu[MenuNum] ; /* Position pointers */ 
  Entry[0] = ExpMenu ; 
  
  for ( i = 0 ; ( i < NumEntries[MenuNum] ) && 
       ( ( !i ) || ( (Entry[i] = Entry[i-1] + (left + 1) ) != NULL) ) ; 
       i++ ) { 
    ExpandNames(Entry[i],*mp,MAX_WIDTH); 
    width = ( ( ( left = strlen(Entry[i]) ) > width ) ? left : width ) ; 
    mp += 2 ;   /* "left" is used here as dummy variable */ 
  } 
 
  width +=  4 ;  /*  2 frame lines plus 2 spaces plus text width */ 
  height =  2 + NumEntries[MenuNum] ; /* 2 frame lines */ 
  left = ( RIGHT + 1 ) - width  ; 
  bottom = ( TOP - 1 ) + height ; 
  if ( left < 1 || bottom > 25 ) { 
    fprintf(stderr,MSG_SCRBOUNDS,Progname);
    exit(1) ; /* In case you changed menu settings but were not */ 
  }            /* careful with bounds */ 
  row = wherey() ; col = wherex() ; /* Save cursor position */ 
  _setcursortype(_NOCURSOR) ;       /* Hide cursor */ 
  TextInWindow = malloc(width * height * 2+1);
  TextInLine = malloc(161) ;
  gettext(left,TOP,RIGHT,bottom,TextInWindow);
  gettext(1,25,80,25,TextInLine);
  
  while ( previous != -1 ) { 
    previous = 0 ; 
    window(left,TOP,RIGHT,bottom) ; 
    clrscr() ;                       /* Clear text in window area */ 
    window(1,1,80,25) ;              /* But work in absolute coordinates */ 
    
    DrawFrame(left,TOP,RIGHT,bottom,1) ;       /* Draw window frame */ 
    
    /* Print menu choices */ 
    
    for ( i = 0 ; i < NumEntries[MenuNum] ; i++ ) { 
      gotoxy(left+2,TOP+1+i) ; 
      cputs(Entry[i]) ; 
    } 
    
    gotoxy(1,25) ; 
    clreol() ; 
    cputs(MSG_CHOICE) ; 
    
    while ( previous >= 0 )  { 
      while (1) { /* Highlight currently selected entry */ 
	textattr(color[(previous == current)]) ; 
	gotoxy(left+2,TOP+1+previous) ; 
	cputs(Entry[previous]) ; 
	i = ( width - 4 ) - strlen(Entry[previous]) ; 
	while ( i-- ) 
	  putch(' ') ; 
	if ( previous == current ) 
	  break ; 
	previous = current ; 
      } 
      textattr(color[0]) ; 
      i = getch();        /* Read character from keyboard */ 
      if ( i == 0 ) {     /* If extended character */ 
	i = getch() ;  /* Get character */ 
	i *= -1 ;      /* Make it different from regular ascii */ 
      } 
      switch ( i ) { 
      case UPKEY : 
      case LEFTKEY : 
	if ( current ) 
	  --current ;  /* Move up one position */ 
	break ; 
      case DOWNKEY : 
      case RIGHTKEY : 
	if ( current < (NumEntries[MenuNum]-1) ) 
	  ++current ;  /* Move down one position */ 
	break ; 
      case HOMEKEY : 
      case PGUPKEY : 
	current = 0 ;  /* Go to first entry */ 
	break ; 
      case ENDKEY : 
      case PGDNKEY : 
	current = NumEntries[MenuNum]-1 ;  /* Go to last entry */ 
	break ; 
      case CR : 
	previous = -1 ;  /* Choose current entry */ 
	break ; 
      case SPACE : 
	previous = -2 ;  /* Momentarily restore original screen */ 
	break ; 
      default : 
	if ( (!isdigit(i)) || 
	    ( (current = i - '0') >=  NumEntries[MenuNum] ) ) 
	  current = -1 ;  /* default */ 
	previous = -1 ; /* A valid digit selects that entry */ 
	break ; 
      } 
    } 
    /* Restore Screen */ 
    puttext(left,TOP,RIGHT,bottom,TextInWindow) ; 
    puttext(1,25,80,25,TextInLine) ; 
    if ( previous == -2 ) 
      getch() ;           /* wait for a keypress if ESC was pressed */ 
    
  } 
 
  free(ExpMenu) ; /* free memory */ 
  free(TextInWindow) ; 
  free(TextInLine) ; 
  
  /* Restore settings */ 
 
  _setcursortype(_NORMALCURSOR) ;      /* show cursor */ 
  gotoxy(col,row) ;                    /* restore cursor position */ 
 
  return current ; 
} 
