#include once "window9.bi"

Private Sub RecurExpandTreeViewItem(iGadget As Long  , iItem As Integer , iFlagExpand As Long)
	
	Dim As Integer iChild = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_CHILD , Cast(LPARAM,iItem)))
	
	If iChild Then 
		
		RecurExpandTreeViewItem(iGadget  , iChild , iFlagExpand)
		
	Endif
	
	Dim As Integer iBr = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_NEXT , Cast(LPARAM,iItem)))
	
	If iBr Then
		
		RecurExpandTreeViewItem(iGadget  , iBr ,  iFlagExpand)
		
	Endif
	
	If iFlagExpand Then
		
		SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_EXPAND , Cast(LPARAM,iItem))
		
	Else
		
		SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_COLLAPSE , Cast(LPARAM,iItem))
		
	Endif
	
End Sub

Sub ExpandTreeViewItem(iGadget As Long  , iItem As Integer , iFlagAll As Long)
	
	If iFlagAll Then
		
		Dim As Integer iChild = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_CHILD , Cast(LPARAM,iItem)))
		
		If iChild Then 
			
			RecurExpandTreeViewItem(iGadget  , iChild , 1)
			
		Endif
		
		SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_EXPAND , Cast(LPARAM,iItem))
		
	Else
		
		SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_EXPAND , Cast(LPARAM,iItem))
		
	Endif
	
	Dim As Integer iTempItem = iItem
	
	while iTempItem
		
		iTempItem = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_PARENT , Cast(LPARAM,iTempItem)))
		
		if iTempItem then
			
			SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_EXPAND , Cast(LPARAM,iTempItem))
			
		EndIf
		
	Wend
	
End Sub

Sub CollapseTreeViewItem(iGadget As Long  , iItem As Integer)
	
	Dim As Integer iChild = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_CHILD , Cast(LPARAM,iItem)))
	
	If iChild Then 
		
		RecurExpandTreeViewItem(iGadget  , iChild , 0)
		
	Endif
	
	SendMessage(Gadgetid(iGadget), TVM_EXPAND , TVE_COLLAPSE , Cast(LPARAM,iItem))
	
End Sub

Sub ExpandTreeViewItemALL(iGadget As Long)
	
	Dim As Integer iRoot = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_ROOT , 0))
	
	RecurExpandTreeViewItem(iGadget , iRoot , 1)
	
End Sub

Sub CollapseTreeViewItemALL(iGadget As Long)
	
	Dim As Integer iRoot = Cast(Integer, SendMessage(Gadgetid(iGadget), TVM_GETNEXTITEM , TVGN_ROOT , 0))
	
	RecurExpandTreeViewItem(iGadget , iRoot , 0)
	
End Sub