From e8a147a6c3e65cf9f00b92b749df68923251280b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 4 Nov 2006 12:20:09 +0000 Subject: [PATCH] invalidate best size of the book controller too when the pages are added/removed, this fixes a lot of sizing problems with treebook git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43027 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/bookctrl.h | 16 ++- src/common/bookctrl.cpp | 276 +++++++++++++++++++++------------------ src/generic/choicbkg.cpp | 1 - src/generic/listbkg.cpp | 2 - src/generic/treebkg.cpp | 2 - 5 files changed, 157 insertions(+), 140 deletions(-) diff --git a/include/wx/bookctrl.h b/include/wx/bookctrl.h index 33599c7a41..9cb9472600 100644 --- a/include/wx/bookctrl.h +++ b/include/wx/bookctrl.h @@ -168,14 +168,14 @@ public: // remove one page from the notebook, without deleting it virtual bool RemovePage(size_t n) { - InvalidateBestSize(); + DoInvalidateBestSize(); return DoRemovePage(n) != NULL; } // remove all pages and delete them virtual bool DeleteAllPages() { - InvalidateBestSize(); + DoInvalidateBestSize(); WX_CLEAR_ARRAY(m_pages); return true; } @@ -186,7 +186,7 @@ public: bool bSelect = false, int imageId = -1) { - InvalidateBestSize(); + DoInvalidateBestSize(); return InsertPage(GetPageCount(), page, text, bSelect, imageId); } @@ -229,6 +229,7 @@ public: // we do have multiple pages virtual bool HasMultiplePages() const { return true; } + protected: // flags for DoSetSelection() enum @@ -279,6 +280,11 @@ protected: // Lay out controls void DoSize(); + // This method also invalidates the size of the controller and should be + // called instead of just InvalidateBestSize() whenever pages are added or + // removed as this also affects the controller + void DoInvalidateBestSize(); + #if wxUSE_HELP // Show the help for the corresponding page void OnHelp(wxHelpEvent& event); @@ -308,10 +314,10 @@ protected: bool m_fitToCurrentPage; // the sizer containing the choice control - wxSizer* m_controlSizer; + wxSizer *m_controlSizer; // the margin around the choice control - int m_controlMargin; + int m_controlMargin; private: diff --git a/src/common/bookctrl.cpp b/src/common/bookctrl.cpp index fef420180a..32a6ffebe2 100644 --- a/src/common/bookctrl.cpp +++ b/src/common/bookctrl.cpp @@ -125,6 +125,18 @@ void wxBookCtrlBase::AssignImageList(wxImageList* imageList) // geometry // ---------------------------------------------------------------------------- +void wxBookCtrlBase::DoInvalidateBestSize() +{ + // notice that it is not necessary to invalidate our own best size + // explicitly if we have m_bookctrl as it will already invalidate the best + // size of its parent when its own size is invalidated and its parent is + // this control + if ( m_bookctrl ) + m_bookctrl->InvalidateBestSize(); + else + wxControl::InvalidateBestSize(); +} + void wxBookCtrlBase::SetPageSize(const wxSize& size) { SetClientSize(CalcSizeFromPage(size)); @@ -161,136 +173,6 @@ wxSize wxBookCtrlBase::DoGetBestSize() const return best; } -#if wxUSE_HELP - -void wxBookCtrlBase::OnHelp(wxHelpEvent& event) -{ - // determine where does this even originate from to avoid redirecting it - // back to the page which generated it (resulting in an infinite loop) - - // notice that we have to check in the hard(er) way instead of just testing - // if the event object == this because the book control can have other - // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv) - wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow); - while ( source && source != this && source->GetParent() != this ) - { - source = source->GetParent(); - } - - if ( source && m_pages.Index(source) == wxNOT_FOUND ) - { - // this event is for the book control itself, redirect it to the - // corresponding page - wxWindow *page = NULL; - - if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton ) - { - // show help for the page under the mouse - const int pagePos = HitTest(ScreenToClient(event.GetPosition())); - - if ( pagePos != wxNOT_FOUND) - { - page = GetPage((size_t)pagePos); - } - } - else // event from keyboard or unknown source - { - // otherwise show the current page help - page = GetCurrentPage(); - } - - if ( page ) - { - // change event object to the page to avoid infinite recursion if - // we get this event ourselves if the page doesn't handle it - event.SetEventObject(page); - - if ( page->GetEventHandler()->ProcessEvent(event) ) - { - // don't call event.Skip() - return; - } - } - } - //else: event coming from one of our pages already - - event.Skip(); -} - -#endif // wxUSE_HELP - -// ---------------------------------------------------------------------------- -// pages management -// ---------------------------------------------------------------------------- - -bool -wxBookCtrlBase::InsertPage(size_t nPage, - wxWindow *page, - const wxString& WXUNUSED(text), - bool WXUNUSED(bSelect), - int WXUNUSED(imageId)) -{ - wxCHECK_MSG( page || AllowNullPage(), false, - _T("NULL page in wxBookCtrlBase::InsertPage()") ); - wxCHECK_MSG( nPage <= m_pages.size(), false, - _T("invalid page index in wxBookCtrlBase::InsertPage()") ); - - m_pages.Insert(page, nPage); - if ( page ) - page->SetSize(GetPageRect()); - - InvalidateBestSize(); - - return true; -} - -bool wxBookCtrlBase::DeletePage(size_t nPage) -{ - wxWindow *page = DoRemovePage(nPage); - if ( !(page || AllowNullPage()) ) - return false; - - // delete NULL is harmless - delete page; - - return true; -} - -wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) -{ - wxCHECK_MSG( nPage < m_pages.size(), NULL, - _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); - - wxWindow *pageRemoved = m_pages[nPage]; - m_pages.RemoveAt(nPage); - InvalidateBestSize(); - - return pageRemoved; -} - -int wxBookCtrlBase::GetNextPage(bool forward) const -{ - int nPage; - - int nMax = GetPageCount(); - if ( nMax-- ) // decrement it to get the last valid index - { - int nSel = GetSelection(); - - // change selection wrapping if it becomes invalid - nPage = forward ? nSel == nMax ? 0 - : nSel + 1 - : nSel == 0 ? nMax - : nSel - 1; - } - else // notebook is empty, no next page - { - nPage = wxNOT_FOUND; - } - - return nPage; -} - wxRect wxBookCtrlBase::GetPageRect() const { const wxSize size = GetControllerSize(); @@ -423,6 +305,140 @@ wxSize wxBookCtrlBase::GetControllerSize() const return size; } +// ---------------------------------------------------------------------------- +// miscellaneous stuff +// ---------------------------------------------------------------------------- + +#if wxUSE_HELP + +void wxBookCtrlBase::OnHelp(wxHelpEvent& event) +{ + // determine where does this even originate from to avoid redirecting it + // back to the page which generated it (resulting in an infinite loop) + + // notice that we have to check in the hard(er) way instead of just testing + // if the event object == this because the book control can have other + // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv) + wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow); + while ( source && source != this && source->GetParent() != this ) + { + source = source->GetParent(); + } + + if ( source && m_pages.Index(source) == wxNOT_FOUND ) + { + // this event is for the book control itself, redirect it to the + // corresponding page + wxWindow *page = NULL; + + if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton ) + { + // show help for the page under the mouse + const int pagePos = HitTest(ScreenToClient(event.GetPosition())); + + if ( pagePos != wxNOT_FOUND) + { + page = GetPage((size_t)pagePos); + } + } + else // event from keyboard or unknown source + { + // otherwise show the current page help + page = GetCurrentPage(); + } + + if ( page ) + { + // change event object to the page to avoid infinite recursion if + // we get this event ourselves if the page doesn't handle it + event.SetEventObject(page); + + if ( page->GetEventHandler()->ProcessEvent(event) ) + { + // don't call event.Skip() + return; + } + } + } + //else: event coming from one of our pages already + + event.Skip(); +} + +#endif // wxUSE_HELP + +// ---------------------------------------------------------------------------- +// pages management +// ---------------------------------------------------------------------------- + +bool +wxBookCtrlBase::InsertPage(size_t nPage, + wxWindow *page, + const wxString& WXUNUSED(text), + bool WXUNUSED(bSelect), + int WXUNUSED(imageId)) +{ + wxCHECK_MSG( page || AllowNullPage(), false, + _T("NULL page in wxBookCtrlBase::InsertPage()") ); + wxCHECK_MSG( nPage <= m_pages.size(), false, + _T("invalid page index in wxBookCtrlBase::InsertPage()") ); + + m_pages.Insert(page, nPage); + if ( page ) + page->SetSize(GetPageRect()); + + DoInvalidateBestSize(); + + return true; +} + +bool wxBookCtrlBase::DeletePage(size_t nPage) +{ + wxWindow *page = DoRemovePage(nPage); + if ( !(page || AllowNullPage()) ) + return false; + + // delete NULL is harmless + delete page; + + return true; +} + +wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) +{ + wxCHECK_MSG( nPage < m_pages.size(), NULL, + _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); + + wxWindow *pageRemoved = m_pages[nPage]; + m_pages.RemoveAt(nPage); + DoInvalidateBestSize(); + + return pageRemoved; +} + +int wxBookCtrlBase::GetNextPage(bool forward) const +{ + int nPage; + + int nMax = GetPageCount(); + if ( nMax-- ) // decrement it to get the last valid index + { + int nSel = GetSelection(); + + // change selection wrapping if it becomes invalid + nPage = forward ? nSel == nMax ? 0 + : nSel + 1 + : nSel == 0 ? nMax + : nSel - 1; + } + else // notebook is empty, no next page + { + nPage = wxNOT_FOUND; + } + + return nPage; +} + 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 096d5e4d2f..6eb0219821 100644 --- a/src/generic/choicbkg.cpp +++ b/src/generic/choicbkg.cpp @@ -257,7 +257,6 @@ wxChoicebook::InsertPage(size_t n, if ( selNew != wxNOT_FOUND ) SetSelection(selNew); - InvalidateBestSize(); return true; } diff --git a/src/generic/listbkg.cpp b/src/generic/listbkg.cpp index a9498bdf3c..a898d98276 100644 --- a/src/generic/listbkg.cpp +++ b/src/generic/listbkg.cpp @@ -325,8 +325,6 @@ wxListbook::InsertPage(size_t n, if ( selNew != -1 ) SetSelection(selNew); - InvalidateBestSize(); - // GetListView()->InvalidateBestSize(); GetListView()->Arrange(); if (GetPageCount() == 1) diff --git a/src/generic/treebkg.cpp b/src/generic/treebkg.cpp index 8f34b7ef4d..3b8b22c21e 100644 --- a/src/generic/treebkg.cpp +++ b/src/generic/treebkg.cpp @@ -258,8 +258,6 @@ bool wxTreebook::DoInsertSubPage(size_t pagePos, wxTreeItemId newId = tree->AppendItem(parentId, text, imageId); - tree->InvalidateBestSize(); - if ( !newId.IsOk() ) { (void)wxBookCtrlBase::DoRemovePage(newPos); -- 2.45.2