/*	OXEGE8.C
**
**	Menu handler for OXEGE
*/


#include "a:stdio.h"
#include "b:oxege.h"

#define KEYUP	11
#define KEYDOWN	10
#define KEYLEFT 8
#define KEYRIGHT 12

char *items;

minit()
	{
	items = malloc( 256 );
	}

setmenu( itemnumber, length, title )
	int itemnumber, length;
	char title[];
	{
	strcpy( items+( (itemnumber-1)*length ), title );
	}


menu( line, column, across, down, length, draw )
	int line, column, across, down, length, draw;
	{
	int lendown, lenacross, item, count, command, newitem;

	if ( draw == 0)
		{
		goto drawdone;
		}

	lendown = 0;
	item = 0;
	lenacross = 0;
	count = 0;
	while ( lendown < down )
		{
		while ( lenacross < across )
			{
			while ( count < length )
				{
				cpoke( line+lendown, 
					column+(length*lenacross)+count,
					' ' | ATTINV, ATTDIM | ATTUND );
				count++;
				}
			count = 0;
			spoke( line+lendown, column+(length*lenacross),
				items+(length*item), ATTDIM | ATTUND );
			item++;
			lenacross++;
			}
		lenacross = 0;
		lendown++;
		}

	drawdone:
	item = 0;
	defwindow( 9, line, column, line, column+length-1 );
	bkgnd( '=' );
	fflush( stdin );
	while( 1 )
		{
		while( cis() != CREADY ) 
			{ 
			tasks(); 
			}
		command = ci();
		switch( command )
			{
			case CR:
				bkgnd( 0 );
				return item+1;
				break;
			case KEYUP:
			case '8':
			case 5:
				newitem = item-across;
				if (newitem >= 0 )
					{ 
					item = newitem;
					}
				break;
			case KEYDOWN:
			case '2':
			case 24:
				newitem = item+across;
				if (newitem < down*across) 
					{
					item = newitem;
					}
				break;
			case KEYLEFT:
			case '4':
			case 19:
				newitem = item-1;
				if (newitem >= 0 ) 
					{
					item = newitem;
					}
				break;
			case KEYRIGHT:
			case '6':
			case 4:
				newitem = item+1;
				if( newitem < down*across) 
					{
					item = newitem;
					}
				break;
			}
		bkgnd( 0 );
		defwindow( 9, 
			line   + (item / across), 
			column + ((item / down )*length),
		   	line   + (item / across), 
			column + ((item / down )*length)+length-1 );
		bkgnd( '=' );
		}
	}

own )*length),
		   	line   + (item / across), 
			column + ((item / down )*length)+length-1 );
		bkgnd( '=' );
		}
	}

