TreeGadget Image change

Just starting out? Need help? Post your questions and find answers here.
User avatar
pb-user
User
User
Posts: 10
Joined: Tue Mar 02, 2010 6:36 pm

TreeGadget Image change

Post 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 :?:
Pb-User

Windows 7 - 64bit - PureBasic 4.51
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: TreeGadget Image change

Post 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  

I may look like a mule, but I'm not a complete ass.
User avatar
pb-user
User
User
Posts: 10
Joined: Tue Mar 02, 2010 6:36 pm

Re: TreeGadget Image change

Post by pb-user »

srod,

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

Windows 7 - 64bit - PureBasic 4.51
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: TreeGadget Image change

Post 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. :)
I may look like a mule, but I'm not a complete ass.
Post Reply