Improve drawing buttons in generic wxSearchCtrl

Several fixes:
* Clear the buttons background, which can be important if their (custom)
  bitmap is transparent.
* Only add margin before/after the corresponding button if it is
  actually visible.
* Select the bitmap out of wxMemoryDC before modifying it.
* Don't assume that the "Cancel" button is always square.

Closes https://github.com/wxWidgets/wxWidgets/pull/1142
This commit is contained in:
Tomay
2019-01-17 00:53:44 +01:00
committed by Vadim Zeitlin
parent 07f64c3b75
commit 10eeb0b262

View File

@@ -222,6 +222,12 @@ protected:
void OnPaint(wxPaintEvent&) void OnPaint(wxPaintEvent&)
{ {
wxPaintDC dc(this); wxPaintDC dc(this);
// Clear the background in case of a user bitmap with alpha channel
dc.SetBrush(m_search->GetBackgroundColour());
dc.Clear();
// Draw the bitmap
dc.DrawBitmap(m_bmp, 0,0, true); dc.DrawBitmap(m_bmp, 0,0, true);
} }
@@ -480,9 +486,8 @@ void wxSearchCtrl::LayoutControls()
wxSize sizeText = m_text->GetBestSize(); wxSize sizeText = m_text->GetBestSize();
// make room for the search menu & clear button // make room for the search menu & clear button
int horizontalBorder = FromDIP(1) + ( sizeText.y - sizeText.y * 14 / 21 ) / 2; int horizontalBorder = FromDIP(1) + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
int x = horizontalBorder; int x = 0;
width -= horizontalBorder*2; int textWidth = width;
if (width < 0) width = 0;
wxSize sizeSearch(0,0); wxSize sizeSearch(0,0);
wxSize sizeCancel(0,0); wxSize sizeCancel(0,0);
@@ -492,11 +497,14 @@ void wxSearchCtrl::LayoutControls()
{ {
sizeSearch = m_searchButton->GetBestSize(); sizeSearch = m_searchButton->GetBestSize();
searchMargin = FromDIP(MARGIN); searchMargin = FromDIP(MARGIN);
x += horizontalBorder;
textWidth -= horizontalBorder;
} }
if ( IsCancelButtonVisible() ) if ( IsCancelButtonVisible() )
{ {
sizeCancel = m_cancelButton->GetBestSize(); sizeCancel = m_cancelButton->GetBestSize();
cancelMargin = FromDIP(MARGIN); cancelMargin = FromDIP(MARGIN);
textWidth -= horizontalBorder;
} }
if ( sizeSearch.x + sizeCancel.x > width ) if ( sizeSearch.x + sizeCancel.x > width )
@@ -506,15 +514,18 @@ void wxSearchCtrl::LayoutControls()
searchMargin = 0; searchMargin = 0;
cancelMargin = 0; cancelMargin = 0;
} }
wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - FromDIP(1); textWidth -= sizeSearch.x + sizeCancel.x + searchMargin + cancelMargin + FromDIP(1);
if (textWidth < 0) textWidth = 0; if (textWidth < 0) textWidth = 0;
// position the subcontrols inside the client area // position the subcontrols inside the client area
if ( IsSearchButtonVisible() )
{
m_searchButton->SetSize(x, (height - sizeSearch.y) / 2, m_searchButton->SetSize(x, (height - sizeSearch.y) / 2,
sizeSearch.x, sizeSearch.y); sizeSearch.x, sizeSearch.y);
x += sizeSearch.x; x += sizeSearch.x;
x += searchMargin; x += searchMargin;
}
#ifdef __WXMSW__ #ifdef __WXMSW__
// The text control is too high up on Windows; normally a text control looks OK because // The text control is too high up on Windows; normally a text control looks OK because
@@ -528,12 +539,12 @@ void wxSearchCtrl::LayoutControls()
m_text->SetSize(x, textY, textWidth, height-textY); m_text->SetSize(x, textY, textWidth, height-textY);
x += textWidth; x += textWidth;
x += cancelMargin;
if ( m_cancelButton ) if ( IsCancelButtonVisible() )
{ {
x += cancelMargin;
m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2, m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2,
sizeCancel.x, height); sizeCancel.x, sizeCancel.y);
} }
} }
@@ -1134,6 +1145,10 @@ wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y )
}; };
mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase)); mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase));
// Stop drawing on the bitmap before possibly calling RescaleBitmap()
// below.
mem.SelectObject(wxNullBitmap);
//=============================================================================== //===============================================================================
// end drawing code // end drawing code
//=============================================================================== //===============================================================================
@@ -1198,10 +1213,10 @@ void wxSearchCtrl::RecalcBitmaps()
if ( if (
!m_cancelBitmap.IsOk() || !m_cancelBitmap.IsOk() ||
m_cancelBitmap.GetHeight() != bitmapHeight || m_cancelBitmap.GetHeight() != bitmapHeight ||
m_cancelBitmap.GetWidth() != bitmapHeight m_cancelBitmap.GetWidth() != bitmapWidth
) )
{ {
m_cancelBitmap = RenderCancelBitmap(bitmapHeight,bitmapHeight); // square m_cancelBitmap = RenderCancelBitmap(bitmapWidth,bitmapHeight);
m_cancelButton->SetBitmapLabel(m_cancelBitmap); m_cancelButton->SetBitmapLabel(m_cancelBitmap);
} }
// else this bitmap was set by user, don't alter // else this bitmap was set by user, don't alter