Unchecking checkboxes using code (solved)

Just starting out? Need help? Post your questions and find answers here.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Unchecking checkboxes using code (solved)

Post by Little John »

Hi all,

we can check the checkboxes in a ListIconGadget using code like this:

Code: Select all

SetGadgetItemState(#ListIcon, item, #PB_ListIcon_Checked)
My problem is, that I couldn't find a corresponding constant #PB_ListIcon_UnChecked in PB 4.40 Beta 3.

It seems to me, that we can use

Code: Select all

SetGadgetItemState(#ListIcon, item, 0)
for unchecking checkboxes in a ListItemGadget.
Is it safe and recommended by the PureBasic team to use '0' for this purpose?

If the answer is "yes", then a constant such as

Code: Select all

#PB_ListIcon_UnChecked = 0
should be added to the compiler and to the docs.

Regards, Little John
Last edited by Little John on Tue Sep 29, 2009 7:52 pm, edited 3 times in total.
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: Missing predefined constant for unchecking checkboxes

Post by eJan »

You can do with '~':

Code: Select all

SetGadgetItemState(#ListIcon, item, ~#PB_ListIcon_Checked)
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Missing predefined constant for unchecking checkboxes

Post by srod »

Or... to preserve all other states (e.g. if the item is selected as part of a multi-select listicon) :

Code: Select all

SetGadgetItemState(0, item, GetGadgetItemState(0, item)&~#PB_ListIcon_Checked)
I may look like a mule, but I'm not a complete ass.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Missing predefined constant for unchecking checkboxes

Post by Little John »

Bingo! :idea:
Thanks a lot, guys!

Happy John :D
USCode
Addict
Addict
Posts: 924
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: Missing predefined constant for unchecking checkboxes

Post by USCode »

eJan wrote:You can do with '~':

Code: Select all

SetGadgetItemState(#ListIcon, item, ~#PB_ListIcon_Checked)
Is this cross-platform?

For clarity and consistency, it seems to me there SHOULD be a #PB_ListIcon_Unchecked constant defined and this should be requested as an enhancement for 4.40.
I'll do that.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Missing predefined constant for unchecking checkboxes

Post by freak »

eJan wrote:You can do with '~':

Code: Select all

SetGadgetItemState(#ListIcon, item, ~#PB_ListIcon_Checked)
This is very wrong. Basically you set every state bit on the item except the checked one. So you are setting #PB_ListIcon_Selected with this for example without knowing.

srod's way is the correct one if you want to just toggle the checked state (and preserve the others). If you want to remove all states (including the selected state) then just assign a 0 value.
quidquid Latine dictum sit altum videtur
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Missing predefined constant for unchecking checkboxes

Post by Little John »

freak wrote:srod's way is the correct one if you want to just toggle the checked state (and preserve the others).
Yes, I understand now that it works bitwise.

However, the following small program does not behave as expected (tested with PB 4.31 and PB 4.40 Beta 3 on Windows XP).

Code: Select all

; Window Constants
Enumeration
   #WinMain
EndEnumeration

; Gadget Constants
Enumeration
   #ListIcon
   #BtnCheck
   #BtnSelect
   #BtnUnCheck
   #BtnUnSelect
EndEnumeration

Define Event

If OpenWindow(#WinMain, 100, 100, 220, 160, "Test") = 0
   MessageRequester("Error", "OpenWindow() failed.")
   End
EndIf

ListIconGadget(#ListIcon, 5, 5, 210, 70, "", 30, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect)
AddGadgetColumn(#ListIcon, 1, "", 85)
AddGadgetColumn(#ListIcon, 2, "", 85)
AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Mary" + #LF$ + "Meyer")

ButtonGadget(#BtnCheck,     10, 90, 90, 25, "Check item")
ButtonGadget(#BtnSelect,   120, 90, 90, 25, "Select item")
ButtonGadget(#BtnUnCheck,   10, 120, 90, 25, "Uncheck item")
ButtonGadget(#BtnUnSelect, 120, 120, 90, 25, "Unselect item")

Repeat
   Event = WaitWindowEvent()
   Select Event
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #BtnCheck
               SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) | #PB_ListIcon_Checked)
            Case #BtnSelect
               SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) | #PB_ListIcon_Selected)
            Case #BtnUnCheck
               SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) & ~#PB_ListIcon_Checked)
            Case #BtnUnSelect
               SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) & ~#PB_ListIcon_Selected)
         EndSelect
   EndSelect
Until Event = #PB_Event_CloseWindow
There are problems concerning the selection:
Clicking at the button "Select item" has no effect.
When I have manually selected the item, clicking at the buttons "Check item" or "Unheck item" makes the selection disappear.

What am I doing wrong here?

TIA, Little John
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Unchecking checkboxes using code

Post by srod »

Either use the #PB_ListIcon_AlwaysShowSelection flag or ensure that you give the listicon the focus when you wish to show the selection with SetActiveGadget().
I may look like a mule, but I'm not a complete ass.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Unchecking checkboxes using code

Post by Little John »

srod wrote:Either use the #PB_ListIcon_AlwaysShowSelection flag or ensure that you give the listicon the focus when you wish to show the selection with SetActiveGadget().
Thanks, srod!

I'm a little reluctant to use SetActiveGadget() for this purpose, because I think that would mess up the focus, wouldn't it?

Well, now that you mention the #PB_ListIcon_AlwaysShowSelection flag ... I think I should run a memory test -- not on my PC, but on my brain. ;-)

Now I recall, that yesterday I tried using that flag, but I didn't see any effect. It was on a PC with a LCD monitor.
Using #PB_ListIcon_AlwaysShowSelection now on a PC with an oldfashioned cathode ray tube, I perceive a very light grey always shown selection. :x Can we change the color of the always shown selection?

Regards, Little John
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Unchecking checkboxes using code (solved)

Post by WilliamL »

This thread turned out to be very interesting!

So the final answer is:
SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) | #PB_ListIcon_Checked)
SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) | #PB_ListIcon_Selected)
SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) & ~#PB_ListIcon_Checked)
SetGadgetItemState(#ListIcon,0, GetGadgetItemState(#ListIcon,0) & ~#PB_ListIcon_Selected)

Maybe the manual could include this info?
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Unchecking checkboxes using code (solved)

Post by Little John »

Now, for making the selected item better visible when the ListItem gadget is inactive, I changed the code so that it colorizes the selected line itself. Here it is crucial not to use the flag #PB_ListIcon_AlwaysShowSelection.

Regards, Little John

Code: Select all

; PB 4.40 Beta 3

; Window Constants
Enumeration
   #WinMain
EndEnumeration

; Gadget Constants
Enumeration
   #ListIcon
   #BtnCheckAll
   #BtnUnCheckAll
EndEnumeration

;-----------------------------------------------------------------------
#SelForeColor = $FFFFFF   ; custom foreground color for selection, when ListIcon gadget is inactive
#SelBackColor = $FF8080   ; custom background color for selection, when ListIcon gadget is inactive

Procedure ShowSelection (activeGadget=-1)
   Static curGadget, newGadget, selected

   If activeGadget <> -1             ; initialization
      curGadget = activeGadget
   Else
      newGadget = GetActiveGadget()
      If curGadget <> newGadget      ; if the focus has changed
         If curGadget = #ListIcon
            selected = GetGadgetState(#ListIcon)
            SetGadgetItemColor(#ListIcon, selected, #PB_Gadget_FrontColor, #SelForeColor, -1)
            SetGadgetItemColor(#ListIcon, selected, #PB_Gadget_BackColor,  #SelBackColor, -1)
         ElseIf newGadget = #ListIcon
            SetGadgetItemColor(#ListIcon, selected, #PB_Gadget_FrontColor, -1, -1)
            SetGadgetItemColor(#ListIcon, selected, #PB_Gadget_BackColor,  -1, -1)
         EndIf
         curGadget = newGadget
      EndIf
   EndIf
EndProcedure
;-----------------------------------------------------------------------

Define Event, LastItem, i

If OpenWindow(#WinMain, 100, 100, 250, 170, "Un/check and custom selection") = 0
   MessageRequester("Error", "OpenWindow() failed.")
   End
EndIf

ListIconGadget(#ListIcon, 5, 5, 240, 110, "", 30, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect)
AddGadgetColumn(#ListIcon, 1, "", 100)
AddGadgetColumn(#ListIcon, 2, "", 100)

ButtonGadget(#BtnCheckAll,    10, 130, 100, 25, "Check all")
ButtonGadget(#BtnUnCheckAll, 140, 130, 100, 25, "Uncheck all")

AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Peter" + #LF$ + "Smith")
AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Mary"  + #LF$ + "Meyer")
AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Jolly" + #LF$ + "Jumper")

LastItem = CountGadgetItems(#ListIcon) - 1

SetActiveGadget(#ListIcon)
SetGadgetState(#ListIcon, 0)
ShowSelection(#ListIcon)

Repeat
   Event = WaitWindowEvent()
   ShowSelection()

   Select Event
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #BtnCheckAll
               For i = 0 To LastItem
                  SetGadgetItemState(#ListIcon,i, GetGadgetItemState(#ListIcon,i) | #PB_ListIcon_Checked)
               Next

            Case #BtnUnCheckAll
               For i = 0 To LastItem
                  SetGadgetItemState(#ListIcon,i, GetGadgetItemState(#ListIcon,i) & ~#PB_ListIcon_Checked)
               Next
         EndSelect
   EndSelect
Until Event = #PB_Event_CloseWindow
Post Reply