REM >TreePick
REM By Dave Lawrence 22Jun95
REM 
REM TreePick is a magic Dynamic Menu that allows you to do things like:
REM 
REM TreePick -type Sprite -dir Root:Backdrops -command "Backdrop -tile %0"
REM 
REM It (recursively) makes menus of files of the given type starting from
REM 'dir'.  When one is clicked on, the command is executed with the
REM filename substituted as expected.
REM
REM Type can now be a range where each type is separated by a comma,
REM   e.g.: Text,Obey,Sprite


ON ERROR PRINT REPORT$;" at line ";ERL:END
DIM buf 512

in$=FNgetargs
SYS "OS_ReadArgs", "type/k,dir/k,command/k,submenu/sk", in$, buf, 512
REM Get file type, look up type number, make sprite name
type$=FNstr0(!buf)
type_i%=0:i%=0
WHILE INSTR(type$,",",i%)<>0
  i%=INSTR(type$,",",i%):i%+=1:type_i%+=1
ENDWHILE
DIM type_t$(type_i%), type_n%(type_i%), type_s$(type_i%)
s%=1
FOR i%=0 TO type_i%
  e%=INSTR(type$,",",s%)
  type_t$(i%)=MID$(type$,s%,e%-s%)
  s%=e%+1
NEXT
FOR i%=0 TO type_i%
  SYS "OS_FSControl",31,type_t$(i%) TO ,,type_n%(i%)
  type_s$(i%) = "small_"+RIGHT$("000"+STR$~type_n%(i%),3)
NEXT
REM Just get the directory
dir$=FNstr0(buf!4)

REM Get the command, rebuild command with %%s replacing %s, make command to pass to sub-directories
command$=FNstr0(buf!8)
submenu%=buf!12:IF submenu%<>0 submenu$=" -submenu" ELSE submenu$=""
sub$=""
FOR i=1 TO LEN command$
 IF MID$(command$,i,1)="%" sub$+="%"
 sub$+=MID$(command$,i,1)
NEXT
sub$="Dynamic:/Director:Menus.Files.TreePick -type "+type$+submenu$+" -command """""+sub$+""""""

menu=FNswi_number("Director_Menu")
endmenu=FNswi_number("Director_EndMenu")
option=FNswi_number("Director_Option")
command=FNswi_number("Director_Command")
subargs=FNswi_number("OS_SubstituteArgs")
dash=FNswi_number("Director_Dash")

REM Read canonicalised path name and take menu name from the current leaf
SYS "OS_FSControl",37,dir$,buf,,,512
canon$=FNstr0(buf)
OSCLI"DirectorParsePath "+canon$
SYS menu,"<Director$CurrentLeaf> TreePick -temp"

i=0
REPEAT
 SYS "OS_GBPB",12,dir$,buf,1,i,128,0 TO ,,,read,i
 IF read=1 THEN
  leaf$=FNstr0(buf+24)
  path$=dir$+"."+leaf$
  CASE buf!16 OF
   WHEN 1:
    type_p%=-1
    FOR i%=0 TO type_i%
      IF buf!20 = type_n%(i%) type_p%=i%:i%=type_i%
    NEXT
    IF type_p%<>-1 THEN
     SYS subargs, path$, buf, 512, command$, LEN command$
     IF submenu%=0 THEN
      SYS option, leaf$+" -sprite "+type_s$(type_p%)
      SYS command, buf
     ELSE
      SYS subargs, path$, buf, 512, command$, LEN command$
      SYS option, leaf$+" -sprite "+type_s$(type_p%)+" -sub """+FNstr0(buf)+""""
     ENDIF
    ENDIF
   WHEN 2,3:
    SYS option, leaf$+" -sprite small_dir -sub "+CHR$34+sub$+" -dir "+path$+CHR$34
  ENDCASE
 ENDIF
UNTIL i=-1
*EndMenu
*Set Director$Menu TreePick
END

DEF FNswi_number(swi$)
SYS "OS_SWINumberFromString",,swi$ TO swi%
=swi%
:
DEF FNgetargs
SYS "OS_GetEnv" TO A$
quit=INSTR(A$,"-quit")
IF quit=0 quit=INSTR(A$,"-QUIT")
A$=MID$(A$,quit+6)
spc=INSTR(A$," ")
A$=MID$(A$,spc+1)
=A$
:
DEF FNstr0(A)
A$=""
WHILE ?A<>0
 A$+=CHR$?A
 A+=1
ENDWHILE
=A$
