Automatic handling of ThreeState checkboxes

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Automatic handling of ThreeState checkboxes

Post by Michael Vogel »

Maybe there's a simple approach to handle checkboxes using the #PB_CheckBox_ThreeState flag, but I don't think so.
Wouldn't it be fine, if PB would be able to handle also three state boxes automatically?

Code: Select all

Procedure SetGadgetThreeState(Gadget,Value,*State.Integer=0)
	If Value=2
		Value=-1
	EndIf
	SetGadgetState(Gadget,Value)
	If *State
		*State\i=Value
	EndIf
	ProcedureReturn value
EndProcedure

*** Initialization ***
OptFatLineState=0
SetGadgetState(#OptFatLine,OptFatLineState)

*** Event-Handling ***
  Case #PB_Event_Gadget,#PB_Event_Menu
     Select EventGadget()
        :
	Case #OptFatLine
		OptFatLineState=(OptFatLineState+1)%3
		SetGadgetThreeState(#OptFatLine,OptFatLineState)

User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Automatic handling of ThreeState checkboxes

Post by Danilo »

Michael Vogel wrote:Wouldn't it be fine, if PB would be able to handle also three state boxes automatically?
Like the following on Windows?

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "CheckBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(1, 10,  10, 250, 20, "CheckBox three state", #BS_AUTO3STATE)
    Repeat
        Select WaitWindowEvent()
            Case #PB_Event_CloseWindow : End
            Case #PB_Event_Gadget      : Debug SendMessage_(GadgetID(1),#BM_GETSTATE,0,0)&3
        EndSelect
    ForEver
EndIf
Makes sense for cross-platform #PB_CheckBox_ThreeState.
BS_AUTO3STATE wrote:Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it.
The state cycles through checked, indeterminate, and cleared.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Automatic handling of ThreeState checkboxes

Post by Shardik »

PB help for CheckBoxGadget() wrote:By clicking on the checkbox, the user can bring it back to either the "on" or "off" state to apply this to all the items. Therefore the "inbetween" state can only be set by the program via SetGadgetState() and not by the user by clicking on the checkbox.
As a workaround for cycling through all 3 states when clicking onto the
CheckBoxGadget it's not necessary to use API functions. The following
example works cross-platform (tested on Windows 7 x86, Kubuntu 9.04,
Xubuntu 10.04 and MacOS X 10.6.8 ):

Code: Select all

OpenWindow(0, 270, 100, 270, 40, "Cycle through CheckBox states")
CheckBoxGadget(0, 10, 10, 250, 20, "3-state CheckBox", #PB_CheckBox_ThreeState)
CheckBoxState = 1

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        CheckBoxState = (CheckBoxState + 1) % 3

        If CheckBoxState = 2
          SetGadgetState(0, #PB_Checkbox_Inbetween)
        EndIf
      EndIf
  EndSelect
ForEver
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Automatic handling of ThreeState checkboxes

Post by Michael Vogel »

Danilo wrote:
Michael Vogel wrote:Wouldn't it be fine, if PB would be able to handle also three state boxes automatically?
Like the following on Windows?

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "CheckBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(1, 10,  10, 250, 20, "CheckBox three state", #BS_AUTO3STATE)
    Repeat
        Select WaitWindowEvent()
            Case #PB_Event_CloseWindow : End
            Case #PB_Event_Gadget      : Debug SendMessage_(GadgetID(1),#BM_GETSTATE,0,0)&3
        EndSelect
    ForEver
EndIf
Makes sense for cross-platform #PB_CheckBox_ThreeState.
BS_AUTO3STATE wrote:Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it.
The state cycles through checked, indeterminate, and cleared.
Exactly, thanks :)

PS: I believe, BM_GETCHECK and BM_SETCHECK should be used for windows
Post Reply