Add wxRadioButton group support to wxGenericValidator

Enable wxGenericValidator(int*) to work with wxRadioButton groups
when applied to the first element of the group.  The value will be
the index of the selected button within the group.

Closes #24264.
This commit is contained in:
Bill Su 2024-01-28 16:13:57 -05:00 committed by Vadim Zeitlin
parent 91de9867ee
commit 74f88f9131
3 changed files with 66 additions and 1 deletions

View file

@ -76,6 +76,10 @@ public:
// Called to transfer data to the window // Called to transfer data to the window
virtual bool TransferFromWindow() override; virtual bool TransferFromWindow() override;
// Called when the validator is associated with a window, is used to check
// that the validator is not being associated with a wrong window.
virtual void SetWindow(wxWindow* win) override;
protected: protected:
void Initialize(); void Initialize();

View file

@ -25,6 +25,9 @@
A wxLB_SINGLE wxListBox can also use an int. wxColourPickerCtrl support. A wxLB_SINGLE wxListBox can also use an int. wxColourPickerCtrl support.
A 3-state wxCheckBox can use wxCheckBoxState. A 3-state wxCheckBox can use wxCheckBoxState.
@since 3.3.0
A group of wxRadioButton controls can also be associated with an int.
For more information, please see @ref overview_validator. For more information, please see @ref overview_validator.
@library{wxcore} @library{wxcore}
@ -79,7 +82,7 @@ public:
wxGenericValidator(int* valPtr); wxGenericValidator(int* valPtr);
/** /**
Constructor taking a wxArrayInt pointer. This will be used for Constructor taking a wxArrayInt pointer. This will be used for
wxListBox, wxCheckListBox. wxListBox, wxCheckListBox, and groups of wxRadioButton.
@param valPtr @param valPtr
A pointer to a variable that contains the value. This variable A pointer to a variable that contains the value. This variable

View file

@ -172,6 +172,26 @@ bool wxGenericValidator::TransferToWindow()
pControl->SetValue(*m_pBool) ; pControl->SetValue(*m_pBool) ;
return true; return true;
} }
else if (m_pInt)
{
const auto last = pControl->GetLastInGroup();
for (int i = 0 ; ; ++i)
{
if (i == *m_pInt)
{
pControl->SetValue(true);
return true;
}
if (pControl == last)
{
wxFAIL_MSG("value out of range or not enough radio buttons");
return false;
}
pControl = pControl->GetNextInGroup();
}
}
} else } else
#endif #endif
@ -499,6 +519,29 @@ bool wxGenericValidator::TransferFromWindow()
*m_pBool = pControl->GetValue() ; *m_pBool = pControl->GetValue() ;
return true; return true;
} }
else if (m_pInt)
{
const auto last = pControl->GetLastInGroup();
for (int i = 0 ; ; ++i)
{
if (pControl->GetValue())
{
*m_pInt = i;
return true;
}
if (pControl == last)
{
// This really should never happen.
wxFAIL_MSG("no selected radio button?");
*m_pInt = wxNOT_FOUND;
return false;
}
pControl = pControl->GetNextInGroup();
}
}
} else } else
#endif #endif
#if wxUSE_TOGGLEBTN #if wxUSE_TOGGLEBTN
@ -795,6 +838,21 @@ bool wxGenericValidator::TransferFromWindow()
return false; return false;
} }
void wxGenericValidator::SetWindow(wxWindow* win)
{
#if wxUSE_RADIOBTN
if (m_pInt)
{
if (wxRadioButton* pControl = wxDynamicCast(win, wxRadioButton))
{
wxCHECK_RET(pControl == pControl->GetFirstInGroup(),
"wxRadioButton group validator must be on first item in group");
}
}
#endif
wxValidator::SetWindow(win);
}
/* /*
Called by constructors to initialize ALL data members Called by constructors to initialize ALL data members
*/ */