File browser/selector
Posted: Sat Jul 05, 2014 10:21 pm
for iPhone and iPad
Code: Select all
option base 1
draw text file_select$(150,100) at 10,150
end
' file browser/selector for Smart Basic file system
' returns selected file with path
' touch a folder to descend in the file structure
' use filter to restrict the number of files shown
' folders are prefixed in the list with "D_"
' position the fileselector with the x,y coordinates
' the background is restored
'
def file_select$(xtop,ytop)
graphics ! fill color .8,.8,.8 ! draw color 0,0,0
graphics clear .8,.8,.8
maxdim=100 ! set lists custom
dim directs$(maxdim),files$(maxdim), combi$(2*maxdim)
s_w=screen_width() ! s_h=screen_height()
wid=220 ! hgt=420
if xtop+wid>s_w-10 then xtop=s_w-wid-10 ! xbot=xtop+wid
if ytop+hgt>s_h-10 then ytop=s_h-hgt-10 ! ybot=ytop+hgt
sprite "fbrow" scan xtop-2,ytop-2,wid+4,hgt+4
draw size 3 ! draw rect xtop,ytop to xbot,ybot
draw line xtop,ytop+24 to xbot,ytop+24
draw color 0,0,1 ! draw text "file select" at xtop+45,ytop+4
draw color 0,0,0
button "back" title "Root" at xtop+150,ytop+340 size 60,30
field "filter" text "filter" at xtop+10,ytop+340 size 120,30
button "cancel" title "Cancel" at xtop+10,ytop+380 size 90,30
button "ok" title "OK" at xtop+120,ytop+380 size 90,30
filter$="" ! fil$=""
fil_lab1: path$="" ! dir$=""
fil_lab2: if dir$>"" then path$=path$ & "/" & dir$
dir path$ list dirs directs$,n1
dir path$ list files files$,n2
fil_lab3:
if n1 then
for i=1 to n1 ! combi$(i)="D_" & directs$(i) ! next i
end if
fil_lab4:
ntot=n1
if n2 then
for i=1 to n2
if filter$="" or filter$="filter" or instr(files$(i),filter$)>=0 then
ntot+=1 ! combi$(ntot)=files$(i)
end if
next i
end if
if ntot=0 then ! ntot=1 ! combi$(1)="" ! end if
dim temp$(ntot)
for i=1 to ntot ! temp$(i)=combi$(i) ! next i
list "fsel" text temp$ at xtop+2,ytop+26 size xbot-xtop-4,300
draw size 3 ! draw line xtop,ytop+330 to xbot,ytop+330
fil_lab5:
if button_pressed("cancel") then
ret$="" ! goto fil_lab6
end if
if button_pressed("ok") then
' debug pause
if fil$>"" then
ret$=path$ & "/" & fil$
goto fil_lab6
end if
end if
if field_changed("filter") then
filter$=field_text$("filter") ! goto fil_lab4
end if
if button_pressed("back") then fil_lab1
sel=list_selected("fsel")
if sel>0 then
if sel>n1 then
fil$=combi$(sel) ! goto fil_lab5
else
dir$=directs$(sel) ! goto fil_lab2
end if
end if
goto fil_lab5
fil_lab6:
field "filter" delete ! button "back" delete
button "cancel" delete ! button "ok" delete
list "fsel" delete
sprite "fbrow" at xtop-2,ytop-2 ! sprite "fbrow" stamp
sprite "fbrow" delete
return ret$
end def