diff --git a/common/properties/pg_editors.cpp b/common/properties/pg_editors.cpp index 9e58e06398..7ae8d76b44 100644 --- a/common/properties/pg_editors.cpp +++ b/common/properties/pg_editors.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -225,38 +226,69 @@ wxPGWindowList PG_CHECKBOX_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGPr } +bool PG_COLOR_EDITOR::OnEvent( wxPropertyGrid* aGrid, wxPGProperty* aProperty, wxWindow* aWindow, + wxEvent& aEvent ) const +{ + return false; +} + + wxPGWindowList PG_COLOR_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGProperty* aProperty, const wxPoint& aPos, const wxSize& aSize ) const { - wxVariant val = aProperty->GetValue(); + auto colorProp = dynamic_cast( aProperty ); + if( !colorProp ) + return nullptr; + + KIGFX::COLOR4D color = colorFromProperty( aProperty ); + KIGFX::COLOR4D defColor = colorFromVariant( colorProp->GetDefaultValue() ); + + COLOR_SWATCH* editor = new COLOR_SWATCH( aGrid->GetPanel(), color, wxID_ANY, + colorProp->GetBackgroundColor(), defColor, + SWATCH_LARGE, true ); + editor->SetPosition( aPos ); + editor->SetSize( aSize ); + + editor->Bind( COLOR_SWATCH_CHANGED, + [=]( wxCommandEvent& aEvt ) + { + wxVariant val; + auto data = new COLOR4D_VARIANT_DATA( editor->GetSwatchColor() ); + val.SetData( data ); + aGrid->ChangePropertyValue( colorProp, val ); + } ); + + if( aGrid->GetInternalFlags() & wxPG_FL_ACTIVATION_BY_CLICK ) + aGrid->CallAfter( [=]() { editor->GetNewSwatchColor(); } ); + + return editor; +} + + +void PG_COLOR_EDITOR::UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const +{ + if( auto swatch = dynamic_cast( aCtrl ) ) + swatch->SetSwatchColor( colorFromProperty( aProperty ), false ); +} + + +KIGFX::COLOR4D PG_COLOR_EDITOR::colorFromVariant( const wxVariant& aVariant ) const +{ KIGFX::COLOR4D color = KIGFX::COLOR4D::UNSPECIFIED; COLOR4D_VARIANT_DATA* data = nullptr; - if( val.IsType( wxS( "COLOR4D" ) ) ) + if( aVariant.IsType( wxS( "COLOR4D" ) ) ) { - data = static_cast( val.GetData() ); + data = static_cast( aVariant.GetData() ); color = data->Color(); } - DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( aGrid ), color, true, - nullptr, KIGFX::COLOR4D::UNSPECIFIED ); - - int res = dialog.ShowModal(); - - if( res == wxID_OK ) - { - data = new COLOR4D_VARIANT_DATA(); - data->SetColor( dialog.GetColor() ); - val.SetData( data ); - aGrid->ChangePropertyValue( aProperty, val ); - } - - // Deselect property so that this gets called again on next click - aGrid->CallAfter( [=]() - { - aGrid->RemoveFromSelection( aProperty ); - } ); - - return nullptr; + return color; +} + + +KIGFX::COLOR4D PG_COLOR_EDITOR::colorFromProperty( wxPGProperty* aProperty ) const +{ + return colorFromVariant( aProperty->GetValue() ); } diff --git a/common/widgets/color_swatch.cpp b/common/widgets/color_swatch.cpp index 38dd17572e..f4ed61c673 100644 --- a/common/widgets/color_swatch.cpp +++ b/common/widgets/color_swatch.cpp @@ -123,7 +123,7 @@ void COLOR_SWATCH::RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor, COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID, const COLOR4D& aBackground, const COLOR4D& aDefault, - SWATCH_SIZE aSwatchSize ) : + SWATCH_SIZE aSwatchSize, bool aTriggerWithSingleClick ) : wxPanel( aParent, aID ), m_color( aColor ), m_background( aBackground ), @@ -164,7 +164,7 @@ COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID, sizer->Add( m_swatch, 0, 0 ); - setupEvents(); + setupEvents( aTriggerWithSingleClick ); } @@ -194,11 +194,11 @@ COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, wxWindowID aID, const wxPoint& aP sizer->Add( m_swatch, 0, 0 ); - setupEvents(); + setupEvents( false ); } -void COLOR_SWATCH::setupEvents() +void COLOR_SWATCH::setupEvents( bool aTriggerWithSingleClick ) { wxWindow* topLevelParent = GetParent(); @@ -224,6 +224,15 @@ void COLOR_SWATCH::setupEvents() { GetNewSwatchColor(); } ); + + if( aTriggerWithSingleClick ) + { + m_swatch->Bind( wxEVT_LEFT_UP, + [this] ( wxMouseEvent& aEvt ) + { + GetNewSwatchColor(); + } ); + } } m_swatch->Bind( wxEVT_MIDDLE_DOWN, diff --git a/include/properties/pg_editors.h b/include/properties/pg_editors.h index 0d21297441..e579364f30 100644 --- a/include/properties/pg_editors.h +++ b/include/properties/pg_editors.h @@ -99,13 +99,15 @@ public: wxPGWindowList CreateControls( wxPropertyGrid* aGrid, wxPGProperty* aProperty, const wxPoint& aPos, const wxSize& aSize ) const override; - void UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const override {} + void UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const override; bool OnEvent( wxPropertyGrid* aGrid, wxPGProperty* aProperty, wxWindow* aWindow, - wxEvent& aEvent ) const override - { - return false; - } + wxEvent& aEvent ) const override; + +private: + KIGFX::COLOR4D colorFromVariant( const wxVariant& aVariant ) const; + + KIGFX::COLOR4D colorFromProperty( wxPGProperty* aProperty ) const; }; #endif //KICAD_PG_EDITORS_H diff --git a/include/widgets/color_swatch.h b/include/widgets/color_swatch.h index dcf8d852b5..e15bb42c18 100644 --- a/include/widgets/color_swatch.h +++ b/include/widgets/color_swatch.h @@ -66,7 +66,7 @@ public: */ COLOR_SWATCH( wxWindow* aParent, const KIGFX::COLOR4D& aColor, int aID, const KIGFX::COLOR4D& aBackground, const KIGFX::COLOR4D& aDefault, - SWATCH_SIZE aSwatchType ); + SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick = false ); /** * constructor for wxFormBuilder @@ -137,7 +137,7 @@ public: const KIGFX::COLOR4D& aCheckerboardBackground ); private: - void setupEvents(); + void setupEvents( bool aTriggerWithSingleClick ); /** * Pass unwanted events on to listeners of this object