From a2ff3bce7d05bfccd785dc8206635d49653135cd Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sun, 25 Jun 2023 16:13:07 -0400 Subject: [PATCH] Properties: custom rendering for COLOR4D --- common/properties/pg_cell_renderer.cpp | 38 ++++++++++++++++++++------ common/properties/pg_properties.cpp | 4 +-- include/properties/pg_properties.h | 5 ++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/common/properties/pg_cell_renderer.cpp b/common/properties/pg_cell_renderer.cpp index 9e58e1824a..83bb928579 100644 --- a/common/properties/pg_cell_renderer.cpp +++ b/common/properties/pg_cell_renderer.cpp @@ -17,8 +17,10 @@ * with this program. If not, see . */ #include +#include #include #include +#include PG_CELL_RENDERER::PG_CELL_RENDERER() : @@ -29,21 +31,39 @@ PG_CELL_RENDERER::PG_CELL_RENDERER() : bool PG_CELL_RENDERER::Render( wxDC &aDC, const wxRect &aRect, const wxPropertyGrid *aGrid, wxPGProperty *aProperty, int aColumn, int aItem, int aFlags ) const { - // Default behavior for value column + wxPGCell cell = aGrid->GetUnspecifiedValueAppearance(); + if( aColumn > 0 ) + { + if( PGPROPERTY_COLOR4D* colorProp = dynamic_cast( aProperty ) ) + { + wxAny av = colorProp->GetValue().GetAny(); + KIGFX::COLOR4D color = av.IsNull() ? KIGFX::COLOR4D::UNSPECIFIED + : av.As(); + + PreDrawCell( aDC, aRect, aGrid, cell, aFlags ); + + wxSize swatchSize = aGrid->ConvertDialogToPixels( wxSize( 24, 16 ) ); + int offset = ( aRect.GetHeight() - swatchSize.GetHeight() ) / 2; + wxRect swatch( aRect.GetPosition() + wxPoint( offset, offset ), swatchSize ); + + aDC.SetPen( *wxTRANSPARENT_PEN ); + aDC.SetBrush( wxBrush( color.ToColour() ) ); + aDC.DrawRectangle( swatch ); + + PostDrawCell( aDC, aGrid, cell, aFlags ); + + return true; + } + + // Default behavior for value column return wxPGDefaultRenderer::Render( aDC, aRect, aGrid, aProperty, aColumn, aItem, aFlags ); + } wxString text; int preDrawFlags = aFlags; -#if wxCHECK_VERSION( 3, 1, 0 ) - wxPGCell cell = aGrid->GetUnspecifiedValueAppearance(); aProperty->GetDisplayInfo( aColumn, aItem, aFlags, &text, &cell ); -#else - const wxPGCell* cellPtr = nullptr; - aProperty->GetDisplayInfo( aColumn, aItem, aFlags, &text, &cellPtr ); - const wxPGCell cell = *cellPtr; -#endif text = wxControl::Ellipsize( text, aDC, wxELLIPSIZE_MIDDLE, aRect.GetWidth() ); @@ -60,4 +80,4 @@ bool PG_CELL_RENDERER::Render( wxDC &aDC, const wxRect &aRect, const wxPropertyG PostDrawCell( aDC, aGrid, cell, preDrawFlags ); return !text.IsEmpty(); -} \ No newline at end of file +} diff --git a/common/properties/pg_properties.cpp b/common/properties/pg_properties.cpp index 77cc0cf2ec..27285b25e0 100644 --- a/common/properties/pg_properties.cpp +++ b/common/properties/pg_properties.cpp @@ -407,7 +407,7 @@ const wxPGEditor* PGPROPERTY_BOOL::DoGetEditorClass() const PGPROPERTY_COLOR4D::PGPROPERTY_COLOR4D( const wxString& aLabel, const wxString& aName, COLOR4D aValue ) : - wxColourProperty( aLabel, aName, aValue.ToColour() ) + wxStringProperty( aLabel, aName, aValue.ToCSSString() ) { } @@ -427,7 +427,7 @@ wxString PGPROPERTY_COLOR4D::ValueToString( wxVariant& aValue, int aFlags ) cons if( aValue.IsType( wxS( "COLOR4D" ) ) ) static_cast( aValue.GetData() )->Write( ret ); else - return wxColourProperty::ValueToString( aValue, aFlags ); + return wxStringProperty::ValueToString( aValue, aFlags ); return ret; } diff --git a/include/properties/pg_properties.h b/include/properties/pg_properties.h index ee8894ad33..c9b3270649 100644 --- a/include/properties/pg_properties.h +++ b/include/properties/pg_properties.h @@ -28,6 +28,7 @@ #include #include #include +#include #include class PROPERTY_BASE; @@ -189,11 +190,11 @@ public: }; -class PGPROPERTY_COLOR4D : public wxColourProperty +class PGPROPERTY_COLOR4D : public wxStringProperty { public: PGPROPERTY_COLOR4D( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL, - COLOR4D aValue = COLOR4D::UNSPECIFIED ); + KIGFX::COLOR4D aValue = KIGFX::COLOR4D::UNSPECIFIED ); virtual ~PGPROPERTY_COLOR4D() = default;