ListIconGadget which item checked?

Just starting out? Need help? Post your questions and find answers here.
funnyguy
User
User
Posts: 69
Joined: Mon Jun 23, 2008 10:57 pm

ListIconGadget which item checked?

Post by funnyguy »

I have a list icon gadget with check box's... the problem is I want to know which item got checked or unchecked on the fly to avoid time consuming looping through the entire list.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

PB will send #PB_EventType_Change when you check/uncheck a box on a listicon gadget. You could record this 'on-the-fly' If you knew which item was affected but in native PB code I don't believe this is possible. (subject to correction of course, I could be wrong) On Windows, you would need to drop into API and send LVM_HITTEST to get this information when you receive a change event, then retrieve the checked state of that item:

Code: Select all

OpenWindow(0,0,0,640,320,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ListIconGadget(0,170,0,320,320,"stuff",315,#PB_ListIcon_CheckBoxes)
AddGadgetItem(0,-1, "stuff 0")
AddGadgetItem(0,-1, "stuff 1")
AddGadgetItem(0,-1, "stuff 2")
AddGadgetItem(0,-1, "stuff 3")
SetActiveGadget(0)
SetGadgetState(0,0)
Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_Gadget
    
      If EventGadget() = 0 And EventType() = #PB_EventType_Change
        GetCursorPos_(cp.POINT)
        MapWindowPoints_(0,GadgetID(0),cp,1)
        With hti.LVHITTESTINFO
          \pt\x = cp\x
          \pt\y = cp\y
          \flags = #LVHT_ONITEM
        EndWith
        SendMessage_(GadgetID(0),#LVM_HITTEST,0,hti)
        item = hti\iitem
        If GetGadgetItemState(0,item) & #PB_ListIcon_Checked 
          Debug "item "+Str(item)+" checked"
        Else
          Debug "item "+Str(item)+" unchecked"
        EndIf
      EndIf
      
  EndSelect
Until ev = #PB_Event_CloseWindow
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Here's another Windows only solution :

Code: Select all

Procedure.i WinCallback(hWnd, uMsg, wParam, lParam)
  Protected result = #PB_ProcessPureBasicEvents, *nmlv.NM_LISTVIEW
  Select uMsg
    Case #WM_NOTIFY
      *nmlv = lParam
      Select *nmlv\hdr\code
        Case #LVN_ITEMCHANGED
          If *nmlv\hdr\hWndFrom = GadgetID(0) And *nmlv\uOldState& #LVIS_STATEIMAGEMASK <> 0 And *nmlv\uNewState& #LVIS_STATEIMAGEMASK
            Select (*nmlv\uNewState& #LVIS_STATEIMAGEMASK)>>12
              Case 1
                Debug "Item " + Str(*nmlv\iItem) + " has been un-checked!"
              Case 2
                Debug "Item " + Str(*nmlv\iItem) + " has been checked!"
            EndSelect
          EndIf
      EndSelect
  EndSelect
  ProcedureReturn result
EndProcedure

OpenWindow(0,0,0,640,320,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
SetWindowCallback(@WinCallback())
ListIconGadget(0,170,0,320,320,"stuff",315,#PB_ListIcon_CheckBoxes) 
AddGadgetItem(0,-1, "stuff 0") 
AddGadgetItem(0,-1, "stuff 1") 
AddGadgetItem(0,-1, "stuff 2") 
AddGadgetItem(0,-1, "stuff 3") 
SetActiveGadget(0) 
SetGadgetState(0,0) 
Repeat 
  ev = WaitWindowEvent() 
Until ev = #PB_Event_CloseWindow 
I may look like a mule, but I'm not a complete ass.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

On this subject, how can I make a ListIcon checkbox become checked or
unchecked if I just click the text next to it, rather than the checkbox itself?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Trap the mouse click and use SetGadgetItemState().
I may look like a mule, but I'm not a complete ass.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Of course! (Slaps forehead). :oops:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply