Page 2 of 2

Re: GetGadgetItemState and ListIconGadget

Posted: Fri Mar 07, 2025 6:28 pm
by HeX0R
So windows only?

Code: Select all

EnableExplicit

Import "Comctl32.lib" 
  SetWindowSubclass(hWnd, *fnSubclass, uIdSubclass, dwRefData)
  GetWindowSubclass(hWnd, *fnSubclass, uIdSubclass, *dwRefData)
  RemoveWindowSubclass(hWnd, *fnSubclass, uIdSubclass)
  DefSubclassProc(hWnd, uMsg, wParam, lParam)
EndImport 

Structure _DATA_
	window.i
	gadget.i
EndStructure

#Type_Checkbox_Changed = #PB_EventType_FirstCustomValue

Define i

Procedure LVProc(hwnd, msg, wparam, lparam, uIdSubclass, *d._DATA_)
	Protected hitinfo.LVHITTESTINFO

	Select msg
		Case #WM_NCDESTROY
			RemoveWindowSubclass(hWnd, @LVProc(), uIdSubclass)
			FreeStructure(*d)
		Case #WM_LBUTTONDOWN
			With hitinfo
				\pt\x = lparam & $FFFF
				\pt\y = lparam >> 16
			EndWith
			SendMessage_(hwnd, #LVM_SUBITEMHITTEST, 0, @hitInfo)
			PostEvent(#PB_Event_Gadget, *d\window, *d\gadget, #Type_Checkbox_Changed, hitinfo\iItem)
	EndSelect

	ProcedureReturn DefSubclassProc(hwnd, msg, wparam, lparam)
EndProcedure

Procedure AddSpecialListIconHandling(ParentWindow.i, ListIconGadget.i)
	Protected *d._DATA_
	
	*d         = AllocateStructure(_DATA_)
	*d\window  = ParentWindow
	*d\gadget  = ListIconGadget
	SetWindowSubclass(GadgetID(ListIconGadget), @LVProc(), 1, *d)
EndProcedure
	

OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0, 10, 10, 480, 380, "Column 1", 100, #PB_ListIcon_GridLines | #PB_ListIcon_CheckBoxes)
For i = 0 To 25
	AddGadgetItem(0, - 1, RSet(Chr(i + 65), 8, Chr(i + 65)))
Next i

AddSpecialListIconHandling(0, 0)


Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			Break
		Case #PB_Event_Gadget
			Select EventGadget()
				Case 0
					Select EventType()
						Case #Type_Checkbox_Changed
							i = EventData()
							If GetGadgetItemState(0, i) & #PB_ListIcon_Checked
								Debug "Checkbox #" + Str(i) + " => ticked!"
							Else
								Debug "Checkbox #" + Str(i) + " => unticked!"
							EndIf
						Case #PB_EventType_Change
							Debug "Selected item changed? => " + Str(GetGadgetState(0))
					EndSelect
			EndSelect
	EndSelect
ForEver

Re: GetGadgetItemState and ListIconGadget

Posted: Fri Mar 07, 2025 9:22 pm
by jak64
Thanks mk-soft for your code

Re: GetGadgetItemState and ListIconGadget

Posted: Fri Mar 07, 2025 9:25 pm
by jak64
Thanks HeX0R,
It works great

Re: GetGadgetItemState and ListIconGadget

Posted: Sat Mar 08, 2025 12:24 am
by ChrisR
Nice example of subclass HeXOR
Works well if you click on the 1st column, but not if you click on the empty "2nd" column.
Looks good with hitinfo\pt\x = 0, by deleting or commenting the line:

Code: Select all

;\pt\x = lparam & $FFFF

Re: GetGadgetItemState and ListIconGadget

Posted: Sat Mar 08, 2025 9:27 am
by HeX0R
the "empy" column is in fact outside the defined area (there is no second column), so from my point of view the code works correct.
No idea why you want to catch clicks outside of the defined area, but sure, you can fool it by setting x position to 0.

Re: GetGadgetItemState and ListIconGadget

Posted: Sat Mar 08, 2025 11:40 am
by ChrisR
I understood well for the drawing area, although it appears as a 2nd column for the ListIcon. So, it is probably better not to send a Postevent if hitinfo\iItem = -1 (rather than \pt\x =0) to not receive a #Type_Checkbox_Changed msg, which can be confusing in this case.