X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b0c0a393c4027a46b83187ea1328949ca75971d0..9e9574fe45b176ee74bba8fad7574cf9906145d1:/src/generic/notebook.cpp diff --git a/src/generic/notebook.cpp b/src/generic/notebook.cpp index 756369436e..8caf0cda1e 100644 --- a/src/generic/notebook.cpp +++ b/src/generic/notebook.cpp @@ -1,10 +1,9 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: notebook.cpp -// Purpose: implementation of wxNotebook +// Name: src/generic/notebook.cpp +// Purpose: generic implementation of wxNotebook // Author: Julian Smart // Modified by: // Created: 17/09/98 -// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -16,27 +15,26 @@ // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "notebook.h" -#endif - -#ifdef __VMS -#pragma message disable unscomzer -#endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#include "wx/string.h" -#include "wx/log.h" -#include "wx/settings.h" -#include "wx/generic/imaglist.h" +#if wxUSE_NOTEBOOK + #include "wx/notebook.h" -#include "wx/dcclient.h" + +#ifndef WX_PRECOMP + #include "wx/string.h" + #include "wx/log.h" + #include "wx/dcclient.h" + #include "wx/settings.h" +#endif + +#include "wx/imaglist.h" #include "wx/generic/tabg.h" // ---------------------------------------------------------------------------- @@ -44,17 +42,14 @@ // ---------------------------------------------------------------------------- // check that the page index is valid -#define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) +#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) // ---------------------------------------------------------------------------- // event table // ---------------------------------------------------------------------------- -DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) - -BEGIN_EVENT_TABLE(wxNotebook, wxControl) - EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) +BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase) + EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange) EVT_SIZE(wxNotebook::OnSize) EVT_PAINT(wxNotebook::OnPaint) EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent) @@ -62,9 +57,6 @@ BEGIN_EVENT_TABLE(wxNotebook, wxControl) EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) END_EVENT_TABLE() -IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) -IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) - // ============================================================================ // implementation // ============================================================================ @@ -73,23 +65,43 @@ IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) // Private class // ============================================================================ +WX_DECLARE_HASH_MAP(int, wxNotebookPage*, wxIntegerHash, wxIntegerEqual, + wxIntToNotebookPageHashMap); + +WX_DECLARE_HASH_MAP(wxNotebookPage*, int, wxPointerHash, wxPointerEqual, + wxNotebookPageToIntHashMap); + // This reuses wxTabView to draw the tabs. class WXDLLEXPORT wxNotebookTabView: public wxTabView { DECLARE_DYNAMIC_CLASS(wxNotebookTabView) public: - wxNotebookTabView(wxNotebook* notebook, long style = wxTAB_STYLE_DRAW_BOX | wxTAB_STYLE_COLOUR_INTERIOR); - ~wxNotebookTabView(void); + wxNotebookTabView(wxNotebook* notebook, long style = wxTAB_STYLE_DRAW_BOX | wxTAB_STYLE_COLOUR_INTERIOR); + virtual ~wxNotebookTabView(void); - // Called when a tab is activated - virtual void OnTabActivate(int activateId, int deactivateId); - // Allows vetoing - virtual bool OnTabPreActivate(int activateId, int deactivateId); + // Called when a tab is activated + virtual void OnTabActivate(int activateId, int deactivateId); + // Allows vetoing + virtual bool OnTabPreActivate(int activateId, int deactivateId); + + // map integer ids used by wxTabView to wxNotebookPage pointers + int GetId(wxNotebookPage *page); + wxNotebookPage *GetPage(int id) { return m_idToPage[id]; } protected: - wxNotebook* m_notebook; + wxNotebook* m_notebook; + +private: + wxIntToNotebookPageHashMap m_idToPage; + wxNotebookPageToIntHashMap m_pageToId; + int m_nextid; }; +static int GetPageId(wxTabView *tabview, wxNotebookPage *page) +{ + return static_cast(tabview)->GetId(page); +} + // ---------------------------------------------------------------------------- // wxNotebook construction // ---------------------------------------------------------------------------- @@ -97,8 +109,8 @@ protected: // common part of all ctors void wxNotebook::Init() { - m_tabView = (wxNotebookTabView*) NULL; - m_nSelection = -1; + m_tabView = NULL; + m_selection = -1; } // default for dynamic class @@ -131,16 +143,17 @@ bool wxNotebook::Create(wxWindow *parent, // base init SetName(name); - m_windowId = id == -1 ? NewControlId() : id; + if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) + style |= wxBK_TOP; - if (!wxControl::Create(parent, id, pos, size, style|wxNO_BORDER, wxDefaultValidator, name)) - return FALSE; + m_windowId = id == wxID_ANY ? NewControlId() : id; - SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); + if (!wxControl::Create(parent, id, pos, size, style|wxNO_BORDER, wxDefaultValidator, name)) + return false; SetTabView(new wxNotebookTabView(this)); - return TRUE; + return true; } // dtor @@ -162,17 +175,20 @@ int wxNotebook::SetSelection(size_t nPage) { wxASSERT( IS_VALID_PAGE(nPage) ); -#if defined (__WIN16__) - m_tabView->SetTabSelection(nPage); -#else wxNotebookPage* pPage = GetPage(nPage); - m_tabView->SetTabSelection((int) (long) pPage); -#endif + m_tabView->SetTabSelection(GetPageId(m_tabView, pPage)); + // TODO return 0; } +int wxNotebook::ChangeSelection(size_t nPage) +{ + // FIXME: currently it does generate events too + return SetSelection(nPage); +} + #if 0 void wxNotebook::AdvanceSelection(bool bForward) { @@ -188,38 +204,30 @@ void wxNotebook::AdvanceSelection(bool bForward) bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) { wxASSERT( IS_VALID_PAGE(nPage) ); -#if defined (__WIN16__) - m_tabView->SetTabText(nPage, strText); - Refresh(); - return TRUE; -#else + wxNotebookPage* page = GetPage(nPage); if (page) { - m_tabView->SetTabText((int) (long) page, strText); + m_tabView->SetTabText(GetPageId(m_tabView, page), strText); Refresh(); - return TRUE; + return true; } -#endif - return FALSE; + + return false; } wxString wxNotebook::GetPageText(size_t nPage) const { wxASSERT( IS_VALID_PAGE(nPage) ); -#if defined (__WIN16__) - return m_tabView->GetTabText(nPage); -#else wxNotebookPage* page = ((wxNotebook*)this)->GetPage(nPage); if (page) - return m_tabView->GetTabText((int) (long) page); + return m_tabView->GetTabText(GetPageId(m_tabView, page)); else return wxEmptyString; -#endif } -int wxNotebook::GetPageImage(size_t nPage) const +int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage)) const { wxASSERT( IS_VALID_PAGE(nPage) ); @@ -227,28 +235,29 @@ int wxNotebook::GetPageImage(size_t nPage) const return 0; } -bool wxNotebook::SetPageImage(size_t nPage, int nImage) +bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage), + int WXUNUSED(nImage)) { wxASSERT( IS_VALID_PAGE(nPage) ); // TODO - return FALSE; + return false; } // set the size (the same for all pages) -void wxNotebook::SetPageSize(const wxSize& size) +void wxNotebook::SetPageSize(const wxSize& WXUNUSED(size)) { // TODO } // set the padding between tabs (in pixels) -void wxNotebook::SetPadding(const wxSize& padding) +void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding)) { // TODO } // set the size of the tabs for wxNB_FIXEDWIDTH controls -void wxNotebook::SetTabSize(const wxSize& sz) +void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) { // TODO } @@ -260,44 +269,39 @@ void wxNotebook::SetTabSize(const wxSize& sz) // remove one page from the notebook and delete it bool wxNotebook::DeletePage(size_t nPage) { - wxCHECK( IS_VALID_PAGE(nPage), FALSE ); + wxCHECK( IS_VALID_PAGE(nPage), false ); - if (m_nSelection != -1) + if (m_selection != -1) { - m_pages[m_nSelection]->Show(FALSE); - m_pages[m_nSelection]->Lower(); + m_pages[m_selection]->Show(false); + m_pages[m_selection]->Lower(); } wxNotebookPage* pPage = GetPage(nPage); -#if defined (__WIN16__) - m_tabView->RemoveTab(nPage); -#else - m_tabView->RemoveTab((int) (long) pPage); -#endif + + m_tabView->RemoveTab(GetPageId(m_tabView, pPage)); m_pages.Remove(pPage); delete pPage; if (m_pages.GetCount() == 0) { - m_nSelection = -1; - m_tabView->SetTabSelection(-1, FALSE); + m_selection = -1; + m_tabView->SetTabSelection(-1, false); } - else if (m_nSelection > -1) + else if (m_selection > -1) { - m_nSelection = -1; -#if defined (__WIN16__) - m_tabView->SetTabSelection(0, FALSE); -#else - m_tabView->SetTabSelection((int) (long) GetPage(0), FALSE); -#endif - if (m_nSelection != 0) - ChangePage(-1, 0); + m_selection = -1; + + m_tabView->SetTabSelection(GetPageId(m_tabView, GetPage(0)), false); + + if (m_selection != 0) + ChangePage(-1, 0); } - RefreshLayout(FALSE); + RefreshLayout(false); - return TRUE; + return true; } bool wxNotebook::DeletePage(wxNotebookPage* page) @@ -306,7 +310,7 @@ bool wxNotebook::DeletePage(wxNotebookPage* page) if (pagePos > -1) return DeletePage(pagePos); else - return FALSE; + return false; } bool wxNotebook::RemovePage(size_t nPage) @@ -317,46 +321,43 @@ bool wxNotebook::RemovePage(size_t nPage) // remove one page from the notebook wxWindow* wxNotebook::DoRemovePage(size_t nPage) { - wxCHECK( IS_VALID_PAGE(nPage), FALSE ); + wxCHECK( IS_VALID_PAGE(nPage), NULL ); - m_pages[nPage]->Show(FALSE); + m_pages[nPage]->Show(false); // m_pages[nPage]->Lower(); wxNotebookPage* pPage = GetPage(nPage); -#if defined (__WIN16__) - m_tabView->RemoveTab(nPage); -#else - m_tabView->RemoveTab((int) (long) pPage); -#endif + + m_tabView->RemoveTab(GetPageId(m_tabView, pPage)); m_pages.Remove(pPage); if (m_pages.GetCount() == 0) { - m_nSelection = -1; - m_tabView->SetTabSelection(-1, TRUE); + m_selection = -1; + m_tabView->SetTabSelection(-1, true); } - else if (m_nSelection > -1) + else if (m_selection > -1) { // Only change the selection if the page we // deleted was the selection. - if (nPage == (size_t)m_nSelection) + if (nPage == (size_t)m_selection) { - m_nSelection = -1; + m_selection = -1; // Select the first tab. Generates a ChangePage. - m_tabView->SetTabSelection(0, TRUE); + m_tabView->SetTabSelection(0, true); } else { - // We must adjust which tab we think is selected. + // We must adjust which tab we think is selected. // If greater than the page we deleted, it must be moved down // a notch. - if (size_t(m_nSelection) > nPage) - m_nSelection -- ; + if (size_t(m_selection) > nPage) + m_selection -- ; } } - RefreshLayout(FALSE); + RefreshLayout(false); return pPage; } @@ -367,7 +368,7 @@ bool wxNotebook::RemovePage(wxNotebookPage* page) if (pagePos > -1) return RemovePage(pagePos); else - return FALSE; + return false; } // Find the position of the wxNotebookPage, -1 if not found. @@ -384,7 +385,7 @@ int wxNotebook::FindPagePosition(wxNotebookPage* page) const // remove all pages bool wxNotebook::DeleteAllPages() { - m_tabView->ClearTabs(TRUE); + m_tabView->ClearTabs(true); size_t nPageCount = GetPageCount(); size_t nPage; @@ -393,7 +394,7 @@ bool wxNotebook::DeleteAllPages() m_pages.Clear(); - return TRUE; + return true; } // same as AddPage() but does it at given position @@ -401,19 +402,15 @@ bool wxNotebook::InsertPage(size_t nPage, wxNotebookPage *pPage, const wxString& strText, bool bSelect, - int imageId) + int WXUNUSED(imageId)) { wxASSERT( pPage != NULL ); - wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE ); + wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false ); + + m_tabView->AddTab(GetPageId(m_tabView, pPage), strText); -// For 16 bit integers (tabs limited to 32768) -#if defined (__WIN16__) - m_tabView->AddTab(nPage, strText); -#else - m_tabView->AddTab((int) (long) pPage, strText); -#endif if (!bSelect) - pPage->Show(FALSE); + pPage->Show(false); // save the pointer to the page m_pages.Insert(pPage, nPage); @@ -421,21 +418,18 @@ bool wxNotebook::InsertPage(size_t nPage, if (bSelect) { // This will cause ChangePage to be called, via OnSelPage -#if defined (__WIN16__) - m_tabView->SetTabSelection(nPage, TRUE); -#else - m_tabView->SetTabSelection((int) (long) pPage, TRUE); -#endif + + m_tabView->SetTabSelection(GetPageId(m_tabView, pPage), true); } // some page must be selected: either this one or the first one if there is // still no selection - if ( m_nSelection == -1 ) + if ( m_selection == -1 ) ChangePage(-1, 0); - RefreshLayout(FALSE); + RefreshLayout(false); - return TRUE; + return true; } // ---------------------------------------------------------------------------- @@ -446,10 +440,10 @@ bool wxNotebook::InsertPage(size_t nPage, // time because doing it in ::Create() doesn't work (for unknown reasons) void wxNotebook::OnSize(wxSizeEvent& event) { - static bool s_bFirstTime = TRUE; + static bool s_bFirstTime = true; if ( s_bFirstTime ) { // TODO: any first-time-size processing. - s_bFirstTime = FALSE; + s_bFirstTime = false; } RefreshLayout(); @@ -465,8 +459,8 @@ void wxNotebook::OnInternalIdle() { wxWindow::OnInternalIdle(); -#if 0 - static bool s_bFirstTime = TRUE; +#if 0 + static bool s_bFirstTime = true; if ( s_bFirstTime ) { /* wxSize sz(GetSize()); @@ -483,7 +477,7 @@ void wxNotebook::OnInternalIdle() GetEventHandler()->ProcessEvent(sizeEvent); Refresh(); */ - s_bFirstTime = FALSE; + s_bFirstTime = false; } #endif } @@ -524,7 +518,7 @@ bool wxNotebook::RefreshLayout(bool force) m_tabView->LayoutTabs(); if (!force && (rect == oldRect)) - return FALSE; + return false; // fit the notebook page to the tab control's display area @@ -541,15 +535,15 @@ bool wxNotebook::RefreshLayout(bool force) } Refresh(); } - return TRUE; + return true; } -void wxNotebook::OnSelChange(wxNotebookEvent& event) +void wxNotebook::OnSelChange(wxBookCtrlEvent& event) { // is it our tab control? if ( event.GetEventObject() == this ) { - if (event.GetSelection() != m_nSelection) + if (event.GetSelection() != m_selection) ChangePage(event.GetOldSelection(), event.GetSelection()); } @@ -560,23 +554,25 @@ void wxNotebook::OnSelChange(wxNotebookEvent& event) void wxNotebook::OnSetFocus(wxFocusEvent& event) { // set focus to the currently selected page if any - if ( m_nSelection != -1 ) - m_pages[m_nSelection]->SetFocus(); + if ( m_selection != -1 ) + m_pages[m_selection]->SetFocus(); event.Skip(); } void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) { - if ( event.IsWindowChange() ) { + if ( event.IsWindowChange() ) + { // change pages AdvanceSelection(event.GetDirection()); } else { // pass to the parent - if ( GetParent() ) { + if ( GetParent() ) + { event.SetCurrentFocus(this); - GetParent()->ProcessEvent(event); + GetParent()->ProcessWindowEvent(event); } } } @@ -590,12 +586,12 @@ void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) void wxNotebook::SetConstraintSizes(bool /* recurse */) { // don't set the sizes of the pages - their correct size is not yet known - wxControl::SetConstraintSizes(FALSE); + wxControl::SetConstraintSizes(false); } bool wxNotebook::DoPhase(int /* nPhase */) { - return TRUE; + return true; } void wxNotebook::Command(wxCommandEvent& WXUNUSED(event)) @@ -614,7 +610,7 @@ void wxNotebook::ChangePage(int nOldSel, int nSel) wxASSERT( nOldSel != nSel ); // impossible if ( nOldSel != -1 ) { - m_pages[nOldSel]->Show(FALSE); + m_pages[nOldSel]->Show(false); m_pages[nOldSel]->Lower(); } @@ -625,11 +621,11 @@ void wxNotebook::ChangePage(int nOldSel, int nSel) Refresh(); - pPage->Show(TRUE); + pPage->Show(true); pPage->Raise(); pPage->SetFocus(); - m_nSelection = nSel; + m_selection = nSel; } void wxNotebook::OnMouseEvent(wxMouseEvent& event) @@ -645,6 +641,17 @@ void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event) ) m_tabView->Draw(dc); } +wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const +{ + // MBN: since the total tab height is really a function of the + // width, this should really call + // GetTotalTabHeightPretendingWidthIs(), but the current + // implementation will suffice, provided the wxNotebook has been + // created with a sensible initial width. + return wxSize( sizePage.x + 12, + sizePage.y + m_tabView->GetTotalTabHeight() + 6 + 4 ); +} + wxRect wxNotebook::GetAvailableClientSize() { int cw, ch; @@ -668,7 +675,8 @@ wxRect wxNotebook::GetAvailableClientSize() IMPLEMENT_CLASS(wxNotebookTabView, wxTabView) -wxNotebookTabView::wxNotebookTabView(wxNotebook *notebook, long style): wxTabView(style) +wxNotebookTabView::wxNotebookTabView(wxNotebook *notebook, long style) + : wxTabView(style), m_nextid(1) { m_notebook = notebook; @@ -681,27 +689,35 @@ wxNotebookTabView::~wxNotebookTabView(void) { } +int wxNotebookTabView::GetId(wxNotebookPage *page) +{ + int& id = m_pageToId[page]; + + if (!id) + { + id = m_nextid++; + m_idToPage[id] = page; + } + + return id; +} + // Called when a tab is activated void wxNotebookTabView::OnTabActivate(int activateId, int deactivateId) { if (!m_notebook) return; - wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_notebook->GetId()); + wxBookCtrlEvent event(wxEVT_NOTEBOOK_PAGE_CHANGED, m_notebook->GetId()); -#if defined (__WIN16__) - int activatePos = activateId; - int deactivatePos = deactivateId; -#else // Translate from wxTabView's ids (which aren't position-dependent) // to wxNotebook's (which are). - wxNotebookPage* pActive = (wxNotebookPage*) activateId; - wxNotebookPage* pDeactive = (wxNotebookPage*) deactivateId; + wxNotebookPage* pActive = GetPage(activateId); + wxNotebookPage* pDeactive = GetPage(deactivateId); int activatePos = m_notebook->FindPagePosition(pActive); int deactivatePos = m_notebook->FindPagePosition(pDeactive); -#endif event.SetEventObject(m_notebook); event.SetSelection(activatePos); event.SetOldSelection(deactivatePos); @@ -711,25 +727,20 @@ void wxNotebookTabView::OnTabActivate(int activateId, int deactivateId) // Allows Vetoing bool wxNotebookTabView::OnTabPreActivate(int activateId, int deactivateId) { - bool retval = TRUE; - + bool retval = true; + if (m_notebook) { - wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_notebook->GetId()); + wxBookCtrlEvent event(wxEVT_NOTEBOOK_PAGE_CHANGING, m_notebook->GetId()); -#if defined (__WIN16__) - int activatePos = activateId; - int deactivatePos = deactivateId; -#else // Translate from wxTabView's ids (which aren't position-dependent) // to wxNotebook's (which are). - wxNotebookPage* pActive = (wxNotebookPage*) activateId; - wxNotebookPage* pDeactive = (wxNotebookPage*) deactivateId; + wxNotebookPage* pActive = GetPage(activateId); + wxNotebookPage* pDeactive = GetPage(deactivateId); int activatePos = m_notebook->FindPagePosition(pActive); int deactivatePos = m_notebook->FindPagePosition(pDeactive); -#endif event.SetEventObject(m_notebook); event.SetSelection(activatePos); event.SetOldSelection(deactivatePos); @@ -739,5 +750,6 @@ bool wxNotebookTabView::OnTabPreActivate(int activateId, int deactivateId) } } return retval; -} +} +#endif // wxUSE_NOTEBOOK