Change TreeGadget Icon

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Change TreeGadget Icon

Post by eddy »

Code updated For 5.20+

Code: Select all

; //////////////////////////////////////////////
; TreeGadget Add Icon
; //////////////////////////////////////////////

Procedure.l AddTreeIcon(TreeID.l,Image.l)
  hItem=AddGadgetItem(TreeID,-1,"",Image)
  tvitem.TV_ITEM
  tvitem\hitem=hItem
  tvitem\mask=#TVIF_IMAGE
  SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem)
  
  
  res=tvitem\iImage   
  ProcedureReturn res
EndProcedure

; //////////////////////////////////////////////
; TreeGadget Change Icon
; //////////////////////////////////////////////

Procedure.l SetTreeIcon(TreeID.l,Index,IcoIndex.l)
  hItem=GadgetItemID(TreeID,Index)
  txt.S=Space(1000)
  tvitem.TV_ITEM
  tvitem\hitem=hItem
  tvitem\psztext=@txt
  tvitem\cchTextMax = 1000
  tvitem\mask=#TVIF_TEXT|#TVIF_IMAGE|#TVIF_HANDLE|#TVIF_SELECTEDIMAGE
  SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem)
  tvitem\iImage=IcoIndex
  tvitem\iSelectedImage=IcoIndex
  SendMessage_(GadgetID(TreeID), #TVM_SETITEM,0,@tvitem)     
EndProcedure

; //////////////////////////////////////////////
; Code Example
; //////////////////////////////////////////////

Enumeration
  #WIN
  #TREE
  #BUTTON
EndEnumeration

If OpenWindow(#WIN, 0, 0, 222, 222, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
  
  ;images
  img0=LoadImage(0, "Data\Drive.bmp")
  img1=LoadImage(1, "Data\CdPlayer.ico")
  
  ;tree gadget
  TreeGadget(#TREE, 0,0,111,222,#PB_Tree_AlwaysShowSelection)                       
  
  ;init tree gadget icon
  img0Index=AddTreeIcon(#TREE,img0)
  img1Index=AddTreeIcon(#TREE,img1)
  
  ;build tree
  AddGadgetItem(#TREE,-1,"1-Entry",img0)
  AddGadgetItem(#TREE,-1,"2-Entry",img1)
  AddGadgetItem(#TREE,-1,"3-Entry",img1)
  
  ButtonGadget(#BUTTON,120,5,80,20,"Change Icons")
  
  
  Repeat
    EventID.l = WaitWindowEvent()
    
    If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf
    
    If eventID = #PB_Event_Gadget
      If EventGadget()=#BUTTON
        If mode
          mode=0
          SetTreeIcon(#TREE,0,img0Index)
          SetTreeIcon(#TREE,1,img1Index)
          SetTreeIcon(#TREE,2,img1Index)
        Else   
          mode=1           
          SetTreeIcon(#TREE,0,img1Index)
          SetTreeIcon(#TREE,1,img0Index)
          SetTreeIcon(#TREE,2,img1Index)
        EndIf               
      EndIf
    EndIf
  Until Quit = 1
  
EndIf

End   ; All the opened windows are closed automatically by PureBasic


Last edited by eddy on Sat May 01, 2004 4:27 pm, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

oops !

Post by DominiqueB »

Thank's for the code,
just a litle things: line 28 contain 2 ||
that makes the compiler complains :wink:
Dominique

Windows 10 64bits. Pure basic 32bits
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

Ok I've fixed it.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

How do we remove an icon from a treenode?
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

You can remove an icon, there's blank area left.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Oh ok, so not a real removal then. Because once an icon is really removed, there is no blank area :)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

If there is one icon, all other items get the blank area. That is a Windows thing, no way around that.

Timo
quidquid Latine dictum sit altum videtur
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

No no, I mean, say you change an icon, etc, then try to remove that icon, from that tree node, once you do that, there is a blank space instead of no icon. So instead of really removing the icon, it replaces the icon with a blank icon.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks Eddy. Useful for me.
How to do same with Items text color and/or fonts?
Calloway
User
User
Posts: 10
Joined: Fri Jan 06, 2006 6:06 pm

Post by Calloway »

Hi!

I want to use this code in PB4. Can You just look over it and make a PB4 compatible version?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

; ////////////////////////////////////////////// 
; TreeGadget Add Icon 
; ////////////////////////////////////////////// 

Procedure.l AddTreeIcon(TreeID.l,Image.l) 
   hItem=AddGadgetItem(TreeID,-1,"",Image) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\mask=#TVIF_IMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   ClearGadgetItemList(TreeID) 
    
   res=tvitem\iImage    
   ProcedureReturn res 
EndProcedure 

; ////////////////////////////////////////////// 
; TreeGadget Change Icon 
; ////////////////////////////////////////////// 

Procedure.l SetTreeIcon(TreeID.l,Index,IcoIndex.l) 
   hItem=GadgetItemID(TreeID,Index) 
   txt.S=Space(1000) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\psztext=@txt 
   tvitem\cchTextMax = 1000 
   tvitem\mask=#TVIF_TEXT|#TVIF_IMAGE|#TVIF_HANDLE|#TVIF_SELECTEDIMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   tvitem\iImage=IcoIndex 
   tvitem\iSelectedImage=IcoIndex 
   SendMessage_(GadgetID(TreeID), #TVM_SETITEM,0,@tvitem)      
EndProcedure 

; ////////////////////////////////////////////// 
; Code Example 
; ////////////////////////////////////////////// 

Enumeration 
   #WIN 
   #TREE 
   #BUTTON 
EndEnumeration 

If OpenWindow(#WIN, 0, 0, 222, 222, "PureBasic Window",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 

   If CreateGadgetList(WindowID(#win)) 
      ;images. LOAD YOUR OWN HERE!
      img0=LoadImage(0, "new.ico") 
      img1=LoadImage(1, "open.ico") 

      ;tree gadget 
      TreeGadget(#TREE, 0,0,111,222,#PB_Tree_AlwaysShowSelection)                        
      
      ;init tree gadget icon 
      img0Index=AddTreeIcon(#TREE,img0) 
      img1Index=AddTreeIcon(#TREE,img1) 
      
      ;build tree 
      AddGadgetItem(#TREE,-1,"1-Entry",img0) 
      AddGadgetItem(#TREE,-1,"2-Entry",img1) 
      AddGadgetItem(#TREE,-1,"3-Entry",img1) 

      ButtonGadget(#BUTTON,120,5,80,20,"Change Icons") 
   EndIf 
    
   Repeat 
      EventID.l = WaitWindowEvent() 

      If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button 
         Quit = 1 
      EndIf 
      
      If eventID = #PB_Event_Gadget 
         If EventGadget()=#BUTTON 
            If mode 
               mode=0 
               SetTreeIcon(#TREE,0,img0Index) 
               SetTreeIcon(#TREE,1,img1Index) 
               SetTreeIcon(#TREE,2,img1Index) 
            Else    
               mode=1            
               SetTreeIcon(#TREE,0,img1Index) 
               SetTreeIcon(#TREE,1,img0Index) 
               SetTreeIcon(#TREE,2,img1Index) 
            EndIf                
         EndIf 
      EndIf 
   Until Quit = 1 
EndIf 
End   ; All the opened windows are closed automatically by PureBasic 
I may look like a mule, but I'm not a complete ass.
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

Calloway wrote:Hi!

I want to use this code in PB4. Can You just look over it and make a PB4 compatible version?
I just dropped it in "PBSourceConverter", hope it is ok.
PBSourceConverter V 1.0.8 wrote:EventGadgetID in Line 75 was renamed to EventGadget!
In Line 47 was in WindowID an parameter on position 1 added!
In Line 45 was OpenWindow Parameter 6 with Parameter 7 swapped!

Code: Select all

; Converted by PBSourceConverter on 21.08.2006 / 00:48
; ////////////////////////////////////////////// 
; TreeGadget Add Icon 
; ////////////////////////////////////////////// 

Procedure.l AddTreeIcon(TreeID.l,Image.l) 
   hItem=AddGadgetItem(TreeID,-1,"",Image) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\mask=#TVIF_IMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   ClearGadgetItemList(TreeID) 
    
   res=tvitem\iImage    
   ProcedureReturn res 
EndProcedure 

; ////////////////////////////////////////////// 
; TreeGadget Change Icon 
; ////////////////////////////////////////////// 

Procedure.l SetTreeIcon(TreeID.l,Index,IcoIndex.l) 
   hItem=GadgetItemID(TreeID,Index) 
   txt.S=Space(1000) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\psztext=@txt 
   tvitem\cchTextMax = 1000 
   tvitem\mask=#TVIF_TEXT|#TVIF_IMAGE|#TVIF_HANDLE|#TVIF_SELECTEDIMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   tvitem\iImage=IcoIndex 
   tvitem\iSelectedImage=IcoIndex 
   SendMessage_(GadgetID(TreeID), #TVM_SETITEM,0,@tvitem)      
EndProcedure 

; ////////////////////////////////////////////// 
; Code Example 
; ////////////////////////////////////////////// 

Enumeration 
   #WIN 
   #TREE 
   #BUTTON 
EndEnumeration 

If OpenWindow(#WIN, 0, 0, 222, 222, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 

   If CreateGadgetList(WindowID(#WIN)) 
      ;images 
      img0=LoadImage(0, "Data\Drive.bmp") 
      img1=LoadImage(1, "Data\CdPlayer.ico") 

      ;tree gadget 
      TreeGadget(#TREE, 0,0,111,222,#PB_Tree_AlwaysShowSelection)                        
      
      ;init tree gadget icon 
      img0Index=AddTreeIcon(#TREE,img0) 
      img1Index=AddTreeIcon(#TREE,img1) 
      
      ;build tree 
      AddGadgetItem(#TREE,-1,"1-Entry",img0) 
      AddGadgetItem(#TREE,-1,"2-Entry",img1) 
      AddGadgetItem(#TREE,-1,"3-Entry",img1) 

      ButtonGadget(#BUTTON,120,5,80,20,"Change Icons") 
   EndIf 
    
   Repeat 
      EventID.l = WaitWindowEvent() 

      If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button 
         Quit = 1 
      EndIf 
      
      If eventID = #PB_Event_Gadget 
         If EventGadget()=#BUTTON 
            If mode 
               mode=0 
               SetTreeIcon(#TREE,0,img0Index) 
               SetTreeIcon(#TREE,1,img1Index) 
               SetTreeIcon(#TREE,2,img1Index) 
            Else    
               mode=1            
               SetTreeIcon(#TREE,0,img1Index) 
               SetTreeIcon(#TREE,1,img0Index) 
               SetTreeIcon(#TREE,2,img1Index) 
            EndIf                
         EndIf 
      EndIf 
   Until Quit = 1 

EndIf 

End   ; All the opened windows are closed automatically by PureBasic 
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
Calloway
User
User
Posts: 10
Joined: Fri Jan 06, 2006 6:06 pm

Post by Calloway »

Thank You For Your Help.

The code runs now in PB4 without errors. But the result is not the same as in PB3.94. In PB3.94 the icons are changing every time I press the Change button. In PB4 the icons are only changing once and every time I press the change button the tree keeps its last icon and doesn't change anymore.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

This took a bit of figuring out!

The problem is that it is easy to switch icons between items of a tree gadget, but not quite so easy to add extra items to the Tree gadget's image list and then switch between them.

The problem with the above code seems to be that the ClearGadgetItemList() function now seems to destroy the image list. I've adjusted the code accordingly.

Code: Select all

; ////////////////////////////////////////////// 
; TreeGadget Add Icon 
;  Purebasic 4 version (adapted by srod).
; ////////////////////////////////////////////// 

Procedure.l AddTreeIcon(TreeID.l,Image.l) 
   hItem=AddGadgetItem(TreeID,-1,"",Image) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\mask=#TVIF_IMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   RemoveGadgetItem(TreeID,0) ;MADE THE CHANGE HERE!
   res=tvitem\iImage    
   ProcedureReturn res 
EndProcedure 

; ////////////////////////////////////////////// 
; TreeGadget Change Icon 
; ////////////////////////////////////////////// 

Procedure.l SetTreeIcon(TreeID.l,Index,IcoIndex.l) 
   hItem=GadgetItemID(TreeID,Index) 
   txt.S=Space(1000) 
   tvitem.TV_ITEM 
   tvitem\hitem=hItem 
   tvitem\psztext=@txt 
   tvitem\cchTextMax = 1000 
   tvitem\mask=#TVIF_TEXT|#TVIF_IMAGE|#TVIF_HANDLE|#TVIF_SELECTEDIMAGE 
   SendMessage_(GadgetID(TreeID), #TVM_GETITEM,0,@tvitem) 
   tvitem\iImage=IcoIndex 
   tvitem\iSelectedImage=IcoIndex 
   SendMessage_(GadgetID(TreeID), #TVM_SETITEM,0,@tvitem)      
EndProcedure 

; ////////////////////////////////////////////// 
; Code Example 
; ////////////////////////////////////////////// 

Enumeration 
   #WIN 
   #TREE 
   #BUTTON 
EndEnumeration 

If OpenWindow(#WIN, 0, 0, 222, 222, "PureBasic Window",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 

   If CreateGadgetList(WindowID(#win)) 
      ;images. LOAD YOUR OWN HERE! 
      img0=LoadImage(0, "new.ico") 
      img1=LoadImage(1, "open.ico") 
      img2=LoadImage(2, "save.ico")
      img3=LoadImage(3, "error.ico")

;tree gadget 
      TreeGadget(#TREE, 0,0,111,222,#PB_Tree_AlwaysShowSelection)                        
     
      ;init tree gadget icon. ADD YOUR OWN ICONS HERE!
      img0Index=AddTreeIcon(#TREE,img0) 
      img1Index=AddTreeIcon(#TREE,img1) 
      img2Index=AddTreeIcon(#TREE,img2) 
      img3Index=AddTreeIcon(#TREE,img3) 

      ;build tree 
      AddGadgetItem(#TREE,-1,"1-Entry",img0,0) 
      AddGadgetItem(#TREE,-1,"2-Entry",img1,0) 
      AddGadgetItem(#TREE,-1,"3-Entry",img2,0) 
      ButtonGadget(#BUTTON,120,5,80,20,"Change Icons") 
   EndIf 
    
   Repeat 
      EventID.l = WaitWindowEvent() 

      If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button 
         Quit = 1 
      EndIf 
      
      If eventID = #PB_Event_Gadget 
         If EventGadget()=#BUTTON 
            If mode 
               mode=0 
               SetTreeIcon(#TREE,0,img0Index) 
               SetTreeIcon(#TREE,1,img1Index) 
               SetTreeIcon(#TREE,2,img2Index) 
            Else    
               mode=1            
               SetTreeIcon(#TREE,0,img3Index) 
               SetTreeIcon(#TREE,1,img3Index) 
               SetTreeIcon(#TREE,2,img3Index) 
            EndIf                
         EndIf 
      EndIf 
   Until Quit = 1 
EndIf 
End   ; All the opened windows are closed automatically by PureBasic 
I may look like a mule, but I'm not a complete ass.
Calloway
User
User
Posts: 10
Joined: Fri Jan 06, 2006 6:06 pm

Post by Calloway »

Wow, thank You! That works! You made my day!
Post Reply