"Quick & dirty" file selector

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

"Quick & dirty" file selector

Post by Henko »

In some cases (prototyping, for instance), a simple, small file selector may be handy.
Here is one with the following properties/shortcoming:
- only files from the current directory may be listed
- a filter on file extension may be used (empty filter "" lists all files)
- background under the selector window is untouched (using "page" mechanism)
- filenames are displayed in a scrollable list

Button are used to add "lines" to the selector page, as normal lines always are drawn on the base page.
An alternative would be to use the "sprite scan" statement to temporarely save the background under the selector window and put is back using the "sprite stamp" statement upon termination of the file selector. In that case, normal lines could be used, but all objects like buttons should have to be hidden or deleted explicitely.

B4BE5EE6-3E6F-467D-8F5C-147AA815E5AC.jpeg
B4BE5EE6-3E6F-467D-8F5C-147AA815E5AC.jpeg (979.47 KiB) Viewed 6662 times

Code: Select all

graphics ! graphics clear ! draw color 0,0,0
f$ = file_select$("sB files","sb",100,100,200,300,.8,.9,.6,1)
pause 1
text ! print f$
end

' paged filesector
' list files with extension ext$ from current directory
' background remains unchanged after fileselector closes
' title$ = title of the fileselector window
' ext$ = file extension filter.
'        If ext$="" then all files will be selected
'
def file_select$(title$,ext$,xs,ys,ww,hh,R,G,B,alpha)
name$="dh%ur/sp"
page name$ set 
page name$ frame xs,ys,ww,hh
page name$ color R,G,B,alpha
button name$&"bottom" text "" at -6,hh-3 size ww+12,3
button name$&"left" text "" at 0,-6 size 3,hh+12
button name$&"right" text "" at ww-3,-6 size 3,hh+12
button name$&"upper1" text "" at -6,0 size ww+12,3
button name$&"upper2" text "" at -6,30 size ww+12,3
button name$&"title" text title$ at 40,3 size 120,27
button name$&"cancel" text " Cancel " at 10,hh-50 size 80,40
button name$&"ok" text " OK " at ww-90,hh-50 size 80,40
dir "" list files fil$,nfiles
dim ff$(nfiles) ! nf=0
for i=0 to nfiles-1 ! f$=fil$(i) ! pos=instr(f$,".")
  if pos>-1 then
    e$=right$(f$,len(f$)-pos-1)
    if e$=ext$ then ! nf+=1 ! ff$(nf-1)=f$ ! end if
    end if
  next i
if nf then
  dim fil$(nf) ! for i=0 to nf-1 ! fil$(i)=ff$(i) ! next i
  end if
set lists custom ! fill color R,G,B
list name$ text fil$ at 3,33 size ww-7,hh-96
page name$ show
nr=-1 ! fsel$="" 
do slowdown
  if bp(name$&"cancel") then ! fsel$="" ! break ! end if
  nr=list_selected(name$) ! if nr=-1 then continue
  if bp(name$&"ok") then ! fsel$=fil$(nr) ! break ! end if
  until forever
page name$ hide ! page "" set ! return fsel$
end def

def bp(a$) = button_pressed(a$)

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: "Quick & dirty" file selector

Post by Henko »

Finally, the window and title bar may be "drawn" using two buttons, thus eliminating the "tric" with the button "lines". This seems the most simple solution. Also, the alpha parameter is removed, as it turned out not to be used in the function.

9C06EE4B-9E3D-4EC9-84B4-AE028F701888.jpeg
9C06EE4B-9E3D-4EC9-84B4-AE028F701888.jpeg (719.77 KiB) Viewed 6657 times

Code: Select all

graphics ! graphics clear ! draw color 0,0,0
f$=file_select$("sB files","sb",100,100,200,300,.8,.9,.6)
pause 1
text ! print f$
end

' paged filesector
' list files with extension ext$ from current directory
' background remains unchanged after fileselector closes
' title$ = title of the fileselector window
' ext$ = file extension filter.
'        If ext$="" then all files will be selected
'
def file_select$(title$,ext$,xs,ys,ww,hh,R,G,B)
name$= "xf%cg/te"
page name$ set 
page name$ frame xs,ys,ww,hh
page name$ color R,G,B,0
fill color R,G,B ! set buttons custom
button name$&"win" text "" at 0,0 size ww,hh ! set buttons default
button name$&"title" text title$ at 1,1 size ww-2,27
button name$&"cancel" text " Cancel " at 10,hh-50 size 80,40
button name$&"ok" text " OK " at ww-90,hh-50 size 80,40
dir "" list files fil$,nfiles
dim ff$(nfiles) ! nf=0
for i=0 to nfiles-1 ! f$=fil$(i) ! pos=instr(f$,".")
  if pos>-1 then
    e$=right$(f$,len(f$)-pos-1)
    if e$=ext$ then ! nf+=1 ! ff$(nf-1)=f$ ! end if
    end if
  next i
if nf then
  dim fil$(nf) ! for i=0 to nf-1 ! fil$(i)=ff$(i) ! next i
  end if
set lists custom ! fill color R,G,B
list name$ text fil$ at 3,33 size ww-7,hh-96
page name$ show
nr=-1 ! fsel$="" 
do slowdown
  if bp(name$&"cancel") then ! fsel$="" ! break ! end if
  nr=list_selected(name$) ! if nr=-1 then continue
  if bp(name$&"ok") then ! fsel$=fil$(nr) ! break ! end if
  until forever
page name$ hide ! page "" set ! return fsel$
end def

def bp(a$) = button_pressed(a$)

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: "Quick & dirty" file selector

Post by matt7 »

Thanks, Henko. This is helpful.

I'm wondering why you need to do a graphics clear though? Why not create a fullscreen page (maybe solid white or black, and maybe with a transparency of 50% or something) and then create the list and buttons for selecting a file on that new page? Then when "Ok" is pressed, delete the page before returning the selected filename. No scanning the graphics screen to a sprite for restoring later would be required.

The background color and alpha can be used to block or obscure what is already on the screen, whether that be TEXT or GRAPHICS, and it also would block the user from interacting with any other objects underneath the new page, forcing them to select a file or cancel the operation.

This would be similar to how my MSGBOX library works.

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: "Quick & dirty" file selector

Post by matt7 »

Disregard my earlier question. I just realized that the reason you switched from drawing lines to using a button to create the window border was to prevent overwriting anything on the graphics layer. I only glanced at the code and didn't realize the graphics clear at the top is only for the demo code showing how to use the file_select$ function.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: "Quick & dirty" file selector

Post by Henko »

:D :D
I was just answering your question in a post, when your post came up
Ok

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: "Quick & dirty" file selector

Post by rbytes »

This is a very simple and handy selector. I will look forward to using it.

Thanks.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: "Quick & dirty" file selector

Post by Dutchman »

Thanks Henko,
My own solution to find and select files is far too complex for easy use. viewtopic.php?f=20&t=1530&p=9321
This solution is very elegant and useful in most cases. :!:
Last edited by Dutchman on Thu Jan 19, 2023 3:50 pm, edited 1 time in total.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: "Quick & dirty" file selector

Post by Henko »

Dutchman wrote:
Tue Mar 05, 2019 9:57 am
Thanks Henko,
My own solution to find and select files is far too complex for easy use. viewtopic.php?f=20&t=1530&p=9321
This solution is very elegant and useful in most cases. :!:
Thank you for the kind words ;)
The next step is a very simple program launcher:
Put the function and a calling statement " run file_selector$(......,"sb",....) " in a new program, name it like "RUN" and make a icon for it on the home screen
(Maybe something must be done to adress the right directory?)

Post Reply