help with TreeGadget

Just starting out? Need help? Post your questions and find answers here.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

cool :wink:
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

TreeView - adding same level at end of child nodes

Post by Daffodil the Duck »

Further to my previous question, I found some terrific code on this forum for TreeViews (TVAddItem) which I played with for a while. Doesn't 100% solve my problem. I am trying to solve adding a new row into a TreeView at the tail end of a set of child nodes so that the new row will be at the same indent/child level as the ones before it. Current AddGadgetItem inserts the new row at a parent level to the child nodes before it. The code I found (thanks to whoever created it, it really is very good) used a procedure called TVAdditem which does what I want but according to the GetGadgetState command, all the rows that it adds have a row number of -1, which ultimately causes more problems later on. Here is the code. Try adding Child Nodes and Same Level nodes using the buttons provided and then click on those rows and you will see the field I added to display the row number has the correct value in it. Try adding using the TVAddItem routine and click on the row, you will see the rows are -1. What have I done wrong?

Code: Select all

#ButtonSameLevel = 0 
#ButtonChild = 1 
#ButtonTVAddItemChild = 2 
#ButtonTVAddItemSameLevel = 3 
#Tree = 4 
#StringRowNumber = 5
#TextRowNumber = 6

#InsertChild = #True
#InsertSameLevel = #False
#DummyImage = 0

; These Structures are needed for: TVAddItem()
; ********************************************
;
  Structure TVITEM
    mask.l
    hItem.l
    state.l
    stateMask.l
    pszText.l
    cchTextMax.l
    iImage.l
    iSelectedImage.l
    cChildren.l
    lParam.l
  EndStructure
;
  Structure TVINSERTSTRUCT
    hParent.l
    hInsertAfter.l
    item.TVITEM
  EndStructure
;
;
;
;
Procedure TVAddItem(gadget.l, position.l, text.s, hImg.l, openflag.l)
  ;
  ; Insert a Item in a TreeView Gadget.
  ; not like AddGadgetItem(), this one supports the position parameter.
  ;
  ; Usage:
  ;***********
  ; gadget.l   = PB Gadget Number
  ; position.l = Item to insert the new one after (starting with 0)
  ; text.s     = Item Text
  ; hImg.l     = ImageID if Image to display
  ; openflag.l = If #TRUE, a new TreeViewNode is created at 'position.l' and the new Item
  ;              is added as it's Child, if #FALSE, the new one is just inserted after the 'position.l'
  ;              Item.
  ;
  ; Note: The hImg.l parameter is only supported, if there are allready some Items with Images.
  ;
  hwndTV.l = GadgetID(gadget)
  hRoot.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_ROOT, 0)
  hItem = hRoot: hParent.l = 0
  For i.l = 0 To position-1
    hItem2.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_CHILD, hItem)
    Repeat
      If hItem2 = #Null: hItem2 = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_NEXT, hItem): EndIf
      If hItem2 = #Null: hItem = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem): EndIf
    Until hItem2 <> #Null
    hItem = hItem2
  Next i
  lpis.TVINSERTSTRUCT
  If openflag = #True
    pitem.TVITEM
    pitem\mask = #TVIF_CHILDREN | #TVIF_HANDLE
    pitem\hItem = hItem
    pitem\cChildren = 1
    SendMessage_(hwndTV, #TVM_SETITEM, 0, @pitem)
    lpis\hParent = hItem
    lpis\hInsertAfter = hItem
  Else 
    lpis\hParent = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem)
    lpis\hInsertAfter = hItem
  EndIf
  lpis\item\mask = #TVIF_TEXT 
  If hImg <> 0
    himl.l = SendMessage_(hwndTV, #TVM_GETIMAGELIST, #TVSIL_NORMAL ,0)
    If himl <> #Null
      lpis\item\mask | #TVIF_IMAGE

; @@@ Makes it crash so commented out. Put back in if you want an image associated
;      iImage.l = ImageList_AddIcon_(himl, hImg)
; @@@

      lpis\item\iImage = iImage
      lpis\item\iSelectedImage = iImage
    EndIf
  EndIf
  lpis\item\cchTextMax = Len(text)
  lpis\item\pszText = @text
  SendMessage_(hwndTV, #TVM_INSERTITEM, 0, @lpis)
EndProcedure




If OpenWindow(0, 0, 0, 500, 500, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "TreeGadget") 
  If CreateGadgetList(WindowID()) 
  
    TreeGadget(#Tree, 5, 5, 290, 400)    
    TextGadget(#TextRowNumber, 5, 410, 40, 20, "Row:")
    StringGadget(#StringRowNumber, 35, 410, 60, 17, "", #PB_String_ReadOnly)

    ButtonGadget(#ButtonSameLevel, 5, 470, 110, 25, "Same Level") 
    ButtonGadget(#ButtonChild, 120, 470, 110, 25, "Child") 
    ButtonGadget(#ButtonTVAddItemChild, 235, 470, 110, 25, "TV Child") 
    ButtonGadget(#ButtonTVAddItemSameLevel, 350, 470, 110, 25, "TV Same Level") 

    
    AddGadgetItem(#Tree, -1, "Item 0") 
    AddGadgetItem(#Tree, -1, "Item 1") 
    OpenTreeGadgetNode(#Tree, 1) 
       AddGadgetItem(#Tree, -1, "ChildItem 1") 
       AddGadgetItem(#Tree, -1, "ChildItem 2") 
       AddGadgetItem(#Tree, -1, "ChildItem 3") 
    CloseTreeGadgetNode(#Tree, 1) 
    AddGadgetItem(#Tree, -1, "Item 2") 
    AddGadgetItem(#Tree, -1, "Item 3") 
    AddGadgetItem(#Tree, -1, "Item 4") 
      
    Repeat 
      Event= WaitWindowEvent() 
      
      If Event = #PB_Event_Gadget 
      
        If EventGadgetID() = #Tree 
            Item = GetGadgetState(#Tree) 
            SetGadgetText(#StringRowNumber, Str(Item))
        EndIf
        

        If EventGadgetID() = #ButtonChild 
          ; get the item that is selected 
          Item = GetGadgetState(#Tree) 

          OpenTreeGadgetNode(#Tree, Item) 
              AddGadgetItem(#Tree, Item+1, "New Child") 
          CloseTreeGadgetNode(#Tree, Item) 
          
          ; automatically expand the node, but keep the selection 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          
          ; give focus from the button back to the tree to show the selection 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonSameLevel 
          Item = GetGadgetState(#Tree) 
          AddGadgetItem(#Tree, Item+1, "New Same Level") 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemChild
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Child", #DummyImage, #InsertChild)
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemSameLevel 
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Same Level", #DummyImage, #InsertSameLevel)
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 
      
      EndIf      
      
    Until Event = #PB_Event_CloseWindow 
  EndIf 
EndIf 

End
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

Your code doesn't run correctly
Because when you click on Item 4, and add TVChild, the child has -1 in response of Getgadgetstate()
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Thanks for reopening this topic because I still haven't found an answer.

My original question was how to add a new row into a child node of a tree that would be at the same level as the child. Using just the PB commands, I can make it work if I am not inserting after the last child in the node. But if I insert the new row after the last child in the node it doesn't work because the new row adds it at the parent level. So that's when I turned to the TV_ procedures found in another posting on this forum and they give the programmer the control to insert the new row at either the same level or the parent level depending on the parameter specified. But any row inserted using the TV_ procedures has a GetGadgetState(#Tree) of "-1" and I don't know why. So I guess that might be my question. Why do all the TV_ inserted rows have a row number returned as -1?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I used your code to run a few tests and the only conclusion I can reach is that Purebasic must be keeping some internal data on the items within the tree; e.g. the number of items in the tree. Consequently, on using the API to insert new items, we cause PB's internal records to fall out of sync.

To see what I mean, try the following which displays Purebasic's count of items in the text box. Watch this fall out of sync after hitting the "TV Same level" button.

Code: Select all

#ButtonSameLevel = 0 
#ButtonChild = 1 
#ButtonTVAddItemChild = 2 
#ButtonTVAddItemSameLevel = 3 
#Tree = 4 
#StringRowNumber = 5 
#TextRowNumber = 6 

#InsertChild = #True 
#InsertSameLevel = #False 
#DummyImage = 0 

; These Structures are needed for: TVAddItem() 
; ******************************************** 
; 
  Structure TVITEM 
    mask.l 
    hItem.l 
    state.l 
    stateMask.l 
    pszText.l 
    cchTextMax.l 
    iImage.l 
    iSelectedImage.l 
    cChildren.l 
    lParam.l 
  EndStructure 
; 
  Structure TVINSERTSTRUCT 
    hParent.l 
    hInsertAfter.l 
    item.TVITEM 
  EndStructure 
; 
; 
; 
; 
Procedure TVAddItem(gadget.l, position.l, text.s, hImg.l, openflag.l) 
  ; 
  ; Insert a Item in a TreeView Gadget. 
  ; not like AddGadgetItem(), this one supports the position parameter. 
  ; 
  ; Usage: 
  ;*********** 
  ; gadget.l   = PB Gadget Number 
  ; position.l = Item to insert the new one after (starting with 0) 
  ; text.s     = Item Text 
  ; hImg.l     = ImageID if Image to display 
  ; openflag.l = If #TRUE, a new TreeViewNode is created at 'position.l' and the new Item 
  ;              is added as it's Child, if #FALSE, the new one is just inserted after the 'position.l' 
  ;              Item. 
  ; 
  ; Note: The hImg.l parameter is only supported, if there are allready some Items with Images. 
  ; 
  hwndTV.l = GadgetID(gadget) 
  hRoot.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_ROOT, 0) 
  hItem = hRoot: hParent.l = 0 
  For i.l = 0 To position-1 
    hItem2.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_CHILD, hItem) 
    Repeat 
      If hItem2 = #Null: hItem2 = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_NEXT, hItem): EndIf 
      If hItem2 = #Null: hItem = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem): EndIf 
    Until hItem2 <> #Null 
    hItem = hItem2 
  Next i 
  lpis.TVINSERTSTRUCT 
  If openflag = #True 
    pitem.TVITEM 
    pitem\mask = #TVIF_CHILDREN | #TVIF_HANDLE 
    pitem\hItem = hItem 
    pitem\cChildren = 1 
    SendMessage_(hwndTV, #TVM_SETITEM, 0, @pitem) 
    lpis\hParent = hItem 
    lpis\hInsertAfter = hItem 
  Else 
    lpis\hParent = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem) 
    lpis\hInsertAfter = hItem 
  EndIf 
  lpis\item\mask = #TVIF_TEXT 
  If hImg <> 0 
    himl.l = SendMessage_(hwndTV, #TVM_GETIMAGELIST, #TVSIL_NORMAL ,0) 
    If himl <> #Null 
      lpis\item\mask | #TVIF_IMAGE 

; @@@ Makes it crash so commented out. Put back in if you want an image associated 
;      iImage.l = ImageList_AddIcon_(himl, hImg) 
; @@@ 

      lpis\item\iImage = iImage 
      lpis\item\iSelectedImage = iImage 
    EndIf 
  EndIf 
  lpis\item\cchTextMax = Len(text) 
  lpis\item\pszText = @text 
  SendMessage_(hwndTV, #TVM_INSERTITEM, 0, @lpis) 
EndProcedure 




If OpenWindow(0, 0, 0, 500, 500, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "TreeGadget") 
  If CreateGadgetList(WindowID()) 
  
    TreeGadget(#Tree, 5, 5, 290, 400)    
    TextGadget(#TextRowNumber, 5, 410, 40, 20, "Row:") 
    StringGadget(#StringRowNumber, 35, 410, 60, 17, "", #PB_String_ReadOnly) 

    ButtonGadget(#ButtonSameLevel, 5, 470, 110, 25, "Same Level") 
    ButtonGadget(#ButtonChild, 120, 470, 110, 25, "Child") 
    ButtonGadget(#ButtonTVAddItemChild, 235, 470, 110, 25, "TV Child") 
    ButtonGadget(#ButtonTVAddItemSameLevel, 350, 470, 110, 25, "TV Same Level") 

    
    AddGadgetItem(#Tree, -1, "Item 0") 
    AddGadgetItem(#Tree, -1, "Item 1") 
    OpenTreeGadgetNode(#Tree, 1) 
       AddGadgetItem(#Tree, -1, "ChildItem 1") 
       AddGadgetItem(#Tree, -1, "ChildItem 2") 
       AddGadgetItem(#Tree, -1, "ChildItem 3") 
    CloseTreeGadgetNode(#Tree, 1) 
    AddGadgetItem(#Tree, -1, "Item 2") 
    AddGadgetItem(#Tree, -1, "Item 3") 
    AddGadgetItem(#Tree, -1, "Item 4") 
      
    Repeat 
      Event= WaitWindowEvent() 
      
      If Event = #PB_Event_Gadget 
      
        If EventGadgetID() = #Tree 
            Item = countgadgetitems(#Tree) 
            SetGadgetText(#StringRowNumber, Str(Item)) 
        EndIf 
        

        If EventGadgetID() = #ButtonChild 
          ; get the item that is selected 
          Item = GetGadgetState(#Tree) 

          OpenTreeGadgetNode(#Tree, Item) 
              AddGadgetItem(#Tree, Item+1, "New Child") 
          CloseTreeGadgetNode(#Tree, Item) 
          
          ; automatically expand the node, but keep the selection 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          
          ; give focus from the button back to the tree to show the selection 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonSameLevel 
          Item = GetGadgetState(#Tree) 
          AddGadgetItem(#Tree, Item+1, "New Same Level") 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemChild 
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Child", #DummyImage, #InsertChild) 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemSameLevel 
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Same Level", #DummyImage, #InsertSameLevel) 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 
      
      EndIf      
      
    Until Event = #PB_Event_CloseWindow 
  EndIf 
EndIf 

End
Now try the following in which I switch Purebasic's count of the number of items for that obtained from the Win API which does not fall out of sync.

Code: Select all

#ButtonSameLevel = 0 
#ButtonChild = 1 
#ButtonTVAddItemChild = 2 
#ButtonTVAddItemSameLevel = 3 
#Tree = 4 
#StringRowNumber = 5 
#TextRowNumber = 6 

#InsertChild = #True 
#InsertSameLevel = #False 
#DummyImage = 0 

; These Structures are needed for: TVAddItem() 
; ******************************************** 
; 
  Structure TVITEM 
    mask.l 
    hItem.l 
    state.l 
    stateMask.l 
    pszText.l 
    cchTextMax.l 
    iImage.l 
    iSelectedImage.l 
    cChildren.l 
    lParam.l 
  EndStructure 
; 
  Structure TVINSERTSTRUCT 
    hParent.l 
    hInsertAfter.l 
    item.TVITEM 
  EndStructure 
; 
; 
; 
; 
Procedure TVAddItem(gadget.l, position.l, text.s, hImg.l, openflag.l) 
  ; 
  ; Insert a Item in a TreeView Gadget. 
  ; not like AddGadgetItem(), this one supports the position parameter. 
  ; 
  ; Usage: 
  ;*********** 
  ; gadget.l   = PB Gadget Number 
  ; position.l = Item to insert the new one after (starting with 0) 
  ; text.s     = Item Text 
  ; hImg.l     = ImageID if Image to display 
  ; openflag.l = If #TRUE, a new TreeViewNode is created at 'position.l' and the new Item 
  ;              is added as it's Child, if #FALSE, the new one is just inserted after the 'position.l' 
  ;              Item. 
  ; 
  ; Note: The hImg.l parameter is only supported, if there are allready some Items with Images. 
  ; 
  hwndTV.l = GadgetID(gadget) 
  hRoot.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_ROOT, 0) 
  hItem = hRoot: hParent.l = 0 
  For i.l = 0 To position-1 
    hItem2.l = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_CHILD, hItem) 
    Repeat 
      If hItem2 = #Null: hItem2 = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_NEXT, hItem): EndIf 
      If hItem2 = #Null: hItem = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem): EndIf 
    Until hItem2 <> #Null 
    hItem = hItem2 
  Next i 
  lpis.TVINSERTSTRUCT 
  If openflag = #True 
    pitem.TVITEM 
    pitem\mask = #TVIF_CHILDREN | #TVIF_HANDLE 
    pitem\hItem = hItem 
    pitem\cChildren = 1 
    SendMessage_(hwndTV, #TVM_SETITEM, 0, @pitem) 
    lpis\hParent = hItem 
    lpis\hInsertAfter = hItem 
  Else 
    lpis\hParent = SendMessage_(hwndTV, #TVM_GETNEXTITEM, #TVGN_PARENT, hItem) 
    lpis\hInsertAfter = hItem 
  EndIf 
  lpis\item\mask = #TVIF_TEXT 
  If hImg <> 0 
    himl.l = SendMessage_(hwndTV, #TVM_GETIMAGELIST, #TVSIL_NORMAL ,0) 
    If himl <> #Null 
      lpis\item\mask | #TVIF_IMAGE 

; @@@ Makes it crash so commented out. Put back in if you want an image associated 
;      iImage.l = ImageList_AddIcon_(himl, hImg) 
; @@@ 

      lpis\item\iImage = iImage 
      lpis\item\iSelectedImage = iImage 
    EndIf 
  EndIf 
  lpis\item\cchTextMax = Len(text) 
  lpis\item\pszText = @text 
  SendMessage_(hwndTV, #TVM_INSERTITEM, 0, @lpis) 
EndProcedure 




If OpenWindow(0, 0, 0, 500, 500, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "TreeGadget") 
  If CreateGadgetList(WindowID()) 
  
    TreeGadget(#Tree, 5, 5, 290, 400)    
    TextGadget(#TextRowNumber, 5, 410, 40, 20, "Row:") 
    StringGadget(#StringRowNumber, 35, 410, 60, 17, "", #PB_String_ReadOnly) 

    ButtonGadget(#ButtonSameLevel, 5, 470, 110, 25, "Same Level") 
    ButtonGadget(#ButtonChild, 120, 470, 110, 25, "Child") 
    ButtonGadget(#ButtonTVAddItemChild, 235, 470, 110, 25, "TV Child") 
    ButtonGadget(#ButtonTVAddItemSameLevel, 350, 470, 110, 25, "TV Same Level") 

    
    AddGadgetItem(#Tree, -1, "Item 0") 
    AddGadgetItem(#Tree, -1, "Item 1") 
    OpenTreeGadgetNode(#Tree, 1) 
       AddGadgetItem(#Tree, -1, "ChildItem 1") 
       AddGadgetItem(#Tree, -1, "ChildItem 2") 
       AddGadgetItem(#Tree, -1, "ChildItem 3") 
    CloseTreeGadgetNode(#Tree, 1) 
    AddGadgetItem(#Tree, -1, "Item 2") 
    AddGadgetItem(#Tree, -1, "Item 3") 
    AddGadgetItem(#Tree, -1, "Item 4") 
      
    Repeat 
      Event= WaitWindowEvent() 
      
      If Event = #PB_Event_Gadget 
      
        If EventGadgetID() = #Tree 
            Item = sendmessage_(gadgetid(#Tree), #TVM_GETCOUNT,0,0)
            SetGadgetText(#StringRowNumber, Str(Item)) 
        EndIf 
        

        If EventGadgetID() = #ButtonChild 
          ; get the item that is selected 
          Item = GetGadgetState(#Tree) 

          OpenTreeGadgetNode(#Tree, Item) 
              AddGadgetItem(#Tree, Item+1, "New Child") 
          CloseTreeGadgetNode(#Tree, Item) 
          
          ; automatically expand the node, but keep the selection 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          
          ; give focus from the button back to the tree to show the selection 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonSameLevel 
          Item = GetGadgetState(#Tree) 
          AddGadgetItem(#Tree, Item+1, "New Same Level") 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemChild 
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Child", #DummyImage, #InsertChild) 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 


        If EventGadgetID() = #ButtonTVAddItemSameLevel 
          Item = GetGadgetState(#Tree) 
          TVAddItem(#Tree, Item, "TV Same Level", #DummyImage, #InsertSameLevel) 
          SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected) 
          ActivateGadget(#Tree)        
        EndIf 
      
      EndIf      
      
    Until Event = #PB_Event_CloseWindow 
  EndIf 
EndIf 

End
I don't know if this helps, but it seems to indicate where the problems lie.
I may look like a mule, but I'm not a complete ass.
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Thanks Srod. It does help. I have to agree with your assumption so yes, it does seem as though the data required to maintain treeviews in PB and the API's are a bit out of sync here. I guess if I want to proceed using the TreeView in my program I am going to have to learn a bit about these API calls and use them throughout instead of the PB TreeView commands. I assume that by using API's, I can replace every PB TreeView command with an appropriate API call. There were more TV_ procedures posted so I will gather them together and give it a shot updating the test code I posted previously. Should be interesting.

Thanks for the help. Saw your response to another TreeView posting. Hope all is well.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Daffodil, I've had a quick glance through what it is you're trying to do. The following code seems to do the same job, but without using any API calls. Let me know if I've misunderstood what it is you're after.

Code: Select all

#ButtonSameLevel = 0 
#ButtonChild = 1 
ID=2

If OpenWindow(0,0,0,355,300,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TreeGadget") And CreateGadgetList(WindowID(0))
    TreeGadget(ID, 10,10,160,160)
    ButtonGadget(#ButtonSameLevel, 200, 30, 100, 25, "Same Level") 
    ButtonGadget(#ButtonChild, 200, 80, 100, 25, "Child") 
    ButtonGadget(10, 200, 230, 100, 25, "State") 
    AddGadgetItem(ID, -1, "Item 0") 
    AddGadgetItem(ID, -1, "Item 1") 
    OpenTreeGadgetNode(ID, 1) 
       AddGadgetItem(ID, -1, "ChildItem 1") 
       AddGadgetItem(ID, -1, "ChildItem 2") 
       AddGadgetItem(ID, -1, "ChildItem 3") 
    CloseTreeGadgetNode(ID, 1) 
    AddGadgetItem(ID, -1, "Item 2") 
    AddGadgetItem(ID, -1, "Item 3") 
    AddGadgetItem(ID, -1, "Item 4") 

  Repeat
    EventID = WaitWindowEvent()
    Select EventID
    Case #PB_Event_Gadget
      Select EventGadgetID()
        case #ButtonSameLevel
          if GetGadgetState(ID) <>-1
;Check if there are any subitems. This is so the index of the new item is correct.
            count=counttreegadgetnodeitems(ID, GetGadgetState(ID))
            addgadgetitem(ID,  GetGadgetState(ID)+count+1, "New "+str(GetGadgetState(ID)+count+1))        
            setgadgetstate(ID,GetGadgetState(ID))
          endif
        case #ButtonChild
          if GetGadgetState(ID) <>-1
;Check if there are any subitems. This is so the index of the new item is correct.
            count=counttreegadgetnodeitems(ID, GetGadgetState(ID))
            OpenTreeGadgetNode(ID, GetGadgetState(ID)) 
            AddGadgetItem(ID, GetGadgetState(ID)+count+1, "New child "+str(GetGadgetState(ID)+count+1)) 
            CloseTreeGadgetNode(ID, GetGadgetState(ID)) 
            setgadgetstate(ID,GetGadgetState(ID))
          endif
        case 10
      EndSelect
    EndSelect
  Until EventID = #PB_Event_CloseWindow

EndIf
btw, yes I'm fine. The treatment was a post surgery kind of precaution as I now have the all clear! Has made me violently sick for a few days though! Thanks.
I may look like a mule, but I'm not a complete ass.
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Srod

Glad to hear you are fine. Thanks for looking at it. I tried out your code and I think you'll see the issue if you expand 'Item 1', highlight 'ChildItem3' and click 'SameLevel'. The item that gets created 'New 5' is not created at the same level but is created as the next parent node, one level higher than the 'ChildItem3'. I have been trying to insert a 'ChildItem4' but have had no luck.

However, SameLevel works just fine if you are not on the last child in a node. Hope that made sense?
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Srod
Wait a sec. You may have it. While the last reply I sent is still true (where the'child' is highlighted), if I highlight the parent node (Item 1) and click "Child" then the child is added as the last child node and that is what I wanted to achieve. Terrific. So in my code (haven't done it yet) I should be able to detect that the highlighted row is a child, back up to the parent row (somehow) and then execute the snippet of code you sent and in theory that should work. Now I'm excited again... Thanks
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Aha I see what you mean. I'll have another look.

*EDIT: :lol:
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

This seems to work.

It follows the method you outlined above, but it does not need to highlight the parent node etc. Instead, if we add an item at the same level as the last child node, it just determines which node is the parent and opens the node afresh (but without moving the focus from the selected item) etc.

A couple of API calls are required, but not to add any items and so there should be no problem with things getting out of sync as before.

Code: Select all

#ButtonSameLevel = 0 
#ButtonChild = 1 
global ID
ID=2

declare AddItemToForwardNode(item)


If OpenWindow(0,0,0,355,300,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TreeGadget") And CreateGadgetList(WindowID(0))
    TreeGadget(ID, 10,10,160,160)
    ButtonGadget(#ButtonSameLevel, 200, 30, 100, 25, "Same Level") 
    ButtonGadget(#ButtonChild, 200, 80, 100, 25, "Child") 
    AddGadgetItem(ID, -1, "Item 0") 
    AddGadgetItem(ID, -1, "Item 1") 
    OpenTreeGadgetNode(ID, 1) 
       AddGadgetItem(ID, -1, "ChildItem 1") 
       AddGadgetItem(ID, -1, "ChildItem 2") 
       AddGadgetItem(ID, -1, "ChildItem 3") 
    CloseTreeGadgetNode(ID, 1) 
    AddGadgetItem(ID, -1, "Item 2") 
    AddGadgetItem(ID, -1, "Item 3") 
    AddGadgetItem(ID, -1, "Item 4") 

  Repeat
    EventID = WaitWindowEvent()
    Select EventID
    Case #PB_Event_Gadget
      Select EventGadgetID()
        case #ButtonSameLevel
          if GetGadgetState(ID) <>-1
;Check if there are any subitems. This is so the index of the new item is correct.
            count=counttreegadgetnodeitems(ID, GetGadgetState(ID))
            if SendMessage_(GadgetID(ID), #TVM_GETNEXTITEM, #TVGN_NEXT, GadgetItemID(ID, GetGadgetState(ID))) = 0 and GetGadgetState(ID) < countgadgetitems(ID)-1
;This means we are at the last item in a node, but not the last item in the tree.
              parentitem = TreeGadgetItemNumber(ID, SendMessage_(GadgetID(ID), #TVM_GETNEXTITEM, #TVGN_PARENT, GadgetItemID(ID, GetGadgetState(ID))))
              AddItemToForwardNode(parentitem)
            Else
              addgadgetitem(ID,  GetGadgetState(ID)+count+1, "New "+str(GetGadgetState(ID)+count+1))
            endif
            setgadgetstate(ID,GetGadgetState(ID))
          endif
        case #ButtonChild
          if GetGadgetState(ID) <>-1
;Check if there are any subitems. This is so the index of the new item is correct.
            AddItemToForwardNode(GetGadgetState(ID))
            setgadgetstate(ID,GetGadgetState(ID))
          endif
        case 10
      EndSelect
    EndSelect
  Until EventID = #PB_Event_CloseWindow

EndIf


procedure AddItemToForwardNode(item)
  protected count
;Check if there are any subitems. This is so the index of the new item is correct.
  count=counttreegadgetnodeitems(ID, item)
  OpenTreeGadgetNode(ID, item) 
    AddGadgetItem(ID, item+count+1, "New "+str(item+count+1)) 
  CloseTreeGadgetNode(ID, item) 
endprocedure
Regards.
I may look like a mule, but I'm not a complete ass.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I arrived too late, but by the way of tree view gadgets:
viewtopic.php?t=7316
this could be interesting too:
viewtopic.php?t=18263
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Very good post Psychophanta! Just what I need!

(I'm impressed of sorting the items code..)
Daffodil the Duck
User
User
Posts: 14
Joined: Mon Aug 15, 2005 10:14 pm
Location: Canada

Post by Daffodil the Duck »

Srod that's terrific. It works and now TreeViews seem to behave as I would want them to behave. It's one of those snippets of code that should be saved somewhere on this forum for ready access. Thanks for taking the time to understand the problem and to solve it. Hope all is well

Daffodil
Post Reply