From cfc5bc6ff2d81da204fcebd8ba8f033cd021faaf Mon Sep 17 00:00:00 2001 From: Bill Su Date: Tue, 12 Dec 2023 20:58:40 -0500 Subject: [PATCH 1/8] more convenient API for wxGenericValidator + wxLB_SINGLE For a single-selection wxListBox, using an int to transfer data to/from wxGenericValidator is more convenient than wxArrayInt. --- interface/wx/valgen.h | 3 +++ src/common/valgen.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/interface/wx/valgen.h b/interface/wx/valgen.h index 40eca58d08..cec4c063b7 100644 --- a/interface/wx/valgen.h +++ b/interface/wx/valgen.h @@ -21,6 +21,9 @@ For example, wxButton and wxTextCtrl transfer data to and from a wxString variable; wxListBox uses a wxArrayInt; wxCheckBox uses a boolean. + @since 3.2.5 + A wxLB_SINGLE wxListBox can also use an int. + For more information, please see @ref overview_validator. @library{wxcore} diff --git a/src/common/valgen.cpp b/src/common/valgen.cpp index 3625f1fc1f..3fa2de301d 100644 --- a/src/common/valgen.cpp +++ b/src/common/valgen.cpp @@ -377,6 +377,17 @@ bool wxGenericValidator::TransferToWindow() return true; } + else if (m_pInt) + { + wxCHECK_MSG( + !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + false, + "multi-select control requires wxArrayInt" + ); + pControl->Check(*m_pInt); + + return true; + } else return false; } else @@ -398,6 +409,17 @@ bool wxGenericValidator::TransferToWindow() for ( i = 0 ; i < count; i++ ) pControl->SetSelection(m_pArrayInt->Item(i)); + return true; + } + else if (m_pInt) + { + wxCHECK_MSG( + !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + false, + "multi-select control requires wxArrayInt" + ); + pControl->SetSelection(*m_pInt); + return true; } } else @@ -654,6 +676,26 @@ bool wxGenericValidator::TransferFromWindow() return true; } + else if (m_pInt) + { + wxCHECK_MSG( + !(pControl->GetWindowStyle()& (wxLB_MULTIPLE || wxLB_EXTENDED)), + false, + "multi-select control requires wxArrayInt" + ); + + size_t i, + count = pControl->GetCount(); + for ( i = 0; i < count; i++ ) + { + if (pControl->IsChecked(i)) + { + *m_pInt = i; + } + } + + return true; + } else return false; } else @@ -676,6 +718,18 @@ bool wxGenericValidator::TransferFromWindow() m_pArrayInt->Add(i); } + return true; + } + else if (m_pInt) + { + wxCHECK_MSG( + !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + false, + "multi-select control requires wxArrayInt" + ); + + *m_pInt = pControl->GetSelection(); + return true; } } else From bdfeb19d19685accc10709a44eaa0c96a6e27a9b Mon Sep 17 00:00:00 2001 From: Bill Su Date: Wed, 13 Dec 2023 18:26:19 -0500 Subject: [PATCH 2/8] HasMultipleSelection() is clearer than handling style bits --- src/common/valgen.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/valgen.cpp b/src/common/valgen.cpp index 3fa2de301d..6724651b48 100644 --- a/src/common/valgen.cpp +++ b/src/common/valgen.cpp @@ -380,7 +380,7 @@ bool wxGenericValidator::TransferToWindow() else if (m_pInt) { wxCHECK_MSG( - !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + !pControl->HasMultipleSelection(), false, "multi-select control requires wxArrayInt" ); @@ -414,7 +414,7 @@ bool wxGenericValidator::TransferToWindow() else if (m_pInt) { wxCHECK_MSG( - !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + !pControl->HasMultipleSelection(), false, "multi-select control requires wxArrayInt" ); @@ -679,7 +679,7 @@ bool wxGenericValidator::TransferFromWindow() else if (m_pInt) { wxCHECK_MSG( - !(pControl->GetWindowStyle()& (wxLB_MULTIPLE || wxLB_EXTENDED)), + !pControl->HasMultipleSelection(), false, "multi-select control requires wxArrayInt" ); @@ -723,7 +723,7 @@ bool wxGenericValidator::TransferFromWindow() else if (m_pInt) { wxCHECK_MSG( - !(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)), + !pControl->HasMultipleSelection(), false, "multi-select control requires wxArrayInt" ); From 8dc3ef4307e2bbe5ad4b9c573d2e998c0bb5fe43 Mon Sep 17 00:00:00 2001 From: Bill Su Date: Wed, 13 Dec 2023 18:31:54 -0500 Subject: [PATCH 3/8] document wxListBox::HasMultipleSelection() as public API --- interface/wx/listbox.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/interface/wx/listbox.h b/interface/wx/listbox.h index 19bf3fe0aa..4f96159f00 100644 --- a/interface/wx/listbox.h +++ b/interface/wx/listbox.h @@ -162,6 +162,11 @@ public: const wxString& name = wxListBoxNameStr); ///@} + /** + return true if the listbox allows multiple selection + */ + bool HasMultipleSelection() const + /** Deselects an item in the list box. From ea4800fcb9eb32389bc7d4b93d2819394b5957f0 Mon Sep 17 00:00:00 2001 From: Bill Su Date: Thu, 14 Dec 2023 18:58:53 -0500 Subject: [PATCH 4/8] comment that single-select wxListBox allows works with int --- include/wx/valgen.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/wx/valgen.h b/include/wx/valgen.h index 8f26bd1740..c389cbfc81 100644 --- a/include/wx/valgen.h +++ b/include/wx/valgen.h @@ -33,6 +33,7 @@ public: // wxCheckBox, wxRadioButton, wx(Bitmap)ToggleButton wxGenericValidator(bool* val); // wxChoice, wxGauge, wxRadioBox, wxScrollBar, wxSlider, wxSpinButton + // single-selection wxListBox wxGenericValidator(int* val); // wxComboBox, wxTextCtrl, wxButton, wxStaticText (read-only) wxGenericValidator(wxString* val); From 4d3acc90d7c9ea39daf50f937ceff878074fc613 Mon Sep 17 00:00:00 2001 From: Bill Su Date: Thu, 14 Dec 2023 19:17:32 -0500 Subject: [PATCH 5/8] wxGenericValidator: add wxColourPickerCtrl support --- include/wx/valgen.h | 4 ++++ interface/wx/valgen.h | 4 ++-- src/common/valgen.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/include/wx/valgen.h b/include/wx/valgen.h index c389cbfc81..9ce773f3c6 100644 --- a/include/wx/valgen.h +++ b/include/wx/valgen.h @@ -16,6 +16,7 @@ class WXDLLIMPEXP_FWD_BASE wxDateTime; class WXDLLIMPEXP_FWD_BASE wxFileName; +class WXDLLIMPEXP_FWD_CORE wxColour; // ---------------------------------------------------------------------------- // wxGenericValidator performs data transfer between many standard controls and @@ -49,6 +50,8 @@ public: wxGenericValidator(float* val); // wxTextCtrl wxGenericValidator(double* val); + // wxColourPickerCtrl + wxGenericValidator(wxColour* val); wxGenericValidator(const wxGenericValidator& copyFrom); @@ -84,6 +87,7 @@ protected: wxFileName* m_pFileName; float* m_pFloat; double* m_pDouble; + wxColour* m_pColour; private: wxDECLARE_CLASS(wxGenericValidator); diff --git a/interface/wx/valgen.h b/interface/wx/valgen.h index cec4c063b7..6951dc22ac 100644 --- a/interface/wx/valgen.h +++ b/interface/wx/valgen.h @@ -15,14 +15,14 @@ - wxButton, wxRadioButton, wxToggleButton, wxBitmapToggleButton, wxSpinButton - wxCheckBox, wxRadioBox, wxComboBox, wxListBox, wxCheckListBox - wxGauge, wxSlider, wxScrollBar, wxChoice, wxStaticText - - wxSpinCtrl, wxTextCtrl + - wxSpinCtrl, wxTextCtrl, wxColourPickerCtrl It checks the type of the window and uses an appropriate type for it. For example, wxButton and wxTextCtrl transfer data to and from a wxString variable; wxListBox uses a wxArrayInt; wxCheckBox uses a boolean. @since 3.2.5 - A wxLB_SINGLE wxListBox can also use an int. + A wxLB_SINGLE wxListBox can also use an int. wxColourPickerCtrl support. For more information, please see @ref overview_validator. diff --git a/src/common/valgen.cpp b/src/common/valgen.cpp index 6724651b48..0edfd52468 100644 --- a/src/common/valgen.cpp +++ b/src/common/valgen.cpp @@ -41,6 +41,9 @@ #if wxUSE_TOGGLEBTN #include "wx/tglbtn.h" #endif +#if wxUSE_COLOURPICKERCTRL + #include +#endif #include "wx/filename.h" #include "wx/valgen.h" @@ -99,6 +102,12 @@ wxGenericValidator::wxGenericValidator(double *val) m_pDouble = val; } +wxGenericValidator::wxGenericValidator(wxColour* val) +{ + Initialize(); + m_pColour = val; +} + wxGenericValidator::wxGenericValidator(const wxGenericValidator& val) : wxValidator() { @@ -119,6 +128,7 @@ bool wxGenericValidator::Copy(const wxGenericValidator& val) m_pFileName = val.m_pFileName; m_pFloat = val.m_pFloat; m_pDouble = val.m_pDouble; + m_pColour = val.m_pColour; return true; } @@ -424,6 +434,20 @@ bool wxGenericValidator::TransferToWindow() } } else #endif + + // colour controls +#if wxUSE_COLOURPICKERCTRL + if (wxDynamicCast(m_validatorWindow, wxColourPickerCtrl)) + { + wxColourPickerCtrl* pControl = (wxColourPickerCtrl*)m_validatorWindow; + if (m_pColour) + { + pControl->SetColour(*m_pColour); + + return true; + } + } else +#endif { // to match the last 'else' above } @@ -734,6 +758,19 @@ bool wxGenericValidator::TransferFromWindow() } } else #endif +#if wxUSE_COLOURPICKERCTRL + if (wxDynamicCast(m_validatorWindow, wxColourPickerCtrl)) + { + wxColourPickerCtrl* pControl = (wxColourPickerCtrl*)m_validatorWindow; + if (m_pColour) + { + *m_pColour = pControl->GetColour(); + + return true; + } + } + else +#endif // unrecognized control, or bad pointer return false; @@ -756,6 +793,7 @@ void wxGenericValidator::Initialize() m_pFileName = nullptr; m_pFloat = nullptr; m_pDouble = nullptr; + m_pColour = nullptr; } #endif // wxUSE_VALIDATORS From 486b25bd2a19daeec23ba07d38f17346e68bce1e Mon Sep 17 00:00:00 2001 From: Bill Su Date: Thu, 14 Dec 2023 21:26:57 -0500 Subject: [PATCH 6/8] fix #include <> to "" --- src/common/valgen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/valgen.cpp b/src/common/valgen.cpp index 0edfd52468..11a6a6a4ac 100644 --- a/src/common/valgen.cpp +++ b/src/common/valgen.cpp @@ -42,7 +42,7 @@ #include "wx/tglbtn.h" #endif #if wxUSE_COLOURPICKERCTRL - #include + #include "wx/clrpicker.h" #endif #include "wx/filename.h" From fb6a7d5a8ec07e1821c4ba68231405f897000d95 Mon Sep 17 00:00:00 2001 From: Bill Su Date: Sun, 17 Dec 2023 02:10:51 -0500 Subject: [PATCH 7/8] wxGenericValidator: add support for wxCheckBoxState --- include/wx/valgen.h | 3 +++ interface/wx/valgen.h | 1 + src/common/valgen.cpp | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/include/wx/valgen.h b/include/wx/valgen.h index 9ce773f3c6..c3d0faaab8 100644 --- a/include/wx/valgen.h +++ b/include/wx/valgen.h @@ -52,6 +52,8 @@ public: wxGenericValidator(double* val); // wxColourPickerCtrl wxGenericValidator(wxColour* val); + // wxCheckBox + wxGenericValidator(wxCheckBoxState* val); wxGenericValidator(const wxGenericValidator& copyFrom); @@ -88,6 +90,7 @@ protected: float* m_pFloat; double* m_pDouble; wxColour* m_pColour; + wxCheckBoxState* m_pCheckBoxState; private: wxDECLARE_CLASS(wxGenericValidator); diff --git a/interface/wx/valgen.h b/interface/wx/valgen.h index 6951dc22ac..845476022d 100644 --- a/interface/wx/valgen.h +++ b/interface/wx/valgen.h @@ -23,6 +23,7 @@ @since 3.2.5 A wxLB_SINGLE wxListBox can also use an int. wxColourPickerCtrl support. + A 3-state wxCheckBox can use wxCheckBoxState. For more information, please see @ref overview_validator. diff --git a/src/common/valgen.cpp b/src/common/valgen.cpp index 11a6a6a4ac..f73516d27a 100644 --- a/src/common/valgen.cpp +++ b/src/common/valgen.cpp @@ -108,6 +108,12 @@ wxGenericValidator::wxGenericValidator(wxColour* val) m_pColour = val; } +wxGenericValidator::wxGenericValidator(wxCheckBoxState* val) +{ + Initialize(); + m_pCheckBoxState = val; +} + wxGenericValidator::wxGenericValidator(const wxGenericValidator& val) : wxValidator() { @@ -129,6 +135,7 @@ bool wxGenericValidator::Copy(const wxGenericValidator& val) m_pFloat = val.m_pFloat; m_pDouble = val.m_pDouble; m_pColour = val.m_pColour; + m_pCheckBoxState = val.m_pCheckBoxState; return true; } @@ -149,6 +156,11 @@ bool wxGenericValidator::TransferToWindow() pControl->SetValue(*m_pBool); return true; } + else if (m_pCheckBoxState && pControl->Is3State()) + { + pControl->Set3StateValue(*m_pCheckBoxState); + return true; + } } else #endif #if wxUSE_RADIOBTN @@ -471,6 +483,11 @@ bool wxGenericValidator::TransferFromWindow() *m_pBool = pControl->GetValue() ; return true; } + else if (m_pCheckBoxState && pControl->Is3State()) + { + *m_pCheckBoxState = pControl->Get3StateValue(); + return true; + } } else #endif #if wxUSE_RADIOBTN @@ -794,6 +811,7 @@ void wxGenericValidator::Initialize() m_pFloat = nullptr; m_pDouble = nullptr; m_pColour = nullptr; + m_pCheckBoxState = nullptr; } #endif // wxUSE_VALIDATORS From ff68b61b84f185b0975f43e1098b97e69f613c08 Mon Sep 17 00:00:00 2001 From: Bill Su <69879726+wsu-cb@users.noreply.github.com> Date: Mon, 18 Dec 2023 11:43:28 -0500 Subject: [PATCH 8/8] Update interface/wx/valgen.h Co-authored-by: VZ --- interface/wx/valgen.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/wx/valgen.h b/interface/wx/valgen.h index 845476022d..3dc150713b 100644 --- a/interface/wx/valgen.h +++ b/interface/wx/valgen.h @@ -15,7 +15,7 @@ - wxButton, wxRadioButton, wxToggleButton, wxBitmapToggleButton, wxSpinButton - wxCheckBox, wxRadioBox, wxComboBox, wxListBox, wxCheckListBox - wxGauge, wxSlider, wxScrollBar, wxChoice, wxStaticText - - wxSpinCtrl, wxTextCtrl, wxColourPickerCtrl + - wxSpinCtrl, wxTextCtrl, wxColourPickerCtrl (since wxWidgets 3.3.0 or later). It checks the type of the window and uses an appropriate type for it. For example, wxButton and wxTextCtrl transfer data to and from a