: How about if I want to load a file in a scrollbox so it can be,
: well, scrolled.
: I know how to load text files. And I know how to scroll on a full
: page. But I use gotoxy, so it doesn't quite work in a box, or at
: least not with the little knowledge I have. Is there a way to
: achieve this?
Here's a text viewer with some limitations to avoid being too complex ( 809 lines of text maximum, 80 char long lines at most, no wrapping, will clip at scroll box boundary, doesn't scroll lines by lines only pages ), but it's setup allows for easy expansion:
{ Compiler: BP/TP 7 }
{$A+,B-,D-,E-,F-,G+,I-,L-,N+,O-,P-,Q-,R-,S-,T-,V-,X+}
{$M 16384,0,655360}
uses crt;
const wpx:byte=5; { view window position x coord }
wpy:byte=5; { view window position y coord }
wsx:byte=60;{ view window x size }
wsy:byte=15;{ view window y size }
var fn:string; { file name }
f:text;
type buffer=array[1..809] of string[80]; { 65529 bytes to fit a 64k segment }
procedure run;
var buf:^buffer; { pointer to buffer }
ext:boolean;
curl,maxl:word; { current line, max. line }
ch:char;
procedure display;
var s:string;
sl:byte absolute s; { sl:=lenght(s); <-- auto updating }
i:word;
begin
window(wpx,wpy,pred(wpx+wsx),pred(wpy+wsy));{ define display window }
clrscr; { clear window }
window(1,1,80,25); { reset window to screen size }
gotoxy(wpx,wpy); { move cursor to start coords.}
i:=curl;
while ((wherey<wsy+wpy) and (i<=maxl)) do begin { check boundaries }
s:=buf^[i];
if sl>wsx then begin { clip line if longer then window x size }
sl:=pred(wsx);write(s);
textcolor(14);write('>');textcolor(7);end
else write(s); { display line }
gotoxy(wpx,wherey+1); { move cursor to next line }
inc(i); { increment buffer pointer }
end;
gotoxy(80,25); { move cursor away from the display window }
end;
begin
new(buf); { reserve memory for buffer }
maxl:=0;
while (not(eof(f)) and (maxl<809)) do begin { check for end of file and }
inc(maxl); { buffer fill level }
readln(f,buf^[maxl]); { read a line into buffer }
end;
ext:=false; { init exit control variable }
clrscr;
for curl:=1 to 2000 do write(#178); { fill screen w/ graph. chars }
textbackground(1);textcolor(15);
gotoxy(1,1); write(' Viewing: ');
gotoxy(12,1);write(fn);
gotoxy(32,1);write(maxl,' lines ');
gotoxy(1,25);write(' Press: PgUp/PgDn/Home/End to navigate Esc to Exit ');
textbackground(0);textcolor(7);
curl:=1;
display;
repeat { main loop }
if keypressed then begin { check for user input }
ch:=readkey; { get scancode for depressed key }
case ch of
#27:{Esc}ext:=true;
#0 :{Extended code} begin
ch:=readkey; { second scan to get the extended keyboard code }
case ch of
#71:{Home}begin curl:=1;display;end;
#81:{PgDn}if curl<=maxl-wsy then begin inc(curl,wsy);display;end
else write(#7);
#73:{PgUp}if curl=1 then write(#7) else begin
if curl>wsy then dec(curl,wsy) else curl:=1;display;end;
#79:{ End}begin
if maxl-wsy>0 then curl:=succ(maxl-wsy) else curl:=1;
display;
end;
end;
end;
end;
end;
until ext; { end main loop }
clrscr;
dispose(buf); { free memory previously reserved }
end;
{#### Main ##################################################################}
begin
if memavail>70000 then begin { check for memory availability }
write('File to view: ');readln(fn); { get user to input file to open }
assign(f,fn); { }
reset(f); { open file for read, recsize = 1 }
if ioresult<>0 then begin { check for input/output errors }
writeln(fn,' not found ! Exiting.'); { echo user if something is wrong }
halt; { exit program }
end; { }
if not(eof(f)) then run { check if its an empty file }
else writeln(fn,' is an empty file.'); { file size = 0 }
close(f); { close file }
end else writeln('Not enough memory to run application !');
end.