From 67bceedfdab3b9ad8163d0ad75fdbae5163b69fd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jul 2018 17:12:02 +0200 Subject: [PATCH] Get rid of m_searchButtonVisible and m_cancelButtonVisible There doesn't seem to be any reason to have these flags, which duplicate the visibility state of m_searchButton and m_cancelButton respectively. Also update the buttons visibility immediately in ShowSearchButton() and ShowCancelButton() instead of doing it in LayoutControls() as before, which was confusing as laying out is not supposed to show/hide anything. Finally, return true, not false, from IsSearchButtonVisible() if the button is actually visible because there is an associated menu, even if ShowSearchButton(false) had been called. This seems more logical and makes the code simpler, but we need to check whether the native Mac version also behaves like this or not. No other changes in behaviour. --- include/wx/generic/srchctlg.h | 3 -- src/generic/srchctlg.cpp | 61 ++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index 7ac2878698..996f4e8560 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -237,9 +237,6 @@ private: wxMenu *m_menu; #endif // wxUSE_MENUS - bool m_searchButtonVisible; - bool m_cancelButtonVisible; - bool m_searchBitmapUser; bool m_cancelBitmapUser; #if wxUSE_MENUS diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 9482c1e3c1..455dd1efa6 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -284,9 +284,6 @@ void wxSearchCtrl::Init() m_menu = NULL; #endif // wxUSE_MENUS - m_searchButtonVisible = true; - m_cancelButtonVisible = false; - m_searchBitmapUser = false; m_cancelBitmapUser = false; #if wxUSE_MENUS @@ -313,9 +310,6 @@ bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id, m_searchButton = new wxSearchButton(this, wxEVT_SEARCH, m_searchBitmap); - m_cancelButton = new wxSearchButton(this, - wxEVT_SEARCH_CANCEL, - m_cancelBitmap); SetBackgroundColour( m_text->GetBackgroundColour() ); m_text->SetBackgroundColour(wxColour()); @@ -354,13 +348,14 @@ void wxSearchCtrl::SetMenu( wxMenu* menu ) if ( m_menu && !hadMenu ) { + m_searchButton->Show(); m_searchButton->SetBitmapLabel(m_searchMenuBitmap); m_searchButton->Refresh(); } else if ( !m_menu && hadMenu ) { m_searchButton->SetBitmapLabel(m_searchBitmap); - if ( m_searchButtonVisible ) + if ( m_searchButton->IsShown() ) { m_searchButton->Refresh(); } @@ -377,15 +372,23 @@ wxMenu* wxSearchCtrl::GetMenu() void wxSearchCtrl::ShowSearchButton( bool show ) { - if ( m_searchButtonVisible == show ) + if ( show == IsSearchButtonVisible() ) { // no change return; } - m_searchButtonVisible = show; - if ( m_searchButtonVisible ) + if ( show ) { RecalcBitmaps(); + + m_searchButton->Show(); + } + else // Requested to hide it. + { + // Only hide the button if we don't need it for the menu, otherwise it + // needs to remain shown. + if ( !HasMenu() ) + m_searchButton->Hide(); } LayoutControls(); @@ -393,25 +396,36 @@ void wxSearchCtrl::ShowSearchButton( bool show ) bool wxSearchCtrl::IsSearchButtonVisible() const { - return m_searchButtonVisible; + return m_searchButton->IsShown() || HasMenu(); } void wxSearchCtrl::ShowCancelButton( bool show ) { - if ( m_cancelButtonVisible == show ) + if ( show == IsCancelButtonVisible() ) { // no change return; } - m_cancelButtonVisible = show; + + // This button is not shown initially, so create it on demand if necessary, + // i.e. if it's the first time we show it. + if ( !m_cancelButton ) + { + m_cancelButton = new wxSearchButton(this, + wxEVT_SEARCH_CANCEL, + m_cancelBitmap); + RecalcBitmaps(); + } + + m_cancelButton->Show(show); LayoutControls(); } bool wxSearchCtrl::IsCancelButtonVisible() const { - return m_cancelButtonVisible; + return m_cancelButton && m_cancelButton->IsShown(); } void wxSearchCtrl::SetDescriptiveText(const wxString& text) @@ -435,12 +449,12 @@ wxSize wxSearchCtrl::DoGetBestClientSize() const wxSize sizeCancel(0,0); int searchMargin = 0; int cancelMargin = 0; - if ( m_searchButtonVisible || HasMenu() ) + if ( IsSearchButtonVisible() ) { sizeSearch = m_searchButton->GetBestSize(); searchMargin = FromDIP(MARGIN); } - if ( m_cancelButtonVisible ) + if ( IsCancelButtonVisible() ) { sizeCancel = m_cancelButton->GetBestSize(); cancelMargin = FromDIP(MARGIN); @@ -481,18 +495,16 @@ void wxSearchCtrl::LayoutControls() wxSize sizeCancel(0,0); int searchMargin = 0; int cancelMargin = 0; - if ( m_searchButtonVisible || HasMenu() ) + if ( IsSearchButtonVisible() ) { sizeSearch = m_searchButton->GetBestSize(); searchMargin = FromDIP(MARGIN); } - if ( m_cancelButtonVisible ) + if ( IsCancelButtonVisible() ) { sizeCancel = m_cancelButton->GetBestSize(); cancelMargin = FromDIP(MARGIN); } - m_searchButton->Show( m_searchButtonVisible || HasMenu() ); - m_cancelButton->Show( m_cancelButtonVisible ); if ( sizeSearch.x + sizeCancel.x > width ) { @@ -525,8 +537,11 @@ void wxSearchCtrl::LayoutControls() x += textWidth; x += cancelMargin; - m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2, - sizeCancel.x, height); + if ( m_cancelButton ) + { + m_cancelButton->SetSize(x, (height - sizeCancel.y) / 2, + sizeCancel.x, height); + } } wxWindowList wxSearchCtrl::GetCompositeWindowParts() const @@ -1185,7 +1200,7 @@ void wxSearchCtrl::RecalcBitmaps() } #endif // wxUSE_MENUS - if ( !m_cancelBitmapUser ) + if ( m_cancelButton && !m_cancelBitmapUser ) { if ( !m_cancelBitmap.IsOk() ||