From 27f1c8f4452c0935ce987bef766e9fcb25190573 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 Feb 2019 15:04:16 +0100 Subject: [PATCH] Fix wxRadioButton grouping in wxQt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create QButtonGroup as needed to honour wxRB_GROUP and wxRB_SINGLE styles. Co-Authored-By: Cătălin Răceanu Co-Authored-By: liamtreacy --- src/qt/radiobut.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/qt/radiobut.cpp b/src/qt/radiobut.cpp index 1a389997da..28af80b56c 100644 --- a/src/qt/radiobut.cpp +++ b/src/qt/radiobut.cpp @@ -12,6 +12,53 @@ #include "wx/qt/private/converter.h" #include +#include + +namespace +{ + +// Tiny helper applying a safe cast to return the handle of wxRadioButton as +// its actual type. +inline +QRadioButton* QtGetRadioButton(wxRadioButton* radioBtn) +{ + return static_cast(radioBtn->GetHandle()); +} + +void QtStartNewGroup(QRadioButton* qtRadioButton) +{ + // Note that the QButtonGroup created here will be deallocated when its + // parent // QRadioButton is destroyed. + QButtonGroup* qtButtonGroup = new QButtonGroup(qtRadioButton); + qtButtonGroup->addButton(qtRadioButton); +} + +bool QtTryJoiningExistingGroup(wxRadioButton* radioBtnThis) +{ + for ( wxWindow* previous = radioBtnThis->GetPrevSibling(); + previous; + previous = previous->GetPrevSibling() ) + { + if ( wxRadioButton* radioBtn = wxDynamicCast(previous, wxRadioButton) ) + { + // We should never join the exclusive group of wxRB_SINGLE button. + if ( !radioBtn->HasFlag(wxRB_SINGLE) ) + { + if ( QButtonGroup *qtGroup = QtGetRadioButton(radioBtn)->group() ) + { + qtGroup->addButton(QtGetRadioButton(radioBtnThis)); + return true; + } + } + + break; + } + } + + return false; +} + +} // anonymous namespace wxRadioButton::wxRadioButton() : m_qtRadioButton(NULL) @@ -42,7 +89,27 @@ bool wxRadioButton::Create( wxWindow *parent, m_qtRadioButton = new QRadioButton( parent->GetHandle() ); m_qtRadioButton->setText( wxQtConvertString( label )); - return QtCreateControl( parent, id, pos, size, style, validator, name ); + if ( !QtCreateControl(parent, id, pos, size, style, validator, name) ) + return false; + + // Check if we need to create a new button group: this must be done when + // explicitly requested to do so (wxRB_GROUP) but also for wxRB_SINGLE + // buttons to prevent them implicitly becoming part of an existing group. + if ( (style & wxRB_GROUP) || (style & wxRB_SINGLE) ) + { + QtStartNewGroup(m_qtRadioButton); + } + else + { + // Otherwise try to join an already existing group. Currently we don't + // do anything if joining it fails, i.e. if there is no current group + // yet, because the radio buttons not explicitly associated with some + // group still work as if they were part of one, so we just ignore the + // return value of this function. + QtTryJoiningExistingGroup(this); + } + + return true; } void wxRadioButton::SetValue(bool value)