#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graph.h>
//#include "netnow\hmistd.h"
#include "iff.h"
#include "shell.h"
#include "types.h"
#include "frontend.h"
#include "frglobs.h"
#include "frontdef.h"

#define PAPER _CYAN
typedef int BOOL;


/*-----------------03/05/95 10:11-------------------
SIMPLE MENU SYSTEM
 Up to 10 menu entries displayed, with title and instructions.
 Keys 0-9 select menu entry- entry highlighted and number returned to app.
--------------------------------------------------*/

/*-----------------01/08/95 15:04-------------------
 I'm about to attempt converting it to use the pcshell. Tch.
--------------------------------------------------*/
/*-----------------01/08/95 16:22-------------------
 nah.
--------------------------------------------------*/


/****************************************************************************
													       GLOBALS
 ***************************************************************************/

char menu[10][40];
char title[30];
char instruct[30];
char oldMensel=0;
char swazzBlazz=0; /*Ooh fuck*/
char oMess[40]; /*Current message*/
char blanx[39]="                                       ";
short mensel;
short menu_h,menu_y;
#define 	MENU_WIDTH	200
#define	MENU_X		60




/***************************************************************************
												           	FUNCTIONS
 ***************************************************************************/



BOOL doKey (void)
{
	return (keydata->keys_held);
}



/*Checks if 'q' is pressed*/
BOOL quitKey (void)
{
	if(keydeb(K_Q))
		return (1);
	else
		return (0);
}



/*Waits for any key to be pressed*/
void anyKey (void)
{
	while (keydata->keys_held==0)
	{}
}


/*Waits until 'q' is pressed. */
void waitQuit (void)
{
	while (quitKey()!=TRUE)
	{
		wait_loop(1);
	}
}



/*Clear the screen in current background colour*/
void cls (void)
{
//	_clearscreen (_GCLEARSCREEN);
}


/*Initialize the menu system (MODE and COLOURS)*/

void openGraph (void)
{
}


/*Resets to default, on application end*/
void closeGraph (void)
{
}


/*Fills a string with blank characters (use memcpy ???)*/
void clrString (char *the_string,short siz)
{
//short i;
//
//for (i=0;i<siz;i++)
//	{
//	the_string[i]=' ';
//	}

}



/*Hellishly boring function*/

void stringInput (char *nm,short ypos,short xpos,short siz)
{
 int i;
 char inch=' ';
 int letters=0;
 int maxText=siz;
 char digit[3];


memset (nm,0,maxText);

while (inch!=13)
{

if (kbhit())
{
	inch= getch();

	/*
	itoa (inch,digit,10);
	_settextposition (ypos+2,xpos);
	_outtext (digit);
	*/

	if (inch!=13)
	{
		if (inch==8)
		{
			if (letters!=0) letters--;
			nm[letters]=' ';
		}

		else

		if (letters<maxText)
		{
			nm[letters]=inch;
			letters++;
		}

	}

}
}

}






/*Never makes a menu */

void makeMenu (char *mtitle,char *minstruct,char *m0,
							 char *m1,char *m2,char *m3,char *m4,char *m5,
							 char *m6,char *m7,char *m8,char *m9)
{
	int i,j,k;

	strcpy (title,mtitle);
	strcpy (instruct,minstruct);

	strcpy (menu[0],m0);
	strcpy (menu[1],m1);
	strcpy (menu[2],m2);
	strcpy (menu[3],m3);
	strcpy (menu[4],m4);
	strcpy (menu[5],m5);
	strcpy (menu[6],m6);
	strcpy (menu[7],m7);
	strcpy (menu[8],m8);
	strcpy (menu[9],m9);

/*
	for(i=0,j=0;i<10;i++)
	{
		k=stringlen(menu[i]);
		if(k>j)
		j=k;
	}

  menu_w=j;
 	menu_x=(SCREEN_WIDTH/2)-(menu_w/2);
*/
  mensel=0;
  oldMensel=0;
}



/* Display all menu entries */

void drawMenu (short entries)
{
	short i,j;
	char msel[20];
	cls();

 	menu_h=entries*10;
	menu_y=(SCREEN_HEIGHT/2)-(menu_h/2);
   memcpy(scrptr,backim.data,320*200);
	disp_3d_transbox(MENU_X-10,menu_y-10,MENU_WIDTH+20,menu_h+20);
 	set_col(255);
	sprint(title,(SCREEN_WIDTH/2)-(stringlen(title))/2,20);

	set_col(255);
	sprint(instruct,(SCREEN_WIDTH/2)-(strlen(instruct))/2,30);

	/************************/
	for (i=0;i<entries;i++)
	{
		if(mensel==i)
		{
			set_col(255);
		}
		else
		{
			set_col(253);
		}

		sprint (menu[i],MENU_X+20,menu_y+(i*10));

	}

 	dispexit();
	disp_cursor();
	/*
	sprint ("MENSEL ",10,30);
	sprintf (msel,"%d",mensel);
	sprint (msel,100,30);
	*/

	dump_vram();
}


void reMessPort ()
{
	messPort (oMess);
}



/*display system message in user port*/

void messPort (char *strg)
{
	strcpy (oMess,strg);

	_settextcolor(_WHITE);
	_setbkcolor (_BLACK);
	_settextposition (24,1);
	_outtext (blanx);
	_settextposition (24,1);
	_outtext(strg);
	_setbkcolor(PAPER);
}



/*Poll keyboard for MENU input, 0-9*/

short menuKey ()
{
	char inch;

	if (kbhit())
	{
 		inch =tolower (getch());

		if (inch=='q') return (-2);
 		if (inch>='0'&&inch<='9')  return (atoi (&inch));
 	}

	return (-1);
}


short menuMouseThingy(short entries)
{int i;

 if(ctrlval !=CTRL_SELECT)
	return -1;
 if(mx<MENU_X || mx>MENU_X+MENU_WIDTH)
	return -1;
 if(my<menu_y || my>menu_y+menu_h)
	return -1;
 mensel=(my-menu_y)/10;
 if(mensel==entries)
	--mensel;
}



/*If menu key pressed , highlight and return entry number*/

short  menuReq (short entries)
{

	drawMenu(entries);
	read_joypad();
	if(menuMouseThingy(entries)!=-1)
		return (mensel);
	switch(ctrlval)
		{
		 case CTRL_SELECT:
			ctrlval=0;
 			return (mensel);
  	 	 case CTRL_UP:
  			if(--mensel<0)
  			mensel=entries-1;
	 		drawMenu(entries);
  			break;
		 case CTRL_CANCEL:
 			return (-2);
	 	 case CTRL_DOWN:
  			if(++mensel==entries)
  				mensel=0;
	 		drawMenu(entries);
  			break;
	 	}
	return (-1);

}



