#include once "window9.bi"

Function AddTreeViewItem OverLoad(ByVal gadget As Long, ByRef sText As USTRING, ByVal IDImage_0 As HBITMAP,ByVal IDImage_Selected As HBITMAP,ByVal pos_ As integer,ByVal parent As Integer) As Integer Export
    dim tvis as TVINSERTSTRUCT
    dim iList as HIMAGELIST = Cast(HIMAGELIST,GetHimageCombo_(gadget))
    Dim As Integer iIndexImage , iIndexSelectImage
    dim as any ptr pStackIndexes = GetProp(GadgetID(gadget), "stackindexes")
    if IDImage_0 then
        if pStackIndexes andalso SizeStackFreeIndexes(pStackIndexes) then
            iIndexImage = PopStackFreeIndexes(pStackIndexes)
            ImageList_Replace(iList,iIndexImage,IDImage_0,0)
        else
            iIndexImage = ImageList_Add(iList,IDImage_0 , 0)
        EndIf
        tvis.item.iImage = iIndexImage
    else
        tvis.item.iImage = -2
    EndIf
    if IDImage_Selected then
        if pStackIndexes andalso SizeStackFreeIndexes(pStackIndexes) then
            iIndexSelectImage = PopStackFreeIndexes(pStackIndexes)
            ImageList_Replace(iList,iIndexSelectImage,IDImage_Selected,0)
        else
            iIndexSelectImage = ImageList_Add(iList,IDImage_Selected , 0)
        EndIf
        tvis.item.iSelectedImage = iIndexSelectImage
    else
        tvis.item.iSelectedImage = -2
    EndIf
    tvis.Item.Mask           = TVIF_TEXT or TVIF_IMAGE or TVIF_SELECTEDIMAGE 
    #ifdef UNICODE
        tvis.item.pszText        = cast(any ptr , *sText)
    #Else
        tvis.item.pszText        = strptr(sText)
    #EndIf
    tvis.item.cchTextMax     = len(sText)
    tvis.hInsertAfter        = Cast(HTREEITEM,pos_)
    tvis.hParent             = Cast(HTREEITEM,parent) 
    Var  hPrev               = TreeView_InsertItem(GadgetID(gadget), @tvis )
    Return Cast(Integer,hPrev)
End Function

Function AddTreeViewItem(ByVal gadget As Long, ByRef sText As USTRING,ByVal IDImage_0 As HICON,ByVal IDImage_Selected As HICON,ByVal pos_ As Integer,ByVal parent As Integer) As Integer Export
    dim as TVINSERTSTRUCT tvis
    dim as HIMAGELIST iList = Cast(HIMAGELIST,GetHimageCombo_(gadget))
    Dim As Integer iIndexImage , iIndexSelectImage
    dim as any ptr pStackIndexes = GetProp(GadgetID(gadget), "stackindexes")
    if IDImage_0 then
        if pStackIndexes andalso SizeStackFreeIndexes(pStackIndexes) then
            iIndexImage = PopStackFreeIndexes(pStackIndexes)
            ImageList_ReplaceIcon(iList,iIndexImage,IDImage_0)
        else
            iIndexImage = ImageList_AddIcon(iList,IDImage_0 )
        EndIf
        tvis.item.iImage = iIndexImage
    else
        tvis.item.iImage = -2
    EndIf
    if IDImage_Selected then
        if pStackIndexes andalso SizeStackFreeIndexes(pStackIndexes) then
            iIndexSelectImage = PopStackFreeIndexes(pStackIndexes)
            ImageList_ReplaceIcon(iList,iIndexSelectImage,IDImage_Selected)
        else
            iIndexSelectImage = ImageList_AddIcon(iList,IDImage_Selected)
        EndIf
        tvis.item.iSelectedImage = iIndexSelectImage
    else
        tvis.item.iSelectedImage = -2
    EndIf
    tvis.Item.Mask           = TVIF_TEXT or TVIF_IMAGE or TVIF_SELECTEDIMAGE 
    #ifdef UNICODE
        tvis.item.pszText        = cast(any ptr , *sText)
    #Else
        tvis.item.pszText        = strptr(sText) 
    #EndIf
    tvis.item.cchTextMax     = len(sText)
    tvis.hInsertAfter        = Cast(HTREEITEM,pos_)
    tvis.hParent             = Cast(HTREEITEM,parent) 
    Var  hPrev               = TreeView_InsertItem(GadgetID(gadget), @tvis )
    Return Cast(Integer,hPrev)
End Function
