From 81dd2befabbba7bd043a539d210cd709c76cebf1 Mon Sep 17 00:00:00 2001 From: James Crook Date: Sat, 6 Apr 2019 16:34:47 +0100 Subject: [PATCH] Translucent background to track name (all platforms) wxDC can draw translucently, if given a bitmap. So we now create a suitable bitmap, using a wxGraphicsContext. --- src/AColor.cpp | 21 +++++++++++++++++++++ src/AColor.h | 2 ++ src/TrackArtist.cpp | 28 ++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/AColor.cpp b/src/AColor.cpp index 20cab7f17..a58ced9d9 100644 --- a/src/AColor.cpp +++ b/src/AColor.cpp @@ -314,6 +314,27 @@ void AColor::UseThemeColour( wxDC * dc, int iBrush, int iPen, int alpha ) dc->SetPen( sparePen ); } +void AColor::UseThemeColour( wxGraphicsContext * gc, int iBrush, int iPen, int alpha ) +{ + if (!inited) + Init(); + // do nothing if no colours set. + if( (iBrush == -1) && ( iPen ==-1)) + return; + wxColour col = wxColour(0,0,0); + if( iBrush !=-1 ){ + col = theTheme.Colour( iBrush ); + col.Set( col.Red(), col.Green(), col.Blue(), alpha); + spareBrush.SetColour( col ); + gc->SetBrush( spareBrush ); + } + if( iPen != -1) + col = theTheme.Colour( iPen ); + sparePen.SetColour( col ); + gc->SetPen( sparePen ); +} + + void AColor::Light(wxDC * dc, bool selected, bool highlight) { if (!inited) diff --git a/src/AColor.h b/src/AColor.h index 3d4695328..0c2281555 100644 --- a/src/AColor.h +++ b/src/AColor.h @@ -19,6 +19,7 @@ #include // member variable class wxDC; +class wxGraphicsContext; class wxRect; /// Used to restore pen, brush and logical-op in a DC back to what they were. @@ -79,6 +80,7 @@ class AColor { static wxColour Blend(const wxColour & c1, const wxColour & c2); static void UseThemeColour( wxDC * dc, int iBrush, int iPen=-1, int alpha = 255 ); + static void UseThemeColour( wxGraphicsContext * gc, int iBrush, int iPen=-1, int alpha = 255 ); static void TrackPanelBackground(wxDC * dc, bool selected); static void Light(wxDC * dc, bool selected, bool highlight = false); diff --git a/src/TrackArtist.cpp b/src/TrackArtist.cpp index d0d7bd786..97ada06cf 100644 --- a/src/TrackArtist.cpp +++ b/src/TrackArtist.cpp @@ -337,11 +337,31 @@ void TrackArt::DrawTrack(TrackPanelDrawingContext &context, wxFont labelFont(12, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); dc.SetFont(labelFont); dc.GetTextExtent( wt->GetName(), &x, &y ); - dc.SetTextForeground(theTheme.Colour( clrTrackPanelText )); - // Shield's background is translucent, alpha=100, but currently - // only on mac. - AColor::UseThemeColour( &dc, clrTrackInfoSelected, clrTrackPanelText, 100 ); + +#ifdef __WXMAC__ + // Mac dc is a graphics dc already. + // Shield's background is translucent, alpha=180 + AColor::UseThemeColour( &dc, clrTrackInfoSelected, clrTrackPanelText, 180 ); dc.DrawRoundedRectangle( rect.x+7, rect.y+1, x+16, y+4, 8.0 ); +#else + // This little dance with wxImage in order to draw to a graphic dc + // which we can then paste as a translucent bitmap onto the real dc. + wxImage image( x+18, y+6 ); + image.InitAlpha(); + unsigned char *alpha=image.GetAlpha(); + memset(alpha, wxIMAGE_ALPHA_TRANSPARENT, image.GetWidth()*image.GetHeight()); + + wxGraphicsContext &gc=*wxGraphicsContext::Create(image); + // Shield's background is translucent, alpha=180. This is to a gc, not a dc. + AColor::UseThemeColour( &gc, clrTrackInfoSelected, clrTrackPanelText, 180 ); + // Draw at 1,1, not at 0,0 to avoid clipping of the antialiasing. + gc.DrawRoundedRectangle( 1, 1, x+16, y+4, 8.0 ); + // delete the gc so as to free and so update the wxImage. + delete &gc; + wxBitmap bitmap( image ); + dc.DrawBitmap( bitmap, rect.x+6, rect.y); +#endif + dc.SetTextForeground(theTheme.Colour( clrTrackPanelText )); dc.DrawText (wt->GetName(), rect.x+15, rect.y+3); // move right 15 pixels to avoid overwriting <- symbol } },