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 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
63 // ============================================================================
65 // ============================================================================
67 // ============================================================================
69 // ============================================================================
71 WX_DECLARE_HASH_MAP(int, wxNotebookPage
*, wxIntegerHash
, wxIntegerEqual
,
72 wxIntToNotebookPageHashMap
);
74 WX_DECLARE_HASH_MAP(wxNotebookPage
*, int, wxPointerHash
, wxPointerEqual
,
75 wxNotebookPageToIntHashMap
);
77 // This reuses wxTabView to draw the tabs.
78 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
80 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
82 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
83 virtual ~wxNotebookTabView(void);
85 // Called when a tab is activated
86 virtual void OnTabActivate(int activateId
, int deactivateId
);
88 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
90 // map integer ids used by wxTabView to wxNotebookPage pointers
91 int GetId(wxNotebookPage
*page
);
92 wxNotebookPage
*GetPage(int id
) { return m_idToPage
[id
]; }
95 wxNotebook
* m_notebook
;
98 wxIntToNotebookPageHashMap m_idToPage
;
99 wxNotebookPageToIntHashMap m_pageToId
;
103 static int GetPageId(wxTabView
*tabview
, wxNotebookPage
*page
)
105 return static_cast<wxNotebookTabView
*>(tabview
)->GetId(page
);
108 // ----------------------------------------------------------------------------
109 // wxNotebook construction
110 // ----------------------------------------------------------------------------
112 // common part of all ctors
113 void wxNotebook::Init()
119 // default for dynamic class
120 wxNotebook::wxNotebook()
125 // the same arguments as for wxControl
126 wxNotebook::wxNotebook(wxWindow
*parent
,
131 const wxString
& name
)
135 Create(parent
, id
, pos
, size
, style
, name
);
139 bool wxNotebook::Create(wxWindow
*parent
,
144 const wxString
& name
)
149 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
152 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
154 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
157 SetTabView(new wxNotebookTabView(this));
163 wxNotebook::~wxNotebook()
168 // ----------------------------------------------------------------------------
169 // wxNotebook accessors
170 // ----------------------------------------------------------------------------
171 int wxNotebook::GetRowCount() const
177 int wxNotebook::SetSelection(size_t nPage
)
179 wxASSERT( IS_VALID_PAGE(nPage
) );
181 wxNotebookPage
* pPage
= GetPage(nPage
);
183 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
189 int wxNotebook::ChangeSelection(size_t nPage
)
191 // FIXME: currently it does generate events too
192 return SetSelection(nPage
);
196 void wxNotebook::AdvanceSelection(bool bForward
)
198 int nSel
= GetSelection();
199 int nMax
= GetPageCount() - 1;
201 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
203 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
207 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
209 wxASSERT( IS_VALID_PAGE(nPage
) );
211 wxNotebookPage
* page
= GetPage(nPage
);
214 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
222 wxString
wxNotebook::GetPageText(size_t nPage
) const
224 wxASSERT( IS_VALID_PAGE(nPage
) );
226 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
228 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
230 return wxEmptyString
;
233 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
235 wxASSERT( IS_VALID_PAGE(nPage
) );
241 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
242 int WXUNUSED(nImage
))
244 wxASSERT( IS_VALID_PAGE(nPage
) );
250 // set the size (the same for all pages)
251 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
256 // set the padding between tabs (in pixels)
257 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
262 // set the size of the tabs for wxNB_FIXEDWIDTH controls
263 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
268 // ----------------------------------------------------------------------------
269 // wxNotebook operations
270 // ----------------------------------------------------------------------------
272 // remove one page from the notebook and delete it
273 bool wxNotebook::DeletePage(size_t nPage
)
275 wxCHECK( IS_VALID_PAGE(nPage
), false );
277 if (m_selection
!= -1)
279 m_pages
[m_selection
]->Show(false);
280 m_pages
[m_selection
]->Lower();
283 wxNotebookPage
* pPage
= GetPage(nPage
);
285 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
287 m_pages
.Remove(pPage
);
290 if (m_pages
.GetCount() == 0)
293 m_tabView
->SetTabSelection(-1, false);
295 else if (m_selection
> -1)
299 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
301 if (m_selection
!= 0)
305 RefreshLayout(false);
310 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
312 int pagePos
= FindPagePosition(page
);
314 return DeletePage(pagePos
);
319 bool wxNotebook::RemovePage(size_t nPage
)
321 return DoRemovePage(nPage
) != NULL
;
324 // remove one page from the notebook
325 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
327 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
329 m_pages
[nPage
]->Show(false);
330 // m_pages[nPage]->Lower();
332 wxNotebookPage
* pPage
= GetPage(nPage
);
334 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
336 m_pages
.Remove(pPage
);
338 if (m_pages
.GetCount() == 0)
341 m_tabView
->SetTabSelection(-1, true);
343 else if (m_selection
> -1)
345 // Only change the selection if the page we
346 // deleted was the selection.
347 if (nPage
== (size_t)m_selection
)
350 // Select the first tab. Generates a ChangePage.
351 m_tabView
->SetTabSelection(0, true);
355 // We must adjust which tab we think is selected.
356 // If greater than the page we deleted, it must be moved down
358 if (size_t(m_selection
) > nPage
)
363 RefreshLayout(false);
368 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
370 int pagePos
= FindPagePosition(page
);
372 return RemovePage(pagePos
);
377 // Find the position of the wxNotebookPage, -1 if not found.
378 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
380 size_t nPageCount
= GetPageCount();
382 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
383 if (m_pages
[nPage
] == page
)
389 bool wxNotebook::DeleteAllPages()
391 m_tabView
->ClearTabs(true);
393 size_t nPageCount
= GetPageCount();
395 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
396 delete m_pages
[nPage
];
403 // same as AddPage() but does it at given position
404 bool wxNotebook::InsertPage(size_t nPage
,
405 wxNotebookPage
*pPage
,
406 const wxString
& strText
,
408 int WXUNUSED(imageId
))
410 wxASSERT( pPage
!= NULL
);
411 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
413 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
418 // save the pointer to the page
419 m_pages
.Insert(pPage
, nPage
);
423 // This will cause ChangePage to be called, via OnSelPage
425 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
428 // some page must be selected: either this one or the first one if there is
429 // still no selection
430 if ( m_selection
== -1 )
433 RefreshLayout(false);
438 // ----------------------------------------------------------------------------
439 // wxNotebook callbacks
440 // ----------------------------------------------------------------------------
442 // @@@ OnSize() is used for setting the font when it's called for the first
443 // time because doing it in ::Create() doesn't work (for unknown reasons)
444 void wxNotebook::OnSize(wxSizeEvent
& event
)
446 static bool s_bFirstTime
= true;
447 if ( s_bFirstTime
) {
448 // TODO: any first-time-size processing.
449 s_bFirstTime
= false;
454 // Processing continues to next OnSize
458 // This was supposed to cure the non-display of the notebook
459 // until the user resizes the window.
461 void wxNotebook::OnInternalIdle()
463 wxWindow::OnInternalIdle();
466 static bool s_bFirstTime
= true;
467 if ( s_bFirstTime
) {
469 wxSize sz(GetSize());
477 wxSize sz(GetSize());
478 wxSizeEvent sizeEvent(sz, GetId());
479 sizeEvent.SetEventObject(this);
480 GetEventHandler()->ProcessEvent(sizeEvent);
483 s_bFirstTime
= false;
488 // Implementation: calculate the layout of the view rect
489 // and resize the children if required
490 bool wxNotebook::RefreshLayout(bool force
)
494 wxRect oldRect
= m_tabView
->GetViewRect();
497 GetClientSize(& cw
, & ch
);
499 int tabHeight
= m_tabView
->GetTotalTabHeight();
502 rect
.y
= tabHeight
+ 4;
504 rect
.height
= ch
- 4 - rect
.y
;
506 m_tabView
->SetViewRect(rect
);
508 m_tabView
->LayoutTabs();
510 // Need to do it a 2nd time to get the tab height with
511 // the new view width, since changing the view width changes the
513 tabHeight
= m_tabView
->GetTotalTabHeight();
515 rect
.y
= tabHeight
+ 4;
517 rect
.height
= ch
- 4 - rect
.y
;
519 m_tabView
->SetViewRect(rect
);
521 m_tabView
->LayoutTabs();
523 if (!force
&& (rect
== oldRect
))
526 // fit the notebook page to the tab control's display area
528 size_t nCount
= m_pages
.Count();
529 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
530 wxNotebookPage
*pPage
= m_pages
[nPage
];
531 wxRect clientRect
= GetAvailableClientSize();
532 if (pPage
->IsShown())
534 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
535 if ( pPage
->GetAutoLayout() )
544 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
546 // is it our tab control?
547 if ( event
.GetEventObject() == this )
549 if (event
.GetSelection() != m_selection
)
550 ChangePage(event
.GetOldSelection(), event
.GetSelection());
553 // we want to give others a chance to process this message as well
557 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
559 // set focus to the currently selected page if any
560 if ( m_selection
!= -1 )
561 m_pages
[m_selection
]->SetFocus();
566 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
568 if ( event
.IsWindowChange() )
571 AdvanceSelection(event
.GetDirection());
574 // pass to the parent
577 event
.SetCurrentFocus(this);
578 GetParent()->ProcessWindowEvent(event
);
583 // ----------------------------------------------------------------------------
584 // wxNotebook base class virtuals
585 // ----------------------------------------------------------------------------
587 // override these 2 functions to do nothing: everything is done in OnSize
589 void wxNotebook::SetConstraintSizes(bool /* recurse */)
591 // don't set the sizes of the pages - their correct size is not yet known
592 wxControl::SetConstraintSizes(false);
595 bool wxNotebook::DoPhase(int /* nPhase */)
600 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
602 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
605 // ----------------------------------------------------------------------------
606 // wxNotebook helper functions
607 // ----------------------------------------------------------------------------
609 // hide the currently active panel and show the new one
610 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
612 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
613 wxASSERT( nOldSel
!= nSel
); // impossible
615 if ( nOldSel
!= -1 ) {
616 m_pages
[nOldSel
]->Show(false);
617 m_pages
[nOldSel
]->Lower();
620 wxNotebookPage
*pPage
= m_pages
[nSel
];
622 wxRect clientRect
= GetAvailableClientSize();
623 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
634 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
637 m_tabView
->OnEvent(event
);
640 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
647 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
649 // MBN: since the total tab height is really a function of the
650 // width, this should really call
651 // GetTotalTabHeightPretendingWidthIs(), but the current
652 // implementation will suffice, provided the wxNotebook has been
653 // created with a sensible initial width.
654 return wxSize( sizePage
.x
+ 12,
655 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
658 wxRect
wxNotebook::GetAvailableClientSize()
661 GetClientSize(& cw
, & ch
);
663 int tabHeight
= m_tabView
->GetTotalTabHeight();
665 // TODO: these margins should be configurable.
668 rect
.y
= tabHeight
+ 6;
669 rect
.width
= cw
- 12;
670 rect
.height
= ch
- 4 - rect
.y
;
679 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
681 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
682 : wxTabView(style
), m_nextid(1)
684 m_notebook
= notebook
;
686 m_notebook
->SetTabView(this);
688 SetWindow(m_notebook
);
691 wxNotebookTabView::~wxNotebookTabView(void)
695 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
697 int& id
= m_pageToId
[page
];
702 m_idToPage
[id
] = page
;
708 // Called when a tab is activated
709 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
714 wxBookCtrlEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
716 // Translate from wxTabView's ids (which aren't position-dependent)
717 // to wxNotebook's (which are).
718 wxNotebookPage
* pActive
= GetPage(activateId
);
719 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
721 int activatePos
= m_notebook
->FindPagePosition(pActive
);
722 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
724 event
.SetEventObject(m_notebook
);
725 event
.SetSelection(activatePos
);
726 event
.SetOldSelection(deactivatePos
);
727 m_notebook
->GetEventHandler()->ProcessEvent(event
);
731 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
737 wxBookCtrlEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
739 // Translate from wxTabView's ids (which aren't position-dependent)
740 // to wxNotebook's (which are).
741 wxNotebookPage
* pActive
= GetPage(activateId
);
742 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
744 int activatePos
= m_notebook
->FindPagePosition(pActive
);
745 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
747 event
.SetEventObject(m_notebook
);
748 event
.SetSelection(activatePos
);
749 event
.SetOldSelection(deactivatePos
);
750 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
752 retval
= event
.IsAllowed();
758 #endif // wxUSE_NOTEBOOK