dim oldFfileToLoad$(4)
dim fileToLoad$(1)
dim fileCount(1)
dim driveCount(1)
dim files$(1,2)
dim fileData$(256)
DRIVE$ = ""
perform checklist for drives
driveCount(1) = checklist quantity()
dim drives$(checklist quantity())
 
 
for d = 1 to driveCount(1)
   drives$(d) = checklist string$(d)
next d
empty checklist
 
clickDelay = 500
clickCount = 0
timestamp = timer()
mousestamp = 0
offset = 0
 
loaderPath$ = ""
 
 
sync on
 
repeat
   cls
 
 
   gosub drawFileLoader
 
   if upkey()=1 then dec offset
   if downkey()=1 then inc offset
   if offset < 0 then offset = 0
   if offset > 256 then offset = 256
 
   ink rgb(255,255,255),0
   center text 320,230, "File Preview"
   displayFileData(fileToLoad$(0))
   displayFile(offset)
 
 
   sync
until spacekey()
end
 
 
 
REM Sort the files
_Sort_Load_Files:
   flag=0
   repeat
      flag=0
      for x = 1 to fileCount(1)-1
         a$ = lower$(getExtension$(files$(x,1)))
         b$ = lower$(getExtension$(files$(x+1,1)))
         if len(a$) = len(files$(x,1)) then files$(x,2) = "1"
         if len(b$) = len(files$(x+1,1)) then files$(x+1,2) = "1"
         if a$ > b$
            t$ = files$(x,2)
            temp$=files$(x,1)
            files$(x,1) = files$(x+1,1)
            files$(x,2) = files$(x+1,2)
            files$(x+1,1) = temp$
            files$(x+1,2) = t$
            flag=1
         endif
      next x
   until flag = 0
RETURN
 
REM
REM
drawFileLoader:
   x = 220
   y = 75
   counter = 0
   displayCount = 10
   charWidth = text width("X")
   tHeight = text height("X")
   width = 200
   height = displayCount*tHeight
   maxChars = (width-12) / charWidth
   if mouseclick() = 0 then mouseFlag = 0
   ink rgb(192,192,192),0
   box x,y,x+width,y+height
 
   ink rgb(128,128,128),0
   box x+width,y,x+width+10,y+height
   drawUpArrow(x+width,y)
   drawDownArrow(x+width,y+height-11)
 
   if mouseWithin(x+width,y,x+width+10,y+11) and mouseclick() = 1 and mouseflag = 0
      mouseFlag = 1
      if fileOffset > 0 then dec fileOffset
   endif
   if mouseWithin(x+width,y+height-11,x+width+10,y+height) and mouseclick() = 1 and mouseflag = 0
      mouseFlag = 1
      if fileOffset < fileCount(0)-displayCount then inc fileOffset
   endif
 
   if mousemovex() = 0 and mousemovey() = 0
      if oldmousex = mousex() and oldmousey = mousey()
         if mousestamp+1000 < timer() then showToolTip = 0
      else
         oldmousex = mousex()
         oldmousey = mousey()
         mousestamp = timer()
         showToolTip = -1
      endif
   else
      showToolTip = -1
   endif
 
   if timestamp+clickDelay < timer() then clickCount = 0
   if loaderPath$ = ""
      for i = 1 to driveCount(1)
         ly = y+counter*tHeight
         oy = abs(tHeight-10)/2 + 1
         if mouseWithin(x,ly+1,x+width,ly+tHeight-1)
            ink rgb(220,220,240),0
            box x,ly+1,x+width,ly+tHeight-1
            if mouseclick() = 1 and mouseFlag = 0
               mouseFlag = 1
               inc clickCount
               if clickCount = 1 then timestamp = timer()
               if clickCount = 2 and timestamp+clickDelay >= timer()
                  fileOffset = 0
                  loaderPath$ = drives$(i)
                  DRIVE$ = drives$(i)
                  loadFiles(loaderPath$)
                  gosub _Sort_Load_Files
                  clickCount = 0
               endif
            endif
         endif
         drawFolder(x+1, ly+oy)
         ink 0,0
         text x+12, ly, drives$(i)
         line x,ly+tHeight,x+width,ly+tHeight
         inc counter
      next i
   else
      for i = 1+fileOffset to displayCount+fileOffset
         if i <= fileCount(1)
            ly = y+counter*tHeight
            oy = abs(tHeight-10)/2 + 1
            if mouseWithin(x,ly+1,x+width,ly+tHeight-1)
               if showToolTip = 0 then showToolTip = i
               ink rgb(220,220,240),0
               box x,ly+1,x+width,ly+tHeight-1
               if mouseclick() = 1 and mouseFlag = 0
                  mouseFlag = 1
                  inc clickCount
                  if clickCount = 1 then timestamp = timer()
                  if clickCount = 2 and timestamp+clickDelay >= timer()
                     if files$(i,2) = "1"
                        fileOffset = 0
                        loaderPath$ = files$(i,1)
                        loadFiles(loaderPath$)
                        gosub _Sort_Load_Files
                        clickCount = 0
                     else
                        fileToLoad$(0) = DRIVE$+loaderPath$+"\"+files$(i,1)
                     endif
                  endif
               endif
            endif
            if files$(i,2) = "1"
               drawFolder(x+1, ly+oy)
            else
               drawFile(x+1, ly+oy)
            endif
 
            display$ = files$(i,1)
            if showToolTip = i
               w = text width(display$)+12
               ink rgb(220,220,240),0
               box x+12,ly+1,x+w,ly+tHeight-1
               ink 0,0
               text x+12, ly, display$
            else
               if len(files$(i,1)) > maxChars then display$ = left$(display$, maxChars-3)+"..."
               ink 0,0
               text x+12, ly, display$
            endif
            line x,ly+tHeight,x+width,ly+tHeight
            inc counter
         endif
      next i
   endif
RETURN
 
 
REM
function drawFolder(x, y)
   ink rgb(243,235,133),0
   box x,y,x+4,y+2
   box x,y+2,x+10,y+9
endfunction
 
REM
function drawFile(x,y)
   ink rgb(243,235,233),0
   box x,y,x+7,y+10
endfunction
 
REM
function drawUpArrow(x,y)
   ink rgb(255,255,255),0
   box x,y,x+10,y+11
   ink 0,0
   line x+5,y+1,x+1,y+9
   line x+5,y+1,x+9,y+9
   line x+1,y+9,x+9,y+9
endfunction
 
REM
function drawDownArrow(x,y)
   ink rgb(255,255,255),0
   box x,y,x+10,y+11
   ink 0,0
   line x+5,y+9,x+1,y+1
   line x+5,y+9,x+9,y+1
   line x+1,y+1,x+9,y+1
endfunction
 
 
 
 
 
REM
function loadFiles(path$)
   undim files$()
   empty checklist
   cd path$
   perform checklist for files
   fileCount(0) = checklist quantity()
   dim files$(checklist quantity(),2)
   r = 0
   for f = 1 to fileCount(0)
      ext$ = getExtension$(checklist string$(f))
      if lower$(ext$) = "txt" or len(ext$) = len(checklist string$(f))
         inc r
         files$(r,1) = checklist string$(f)
      endif
   next f
   fileCount(1) = r
endfunction
 
 
 
 
REM
function getExtension$(name$)
   if name$ = "." or name$ = ".." then exitfunction name$
   length = len(name$)
   for i = length to 1 step -1
      if mid$(name$,i) = "." then exitfunction right$(name$,length-i)
   next i
endfunction name$
 
REM boolean, true if mouse within specified co-ordinates
function mouseWithin(x1,y1,x2,y2)
  if mousex() >= x1 and mousex() <= x2 and mousey() >= y1 and mousey() <= y2 then exitfunction 1
endfunction 0
 
 
 
REM
REM
REM
function displayFileData(filename$)
   if filename$ <> oldFfileToLoad$(1)
      oldFfileToLoad$(1) = filename$
      open to read 1, filename$
      wordCount(1)
      close file 1
   else
      text 1,1,"Filename: "+filename$
      text 1,20,"Word count: "+oldFfileToLoad$(2)
      text 1,36,"Character count w/ spaces: "+oldFfileToLoad$(3)
      text 1,52,"Character count no spaces: "+oldFfileToLoad$(4)
   endif
endfunction
 
 
REM nFile = file number to scan
function wordCount(nFile)
   undim fileData$()
   dim fileData$(256)
   c = 1
   rem only counts words with more than this many characters
   minChars = 1
   wCount = 0
   charCount = 0
   spaces = 0
   while file end(nFile) = 0
      read string nFile, s$
      fileData$(c) = s$
      inc c
      charCount = charCount + len(s$)
      start = 1
      for i=1 to len(s$)
         if mid$(s$, i) = " "
            inc spaces
            if i-start > minChars then inc wCount
            start = i
         endif
      next i
      rem end of line word check check
      if i-start > minChars then inc wCount
   endwhile
   charCountNoSpaces = charcount-spaces
 
   oldFfileToLoad$(2) = str$(wCount)
   oldFfileToLoad$(3) = str$(charCount)
   oldFfileToLoad$(4) = str$(charCountNoSpaces)
endfunction
 
 
REM
function displayFile(offset)
   ink rgb(210,232,236),0
   box 1,250,screen width(),480
   ink 0,0
   h = text height("X")
   lines = 230/h
   if offset+lines > 256 then offset = 256-lines
   y = 0
   for i = offset to offset+h
      text 10,250+y*h,fileData$(i)
      inc y
   next i
 
endfunction