
 /****************************************************************************
 **									    **
 **		-- TEdi --    (c) 21.4.88 by TAA			    **
 **									    **
 display*********************************************************************/


 /******  Globals  */

extern struct System System;
extern struct File *Wfile;
extern struct Line *Wline;
extern struct Window *w;
extern UBYTE Wpos,Wlen;
extern int Wy,Wx;
extern char *Wbuf,*bmp;

char Size[3][18] = { {
		0x9b,'1','3',0x74,
		0x9b,'7','9',0x75,
		0x9b,'0','0',0x78,
		0x9b,'0','0',0x79,
		0x00 },{
		0x9b,'1','3',0x74,
		0x9b,'7','9',0x75,
		0x9b,'0','0',0x78,
		0x9b,'1','1','2',0x79,
		0x00 },{
		0x9b,'2','7',0x74,
		0x9b,'7','9',0x75,
		0x9b,'0','0',0x78,
		0x9b,'0','0',0x79,
		0x00 } };

char Lstr[] = {"                                                                                 "};
char DOWN[] = { 0x9b,0x54,0x00 },UP[] = { 0x9b,0x53,0x00 };
char DELINE[] = { 0x9b,0x4d,0x00 },INSLINE[] = { 0x9b,0x4c,0x00 };
ULONG m0,m1,m2;
int p2flag;


 /******  SubRoutines  */

DisplaySize()
{
if((System.AnzFiles == 1)||(System.Mode == 1)) print(Size[2]);
else					       print(Size[System.Active]);
}


PrtPage(line)
struct Line *line;
{
int zeilen,anf=0,i;

prtmd(1,0,JAM2);
zeilen = (System.Mode == 1)?27:13;
if (zeilen == 13) anf = System.Active*14;
for(i=anf;i<(zeilen+anf);i++)
	{
	if (line != 0)
		{
		prt(0,i,line->string,line->len);
		line = line->next;
		}
	else
		{
		prt(0,i,Lstr,1);
		}
	}
}


InfoPrt(index)
int index;
{
int y;
struct File *f;

y = (index == 1)?27:((System.Mode == 1)?27:13);
prtmd(2,1,JAM2);
f = &(System.Root[index]);
cpystr(" File: ",7,bmp);
cpystr(Lstr,36,bmp+7);
cpystr(f->Fname,lenstr(f->Fname),bmp+7);
cpystr(" Line:     ",11,bmp+43);
cpystr("   Mode: ",9,bmp+54);
if (f->Mode == 0) cpystr("Insert",6,bmp+63);
else cpystr("OverWr",6,bmp+63);
if (index == 0) cpystr("   1      ",10,bmp+69);
else cpystr("   2      ",10,bmp+69);
Move(w->RPort,0,y*8+6);
Text(w->RPort,bmp,79);
prtdez(50,y,4,f->ActLine);
}


PrtLine(pos,y,str,len,Mode)
int pos,y,len,Mode;
char *str;
{
int i;
UWORD k=0,j=0,l,klast,yn;

yn = trans(y);
if (Mode == 1)
	{
	for(i=0;i<pos;i++) k = (*(str+i) == 9)?(k & 0xfff8)+8:k+1;
	Wx = k;
	}
else
	{
	for(i=0;(i<len-1)&&(k<Wx);i++)
		{
		klast = k;
		k     = (*(str+i) == 9)?(k & 0xfff8)+8:k+1;
		}
	if (k > Wx)
		{
		i--;
		k = klast;
		}
	Wx   = k;
	Wpos = i;
	}
if (k>78)
	{
	l = ((k-8)/71)*71;
	for(i=0;j<l;i++) j = (*(str+i) == 9)?(j & 0xfff8)+8:j+1;
	p2flag = 1;
	}
else
	{
	i = 0;
	p2flag = 0;
	}
cursor(0);
prtmd(1,0,JAM2);
prt(0,yn,str+i,len-i);
setcur(k-j+1,y+1);
cursor(1);
}


CSICheck()
{
char a,w=0;

a = get();
switch(a)
	{
	case 63: w = 9; break;
	case 65: w = 1; break;
	case 66: w = 2; break;
	case 68: w = 3; break;
	case 67: w = 4; break;
	case 84: w = 5; break;
	case 83: w = 6; break;
	case 32: a = get();
		  if (a == 65) w = 7;
		  else w = 8;
	}
while(get() != 0);
return(w);
}


cpystr(st1,len,st2)
ULONG len;
char *st1,*st2;
{
if (len == 0) return(0);
m0 = st1;
m1 = st2;
m2 = len;

mcpy();
}

#asm
_mcpy	
	move.l _m0,a0
	move.l _m1,a1
	move.l _m2,d0
lab1	move.b (a0)+,(a1)+
	subq.l #1,d0
	bne    lab1
	rts

#endasm


int trans(y)
int y;
{
return(y+ ((System.Mode == 1)?0:System.Active*14) );
}


int size()
{
return(13+System.Mode*14);
}


ModLine()
{
FreeMem(Wline->string,Wline->len);
Wline->string = AllocMem(Wlen,MEMF_PUBLIC);
cpystr(Wbuf,Wlen,Wline->string);
Wline->len = Wlen;
}


AutoScroll(direct)
int direct;
{
if (direct == 0)			/* prev */
	{
	if (Wline->prev == 0) return(1);
	if (p2flag == 1)
		{
		cursor(0);
		prtmd(1,0,JAM2);
		prt(0,trans(Wy),Wbuf,Wlen);
		}
	ModLine();
	if(Wy == 0) print(DOWN);
	else Wy--;
	Wline = Wline->prev;
	Wlen  = Wline->len;
	cpystr(Wline->string,Wlen,Wbuf);
	Wfile->ActLine--;
	ActPrt();
	PrtLine(Wpos,Wy,Wbuf,Wlen,0);
	}
else
	{
	if (Wline->next == 0) return(1);
	if (p2flag == 1)
		{
		cursor(0);
		prtmd(1,0,JAM2);
		prt(0,trans(Wy),Wbuf,Wlen);
		}
	ModLine();
	if(Wy == (size()-1)) print(UP);
	else Wy++;
	Wline = Wline->next;
	Wlen  = Wline->len;
	cpystr(Wline->string,Wlen,Wbuf);
	Wfile->ActLine++;
	ActPrt();
	PrtLine(Wpos,Wy,Wbuf,Wlen,0);
	}
return(0);
}


ActPrt()
{
int y;

y = (System.Active == 1)?27:((System.Mode == 1)?27:13);
prtmd(2,1,JAM2);
prtdez(50,y,4,Wfile->ActLine);
}


killine(line)
struct Line *line;
{
FreeMem(line->string,line->len);
FreeMem(line,sizeof(struct Line));
}


Shift(direct,y)
int direct,y;
{
struct Line *Help;

if(direct == 0)
	{
	Help = search(Wline,size()-y);
	cursor(0);
	setcur(0,y+1);
	print(DELINE);
	prtmd(1,0,JAM2);
	if (Help != 0) prt(0,trans(size()-1),Help->string,Help->len);
	}
else
	{
	cursor(0);
	setcur(0,y+1);
	print(INSLINE);
	}
}


int search(line,value)
struct Line *line;
int value;
{
int i,x;
struct Line *Help;

Help = line;
if (value == 0) return(Help);
x    = (value < 0)?-1:1;
for(i=0;(i != value) && (Help != 0);i += x) Help = (x == 1)?Help->next:Help->prev;
return(Help);
}


 /******  EndofTextFile */

