From 60d5c563d29ef0d99cdcfb5ac235f62f64476002 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 31 Oct 2010 13:33:34 +0000 Subject: [PATCH] Avoid events when implicitly selecting first wxBookCtrl page. The first page added to a wxBookCtrlBase-derived control is always selected, even if "bSelect" argument of AddPage() was false. This is necessary because a non-empty book control must always have a selection but the "selection changed" event generated when doing it is unexpected. Fix this by not generating any events when the first page is implicitly selected. Closes #12075. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65967 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/bookctrl.h | 11 +++++++++++ src/common/bookctrl.cpp | 13 +++++++++++++ src/generic/choicbkg.cpp | 13 +------------ src/generic/listbkg.cpp | 13 +------------ src/msw/notebook.cpp | 11 +---------- src/os2/notebook.cpp | 14 +------------- src/osx/notebook_osx.cpp | 11 +---------- 7 files changed, 29 insertions(+), 57 deletions(-) diff --git a/include/wx/bookctrl.h b/include/wx/bookctrl.h index c7c7c27f9c..86a89acfd1 100644 --- a/include/wx/bookctrl.h +++ b/include/wx/bookctrl.h @@ -252,6 +252,17 @@ protected: // choose the default border for this window virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; } + // After the insertion of the page in the method InsertPage, calling this + // method sets the selection to the given page or the first one if there is + // still no selection. The "selection changed" event is sent only if + // bSelect is true, so when it is false, no event is sent even if the + // selection changed from wxNOT_FOUND to 0 when inserting the first page. + // + // Returns true if the selection was set to the specified page (explicitly + // because of bSelect == true or implicitly because it's the first page) or + // false otherwise. + bool DoSetSelectionAfterInsertion(size_t n, bool bSelect); + // set the selection to the given page, sending the events (which can // possibly prevent the page change from taking place) if SendEvent flag is // included diff --git a/src/common/bookctrl.cpp b/src/common/bookctrl.cpp index cd3baf71e6..5392388681 100644 --- a/src/common/bookctrl.cpp +++ b/src/common/bookctrl.cpp @@ -471,6 +471,19 @@ int wxBookCtrlBase::GetNextPage(bool forward) const return nPage; } +bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n, bool bSelect) +{ + if ( bSelect ) + SetSelection(n); + else if ( m_selection == wxNOT_FOUND ) + ChangeSelection(0); + else // We're not going to select this page. + return false; + + // Return true to indicate that we selected this page. + return true; +} + int wxBookCtrlBase::DoSetSelection(size_t n, int flags) { wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND, diff --git a/src/generic/choicbkg.cpp b/src/generic/choicbkg.cpp index 09e9465ffd..52fd088591 100644 --- a/src/generic/choicbkg.cpp +++ b/src/generic/choicbkg.cpp @@ -195,20 +195,9 @@ wxChoicebook::InsertPage(size_t n, GetChoiceCtrl()->Select(m_selection); } - // some page should be selected: either this one or the first one if there - // is still no selection - int selNew = wxNOT_FOUND; - if ( bSelect ) - selNew = n; - else if ( m_selection == wxNOT_FOUND ) - selNew = 0; - - if ( selNew != m_selection ) + if ( !DoSetSelectionAfterInsertion(n, bSelect) ) page->Hide(); - if ( selNew != wxNOT_FOUND ) - SetSelection(selNew); - return true; } diff --git a/src/generic/listbkg.cpp b/src/generic/listbkg.cpp index fddedd14ae..0861211c0e 100644 --- a/src/generic/listbkg.cpp +++ b/src/generic/listbkg.cpp @@ -343,20 +343,9 @@ wxListbook::InsertPage(size_t n, GetListView()->Focus(m_selection); } - // some page should be selected: either this one or the first one if there - // is still no selection - int selNew = wxNOT_FOUND; - if ( bSelect ) - selNew = n; - else if ( m_selection == wxNOT_FOUND ) - selNew = 0; - - if ( selNew != m_selection ) + if ( !DoSetSelectionAfterInsertion(n, bSelect) ) page->Hide(); - if ( selNew != wxNOT_FOUND ) - SetSelection(selNew); - UpdateSize(); return true; diff --git a/src/msw/notebook.cpp b/src/msw/notebook.cpp index f5487c26b9..046096b05f 100644 --- a/src/msw/notebook.cpp +++ b/src/msw/notebook.cpp @@ -842,16 +842,7 @@ bool wxNotebook::InsertPage(size_t nPage, m_selection++; } - // some page should be selected: either this one or the first one if there - // is still no selection - int selNew = wxNOT_FOUND; - if ( bSelect ) - selNew = nPage; - else if ( m_selection == wxNOT_FOUND ) - selNew = 0; - - if ( selNew != wxNOT_FOUND ) - SetSelection(selNew); + DoSetSelectionAfterInsertion(nPage, bSelect); InvalidateBestSize(); diff --git a/src/os2/notebook.cpp b/src/os2/notebook.cpp index fb45fe10a3..f0a88c0c6a 100644 --- a/src/os2/notebook.cpp +++ b/src/os2/notebook.cpp @@ -648,19 +648,7 @@ bool wxNotebook::InsertPage ( size_t nPage, pPage->Show(false); } - // - // Some page should be selected: either this one or the first one if there is - // still no selection - // - int nSelNew = wxNOT_FOUND; - - if (bSelect) - nSelNew = nPage; - else if ( m_selection == wxNOT_FOUND ) - nSelNew = 0; - - if (nSelNew != wxNOT_FOUND) - SetSelection(nSelNew); + DoSetSelectionAfterInsertion(nPage, bSelect); InvalidateBestSize(); diff --git a/src/osx/notebook_osx.cpp b/src/osx/notebook_osx.cpp index 110a36e991..fb58c48509 100644 --- a/src/osx/notebook_osx.cpp +++ b/src/osx/notebook_osx.cpp @@ -240,16 +240,7 @@ bool wxNotebook::InsertPage(size_t nPage, m_peer->SetValue( m_selection + 1 ) ; } - // some page should be selected: either this one or the first one if there - // is still no selection - int selNew = wxNOT_FOUND; - if ( bSelect ) - selNew = nPage; - else if ( m_selection == wxNOT_FOUND ) - selNew = 0; - - if ( selNew != wxNOT_FOUND ) - SetSelection( selNew ); + DoSetSelectionAfterInsertion(nPage, bSelect); InvalidateBestSize(); -- 2.45.2