Page 2 of 2

Re: TreeGadget population help

Posted: Sat Jun 17, 2023 12:40 pm
by Axolotl
I can do it in a proc ...... :oops:

Code: Select all

Dim f$(10)
f$(0)="File 01"
f$(1)="Folder 1\File 11"
f$(2)="Folder 1\File 12"
f$(3)="Folder 1\File 13"
f$(4)="Folder 2\File 21"
f$(5)="Folder 2\File 22"
f$(6)="Folder 2\File 23"
f$(7)="Folder 3\Sub-folder 3\File 331"
f$(8)="Folder 3\Sub-folder 3\File 332"
f$(9)="Folder 3\Sub-folder 3\File 333"
f$(10)="Folder 4\File 41"

;/---------------------------------------------------------------------------------------------------------------------
;| 
Procedure FillTreeGadget(Gadget, Array Files.s(1)) 
  Protected fn.s, currdir.s, idx, count, level, cnt  
  Dim lastdir.s(0) 

  count = ArraySize(Files()) 
  For idx = 0 To count  ; 
    fn = Files(idx) 
    cnt = CountString(fn, #PS$) 
    For level = 0 To cnt - 1
      currdir = StringField(fn, level+1, #PS$) 
      
      If lastdir(level) <> currdir 
        AddGadgetItem(0, -1, currdir, 0, level) 
        lastdir(level) = currdir 
        ReDim lastdir(level+1) 
      EndIf 
    Next level 

    fn = StringField(fn, level+1, #PS$) 
    AddGadgetItem(0, -1, fn, 0, level)
  Next
  Dim lastdir(0) 
  ProcedureReturn 0 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

  If OpenWindow(0, 0, 0, 355, 200, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TreeGadget(0, 10, 10, 340, 180)   ; TreeGadget standard
    FillTreeGadget(0, f$()) 
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf

Now, you can choose from it.

Re: TreeGadget population help

Posted: Sat Jun 17, 2023 12:57 pm
by BarryG
Axolotl wrote: Sat Jun 17, 2023 12:40 pmI can do it in a proc ...... :oops:
Don't be embarrassed: that's the only one that worked! Good job.

Your earlier code, and mk-soft's, both broke if I added a standalone file into "Folder 3" like this:

Code: Select all

f$(x)="Folder 3\File 31" ; Added this as a test, and it broke all except Axolotl's last post.
f$(x)="Folder 3\Sub-folder 3\File 331"
f$(x)="Folder 3\Sub-folder 3\File 332"
f$(x)="Folder 3\Sub-folder 3\File 333"
So, the problem seems to be solved. Thanks! The final test code:

Code: Select all

Dim f$(14)
f$(0)="File 01"
f$(1)="Folder 1\File 11"
f$(2)="Folder 1\File 12"
f$(3)="Folder 1\File 13"
f$(4)="Folder 2\File 21"
f$(5)="Folder 2\File 22"
f$(6)="Folder 2\File 23"
f$(7)="Folder 3\File 31"
f$(8)="Folder 3\Sub-folder 3\File 331"
f$(9)="Folder 3\Sub-folder 3\File 332"
f$(10)="Folder 3\Sub-folder 3\File 333"
f$(11)="Folder 4\File 41"
f$(12)="Folder 4\Sub-folder 41\File 441"
f$(13)="Folder 4\Sub-folder 42\Sub-folder 421\File 4421"
f$(14)="Folder 4\File 42"

Procedure FillTreeGadget(Gadget, Array Files.s(1)) 
  Protected fn.s, currdir.s, idx, count, level, cnt  
  Dim lastdir.s(0) 
  
  count = ArraySize(Files()) 
  For idx = 0 To count
    fn = Files(idx) 
    cnt = CountString(fn, #PS$) 
    For level = 0 To cnt - 1
      currdir = StringField(fn, level+1, #PS$) 
      If lastdir(level) <> currdir 
        AddGadgetItem(0, -1, currdir, 0, level) 
        lastdir(level) = currdir 
        ReDim lastdir(level+1) 
      EndIf 
    Next 
    fn = StringField(fn, level+1, #PS$) 
    AddGadgetItem(0, -1, fn, 0, level)
  Next
  Dim lastdir(0) 
  ProcedureReturn 0 
EndProcedure 

If OpenWindow(0, 0, 0, 355, 200, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TreeGadget(0, 10, 10, 340, 180)   ; TreeGadget standard
  FillTreeGadget(0, f$()) 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: TreeGadget population help

Posted: Sat Jun 17, 2023 2:42 pm
by AZJIO
BarryG
You need to put the name of the author before the procedures so that the work is not forgotten.

I have a program to get a list of files. I added file list view to it as a virtual file system. I liked what came out of it. I can get the files sorted by date and the virtual file system will create a date tree list with files in each date item. But I could not create a structured list, which is shown on your left in the screenshot.
In the end, I just now tried the Axolotl procedure to get a virtual file system using a saved list of files and it worked. Previously this was done on AutoIt3

Code: Select all

EnableExplicit

Global File$, hIcon1, hIcon2
File$ = OpenFileRequester("Open file list", GetCurrentDirectory(), "list of files (*.txt)", 0)
If Asc(File$)
	If Right(File$, 4) <> ".txt"
		File$ + ".txt"
	EndIf
	ExtractIconEx_("Shell32.dll", 3, 0, @hIcon1, 1)
	ExtractIconEx_("Shell32.dll", 0, 0, @hIcon2, 1)
Else
	End
EndIf


Procedure main()
	Protected fn.s, currdir.s, cnt, level, id_file, Format
	Protected Dim lastdir.s(130)

	If OpenWindow(0, 0, 0, 460, 700, "list of files", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		TreeGadget(0, 10, 10, 440, 680)

		id_file = ReadFile(#PB_Any, File$)
		If id_file
			Format = ReadStringFormat(id_file)
			While Eof(id_file) = 0
				fn = ReadString(id_file, Format)
; 				MessageRequester("", "|" + fn + "|")
; 				Break

				cnt = CountString(fn, #PS$)
				For level = 0 To cnt - 1
					currdir = StringField(fn, level + 1, #PS$)
					If lastdir(level) <> currdir
						AddGadgetItem(0, -1, currdir, hIcon1, level)
						lastdir(level) = currdir
						ReDim lastdir(level + 1)
					EndIf
				Next level

				fn = StringField(fn, level + 1, #PS$)
				AddGadgetItem(0, -1, fn, hIcon2, level)

			Wend
			CloseFile(id_file)
		EndIf
		ReDim lastdir(0)

		Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
	EndIf
	ProcedureReturn 0
EndProcedure

main()

If hIcon1
	DestroyIcon_(hIcon1)
EndIf
If hIcon2
	DestroyIcon_(hIcon2)
EndIf
End

Re: [Solved] TreeGadget population help

Posted: Sat Jun 17, 2023 2:50 pm
by Olli
simple map added : if structured (level and most recent index), this allows to insert new full path named files on the fly.

Code: Select all

; header
global newmap node.s()
for each array string,
; indexed by i integer

Code: Select all

depth = countstring(f$(i), "\")
fullNode.s = ""
for level = 0 to depth
 node.s = stringfield(f$(i), level + 1, "\")
 fullNode + node + left("\", bool(level < depth) )
 if findmapelement(node(), fullNode)=0
  addgadgetitem(gadget, -1, node, 0, level)
  if level < depth
   node(fullNode) = node
  endif
 endif
next