I know this thread is a little old but the code in it was really really helpful. Although it still leaves one issue that I just can't seem to resolve. When I am trying to add another node to my tree and I want that node to be at the same level as the node I have selected, I can do it with AddGadgetItem as per the code EXCEPT where the next row is up a level. If the following node after the selected node is up a level then the inserted node will also be up a level. I have tried a few variations and I just can't seem to solve it. Here is the sample code modified to try to demonstrate the issue:
Code: Select all
#ButtonSameLevel = 0
#ButtonChild = 1
#Tree = 2
#StringRowNumber = 3
#TextRowNumber = 4
If OpenWindow(0, 0, 0, 300, 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, 195, 470, 100, 25, "SameLevel")
ButtonGadget(#ButtonChild, 100, 470, 100, 25, "Child")
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)
; we must open the node that will be the SameLevel of our new item
OpenTreeGadgetNode(#Tree, Item)
; the new item must be below the selected one, so add at position Item+1
AddGadgetItem(#Tree, Item+1, "New Child")
; close the node again..
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
; get the item that is selected
Item = GetGadgetState(#Tree)
; the new item must be below the selected one, so add at position Item+1
AddGadgetItem(#Tree, Item+1, "New SameLevel")
; 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
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
End
So if you run this, then select child item 2 and click 'SameLevel' and you will see a node added at the same level. Select child item 3 and click 'SameLevel' and now you see the issue I am trying to figure out. The new item is added up one level. I realise there are occasions where you might actully want this to happen but there seems no way to specify which of the two situations you want. I have figured out that if I want to add a node up a level, I can always code to go back up the tree to the last Parent node and AddGadgetItem from there. It seems to add at the same level after all the child nodes. I just can't figure it out. Help!!!