'===========================================================================
' Subject: VIEW BOX SUBROUTINE                Date: 06-23-97 (18:42)       
'  Author: Daniel Kalna                       Code: QB, QBasic, PDS        
'  Origin: drk@Xswbell.net                  Packet: TEXT.ABC
'===========================================================================
'  View Box v1.0:  By Daniel Kalna
'  http://www.geocities.com/SiliconValley/Lakes/4616/
'  Written with QuickBasic 4.5  Should work in Qbasic, haven't tried it.

'  Displays plain ASCII text files in a View Box window.
'  Use /AH option if text file contains more than 6000 lines.
'  Uses dynamic array to save memory workspace.
'  Able to view a text file over 2 megabytes big with 16000 lines.
'  Scroll up/down thru file.  PgUp/PgDn/Home/End keys work too.
'  Esc key exits the subroutine.
'  Allows left/right scrolling for files wider than the viewbox window
'  Cut and paste the subroutine into your own program.

DECLARE SUB ViewBox (FileName$, ULR%, ULC%, LRR%, LRC%)

DEFINT A-Z

COLOR 1, 11
CLS

PRINT "                         View Box Subroutine Demo"
PRINT
PRINT "         This demo displays the source code for this demo program"
PRINT "         in a viewbox window."

FileName$ = "viewbox.bas"

ULR = 8: ULC = 10       'upper left row / upper left column
LRR = 23: LRC = 70     'lower right row / lower right column

COLOR 11, 1

ViewBox FileName$, ULR, ULC, LRR, LRC

CLOSE
END

SUB ViewBox (FileName$, ULR, ULC, LRR, LRC)

 DisplayedX = LRR - ULR - 1   ' Number of rows displayed at one time
 ClipLength = LRC - ULC - 1   'number of columns displayed at one time
 OffsetY = 1
 TempWipe$ = "±" + STRING$(ClipLength, 32) + "±"

 '-- analyze text file, count the number of lines in the file
 FileNumber = FREEFILE
 OPEN FileName$ FOR INPUT AS #FileNumber
 WHILE NOT EOF(FileNumber)
   LINE INPUT #FileNumber, Record$
   LineCount = LineCount + 1
 WEND
 CLOSE #FileNumber

 'Build listbox frame
  FOR i = ULR TO LRR: LOCATE i, ULC: PRINT TempWipe$; : NEXT i
  LOCATE LRR, ULC: PRINT STRING$(ClipLength + 2, 177);
  LOCATE ULR, ULC: PRINT STRING$(ClipLength + 2, 177);

 'build index, record byte location and line length of each line
 Pointer = 1
 REDIM TempIndex(LineCount + 1, 2) AS LONG
 OPEN FileName$ FOR INPUT AS #FileNumber
 TempIndex(Pointer, 1) = 1               'first record location is one
 WHILE NOT EOF(FileNumber)
   LINE INPUT #FileNumber, Record$       'get record
   TempIndex(Pointer, 2) = LEN(Record$)  'record length
   IF TempIndex(Pointer, 2) > MaxLength THEN MaxLength = TempIndex(Pointer, 2)
   Pointer = Pointer + 1
   TempIndex(Pointer, 1) = TempIndex(Pointer - 1, 1) + LEN(Record$) + 2
 WEND
 CLOSE #FileNumber


 '-- display file
 OPEN FileName$ FOR BINARY AS #FileNumber

 ExitFlag = 0
 Pointer = 1

 WHILE ExitFlag = 0
   'display screen of text
   LOCATE ULR + 1, ULC + 1
   Xloc = ULR
   IF Pointer < 1 THEN Pointer = 1
   FOR i = Pointer TO Pointer + (DisplayedX - 1)   'rows (ULR+1 to LRR-1)
     IF i <= LineCount THEN
       SEEK #FileNumber, TempIndex(i, 1)
       Record$ = INPUT$(TempIndex(i, 2), #FileNumber)
       Xloc = Xloc + 1
       LOCATE Xloc, ULC: PRINT TempWipe$;
       LOCATE Xloc, ULC + 1
       PRINT MID$(Record$, OffsetY, ClipLength);
     END IF
   NEXT i

   '-- process keyboard input
   KeyCode$ = "": WHILE KeyCode$ = "": KeyCode$ = INKEY$: WEND
   IF KeyCode$ = CHR$(27) THEN CLOSE FileNumer: EXIT SUB
   IF KeyCode$ = CHR$(0) + CHR$(72) THEN Pointer = Pointer - 1: IF Pointer < 1 THEN Pointer = 1
   IF KeyCode$ = CHR$(0) + CHR$(80) THEN Pointer = Pointer + 1: IF Pointer + (DisplayedX - 1) > LineCount THEN Pointer = LineCount - (DisplayedX - 1)
   IF KeyCode$ = CHR$(0) + CHR$(75) THEN OffsetY = OffsetY - 5: IF OffsetY < 1 THEN OffsetY = 1
   IF KeyCode$ = CHR$(0) + CHR$(77) THEN
      OffsetY = OffsetY + 5
      IF OffsetY > (MaxLength - ClipLength + 1) THEN OffsetY = (MaxLength - ClipLength + 1)
      IF OffsetY < 1 THEN OffsetY = 1
   END IF
   IF KeyCode$ = CHR$(0) + CHR$(73) THEN Pointer = Pointer - (DisplayedX - 1): IF Pointer < 1 THEN Pointer = 1
   IF KeyCode$ = CHR$(0) + CHR$(81) THEN Pointer = Pointer + (DisplayedX - 1): IF Pointer + (DisplayedX - 1) > LineCount THEN Pointer = LineCount - (DisplayedX - 1)
   IF KeyCode$ = CHR$(0) + CHR$(71) THEN Pointer = 1
   IF KeyCode$ = CHR$(0) + CHR$(79) THEN Pointer = LineCount - (DisplayedX - 1)
 WEND

 CLOSE FileNumber
 ERASE TempIndex

END SUB
