1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notebook.cpp
3 // Purpose: generic implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/notebook.h"
32 #include "wx/string.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
38 #include "wx/imaglist.h"
39 #include "wx/generic/tabg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
53 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
54 EVT_SIZE(wxNotebook::OnSize
)
55 EVT_PAINT(wxNotebook::OnPaint
)
56 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
57 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
58 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
61 // ============================================================================
63 // ============================================================================
65 // ============================================================================
67 // ============================================================================
69 WX_DECLARE_HASH_MAP(int, wxNotebookPage
*, wxIntegerHash
, wxIntegerEqual
,
70 wxIntToNotebookPageHashMap
);
72 WX_DECLARE_HASH_MAP(wxNotebookPage
*, int, wxPointerHash
, wxPointerEqual
,
73 wxNotebookPageToIntHashMap
);
75 // This reuses wxTabView to draw the tabs.
76 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
78 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
80 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
81 virtual ~wxNotebookTabView(void);
83 // Called when a tab is activated
84 virtual void OnTabActivate(int activateId
, int deactivateId
);
86 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
88 // map integer ids used by wxTabView to wxNotebookPage pointers
89 int GetId(wxNotebookPage
*page
);
90 wxNotebookPage
*GetPage(int id
) { return m_idToPage
[id
]; }
93 wxNotebook
* m_notebook
;
96 wxIntToNotebookPageHashMap m_idToPage
;
97 wxNotebookPageToIntHashMap m_pageToId
;
101 static int GetPageId(wxTabView
*tabview
, wxNotebookPage
*page
)
103 return static_cast<wxNotebookTabView
*>(tabview
)->GetId(page
);
106 // ----------------------------------------------------------------------------
107 // wxNotebook construction
108 // ----------------------------------------------------------------------------
110 // common part of all ctors
111 void wxNotebook::Init()
117 // default for dynamic class
118 wxNotebook::wxNotebook()
123 // the same arguments as for wxControl
124 wxNotebook::wxNotebook(wxWindow
*parent
,
129 const wxString
& name
)
133 Create(parent
, id
, pos
, size
, style
, name
);
137 bool wxNotebook::Create(wxWindow
*parent
,
142 const wxString
& name
)
147 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
150 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
152 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
155 SetTabView(new wxNotebookTabView(this));
161 wxNotebook::~wxNotebook()
166 // ----------------------------------------------------------------------------
167 // wxNotebook accessors
168 // ----------------------------------------------------------------------------
169 int wxNotebook::GetRowCount() const
175 int wxNotebook::SetSelection(size_t nPage
)
177 wxASSERT( IS_VALID_PAGE(nPage
) );
179 wxNotebookPage
* pPage
= GetPage(nPage
);
181 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
187 int wxNotebook::ChangeSelection(size_t nPage
)
189 // FIXME: currently it does generate events too
190 return SetSelection(nPage
);
194 void wxNotebook::AdvanceSelection(bool bForward
)
196 int nSel
= GetSelection();
197 int nMax
= GetPageCount() - 1;
199 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
201 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
205 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
207 wxASSERT( IS_VALID_PAGE(nPage
) );
209 wxNotebookPage
* page
= GetPage(nPage
);
212 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
220 wxString
wxNotebook::GetPageText(size_t nPage
) const
222 wxASSERT( IS_VALID_PAGE(nPage
) );
224 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
226 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
228 return wxEmptyString
;
231 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
233 wxASSERT( IS_VALID_PAGE(nPage
) );
239 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
240 int WXUNUSED(nImage
))
242 wxASSERT( IS_VALID_PAGE(nPage
) );
248 // set the size (the same for all pages)
249 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
254 // set the padding between tabs (in pixels)
255 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
260 // set the size of the tabs for wxNB_FIXEDWIDTH controls
261 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
266 // ----------------------------------------------------------------------------
267 // wxNotebook operations
268 // ----------------------------------------------------------------------------
270 // remove one page from the notebook and delete it
271 bool wxNotebook::DeletePage(size_t nPage
)
273 wxCHECK( IS_VALID_PAGE(nPage
), false );
275 if (m_selection
!= -1)
277 m_pages
[m_selection
]->Show(false);
278 m_pages
[m_selection
]->Lower();
281 wxNotebookPage
* pPage
= GetPage(nPage
);
283 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
285 m_pages
.Remove(pPage
);
288 if (m_pages
.GetCount() == 0)
291 m_tabView
->SetTabSelection(-1, false);
293 else if (m_selection
> -1)
297 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
299 if (m_selection
!= 0)
303 RefreshLayout(false);
308 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
310 int pagePos
= FindPagePosition(page
);
312 return DeletePage(pagePos
);
317 bool wxNotebook::RemovePage(size_t nPage
)
319 return DoRemovePage(nPage
) != NULL
;
322 // remove one page from the notebook
323 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
325 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
327 m_pages
[nPage
]->Show(false);
328 // m_pages[nPage]->Lower();
330 wxNotebookPage
* pPage
= GetPage(nPage
);
332 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
334 m_pages
.Remove(pPage
);
336 if (m_pages
.GetCount() == 0)
339 m_tabView
->SetTabSelection(-1, true);
341 else if (m_selection
> -1)
343 // Only change the selection if the page we
344 // deleted was the selection.
345 if (nPage
== (size_t)m_selection
)
348 // Select the first tab. Generates a ChangePage.
349 m_tabView
->SetTabSelection(0, true);
353 // We must adjust which tab we think is selected.
354 // If greater than the page we deleted, it must be moved down
356 if (size_t(m_selection
) > nPage
)
361 RefreshLayout(false);
366 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
368 int pagePos
= FindPagePosition(page
);
370 return RemovePage(pagePos
);
375 // Find the position of the wxNotebookPage, -1 if not found.
376 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
378 size_t nPageCount
= GetPageCount();
380 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
381 if (m_pages
[nPage
] == page
)
387 bool wxNotebook::DeleteAllPages()
389 m_tabView
->ClearTabs(true);
391 size_t nPageCount
= GetPageCount();
393 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
394 delete m_pages
[nPage
];
401 // same as AddPage() but does it at given position
402 bool wxNotebook::InsertPage(size_t nPage
,
403 wxNotebookPage
*pPage
,
404 const wxString
& strText
,
406 int WXUNUSED(imageId
))
408 wxASSERT( pPage
!= NULL
);
409 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
411 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
416 // save the pointer to the page
417 m_pages
.Insert(pPage
, nPage
);
421 // This will cause ChangePage to be called, via OnSelPage
423 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
426 // some page must be selected: either this one or the first one if there is
427 // still no selection
428 if ( m_selection
== -1 )
431 RefreshLayout(false);
436 // ----------------------------------------------------------------------------
437 // wxNotebook callbacks
438 // ----------------------------------------------------------------------------
440 // @@@ OnSize() is used for setting the font when it's called for the first
441 // time because doing it in ::Create() doesn't work (for unknown reasons)
442 void wxNotebook::OnSize(wxSizeEvent
& event
)
444 static bool s_bFirstTime
= true;
445 if ( s_bFirstTime
) {
446 // TODO: any first-time-size processing.
447 s_bFirstTime
= false;
452 // Processing continues to next OnSize
456 // This was supposed to cure the non-display of the notebook
457 // until the user resizes the window.
459 void wxNotebook::OnInternalIdle()
461 wxWindow::OnInternalIdle();
464 static bool s_bFirstTime
= true;
465 if ( s_bFirstTime
) {
467 wxSize sz(GetSize());
475 wxSize sz(GetSize());
476 wxSizeEvent sizeEvent(sz, GetId());
477 sizeEvent.SetEventObject(this);
478 GetEventHandler()->ProcessEvent(sizeEvent);
481 s_bFirstTime
= false;
486 // Implementation: calculate the layout of the view rect
487 // and resize the children if required
488 bool wxNotebook::RefreshLayout(bool force
)
492 wxRect oldRect
= m_tabView
->GetViewRect();
495 GetClientSize(& cw
, & ch
);
497 int tabHeight
= m_tabView
->GetTotalTabHeight();
500 rect
.y
= tabHeight
+ 4;
502 rect
.height
= ch
- 4 - rect
.y
;
504 m_tabView
->SetViewRect(rect
);
506 m_tabView
->LayoutTabs();
508 // Need to do it a 2nd time to get the tab height with
509 // the new view width, since changing the view width changes the
511 tabHeight
= m_tabView
->GetTotalTabHeight();
513 rect
.y
= tabHeight
+ 4;
515 rect
.height
= ch
- 4 - rect
.y
;
517 m_tabView
->SetViewRect(rect
);
519 m_tabView
->LayoutTabs();
521 if (!force
&& (rect
== oldRect
))
524 // fit the notebook page to the tab control's display area
526 size_t nCount
= m_pages
.Count();
527 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
528 wxNotebookPage
*pPage
= m_pages
[nPage
];
529 wxRect clientRect
= GetAvailableClientSize();
530 if (pPage
->IsShown())
532 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
533 if ( pPage
->GetAutoLayout() )
542 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
544 // is it our tab control?
545 if ( event
.GetEventObject() == this )
547 if (event
.GetSelection() != m_selection
)
548 ChangePage(event
.GetOldSelection(), event
.GetSelection());
551 // we want to give others a chance to process this message as well
555 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
557 // set focus to the currently selected page if any
558 if ( m_selection
!= -1 )
559 m_pages
[m_selection
]->SetFocus();
564 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
566 if ( event
.IsWindowChange() )
569 AdvanceSelection(event
.GetDirection());
572 // pass to the parent
575 event
.SetCurrentFocus(this);
576 GetParent()->ProcessWindowEvent(event
);
581 // ----------------------------------------------------------------------------
582 // wxNotebook base class virtuals
583 // ----------------------------------------------------------------------------
585 // override these 2 functions to do nothing: everything is done in OnSize
587 void wxNotebook::SetConstraintSizes(bool /* recurse */)
589 // don't set the sizes of the pages - their correct size is not yet known
590 wxControl::SetConstraintSizes(false);
593 bool wxNotebook::DoPhase(int /* nPhase */)
598 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
600 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
603 // ----------------------------------------------------------------------------
604 // wxNotebook helper functions
605 // ----------------------------------------------------------------------------
607 // hide the currently active panel and show the new one
608 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
610 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
611 wxASSERT( nOldSel
!= nSel
); // impossible
613 if ( nOldSel
!= -1 ) {
614 m_pages
[nOldSel
]->Show(false);
615 m_pages
[nOldSel
]->Lower();
618 wxNotebookPage
*pPage
= m_pages
[nSel
];
620 wxRect clientRect
= GetAvailableClientSize();
621 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
632 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
635 m_tabView
->OnEvent(event
);
638 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
645 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
647 // MBN: since the total tab height is really a function of the
648 // width, this should really call
649 // GetTotalTabHeightPretendingWidthIs(), but the current
650 // implementation will suffice, provided the wxNotebook has been
651 // created with a sensible initial width.
652 return wxSize( sizePage
.x
+ 12,
653 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
656 wxRect
wxNotebook::GetAvailableClientSize()
659 GetClientSize(& cw
, & ch
);
661 int tabHeight
= m_tabView
->GetTotalTabHeight();
663 // TODO: these margins should be configurable.
666 rect
.y
= tabHeight
+ 6;
667 rect
.width
= cw
- 12;
668 rect
.height
= ch
- 4 - rect
.y
;
677 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
679 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
680 : wxTabView(style
), m_nextid(1)
682 m_notebook
= notebook
;
684 m_notebook
->SetTabView(this);
686 SetWindow(m_notebook
);
689 wxNotebookTabView::~wxNotebookTabView(void)
693 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
695 int& id
= m_pageToId
[page
];
700 m_idToPage
[id
] = page
;
706 // Called when a tab is activated
707 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
712 wxBookCtrlEvent
event(wxEVT_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
714 // Translate from wxTabView's ids (which aren't position-dependent)
715 // to wxNotebook's (which are).
716 wxNotebookPage
* pActive
= GetPage(activateId
);
717 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
719 int activatePos
= m_notebook
->FindPagePosition(pActive
);
720 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
722 event
.SetEventObject(m_notebook
);
723 event
.SetSelection(activatePos
);
724 event
.SetOldSelection(deactivatePos
);
725 m_notebook
->GetEventHandler()->ProcessEvent(event
);
729 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
735 wxBookCtrlEvent
event(wxEVT_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
737 // Translate from wxTabView's ids (which aren't position-dependent)
738 // to wxNotebook's (which are).
739 wxNotebookPage
* pActive
= GetPage(activateId
);
740 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
742 int activatePos
= m_notebook
->FindPagePosition(pActive
);
743 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
745 event
.SetEventObject(m_notebook
);
746 event
.SetSelection(activatePos
);
747 event
.SetOldSelection(deactivatePos
);
748 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
750 retval
= event
.IsAllowed();
756 #endif // wxUSE_NOTEBOOK