- wxNotebookPage* nb_page = GetNotebookPage(page);
- if (!nb_page) return FALSE;
-
- int page_num = 0;
- GList *child = GTK_NOTEBOOK(m_widget)->children;
- while (child)
- {
- if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
- page_num++;
- child = child->next;
- }
+ // We cannot remove the page yet, as GTK sends the "switch_page"
+ // signal before it has removed the notebook-page from its
+ // corresponding list. Thus, if we were to remove the page from
+ // m_pages at this point, the two lists of pages would be out
+ // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
+ wxNotebookPage *client = GetPage(page);
+ if ( !client )
+ return NULL;
+
+ gtk_widget_unrealize( client->m_widget );
+
+ // we don't need to unparent the client->m_widget; GTK+ will do
+ // that for us (and will throw a warning if we do it!)
+ gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
+
+ // It's safe to remove the page now.
+ wxASSERT_MSG(GetPage(page) == client, wxT("pages changed during delete"));
+ wxNotebookBase::DoRemovePage(page);
+
+ wxGtkNotebookPage* p = GetNotebookPage(page);
+ m_pagesData.DeleteObject(p);
+ delete p;
+
+ return client;
+}