Page 1 of 1

Tree checked state from subclass

Posted: Sun Sep 26, 2010 1:22 pm
by LuaDev
I spent a few days searching the forum and trying different ways to catch the TreeGadget's 'Check' event from a window callback, the forum and code archive turned up no results so i thought i would share my final result for anyone else that also needed to do such

Code: Select all

#TVM_GETITEMSTATE = #TV_FIRST + 39
#TVM_MAPHTREEITEMTOACCID = #TV_FIRST + 43

Procedure TreeCallback(hWnd, uMsg, wParam, lParam) 
  
  Protected *nml.NM_TREEVIEW  
  
  If uMsg = #WM_NOTIFY
    
    *nml = lParam
    
    Select *nml\hdr\code

      Case #NM_CLICK
      
        tch.TC_HITTESTINFO
        GetCursorPos_(tch\pt)
        MapWindowPoints_(#Null,GadgetID(0),tch\pt,1)
        Protected ItenHandle = SendMessage_(GadgetID(0), #TVM_HITTEST,0, tch)
        
        If tch\flags = 64 ; check box
          
          ItemIndex = SendMessage_(GadgetID(0), #TVM_MAPHTREEITEMTOACCID, ItenHandle, 0)-1
          ItemState = SendMessage_(GadgetID(0), #TVM_GETITEMSTATE, ItenHandle, #TVIS_STATEIMAGEMASK)
          If ItemState >> 12 = 1
            
            Debug "item ("+Str(ItemIndex)+") checked"
            
          ElseIf ItemState >> 12 = 2
            
            Debug "item ("+Str(ItemIndex)+") unchecked"
            
          EndIf
          
        
        EndIf
                  
    EndSelect 
  EndIf 

  ProcedureReturn #PB_ProcessPureBasicEvents 
  
EndProcedure 


If OpenWindow(0, 0, 0, 180, 180, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  SetWindowCallback(@TreeCallback())
  
  TreeGadget(0, 10, 10, 160, 160, #PB_Tree_CheckBoxes)
  
  node=0
  For i=0 To 100
     AddGadgetItem (0, -1, "Item "+Str(i), 0, node)
     node+1
     If node = 9
       node=0
     EndIf
  Next

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

Re: Tree checked state from subclass

Posted: Sun Sep 26, 2010 3:26 pm
by srod
You can also change the check state by selecting the item and hitting the spacebar! :wink:

The code crashes here on Vista x86.

**EDIT : the crash is the purifier which says that there is stack corruption. Everything is okay when I disable the purifier, except for the spacebar business!

Re: Tree checked state from subclass

Posted: Sun Sep 26, 2010 6:12 pm
by LuaDev
crashes on Win 7 x86 with the purifier enabled as well

any idea why ?, and anything i can do to stop it ? (apart from disable the purifier)

Thanks for the space bar tip, i'll have to check the #NM_KEYDOWN as well

Re: Tree checked state from subclass

Posted: Mon Sep 27, 2010 1:02 pm
by srod
LuaDev wrote:crashes on Win 7 x86 with the purifier enabled as well

any idea why ?, and anything i can do to stop it ? (apart from disable the purifier)
Change TC_HITTESTINFO to the correct TV_HITTESTINFO. :wink:

btw, your code only works under XP onwards and then only when themes are enabled because of the #TVM_MAPHTREEITEMTOACCID message. Now you can avoid this message because PB actually stores item indexes in the item's lParam field of the TV_ITEM structure. Use the TVM_GETITEM message to retrieve this value.

I will also add that you might wish to have a look at my EasyVENT utility as this handles all of this stuff for you. :wink: Mind you, always something to be said about doing this kind of thing yourself.

Re: Tree checked state from subclass

Posted: Mon Sep 27, 2010 8:08 pm
by LuaDev
thanks for the tips, your a walking encyclopaedia of PB knowledge, how do you remember all this stuff

i have used EasyVENT in previous projects, its a great utility for sure, but as you said, there comes a time when you have to know this stuff for yourself, guess iv still lots to learn :(

Thanks again for your help

Re: Tree checked state from subclass

Posted: Mon Sep 27, 2010 8:15 pm
by srod
LuaDev wrote:thanks for the tips, your a walking encyclopaedia of PB knowledge, how do you remember all this stuff
I work with PB every day without exception. :)