Listicongadget with custom checkboxes

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Listicongadget with custom checkboxes

Post by Mesa »

Code: Select all

;
;Mesa February 2022


Enumeration imagecheckbox
	#imCheckNo
	#imCheckYes
	#imCheckNoGray
	#imCheckYesGray
EndEnumeration

Enumeration gadget
	#listicon
	#btnlock
EndEnumeration

CreateImage(#imCheckNo,16,16,24,#White)
StartDrawing(ImageOutput(#imCheckNo))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1,1,15,15,#Black)
StopDrawing()

CreateImage(#imCheckYes,16,16,24,#White)
StartDrawing(ImageOutput(#imCheckYes))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1,1,15,15,#Black)
LineXY(1,1,15,15,#Black)
LineXY(15,1,1,15,#Black)
StopDrawing()

CreateImage(#imCheckNoGray,16,16,24,#Gray)
StartDrawing(ImageOutput(#imCheckNoGray))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1,1,15,15,#Black)
StopDrawing()

CreateImage(#imCheckYesGray,16,16,24,#Gray)
StartDrawing(ImageOutput(#imCheckYesGray))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1,1,15,15,#Black)
LineXY(1,1,15,15,#Black)
LineXY(15,1,1,15,#Black)
StopDrawing()

Global Dim checkstate(1)
ItemHeight = 18 ; Change this value if you change the fontsize of the listicongadget

If OpenWindow(0, 100, 100, 600, 200, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ListIconGadget(#listicon, 40, 5, 470, 140, "Name", 100)
	
	AddGadgetColumn(#listicon, 1, "Address", 250)
	
	margin$=" "
	For i=0 To 50
		AddGadgetItem(#listicon, -1, margin$+"Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay",ImageID(#imCheckNo))
		AddGadgetItem(#listicon, -1, margin$+"Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity",ImageID(#imCheckNo))
	Next i
	
	ReDim checkstate(CountGadgetItems(#listicon))
	ButtonGadget(#btnlock, 20 , 160  , 100, 30, "Lock",#PB_Button_Toggle  ) 
	
	gadgetx = GadgetX(#listicon,#PB_Gadget_ScreenCoordinate)
	gadgety = GadgetY(#listicon,#PB_Gadget_ScreenCoordinate) 
	
	AddWindowTimer(0,125,10)
	
	Repeat
		
		Select WaitWindowEvent(1)
				
			Case #PB_Event_CloseWindow
				Quit = 1
				
			Case #PB_Event_Timer 
				If flag = 1
					checkx = DesktopMouseX()
					checky = DesktopMouseY()
					If checkx > gadgetx And checkx < (gadgetx+ItemHeight)
						item = Int((checky - gadgety - ItemHeight)/ItemHeight)
						itemstste = GetGadgetItemState(#listicon,item)      
						checkstate(item) = checkstate(item)!1
						If checkstate(item) = #PB_ListIcon_Selected 
							count + 1
							SetGadgetItemImage(#listicon, item, ImageID(#imCheckYes))
						Else
							count - 1
							SetGadgetItemImage(#listicon, item, ImageID(#imCheckNo))
						EndIf
					EndIf
					If count <> oldcount
						Debug "Item : "+Str(item)+" Checkbox state has changed "
					EndIf        
					oldcount = count
					flag = 0
				EndIf    
				
			Case #PB_Event_Gadget
				
				Select EventGadget()
					Case #listicon				
						Select EventType()                   
							Case #PB_EventType_LeftClick
								If GetGadgetState(#btnlock)=0	
									; if not locked	
									flag = 1
								EndIf
						EndSelect 						
						
					Case  #btnlock
						;If this button is toggled, it's not allowed to check or uncheck anything and checkboxes are grayed
						;else checkboxes are enabled and white
						Select EventType()
							Case #PB_EventType_LeftClick
								
								If GetGadgetState(#btnlock)=1
									im0=#imCheckNoGray
									im1=#imCheckYesGray
								Else
									im0=#imCheckNo
									im1=#imCheckYes
								EndIf
								
								For i=0 To CountGadgetItems(#listicon) -1
									If checkstate(i)=0
										SetGadgetItemImage(#listicon, i, ImageID(im0))
									Else
										SetGadgetItemImage(#listicon, i, ImageID(im1))
									EndIf
								Next i
								
						EndSelect					
						
				EndSelect
				
		EndSelect
		
	Until Quit = 1
EndIf
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: Listicongadget with custom checkboxes

Post by BarryG »

When I click a checkbox, the checkbox immediately below it gets the cross in it.
User avatar
ChrisR
Addict
Addict
Posts: 1150
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Listicongadget with custom checkboxes

Post by ChrisR »

Same for me !
Seems to work well with the state saved in GadgetItemData

Code: Select all

;
;Mesa February 2022


Enumeration imagecheckbox
  #imCheckNo
  #imCheckYes
  #imCheckNoGray
  #imCheckYesGray
EndEnumeration

Enumeration gadget
  #listicon
  #btnlock
EndEnumeration

CreateImage(#imCheckNo, 16, 16, 24, #White)
StartDrawing(ImageOutput(#imCheckNo))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1, 1, 15, 15, #Black)
StopDrawing()

CreateImage(#imCheckYes, 16, 16, 24, #White)
StartDrawing(ImageOutput(#imCheckYes))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1, 1, 15, 15, #Black)
LineXY(1, 1, 15, 15, #Black)
LineXY(15, 1, 1, 15, #Black)
StopDrawing()

CreateImage(#imCheckNoGray, 16, 16, 24, #Gray)
StartDrawing(ImageOutput(#imCheckNoGray))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1, 1, 15, 15, #Black)
StopDrawing()

CreateImage(#imCheckYesGray, 16, 16, 24, #Gray)
StartDrawing(ImageOutput(#imCheckYesGray))
DrawingMode(#PB_2DDrawing_Outlined )
Box(1, 1, 15, 15, #Black)
LineXY(1, 1, 15, 15, #Black)
LineXY(15, 1, 1, 15, #Black)
StopDrawing()

ItemHeight = 18 ; Change this value if you change the fontsize of the listicongadget

If OpenWindow(0, 100, 100, 600, 200, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListIconGadget(#listicon, 40, 5, 470, 140, "Name", 100)
  
  AddGadgetColumn(#listicon, 1, "Address", 250)
  
  margin$ = " "
  For i = 0 To 50
    AddGadgetItem(#listicon, -1, margin$ + "Harry Rannit" + Chr(10) + "12 Parliament Way, Battle Street, By the Bay", ImageID(#imCheckNo))
    AddGadgetItem(#listicon, -1, margin$ + "Ginger Brokeit" + Chr(10) + "130 PureBasic Road, BigTown, CodeCity", ImageID(#imCheckNo))
    SetGadgetItemData(#listicon, -1, #False)
  Next i
  
  ButtonGadget(#btnlock, 20 , 160  , 100, 30, "Lock", #PB_Button_Toggle  )
  
  Repeat
    
    Select WaitWindowEvent(1)
        
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
          Case #listicon
            Select EventType()
              Case #PB_EventType_LeftClick
                If GetGadgetState(#btnlock) = 0   ; If Unlock
                  If GetGadgetItemData(#listicon, GetGadgetState(#listicon))
                    SetGadgetItemImage(#listicon, GetGadgetState(#listicon), ImageID(#imCheckNo))
                    SetGadgetItemData(#listicon, GetGadgetState(#listicon), #False)
                  Else
                    SetGadgetItemImage(#listicon, GetGadgetState(#listicon), ImageID(#imCheckYes))
                    SetGadgetItemData(#listicon, GetGadgetState(#listicon), #True)
                  EndIf
                EndIf
            EndSelect
            
          Case  #btnlock
						;If this button is toggled, it's not allowed to check or uncheck anything and checkboxes are grayed
						;else checkboxes are enabled and white
            Select EventType()
              Case #PB_EventType_LeftClick
                If GetGadgetState(#btnlock) = 1
                  SetGadgetText(#btnlock, "Unlock")
                  im0 = #imCheckNoGray
                  im1 = #imCheckYesGray
                Else
                  SetGadgetText(#btnlock, "Lock")
                  im0 = #imCheckNo
                  im1 = #imCheckYes
                EndIf
                
                For i = 0 To CountGadgetItems(#listicon) - 1
                  If GetGadgetItemData(#listicon, i)
                    SetGadgetItemImage(#listicon, i, ImageID(im1))
                  Else
                    SetGadgetItemImage(#listicon, i, ImageID(im0))
                  EndIf
                Next i
                
            EndSelect
            
        EndSelect
        
    EndSelect
    
  Until Quit = 1
EndIf
Post Reply