#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 buf As String*256
  Dim TV_ITEM_ As TV_ITEM
  TV_ITEM_.hItem=Cast(HTREEITEM,itembegin)
  TV_ITEM_.pszText=Cast(LPSTR,StrPtr(buf))
  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
  TV_ITEM_1.item.pszText=Cast(LPSTR,StrPtr(buf))
  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 Integer,ByVal itembegin As Integer, ByVal itemend As Integer, ByVal Parent As Integer)As Integer Export
  Function=Move_(gadget,itembegin,itemend,Parent)
  SendMessage(GadgetID(gadget),TVM_DELETEITEM,0,Cast(lparam,itembegin))
End Function
