Page 1 of 1

TreeGadget Image change

Posted: Thu Mar 04, 2010 9:25 pm
by pb-user
Hello,

after playing a while with the treeview gadget I'm looking for a solution to change the
image after a node is opend.

I only found this thread

viewtopic.php?t=7407

but the example file is not abailable. Could anybody help with this :?:

Re: TreeGadget Image change

Posted: Thu Mar 04, 2010 9:51 pm
by srod
One way is to simply delete the item and then add a new one with a new image etc. Though of course this is made more complex by having to possibly reinstate child items as well.

However, if you don't mind dipping in to the api...

The following quick hack shows one way of dynamically altering item images in a tree gadget in response to a button click. If you wish to change the image in response to a parent item being expanded/collapsed then you will need a Window callback to trap the appropriate notification etc. I'll leave that to you though.

Windows only. You will need to edit the code to use 3 icons of your own.

Code: Select all

#Tree = 0
#Button = 1

;Images. Load your own here. 
  img0 = LoadImage(0, "new.ico") 
  img1 = LoadImage(1, "open.ico") 
  img2 = LoadImage(2, "save.ico")


;The following procedure cheats and uses PB to add our icons to the tree gadget's 'normal' image list prior to adding our normal
;items. It is usually easier to add all icons first before attempting to switch images etc.
  Procedure AddIconToTree(id, hImage)
    AddGadgetItem(id, 0, "", hImage)
    RemoveGadgetItem(id, 0)
  EndProcedure


If OpenWindow(0, 0, 0, 300, 300, "PureBasic Window",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered) 

  TreeGadget(#Tree, 10, 10, 280, 250, #PB_Tree_AlwaysShowSelection)                        
    ;Add our 3 icons before adding items to the tree gadget.
      AddIconToTree(#Tree, img0)
      AddIconToTree(#Tree, img1)
      AddIconToTree(#Tree, img2)

  ;Add some items.
    AddGadgetItem(#Tree, -1, "Parent", img0, 0)
    AddGadgetItem(#Tree, -1, "Child", img1, 1)

  ButtonGadget(#Button, 10, 270, 100, 20, "CHANGE ICONS!")

  Repeat 
      eventID = WaitWindowEvent() 
      Select eventID
        Case #PB_Event_CloseWindow 
          Quit = 1 
      
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #Button
              ;Change the two items icons to the 3rd icon we loaded.
                With tvi.TVITEM
                  \mask = #TVIF_IMAGE|#TVIF_HANDLE|#TVIF_SELECTEDIMAGE
                  \iImage = 3  ;Image index 3 (the 3rd icon - actually, it is the 4th icon, but that is another story!).
                  \iSelectedImage = 3
                EndWith
                For i = 0 To 1
                  tvi\hItem = GadgetItemID(#Tree, i)
                  SendMessage_(GadgetID(#TREE), #TVM_SETITEM, 0, tvi)
                Next
          EndSelect
      EndSelect
   Until Quit = 1 
EndIf 
End  


Re: TreeGadget Image change

Posted: Fri Mar 05, 2010 4:50 pm
by pb-user
srod,

thx for example code. I think better to keep the static icon. :)
Or I will use the explorer gadget.

Re: TreeGadget Image change

Posted: Fri Mar 05, 2010 8:51 pm
by srod
pb-user wrote:srod,

thx for example code. I think better to keep the static icon. :)
Or I will use the explorer gadget.
Your choice - I do this kind of thing quite often to good effect. :)