Page 1 of 1

Unchecking checkboxes using code (solved)

Posted: Sun Sep 27, 2009 6:53 pm
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

Re: Missing predefined constant for unchecking checkboxes

Posted: Sun Sep 27, 2009 7:41 pm
by eJan
You can do with '~':

Code: Select all

SetGadgetItemState(#ListIcon, item, ~#PB_ListIcon_Checked)

Re: Missing predefined constant for unchecking checkboxes

Posted: Sun Sep 27, 2009 7:45 pm
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)

Re: Missing predefined constant for unchecking checkboxes

Posted: Sun Sep 27, 2009 8:09 pm
by Little John
Bingo! :idea:
Thanks a lot, guys!

Happy John :D

Re: Missing predefined constant for unchecking checkboxes

Posted: Mon Sep 28, 2009 6:31 pm
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.

Re: Missing predefined constant for unchecking checkboxes

Posted: Mon Sep 28, 2009 7:57 pm
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.

Re: Missing predefined constant for unchecking checkboxes

Posted: Tue Sep 29, 2009 6:16 am
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

Re: Unchecking checkboxes using code

Posted: Tue Sep 29, 2009 10:19 am
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().

Re: Unchecking checkboxes using code

Posted: Tue Sep 29, 2009 11:35 am
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

Re: Unchecking checkboxes using code (solved)

Posted: Wed Sep 30, 2009 9:01 pm
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?

Re: Unchecking checkboxes using code (solved)

Posted: Sun Oct 04, 2009 7:58 am
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