/* -	-	SCREEN	-	-	*/
#ifndef BRN
#define BRN 0
ulong YLO = 2;
ulong BLK = 1; 
#define GRN 3
#endif
char GetKey() /* keys convert to WS control codes. del = ^G */
{
	char C = 0; int class, len;
	do {
		if((txmsg = (struct IntuiMessage *)GetMsg(txwin->UserPort)) == 0L)
		{
			Wait(1 << txwin->UserPort->mp_SigBit); continue; /* re-do */
		}
		class = txmsg->Class;
		if(class == RAWKEY)
		{ /* convert arrow keys et al to char code */
			if(txmsg->Code & IECODE_UP_PREFIX) {;}
			else
			{
				buff[0] = buff[1] = buff[2] = 0;
				ievent.ie_Code = txmsg->Code;
				ievent.ie_Qualifier = txmsg->Qualifier;
				ievent.ie_position.ie_addr = * (APTR *)txmsg->IAddress;
				len = (RawKeyConvert(&ievent,buff,10,NULL));
				if(len > 0)
				{
					if(len == 1) { C = buff[0]; if(C == 0x7f) C = 7; }
					else										/*DEL .. ^code*/
					{
						C = buff[1];
						if(buff[2] == '~') /* help & function keys */
						{
							func = '?';
							if((C >= '0') && (C <= '9')) func = C + 1;
							C = 31;
						}
						switch(C) /* arrow keys */
						{
						case 'A': C = 5;	break; /*^E ^*/
						case 'B': C = 24; break; /*^X v*/
						case 'C': C = 4;	break; /*^D >*/
						case 'D': C = 19; break; /*^S <*/
						case 'T': C = 18; break; /*^R ^^*/
						case 'S': C = 3;	break; /*^C vv*/
						case ' ':
						{
							if(buff[2] == 'A') C = 1; /*^A <<*/
							if(buff[2] == '@') C = 6; /*^F >>*/ break;
						}
						}
					}
				}
			}
		}
		ReplyMsg((struct Message *)txmsg);
	} while(C == 0);
	return(C);
}

SetAttr(S) /* Normal = 0 Uline = 1/2 Bold = 3/4 italic = 5/6 */
int S;
{
	ulong en;
	switch(S)
	{
	case 0: Tstyle = 0; break;
	case 1: Tstyle = Tstyle | 1; break;
	case 2: Tstyle = Tstyle & 6; break;
	case 3: Tstyle = Tstyle | 2; break;
	case 4: Tstyle = Tstyle & 5; break;
	case 5: Tstyle = Tstyle | 4; break;
	case 6: Tstyle = Tstyle & 3; break;
	}
	en = AskSoftStyle(txrp); SetSoftStyle(txrp,Tstyle,en);
}

SetCols() { SetAPen(txrp,COLOR); SetBPen(txrp,BCOLOR); }
YR() { COLOR = YLO; BCOLOR = BRN; SetCols(); SetAttr(0); }
UYR(){ COLOR = YLO; BCOLOR = BRN; SetCols(); Tstyle = 0; SetAttr(1); }
YK() { COLOR = YLO; BCOLOR = BLK; SetCols(); SetAttr(0); }
KY() { COLOR = BLK; BCOLOR = YLO; SetCols(); SetAttr(0); }
KR() { COLOR = BLK; BCOLOR = BRN; SetCols(); SetAttr(0); }
UKR(){ COLOR = BLK; BCOLOR = BRN; SetCols(); Tstyle = 0; SetAttr(1); }
KG() { COLOR = BLK; BCOLOR = GRN; SetCols(); SetAttr(0); }
GK() { COLOR = GRN; BCOLOR = BLK; SetCols(); SetAttr(0); }

L1() { GX = 4; GY = 18; }
Nl() { GX = 4; GY += 8; }

int SetStLen(istr) char *istr;
{
	char C, *str; int E = 0, L = -1;
	str = istr;
	do { C = *(str++); ++L; if((C == 0) || (C == 10) || (L >= MAXSTR)) E = 1; } while(E == 0);
	*(--str) = 0;
	if(L >= MAXSTR) {
		do { C = *(--str); --L; if((C > 32) || (L < 0)) E = 2; } while(E == 1);
		*(++str) = 0;
	}
	return(L);
}

WriteStr(str)
char *str;
{
	ulong L;
	Move(txrp,(long)GX,(long)GY); L = (ulong)SetStLen(str); Text(txrp,str,L); GX +=(L << 3);
}

WriteStN(str,L)
char *str;
int L;
{
	Move(txrp,(long)GX,(long)GY); Text(txrp,str,(ulong)L); GX +=(L << 3);
}

WriteCh(C) /* due to the extra Moves it's faster to compose a str */
char C;
{
	Move(txrp,(long)GX,(long)GY); Nstr[0] = C; Text(txrp,Nstr,(ulong)1); GX += 8;
}

WriteCN(C,N)
char C;
int N;
{
	int L; char *str;
	str = Nstr;
	for(L = 0; L < N; L++) *str++ = C; WriteStN(Nstr,N);
}

WriteLong(W)
long W;
{
	int M = 79, S = 0; char *str;
	str = Nstr+M; *(str--) = 0;
	if(W < 0) { W = -W; S = 1; }
	do { *(str--) = ((W % 10) + 48); W /= 10; --M; } while((W > 0) && (M > 1));
	if(S == 1) *(str--) = '-';
	WriteStr(++str);
}
WriteInt(N) int N; {	 WriteLong((long)N); }

/*origin 1,1*/
GotoY(L) int L; {	 GX = 4; GY = (L << 3) + 10; }
GotoXY(N,L) int N,L; {	GX = (N << 3) - 4; GY = (L << 3) + 10; }
 /*Bs() { GX -= 8; if(GX < 4) GX = 4; }*/
 /*BsN(L) int L; { GX -=(L * 8); if(GX < 4) GX = 4; }*/
Cel() { int N; N = (PW - (GX >> 3)); WriteCN(' ',N); GX -= (N << 3); }
Cll(L) int L; {  GotoY(L); WriteCN(' ',PW); GX = 4; }
Cls()
{
	int L;
	YR(); Tstyle = 0; SetAttr(3); SetAttr(5); /*Bold ital wide cls*/
	for(L = TOP; L <= BOT; L++) Cll(L);
	SetAttr(0);
}

char UPcase(C) char C; { if((C < 97) || (C > 122)) {;} else C -= 32; return(C); }

long StoL(str)
char *str;
{
	char C; int L, M = 0; long N = 0;
	L = SetStLen(str);
	if((L > 0) && (*str != 10)) {
		do {
			C = *str++ - 48; M++;
			if((C >= 0) && (C <= 9)) N = (N * 10) + C; else N = 0;
		} while((M < L) && (N != 0));
	}
	return(N);
}

StJoin(str,str2) /*str(LLen) + str2*/
char *str; char *str2;
{
	char C; int E = 0, L, P = 0;
	L = LLen; str += L; L--;
	do { C = *(str2++); *str++ = C; ++L; if((C == 0) || (L >= MaxStLen)) E = 1; } while(E == 0);
	*str = 0; LLen = L;
}

StCat(str,str2) /*str + str2 sets LLen*/
char *str; char *str2;
{	LLen = SetStLen(str); StJoin(str,str2); }

StCopy(str,str2) /*str << str2 sets LLen */
char *str; char *str2;
{	LLen = 0; StJoin(str,str2); }

/*char *AllocStr(N)
int N;
{
	ulong S;
	S = ((N / 4) + 2); Gp1 = (char *) calloc(S,(ulong)4);
	if(Gp1 == 0) { Gp1 = MT; AF = 0; MemErr = 1; }
	return(Gp1);
}
*/
/* -	-	SCREEN	-	-	*/
