From b91173f76ef3b69c727ed643d439a62935e22a2f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 23 Mar 2022 13:43:44 +0100 Subject: [PATCH] Fix comparing wxDataFormat with wxDF_INVALID in wxGTK Define wxDataFormat::operator==(wxDataFormatId) instead of relying on the implicit conversion from wxDataFormatId to wxDataFormat, as this can't be done when the format ID is wxDF_INVALID because creating wxDataFormat results in an assert failure in this case, while comparing with wxDF_INVALID is clearly a perfectly valid operation. Add a unit test checking for this. Closes #22213. --- include/wx/gtk/dataform.h | 9 +++++---- tests/misc/guifuncs.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/wx/gtk/dataform.h b/include/wx/gtk/dataform.h index ae5a2c232b..e248653eea 100644 --- a/include/wx/gtk/dataform.h +++ b/include/wx/gtk/dataform.h @@ -33,10 +33,11 @@ public: wxDataFormat& operator=(NativeFormat format) { SetId(format); return *this; } - // comparison (note that we rely on implicit conversions for comparison - // with wxDataFormatId, but have to provide them explicitly for comparison - // with NativeFormat to avoid ambiguity between comparing from it to - // wxDataFormat or vice versa) + // comparison + bool operator==(wxDataFormatId type) const + { return m_type == type; } + bool operator!=(wxDataFormatId type) const + { return m_type != type; } bool operator==(NativeFormat format) const { return m_format == (NativeFormat)format; } bool operator!=(NativeFormat format) const diff --git a/tests/misc/guifuncs.cpp b/tests/misc/guifuncs.cpp index 1461a03147..857cbdaab1 100644 --- a/tests/misc/guifuncs.cpp +++ b/tests/misc/guifuncs.cpp @@ -70,6 +70,13 @@ TEST_CASE("GUI::URLDataObject", "[guifuncs]") CHECK( wxTheClipboard->SetData(dobj) ); wxTheClipboard->Flush(); } + +TEST_CASE("GUI::DataFormatCompare", "[guifuncs][dataformat]") +{ + const wxDataFormat df(wxDF_TEXT); + CHECK( df == wxDF_TEXT ); + CHECK( df != wxDF_INVALID ); +} #endif // wxUSE_DATAOBJ TEST_CASE("GUI::ParseFileDialogFilter", "[guifuncs]")