diff --git a/docs/changes.txt b/docs/changes.txt index 4a41c873e9..3a288f2ca3 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -62,6 +62,9 @@ Changes in behaviour not resulting in compilation errors - Generic wxDataViewCtrl now always resizes its last column to fill all the available space, as the GTK+ version always did. +- wxGTK wxNotebook::AddPage() doesn't generate any events any more for the + first page being added, for consistency with the other ports. + Changes in behaviour which may result in build errors ----------------------------------------------------- diff --git a/include/wx/qt/notebook.h b/include/wx/qt/notebook.h index 3175f35a0f..6fbb072b7c 100644 --- a/include/wx/qt/notebook.h +++ b/include/wx/qt/notebook.h @@ -42,8 +42,8 @@ public: virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const; - int SetSelection(size_t nPage) { return DoSetSelection(nPage, SetSelection_SendEvent); } - int ChangeSelection(size_t nPage) { return DoSetSelection(nPage); } + int SetSelection(size_t nPage); + int ChangeSelection(size_t nPage); virtual bool DeleteAllPages() wxOVERRIDE; @@ -51,7 +51,6 @@ public: protected: virtual wxWindow *DoRemovePage(size_t page); - int DoSetSelection(size_t nPage, int flags = 0); private: QTabWidget *m_qtTabWidget; diff --git a/interface/wx/bookctrl.h b/interface/wx/bookctrl.h index 7789496765..dc775d00a6 100644 --- a/interface/wx/bookctrl.h +++ b/interface/wx/bookctrl.h @@ -249,7 +249,10 @@ public: The page must have the book control itself as the parent and must not have been added to this control previously. - The call to this function may generate the page changing events. + The call to this function will generate the page changing and page + changed events if @a select is true, but not when inserting the very + first page (as there is no previous page selection to switch from in + this case and so it wouldn't make sense to e.g. veto such event). @param page Specifies the new page. diff --git a/src/gtk/notebook.cpp b/src/gtk/notebook.cpp index 71bd4d8ec2..a47d136a1e 100644 --- a/src/gtk/notebook.cpp +++ b/src/gtk/notebook.cpp @@ -464,7 +464,12 @@ bool wxNotebook::InsertPage( size_t position, pageData->m_label, false, false, m_padding); gtk_widget_show_all(pageData->m_box); + + // Inserting the page may generate selection changing events that are not + // expected here: we will send them ourselves below if necessary. + g_signal_handlers_block_by_func(m_widget, (void*)switch_page, this); gtk_notebook_insert_page(notebook, win->m_widget, pageData->m_box, position); + g_signal_handlers_unblock_by_func(m_widget, (void*)switch_page, this); /* apply current style */ #ifdef __WXGTK3__ @@ -478,10 +483,7 @@ bool wxNotebook::InsertPage( size_t position, } #endif - if (select && GetPageCount() > 1) - { - SetSelection( position ); - } + DoSetSelectionAfterInsertion(position, select); InvalidateBestSize(); return true; diff --git a/src/qt/notebook.cpp b/src/qt/notebook.cpp index 92c9fe3e82..bb5750b6a3 100644 --- a/src/qt/notebook.cpp +++ b/src/qt/notebook.cpp @@ -154,12 +154,8 @@ bool wxNotebook::InsertPage(size_t n, wxWindow *page, const wxString& text, // reenable firing qt signals as internal wx initialization was completed m_qtTabWidget->blockSignals(false); - m_selection = m_qtTabWidget->currentIndex(); - if (bSelect && GetPageCount() > 1) - { - SetSelection( n ); - } + DoSetSelectionAfterInsertion(n, bSelect); return true; } @@ -180,27 +176,31 @@ bool wxNotebook::DeleteAllPages() return true; } -int wxNotebook::DoSetSelection(size_t page, int flags) +int wxNotebook::SetSelection(size_t page) { wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index"); int selOld = GetSelection(); - // do not fire signals for certain methods (i.e. ChangeSelection - if ( !(flags & SetSelection_SendEvent) ) - { - m_qtTabWidget->blockSignals(true); - } // change the QTabWidget selected page: m_selection = page; m_qtTabWidget->setCurrentIndex( page ); - if ( !(flags & SetSelection_SendEvent) ) - { - m_qtTabWidget->blockSignals(false); - } + return selOld; } +int wxNotebook::ChangeSelection(size_t nPage) +{ + // ChangeSelection() is not supposed to generate events, unlike + // SetSelection(). + m_qtTabWidget->blockSignals(true); + + const int selOld = SetSelection(nPage); + + m_qtTabWidget->blockSignals(false); + + return selOld; +} wxWindow *wxNotebook::DoRemovePage(size_t page) { diff --git a/tests/controls/notebooktest.cpp b/tests/controls/notebooktest.cpp index 1812d08a97..60c9cdd31b 100644 --- a/tests/controls/notebooktest.cpp +++ b/tests/controls/notebooktest.cpp @@ -20,7 +20,9 @@ #endif // WX_PRECOMP #include "wx/notebook.h" + #include "bookctrlbasetest.h" +#include "testableframe.h" class NotebookTestCase : public BookCtrlBaseTestCase, public CppUnit::TestCase { @@ -118,4 +120,48 @@ void NotebookTestCase::NoEventsOnDestruction() CHECK( m_numPageChanges == 1 ); } +TEST_CASE("wxNotebook::AddPageEvents", "[wxNotebook][AddPage][event]") +{ + wxNotebook* const + notebook = new wxNotebook(wxTheApp->GetTopWindow(), wxID_ANY, + wxDefaultPosition, wxSize(400, 200)); + + CHECK( notebook->GetSelection() == wxNOT_FOUND ); + + EventCounter countPageChanging(notebook, wxEVT_NOTEBOOK_PAGE_CHANGING); + EventCounter countPageChanged(notebook, wxEVT_NOTEBOOK_PAGE_CHANGED); + + // Add the first page, it is special. + notebook->AddPage(new wxPanel(notebook), "Initial page"); + + // The selection should have been changed. + CHECK( notebook->GetSelection() == 0 ); + + // But no events should have been generated. + CHECK( countPageChanging.GetCount() == 0 ); + CHECK( countPageChanged.GetCount() == 0 ); + + + // Add another page without selecting it. + notebook->AddPage(new wxPanel(notebook), "Unselected page"); + + // Selection shouldn't have changed. + CHECK( notebook->GetSelection() == 0 ); + + // And no events should have been generated, of course. + CHECK( countPageChanging.GetCount() == 0 ); + CHECK( countPageChanged.GetCount() == 0 ); + + + // Finally add another page and do select it. + notebook->AddPage(new wxPanel(notebook), "Selected page", true); + + // It should have become selected. + CHECK( notebook->GetSelection() == 2 ); + + // And events for the selection change should have been generated. + CHECK( countPageChanging.GetCount() == 1 ); + CHECK( countPageChanged.GetCount() == 1 ); +} + #endif //wxUSE_NOTEBOOK