Directory list and treeview

BlitzMax Forums/BlitzMax GUI Programming/Directory list and treeview

Hi :)

I'm trying to do an explorer like treeview but i get
problem with recursivity :( Anybody can help me ?

' createtreeview.bmx

Strict 

Global window:TGadget=CreateWindow("My Window",50,50,240,240)
Global treeview:TGadget=CreateTreeView(0,0,200,200,window)
SetGadgetLayout treeview,2,2,2,2

Global root:TGadget=TreeViewRoot(treeview)

Global Drive:String="C:"
Global Direc:String="Program Files\2nd Speech Center"

Global directory:TGadget
directory=AddTreeViewNode(Drive,root)
directory=AddTreeViewNode(Direc,directory)

Global Previous:TGadget

ListFiles(Drive+Direc)

While WaitEvent()
	Print CurrentEvent.ToString()
	Select EventID()
	Case EVENT_WINDOWCLOSE
		End
	End Select
Wend

Function ListFiles( rootdir:String)
	Local dir:Int = ReadDir( rootdir)
	If Not dir Then Return
	
	'If Not rootdir.EndsWith("/") Then rootdir:+ "/"
	
	Repeat
		Local fn:String = NextFile(dir)
		If Not fn Then Exit ' <-- this is important, we dont want an endless loop
		
		If fn = ".." Then Continue
		
		If FileType( rootdir+"/"+fn) = 2 And fn<>"." And fn<>".." Then
			If Previous<>Null
				directory=Previous
			Else
				Previous=AddTreeViewNode(fn,directory)
			EndIf
			
				
			ListFiles( rootdir+"/"+fn) ' <-- recurse over this folder
			
			Print "--------------------------------"
			'Print "1) "+rootdir+"/"+fn
			'Print "2) "+rootdir
			Print "3) "+fn
			Print "--------------------------------"
		Else
		
			'Print rootdir+"/"+fn
		EndIf
	Forever
	CloseDir dir
EndFunction


I found this way :
http://www.2dgamecreators.com/maxgui/T18%20-%20TreeView.html

Try this:
Strict 

Global window:TGadget=CreateWindow("My Window",50,50,240,240)
Global treeview:TGadget=CreateTreeView(0,0,200,200,window)
SetGadgetLayout treeview,2,2,2,2

Global root:TGadget=TreeViewRoot(treeview)

Global Drive:String="C:"
Global Direc:String="library"

Global directory:TGadget
directory=AddTreeViewNode(Drive,root)
directory=AddTreeViewNode(Direc,directory)

Global Previous:TGadget

ListFiles(directory, Drive+Direc)

While WaitEvent()
	Print CurrentEvent.ToString()
	Select EventID()
	Case EVENT_WINDOWCLOSE
		End
	End Select
Wend

Function ListFiles( folder:TGadget, rootdir:String)
	Local dir:Int = ReadDir( rootdir)
	Local subfolder:TGadget
	If Not dir Then Return
	
	'If Not rootdir.EndsWith("/") Then rootdir:+ "/"
	Repeat
		Local fn:String = NextFile(dir)
		If Not fn Then Exit ' <-- this is important, we dont want an endless loop
		
		If FileType( rootdir+"/"+fn) = 2 And fn<>"." And fn<>".." Then
			subFolder = AddTreeViewNode( fn, folder )			
			ListFiles( subFolder, rootdir+"/"+fn) ' <-- recurse over this folder
		EndIf
	Forever
	CloseDir dir
EndFunction


Many thanks :)

I know I'm new to blitzMax but...
Scaremongers code doesn't seem to do anything.


Firstly I had to add
Import MaxGui.Drivers

the end result is only a tree with

c:\
|--Library

I neglected to mention I'm testing with the demo version 1.3.

Scaremonger's example isn't really a fully featured explorer treeview. I've hacked at it a bit to make it slightly more flexible, but we could still probably do with, perhaps, a TProxyGadget implementation.

Strict 

Import MaxGUI.Drivers

Global window:TGadget=CreateWindow("My Window",50,50,240,400)
Global treeview:TGadget=CreateTreeView(0,0,ClientWidth(window),ClientHeight(window),window)
SetGadgetLayout treeview,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED

Global root:TGadget=TreeViewRoot(treeview)

Global Drive:String="C:"

Global directory:TGadget
directory=AddTreeViewNode(Drive,root)

Global Previous:TGadget

ListFiles(directory, Drive)

While WaitEvent()
	Print CurrentEvent.ToString()
	Select EventID()
	Case EVENT_GADGETOPEN
		If CountTreeViewNodes( TGadget(EventExtra()) ) <= 1 Then
			ListFiles( TGadget(EventExtra()), String(GadgetExtra(TGadget(EventExtra()))) )
		EndIf
	Case EVENT_WINDOWCLOSE
		End
	End Select
Wend

Function ListFiles( folder:TGadget, rootdir:String)
	Local subfolder:TGadget
	For Local tmpNode:TGadget = EachIn folder.kids
		FreeGadget(tmpNode)
	Next
	For Local fn$ = EachIn LoadDir(rootdir)
		subFolder = AddTreeViewNode( fn, folder )
		SetGadgetExtra( subFolder,rootdir+"/"+fn)
		If LoadDir(rootdir+"/"+fn) Then
			AddTreeViewNode( "", subFolder )
		EndIf
	Next
	ExpandTreeViewNode(folder)
EndFunction


Thank you SebHoll for the very nice example code