#include once "window9.bi"

Function createItem(ByVal gadget As Integer, _
					ByVal itembegin As Integer, _
					ByVal itemend As Integer, _
					ByVal Parent As Integer) As Integer
	If itembegin=itemend Then
		Exit function
	EndIf	
	Dim TV_ITEM_ As TV_ITEM
	TV_ITEM_.hItem=Cast(HTREEITEM,itembegin)
	#ifdef UNICODE
		Dim buf As WString*256
		TV_ITEM_.pszText=@buf	
	#else	
		Dim buf As String*256
		TV_ITEM_.pszText=Cast(LPSTR,StrPtr(buf))		
	#EndIf
	TV_ITEM_.cchTextMax=256
	TV_ITEM_.mask=TVIF_IMAGE Or TVIF_SELECTEDIMAGE Or TVIF_TEXT
	SendMessage(GadgetID(gadget),TVM_GETITEM,0,cast(LPARAM,@TV_ITEM_))
	Dim TV_ITEM_1 As TVINSERTSTRUCT
	TV_ITEM_1.item.mask=TVIF_IMAGE Or TVIF_SELECTEDIMAGE Or TVIF_TEXT
	#ifdef UNICODE
		TV_ITEM_1.item.pszText=@buf
	#else	
		TV_ITEM_1.item.pszText=Cast(LPSTR,StrPtr(buf))
	#EndIf	
	TV_ITEM_1.item.cchTextMax=256
	TV_ITEM_1.item.iImage =TV_ITEM_.iimage
	TV_ITEM_1.item.iSelectedImage=TV_ITEM_.iSelectedImage
	TV_ITEM_1.hInsertAfter          =  Cast(HTREEITEM,itemend)
	TV_ITEM_1.hParent               = Cast(HTREEITEM,Parent)
	Return Cast(Integer,TreeView_InsertItem(GadgetID(gadget), @TV_ITEM_1 ))
End Function

Function Move_(ByVal gadget As Integer, _
				ByVal itembegin As Integer, _
				ByVal itemend As Integer, _
				ByVal Parent As Integer) As Integer Export
	Static As Integer ECX_
	Var Par__=createItem(gadget,itembegin,itemend,Parent)
	Function=Par__
	Do
		Var child=TreeView_GetChild(GadgetID(gadget),itembegin)
		If child Then
			ECX_+=1
			Move_(gadget,Cast(Integer,child),itembegin,Par__)      
		Else
			If ECX_>0 then
				var  NEXT_=TreeView_GetNextSibling(GadgetID(gadget),itembegin)
				If NEXT_ Then
					Move_(gadget,Cast(Integer,NEXT_),itemend,Cast(Integer,TreeView_GetParent(GadgetID(gadget),Par__)))
				Else
					ECX_-=1
					Exit Function        
				EndIf
			Else
				Exit function
			endif
		EndIf
	Loop Until ECX_=0
End Function

Function MoveItemTreeView(ByVal gadget As Long, _
						ByVal itembegin As Integer, _
						ByVal itemend As Integer, _
						ByVal Parent As Integer) As Integer Export
	Function=Move_(CInt(gadget),itembegin,itemend,Parent)
	SendMessage(GadgetID(gadget),TVM_DELETEITEM,0,Cast(lparam,itembegin))
End Function
