'==============
'FILESELECT.BAS v1.1
'==============
'Simple file selector box for graphical screen modes.
'Coded by Dav, NOV/2021 (with the kind help of the QB64.org forum gurus!)
'* NOW WORKS UNDER LINUX!
'Navigate through directories and select a file.
'Use ARROWS, PAGE Up/Down, HOME, END to scroll list.
'Press ENTER to select highlighted file or enter highlighted dir.
'ESC will cancel and close box
'things to do: auto center box on screen, and ditch x/y input?
' ...will need to adjust box size based on file/dir chr length
' Add user defined colors instead...?
'=== draw a background
'=== Ask user to select a file
a$ = FileSelect$(5, 15, 20, 55, "*.*")
PRINT "You selected: "; a$
FUNCTION FileSelect$
(y
, x
, y2
, x2
, Filespec$
)
'=== save original place of cursor
'=== save colors
'=== Save whole screen
_MEMCOPY scr1
, scr1.OFFSET
, scr1.SIZE
TO scr2
, scr2.OFFSET
'=== Generate a unique temp filename to use based on date + timer
loadagain:
top = 0
selection = 0
'=== list directories
SHELL _HIDE "find . -maxdepth 1 -type d > " + tmp$
'=== make room for names
REDIM FileNames$
(10000) 'space for 10000 filenames
'=== only show the ".." when not at root dir
FileNames$(0) = ".."
LineCount = 1
LineCount = 0
'=== Open temp file
'=== load, ignoring the . entry added under Linux
'also remove the ./ added at the beginning when under linux
FileNames$(LineCount) = "[" + rl$ + "]"
LineCount = LineCount + 1
'=== now grab list of files...
IF Filespec$
= "*.*" THEN Filespec$
= "" SHELL _HIDE "find -maxdepth 1 -type f -name '" + Filespec$
+ "*' > " + tmp$
SHELL _HIDE "dir /b /A:-D " + Filespec$
+ " > " + tmp$
'=== open temp file
'=== load, ignoring the generated temp file...
'also remove the ./ added at the beginning when under linux
FileNames$(LineCount) = rl$
LineCount = LineCount + 1
'=== Remove the temp file created
'=== draw a box
'=== show current working dir at top
'=== Shorten it is too long, for display purposes
CurDir$
= MID$(CurDir$
, 1, x2
- x
- 3) + "..."
'=== scroll through list...
'=== directories get a different color...
'=== Get user input
IF selection
> 0 THEN selection
= selection
- 1 IF selection
< top
THEN top
= selection
IF selection
< (LineCount
- 1) THEN selection
= selection
+ 1 IF selection
> (top
+ (y2
- 2)) THEN top
= selection
- y2
+ 1
top = top - y2
selection = selection - y2
IF selection
< 0 THEN selection
= 0
top = top + y2
selection = selection + y2
IF top
>= LineCount
- y2
THEN top
= LineCount
- y2
IF selection
>= LineCount
THEN selection
= LineCount
- 1
top = 0: selection = 0
selection = LineCount - 1
top = selection - y2 + 1
FileSelect$ = ""
'=== if .. then go up one dir
'see if directory
test$
= RTRIM$(FileNames$
(selection
)) test$
= MID$(test$
, 2, LEN(test$
) - 2)
FileSelect$
= C$
+ RTRIM$(FileNames$
(selection
))
'=== Restore the whole screen
_MEMCOPY scr2
, scr2.OFFSET
, scr2.SIZE
TO scr1
, scr1.OFFSET
'=== restore original y,x and color