mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-09-26 18:18:26 +08:00
Add persistence support for groups of wxRadioButton controls
Unix builds / Ubuntu 24.04 wxGTK with ASAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 compatible 3.0 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK UTF-8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxQt (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxX11 (push) Has been cancelled
Unix builds / Ubuntu 20.04 wxGTK 3 with clang (push) Has been cancelled
Unix builds / Ubuntu 22.04 wxGTK with wx containers (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK with UBSAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxDFB (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 static with gcc 4.8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 2 (push) Has been cancelled
CMake builds / Ubuntu 22.04 wxGTK 3 (push) Has been cancelled
CMake builds / Windows MSVC (push) Has been cancelled
CMake builds / macOS latest wxOSX Ninja (push) Has been cancelled
CMake builds / macOS 14 wxOSX Xcode (push) Has been cancelled
CMake builds / macOS 14 wxIOS (push) Has been cancelled
CMake builds / Windows wxQt 6.8 (push) Has been cancelled
Mac builds / wxMac Universal C++14 (push) Has been cancelled
Mac builds / wxMac ARM with ASAN (push) Has been cancelled
Mac builds / wxiOS (push) Has been cancelled
Mac builds / wxMac Intel C++17 (push) Has been cancelled
Mac Xcode builds / iOS Simulator static (push) Has been cancelled
Mac Xcode builds / macOS dynamic Release (push) Has been cancelled
Mac Xcode builds / iOS static Debug (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Debug x64 (push) Has been cancelled
MSW builds / wxMSW vs2019 DLL Release x64 (push) Has been cancelled
MSW builds / wxMSW vs2019 Debug Win32 (push) Has been cancelled
MSW builds / wxMSW vs2022 Release arm64 (push) Has been cancelled
MSW builds / msw-msys2-clang (push) Has been cancelled
MSW cross-builds / wxMSW 64 bits (push) Has been cancelled
MSW cross-builds / wxMSW/Univ (push) Has been cancelled
MSW cross-builds / wxMSW 32 bits (push) Has been cancelled
Code Checks / Check Spelling (push) Has been cancelled
Code Checks / Check Whitespace (push) Has been cancelled
Code Checks / Check Mixed EOL (push) Has been cancelled
Code Checks / Check C++ Style (push) Has been cancelled
Code Checks / Check All Headers In allheaders.h (push) Has been cancelled
Update Documentation / Update Online Documentation (push) Has been cancelled
No Response / no-response (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK with ASAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 compatible 3.0 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK UTF-8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxQt (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxX11 (push) Has been cancelled
Unix builds / Ubuntu 20.04 wxGTK 3 with clang (push) Has been cancelled
Unix builds / Ubuntu 22.04 wxGTK with wx containers (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK with UBSAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxDFB (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 static with gcc 4.8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 2 (push) Has been cancelled
CMake builds / Ubuntu 22.04 wxGTK 3 (push) Has been cancelled
CMake builds / Windows MSVC (push) Has been cancelled
CMake builds / macOS latest wxOSX Ninja (push) Has been cancelled
CMake builds / macOS 14 wxOSX Xcode (push) Has been cancelled
CMake builds / macOS 14 wxIOS (push) Has been cancelled
CMake builds / Windows wxQt 6.8 (push) Has been cancelled
Mac builds / wxMac Universal C++14 (push) Has been cancelled
Mac builds / wxMac ARM with ASAN (push) Has been cancelled
Mac builds / wxiOS (push) Has been cancelled
Mac builds / wxMac Intel C++17 (push) Has been cancelled
Mac Xcode builds / iOS Simulator static (push) Has been cancelled
Mac Xcode builds / macOS dynamic Release (push) Has been cancelled
Mac Xcode builds / iOS static Debug (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Debug x64 (push) Has been cancelled
MSW builds / wxMSW vs2019 DLL Release x64 (push) Has been cancelled
MSW builds / wxMSW vs2019 Debug Win32 (push) Has been cancelled
MSW builds / wxMSW vs2022 Release arm64 (push) Has been cancelled
MSW builds / msw-msys2-clang (push) Has been cancelled
MSW cross-builds / wxMSW 64 bits (push) Has been cancelled
MSW cross-builds / wxMSW/Univ (push) Has been cancelled
MSW cross-builds / wxMSW 32 bits (push) Has been cancelled
Code Checks / Check Spelling (push) Has been cancelled
Code Checks / Check Whitespace (push) Has been cancelled
Code Checks / Check Mixed EOL (push) Has been cancelled
Code Checks / Check C++ Style (push) Has been cancelled
Code Checks / Check All Headers In allheaders.h (push) Has been cancelled
Update Documentation / Update Online Documentation (push) Has been cancelled
No Response / no-response (push) Has been cancelled
Allow to easily save and restore the selected radio button. Closes #25530.
This commit is contained in:
@@ -3955,6 +3955,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \
|
||||
wx/persist.h \
|
||||
wx/persist/bookctrl.h \
|
||||
wx/persist/dataview.h \
|
||||
wx/persist/radiobut.h \
|
||||
wx/persist/splitter.h \
|
||||
wx/persist/toplevel.h \
|
||||
wx/persist/treebook.h \
|
||||
|
||||
@@ -1229,6 +1229,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
|
||||
wx/persist.h
|
||||
wx/persist/bookctrl.h
|
||||
wx/persist/dataview.h
|
||||
wx/persist/radiobut.h
|
||||
wx/persist/splitter.h
|
||||
wx/persist/toplevel.h
|
||||
wx/persist/treebook.h
|
||||
|
||||
@@ -1130,6 +1130,7 @@ set(GUI_CMN_HDR
|
||||
wx/persist.h
|
||||
wx/persist/bookctrl.h
|
||||
wx/persist/dataview.h
|
||||
wx/persist/radiobut.h
|
||||
wx/persist/splitter.h
|
||||
wx/persist/toplevel.h
|
||||
wx/persist/treebook.h
|
||||
|
||||
@@ -1158,6 +1158,7 @@ GUI_CMN_HDR =
|
||||
wx/persist/checkbox.h
|
||||
wx/persist/combobox.h
|
||||
wx/persist/dataview.h
|
||||
wx/persist/radiobut.h
|
||||
wx/persist/splitter.h
|
||||
wx/persist/toplevel.h
|
||||
wx/persist/treebook.h
|
||||
|
||||
@@ -2049,6 +2049,7 @@
|
||||
<ClInclude Include="..\..\include\wx\progdlg.h" />
|
||||
<ClInclude Include="..\..\include\wx\quantize.h" />
|
||||
<ClInclude Include="..\..\include\wx\radiobox.h" />
|
||||
<ClInclude Include="..\..\include\wx\persist\radiobut.h" />
|
||||
<ClInclude Include="..\..\include\wx\radiobut.h" />
|
||||
<ClInclude Include="..\..\include\wx\range.h" />
|
||||
<ClInclude Include="..\..\include\wx\rawbmp.h" />
|
||||
|
||||
@@ -2086,6 +2086,9 @@
|
||||
<ClInclude Include="..\..\include\wx\persist\dataview.h">
|
||||
<Filter>Common Headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\wx\persist\radiobut.h">
|
||||
<Filter>Common Headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\wx\persist\splitter.h">
|
||||
<Filter>Common Headers</Filter>
|
||||
</ClInclude>
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/persist/radiobut.h
|
||||
// Purpose: Persistence support for wxRadioButton.
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2025-06-12
|
||||
// Copyright: (c) 2025 Vadim Zeitlin
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _WX_PERSIST_RADIOBUT_H_
|
||||
#define _WX_PERSIST_RADIOBUT_H_
|
||||
|
||||
#include "wx/persist/window.h"
|
||||
|
||||
#include "wx/radiobut.h"
|
||||
|
||||
#if wxUSE_RADIOBTN
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// string constants used by wxPersistentRadioButton
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#define wxPERSIST_RADIOBUTTON_KIND wxASCII_STR("RadioButton")
|
||||
|
||||
#define wxPERSIST_RADIOBUTTON_VALUE wxASCII_STR("Value")
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxPersistentRadioButton: supports saving/restoring radio buttons state
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// This class should be always used with the first button in a group of radio
|
||||
// buttons and it saves its state as an integer indicating the offset of the
|
||||
// selected radio button in this group.
|
||||
//
|
||||
// Currently radio buttons with wxRB_SINGLE style are not supported. While it
|
||||
// wouldn't be very difficult to add support for them if anybody really needs
|
||||
// it, it seems relatively unlikely, so for now we don't bother to do it.
|
||||
|
||||
class wxPersistentRadioButton : public wxPersistentWindow<wxRadioButton>
|
||||
{
|
||||
public:
|
||||
explicit wxPersistentRadioButton(wxRadioButton* radiobutton)
|
||||
: wxPersistentWindow<wxRadioButton>(radiobutton)
|
||||
{
|
||||
wxASSERT_MSG( !radiobutton->HasFlag(wxRB_SINGLE),
|
||||
"wxPersistentRadioButton doesn't support wxRB_SINGLE" );
|
||||
|
||||
wxASSERT_MSG( radiobutton->HasFlag(wxRB_GROUP),
|
||||
"wxPersistentRadioButton should be used with the first "
|
||||
"radio button in a group" );
|
||||
}
|
||||
|
||||
virtual void Save() const override
|
||||
{
|
||||
const wxRadioButton* button = Get();
|
||||
for ( int n = 0;; ++n )
|
||||
{
|
||||
if ( button->GetValue() )
|
||||
{
|
||||
// We found the selected button, save its index.
|
||||
SaveValue(wxPERSIST_RADIOBUTTON_VALUE, n);
|
||||
break;
|
||||
}
|
||||
|
||||
button = button->GetNextInGroup();
|
||||
if ( !button )
|
||||
{
|
||||
wxFAIL_MSG("Didn't find a selected radio button in the group?");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool Restore() override
|
||||
{
|
||||
int value;
|
||||
if ( !RestoreValue(wxPERSIST_RADIOBUTTON_VALUE, &value) )
|
||||
return false;
|
||||
|
||||
wxRadioButton* button = Get();
|
||||
for ( int n = 0;; ++n )
|
||||
{
|
||||
if ( n == value )
|
||||
{
|
||||
// We found the button with the saved index, set its value.
|
||||
button->SetValue(true);
|
||||
break;
|
||||
}
|
||||
|
||||
button = button->GetNextInGroup();
|
||||
|
||||
if ( !button )
|
||||
{
|
||||
// The saved value is invalid, silently ignore it because there
|
||||
// doesn't seem to be much else that we can do in this case.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual wxString GetKind() const override { return wxPERSIST_RADIOBUTTON_KIND; }
|
||||
};
|
||||
|
||||
inline wxPersistentObject *wxCreatePersistentObject(wxRadioButton* radiobutton)
|
||||
{
|
||||
return new wxPersistentRadioButton(radiobutton);
|
||||
}
|
||||
|
||||
#endif // wxUSE_RADIOBTN
|
||||
|
||||
#endif // _WX_PERSIST_RADIOBUT_H_
|
||||
@@ -0,0 +1,75 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/persist/radiobutton.h
|
||||
// Purpose: Interface of wxPersistentRadioButton
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2025-06-15
|
||||
// Copyright: (c) 2025 Vadim Zeitlin <vadim@wxwidgets.org>
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
Persistence adapter for wxRadioButton controls.
|
||||
|
||||
This adapter saves and restores the index of the selected wxRadioButton in
|
||||
a group of radio buttons, to allow to retain the selection across program
|
||||
executions.
|
||||
|
||||
Example of using it:
|
||||
@code
|
||||
// Assume that all these controls are added to some sizer elsewhere.
|
||||
auto* label = new wxStaticText(this, wxID_ANY, "Play with:");
|
||||
|
||||
auto* black = new wxRadioButton(this, wxID_ANY, "&Black",
|
||||
wxDefaultPosition,
|
||||
wxDefaultSize,
|
||||
wxRB_GROUP);
|
||||
auto* white = new wxRadioButton(this, wxID_ANY, "&White");
|
||||
|
||||
// We register the first radio button here, but all radio buttons in the
|
||||
// same group are potentially affected by this call.
|
||||
wxPersistentRegisterAndRestore(black);
|
||||
@endcode
|
||||
|
||||
During the first program execution, black colour will be selected, but if
|
||||
the user selects white pieces, this selection will be restored during the
|
||||
subsequent run.
|
||||
|
||||
@since 3.3.0
|
||||
*/
|
||||
class wxPersistentRadioButton : public wxPersistentWindow<wxRadioButton>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
Constructor.
|
||||
|
||||
Please note that the radio button must be the first one in the group,
|
||||
i.e. have ::wxRB_GROUP style set, otherwise an assertion will be
|
||||
triggered.
|
||||
|
||||
Also note that currently ::wxRB_SINGLE style is not supported.
|
||||
|
||||
@param radiobutton
|
||||
The associated radiobutton.
|
||||
*/
|
||||
explicit wxPersistentRadioButton(wxRadioButton *radiobutton);
|
||||
|
||||
/**
|
||||
Save the currently selected button index.
|
||||
|
||||
The 0-based index of the selected radio button in the group is saved as
|
||||
radio button value.
|
||||
*/
|
||||
virtual void Save() const;
|
||||
|
||||
/**
|
||||
Restore the previously saved selection.
|
||||
|
||||
If the saved index is valid, i.e. is positive and less than the number
|
||||
of radio buttons in the group, the radio button with the corresponding
|
||||
index will be selected.
|
||||
*/
|
||||
virtual bool Restore();
|
||||
};
|
||||
|
||||
/// Overload allowing persistence adapter creation for wxRadioButton objects.
|
||||
wxPersistentObject *wxCreatePersistentObject(wxRadioButton *radiobutton);
|
||||
@@ -268,6 +268,7 @@
|
||||
#include <wx/persist/checkbox.h>
|
||||
#include <wx/persist/combobox.h>
|
||||
#include <wx/persist/dataview.h>
|
||||
#include <wx/persist/radiobut.h>
|
||||
#include <wx/persist/splitter.h>
|
||||
#include <wx/persist/toplevel.h>
|
||||
#include <wx/persist/treebook.h>
|
||||
|
||||
Reference in New Issue
Block a user