Make using custom colour for wxDataViewProgressRenderer work again

This used to work in 3.0, but got broken with the switch to using a
native function for rendering the gauge in generic wxDataViewCtrl
86cf756ba9 (see #16406).

Restore it now, even if it requires not one, but two hacks: one at
wxRendererGeneric level to guess whether a custom colour is being used
or not by examining wxDC::GetBackground(), and the other one in
wxDataViewProgressRenderer itself to fall back to wxRendererGeneric if
the colour is specified. But it is arguably worth doing this to fix the
regression, even if it would be nice to provide a better API instead
(see #18076).

Closes #17885.
This commit is contained in:
Vadim Zeitlin 2018-02-03 19:21:46 +01:00
parent 4b69c1a718
commit 30f73aea6a
2 changed files with 25 additions and 2 deletions

View File

@ -1373,7 +1373,18 @@ wxString wxDataViewProgressRenderer::GetAccessibleDescription() const
bool bool
wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state)) wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state))
{ {
wxRendererNative::Get().DrawGauge( const wxDataViewItemAttr& attr = GetAttr();
if ( attr.HasColour() )
dc->SetBackground(attr.GetColour());
// This is a hack, but native renderers don't support using custom colours,
// but typically gauge colour is important (e.g. it's commonly green/red to
// indicate some qualitative difference), so we fall back to the generic
// implementation which looks ugly but does support using custom colour.
wxRendererNative& renderer = attr.HasColour()
? wxRendererNative::GetGeneric()
: wxRendererNative::Get();
renderer.DrawGauge(
GetOwner()->GetOwner(), GetOwner()->GetOwner(),
*dc, *dc,
rect, rect,

View File

@ -912,6 +912,18 @@ void wxRendererGeneric::DrawGauge(wxWindow* win,
int max, int max,
int flags) int flags)
{ {
// This is a hack, but we want to allow customizing the colour used for the
// gauge body, as this is important for the generic wxDataViewCtrl
// implementation which uses this method. So we assume that if the caller
// had set up a brush using background colour different from the default,
// it should be used. Otherwise we use the default one.
const wxBrush& bg = dc.GetBackground();
wxColour colBar;
if ( bg.IsOk() && bg.GetColour() != win->GetBackgroundColour() )
colBar = bg.GetColour();
else
colBar = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
// Use same background as text controls. // Use same background as text controls.
DrawTextCtrl(win, dc, rect); DrawTextCtrl(win, dc, rect);
@ -929,7 +941,7 @@ void wxRendererGeneric::DrawGauge(wxWindow* win,
progRect.width = wxMulDivInt32(progRect.width, value, max); progRect.width = wxMulDivInt32(progRect.width, value, max);
} }
dc.SetBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); dc.SetBrush(colBar);
dc.SetPen(*wxTRANSPARENT_PEN); dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle(progRect); dc.DrawRectangle(progRect);
} }