From 317ad650795341d7b228baa02baac02bc3d69725 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 6 Apr 2021 22:46:37 +0200 Subject: [PATCH 1/2] Move SetTextColoursAndFont() to wxGridCellRenderer Move this function from wxGridCellStringRenderer to its base class to allow using it even in the classes not drawing any strings. Also document it. --- include/wx/generic/grid.h | 7 ++++ include/wx/generic/gridctrl.h | 6 --- interface/wx/grid.h | 17 ++++++++ src/generic/gridctrl.cpp | 73 +++++++++++++++++------------------ 4 files changed, 60 insertions(+), 43 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 08f5aa951d..d7a3890764 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -228,6 +228,13 @@ public: // create a new object which is the copy of this one virtual wxGridCellRenderer *Clone() const = 0; + +protected: + // set the text colours before drawing + void SetTextColoursAndFont(const wxGrid& grid, + const wxGridCellAttr& attr, + wxDC& dc, + bool isSelected); }; // Smart pointer to wxGridCellRenderer, calling DecRef() on it automatically. diff --git a/include/wx/generic/gridctrl.h b/include/wx/generic/gridctrl.h index 1bdb812751..42f67cf383 100644 --- a/include/wx/generic/gridctrl.h +++ b/include/wx/generic/gridctrl.h @@ -41,12 +41,6 @@ public: { return new wxGridCellStringRenderer; } protected: - // set the text colours before drawing - void SetTextColoursAndFont(const wxGrid& grid, - const wxGridCellAttr& attr, - wxDC& dc, - bool isSelected); - // calc the string extent for given string/font wxSize DoGetBestSize(const wxGridCellAttr& attr, wxDC& dc, diff --git a/interface/wx/grid.h b/interface/wx/grid.h index 673164dabd..c3377cee48 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -109,6 +109,23 @@ public: wxDC& dc); protected: + /** + Helper function setting the correct colours and font. + + This function can be useful in the derived classes Draw() + implementation as it takes care of setting the appropriate colours and + font for @a dc depending on the global @a grid attributes, cell + attributions specified in @a attr and whether @a isSelected is @true. + + Simply call it before doing any drawing in the derived class version to + use consistent colours and font for all cells. + + @since 3.1.5 + */ + void SetTextColoursAndFont(const wxGrid& grid, + const wxGridCellAttr& attr, + wxDC& dc, + bool isSelected); /** The destructor is private because only DecRef() can delete us. */ diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp index 11d4de5442..668151a520 100644 --- a/src/generic/gridctrl.cpp +++ b/src/generic/gridctrl.cpp @@ -69,6 +69,42 @@ void wxGridCellRenderer::Draw(wxGrid& grid, dc.DrawRectangle(rect); } +void wxGridCellRenderer::SetTextColoursAndFont(const wxGrid& grid, + const wxGridCellAttr& attr, + wxDC& dc, + bool isSelected) +{ + dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT ); + + // TODO some special colours for attr.IsReadOnly() case? + + // different coloured text when the grid is disabled + if ( grid.IsThisEnabled() ) + { + if ( isSelected ) + { + wxColour clr; + if ( grid.HasFocus() ) + clr = grid.GetSelectionBackground(); + else + clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW); + dc.SetTextBackground( clr ); + dc.SetTextForeground( grid.GetSelectionForeground() ); + } + else + { + dc.SetTextBackground( attr.GetBackgroundColour() ); + dc.SetTextForeground( attr.GetTextColour() ); + } + } + else + { + dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); + dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); + } + + dc.SetFont( attr.GetFont() ); +} // ---------------------------------------------------------------------------- // wxGridCellDateTimeRenderer @@ -573,43 +609,6 @@ wxGridCellAutoWrapStringRenderer::GetBestWidth(wxGrid& grid, // wxGridCellStringRenderer // ---------------------------------------------------------------------------- -void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid, - const wxGridCellAttr& attr, - wxDC& dc, - bool isSelected) -{ - dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT ); - - // TODO some special colours for attr.IsReadOnly() case? - - // different coloured text when the grid is disabled - if ( grid.IsThisEnabled() ) - { - if ( isSelected ) - { - wxColour clr; - if ( grid.HasFocus() ) - clr = grid.GetSelectionBackground(); - else - clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW); - dc.SetTextBackground( clr ); - dc.SetTextForeground( grid.GetSelectionForeground() ); - } - else - { - dc.SetTextBackground( attr.GetBackgroundColour() ); - dc.SetTextForeground( attr.GetTextColour() ); - } - } - else - { - dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); - dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); - } - - dc.SetFont( attr.GetFont() ); -} - wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr, wxDC& dc, const wxString& text) From 8b6d01c79811a6b991e9da9ceb704cacdc10182e Mon Sep 17 00:00:00 2001 From: Ilya Sinitsyn Date: Fri, 14 Aug 2020 20:41:47 +0700 Subject: [PATCH 2/2] Fix grid demo stars drawing to use correct colours and font Just add a call to SetTextColoursAndFont() now that it is present in this renderer base class (see the parent commit) to ensure that the correct, i.e. corresponding to the column attribute, colour is used for the stars. --- samples/grid/griddemo.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 7218e1c202..aa78218393 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -162,6 +162,8 @@ public: { wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); + SetTextColoursAndFont(grid, attr, dc, isSelected); + grid.DrawTextRectangle(dc, GetStarString(GetStarValue(grid, row, col)), rect, attr); }