File browser/selector

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

File browser/selector

Post by Henko »

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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: File browser/selector

Post by Mr. Kibernetik »

Very nice, but I found there is no "parent folder" button.

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

Re: File browser/selector

Post by Henko »

Mr. Kibernetik wrote:Very nice, but I found there is no "parent folder" button.
Quite right. But i needed a quick fileselection box for an app i'm busy with. Moreover, you might have such a function in your development plan. If not, please tell me and i will add the parent folder button. It would replace then the "Root" button. Implementation is not too difficult, using a LIFO stack.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: File browser/selector

Post by Mr. Kibernetik »

Actually there is possibility to go to "parent folder". It is ".." (two dots) path.

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

Re: File browser/selector

Post by Henko »

Mr. Kibernetik wrote:Actually there is possibility to go to "parent folder". It is ".." (two dots) path.
Ahh, of course! Thanks. I will upload an update.

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

Re: File browser update

Post by Henko »

Added the possibility to step back to the parentdirectories, using the "root" button (now called the "back ^" button.

@ mr. K.: i couldn't figure out how to use ".." In this case. I need to build the path in a string to be able to return it together with the selected file. But it works all right now.


Code: Select all

option base 1
draw text file_select$(150,100) at 10,150
end

' file browser/selector for use in Smart Basic programs
' returns selected file with path
' touch a folder to descend in the file structure
' touch "back ^" button to go back one level (parent directory)
' 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 original background is restored after quitting the function
' 
def file_select$(xtop,ytop)
maxdim=100 ! set lists custom
dim directs$(maxdim),files$(maxdim), combi$(2*maxdim)
graphics ! fill color .8,.8,.8 ! draw color 0,0,0
graphics clear .8,.8,.8
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 "back ^" 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$
fil_lab21:
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 
  for k=len(path$) to 1 step -1
    if mid$(path$,k,1)="/" then break
    next k
  if k then ! path$=left$(path$,k-1) ! goto fil_lab21 ! end if
  end if
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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: File browser update

Post by Mr. Kibernetik »

Henko wrote:i couldn't figure out how to use ".." In this case.
DIR ".." SET
changes current directory to parent directory

This program prints current directory listing, goes to "1" folder, prints its listing, then goes back to parent directory and again prints its listing:

Code: Select all

def f
dir "" list files f$,n
option base 1
for i=1 to n
  print f$(i)
  next i
print
end def

f
dir "1" set
f
dir ".." set
f

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

Re: File browser/selector

Post by Henko »

Not yet to the point.... I'll clarify.
The function is to be embedded in a user program. From within the app., the user must be able to retrieve a filename from any directory (to open it, or whatever he wished to do with the file). Hence the function must keep track of the searchpath and must return a string, containing the selected file, inclusive the entire path.
In the present version, the path is a string, built during the search proces. If the user touches a subdirectory, that directory is "pushed" onto the end of the string. If the user hits the "back ^" button, the last part of the pathstring is "popped" away.
If the "ok" button is pressed, the pathstring and the selected filename are concatenated and returned to the calling program.
The expression set dir ".." Doesn't make the parent directoryname available to the program in string format? It just set a SB internal parh?

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: File browser/selector

Post by Mr. Kibernetik »

You are right, it doesn't return any name - it just sets parent directory as current.

Post Reply