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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
55 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
56 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
57 EVT_SIZE(wxNotebook::OnSize
)
58 EVT_PAINT(wxNotebook::OnPaint
)
59 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
67 // ============================================================================
69 // ============================================================================
71 // ============================================================================
73 // ============================================================================
75 WX_DECLARE_HASH_MAP(int, wxNotebookPage
*, wxIntegerHash
, wxIntegerEqual
,
76 wxIntToNotebookPageHashMap
);
78 WX_DECLARE_HASH_MAP(wxNotebookPage
*, int, wxPointerHash
, wxPointerEqual
,
79 wxNotebookPageToIntHashMap
);
81 // This reuses wxTabView to draw the tabs.
82 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
84 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
86 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
87 virtual ~wxNotebookTabView(void);
89 // Called when a tab is activated
90 virtual void OnTabActivate(int activateId
, int deactivateId
);
92 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
94 // map integer ids used by wxTabView to wxNotebookPage pointers
95 int GetId(wxNotebookPage
*page
);
96 wxNotebookPage
*GetPage(int id
) { return m_idToPage
[id
]; }
99 wxNotebook
* m_notebook
;
102 wxIntToNotebookPageHashMap m_idToPage
;
103 wxNotebookPageToIntHashMap m_pageToId
;
107 static int GetPageId(wxTabView
*tabview
, wxNotebookPage
*page
)
109 return wx_static_cast(wxNotebookTabView
*, tabview
)->GetId(page
);
112 // ----------------------------------------------------------------------------
113 // wxNotebook construction
114 // ----------------------------------------------------------------------------
116 // common part of all ctors
117 void wxNotebook::Init()
119 m_tabView
= (wxNotebookTabView
*) NULL
;
123 // default for dynamic class
124 wxNotebook::wxNotebook()
129 // the same arguments as for wxControl
130 wxNotebook::wxNotebook(wxWindow
*parent
,
135 const wxString
& name
)
139 Create(parent
, id
, pos
, size
, style
, name
);
143 bool wxNotebook::Create(wxWindow
*parent
,
148 const wxString
& name
)
153 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
156 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
158 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
161 SetTabView(new wxNotebookTabView(this));
167 wxNotebook::~wxNotebook()
172 // ----------------------------------------------------------------------------
173 // wxNotebook accessors
174 // ----------------------------------------------------------------------------
175 int wxNotebook::GetRowCount() const
181 int wxNotebook::SetSelection(size_t nPage
)
183 wxASSERT( IS_VALID_PAGE(nPage
) );
185 wxNotebookPage
* pPage
= GetPage(nPage
);
187 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
193 int wxNotebook::ChangeSelection(size_t nPage
)
195 // FIXME: currently it does generate events too
196 return SetSelection(nPage
);
200 void wxNotebook::AdvanceSelection(bool bForward
)
202 int nSel
= GetSelection();
203 int nMax
= GetPageCount() - 1;
205 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
207 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
211 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
213 wxASSERT( IS_VALID_PAGE(nPage
) );
215 wxNotebookPage
* page
= GetPage(nPage
);
218 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
226 wxString
wxNotebook::GetPageText(size_t nPage
) const
228 wxASSERT( IS_VALID_PAGE(nPage
) );
230 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
232 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
234 return wxEmptyString
;
237 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
239 wxASSERT( IS_VALID_PAGE(nPage
) );
245 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
246 int WXUNUSED(nImage
))
248 wxASSERT( IS_VALID_PAGE(nPage
) );
254 // set the size (the same for all pages)
255 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
260 // set the padding between tabs (in pixels)
261 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
266 // set the size of the tabs for wxNB_FIXEDWIDTH controls
267 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
272 // ----------------------------------------------------------------------------
273 // wxNotebook operations
274 // ----------------------------------------------------------------------------
276 // remove one page from the notebook and delete it
277 bool wxNotebook::DeletePage(size_t nPage
)
279 wxCHECK( IS_VALID_PAGE(nPage
), false );
281 if (m_nSelection
!= -1)
283 m_pages
[m_nSelection
]->Show(false);
284 m_pages
[m_nSelection
]->Lower();
287 wxNotebookPage
* pPage
= GetPage(nPage
);
289 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
291 m_pages
.Remove(pPage
);
294 if (m_pages
.GetCount() == 0)
297 m_tabView
->SetTabSelection(-1, false);
299 else if (m_nSelection
> -1)
303 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
305 if (m_nSelection
!= 0)
309 RefreshLayout(false);
314 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
316 int pagePos
= FindPagePosition(page
);
318 return DeletePage(pagePos
);
323 bool wxNotebook::RemovePage(size_t nPage
)
325 return DoRemovePage(nPage
) != NULL
;
328 // remove one page from the notebook
329 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
331 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
333 m_pages
[nPage
]->Show(false);
334 // m_pages[nPage]->Lower();
336 wxNotebookPage
* pPage
= GetPage(nPage
);
338 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
340 m_pages
.Remove(pPage
);
342 if (m_pages
.GetCount() == 0)
345 m_tabView
->SetTabSelection(-1, true);
347 else if (m_nSelection
> -1)
349 // Only change the selection if the page we
350 // deleted was the selection.
351 if (nPage
== (size_t)m_nSelection
)
354 // Select the first tab. Generates a ChangePage.
355 m_tabView
->SetTabSelection(0, true);
359 // We must adjust which tab we think is selected.
360 // If greater than the page we deleted, it must be moved down
362 if (size_t(m_nSelection
) > nPage
)
367 RefreshLayout(false);
372 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
374 int pagePos
= FindPagePosition(page
);
376 return RemovePage(pagePos
);
381 // Find the position of the wxNotebookPage, -1 if not found.
382 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
384 size_t nPageCount
= GetPageCount();
386 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
387 if (m_pages
[nPage
] == page
)
393 bool wxNotebook::DeleteAllPages()
395 m_tabView
->ClearTabs(true);
397 size_t nPageCount
= GetPageCount();
399 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
400 delete m_pages
[nPage
];
407 // same as AddPage() but does it at given position
408 bool wxNotebook::InsertPage(size_t nPage
,
409 wxNotebookPage
*pPage
,
410 const wxString
& strText
,
412 int WXUNUSED(imageId
))
414 wxASSERT( pPage
!= NULL
);
415 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
417 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
422 // save the pointer to the page
423 m_pages
.Insert(pPage
, nPage
);
427 // This will cause ChangePage to be called, via OnSelPage
429 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
432 // some page must be selected: either this one or the first one if there is
433 // still no selection
434 if ( m_nSelection
== -1 )
437 RefreshLayout(false);
442 // ----------------------------------------------------------------------------
443 // wxNotebook callbacks
444 // ----------------------------------------------------------------------------
446 // @@@ OnSize() is used for setting the font when it's called for the first
447 // time because doing it in ::Create() doesn't work (for unknown reasons)
448 void wxNotebook::OnSize(wxSizeEvent
& event
)
450 static bool s_bFirstTime
= true;
451 if ( s_bFirstTime
) {
452 // TODO: any first-time-size processing.
453 s_bFirstTime
= false;
458 // Processing continues to next OnSize
462 // This was supposed to cure the non-display of the notebook
463 // until the user resizes the window.
465 void wxNotebook::OnInternalIdle()
467 wxWindow::OnInternalIdle();
470 static bool s_bFirstTime
= true;
471 if ( s_bFirstTime
) {
473 wxSize sz(GetSize());
481 wxSize sz(GetSize());
482 wxSizeEvent sizeEvent(sz, GetId());
483 sizeEvent.SetEventObject(this);
484 GetEventHandler()->ProcessEvent(sizeEvent);
487 s_bFirstTime
= false;
492 // Implementation: calculate the layout of the view rect
493 // and resize the children if required
494 bool wxNotebook::RefreshLayout(bool force
)
498 wxRect oldRect
= m_tabView
->GetViewRect();
501 GetClientSize(& cw
, & ch
);
503 int tabHeight
= m_tabView
->GetTotalTabHeight();
506 rect
.y
= tabHeight
+ 4;
508 rect
.height
= ch
- 4 - rect
.y
;
510 m_tabView
->SetViewRect(rect
);
512 m_tabView
->LayoutTabs();
514 // Need to do it a 2nd time to get the tab height with
515 // the new view width, since changing the view width changes the
517 tabHeight
= m_tabView
->GetTotalTabHeight();
519 rect
.y
= tabHeight
+ 4;
521 rect
.height
= ch
- 4 - rect
.y
;
523 m_tabView
->SetViewRect(rect
);
525 m_tabView
->LayoutTabs();
527 if (!force
&& (rect
== oldRect
))
530 // fit the notebook page to the tab control's display area
532 size_t nCount
= m_pages
.Count();
533 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
534 wxNotebookPage
*pPage
= m_pages
[nPage
];
535 wxRect clientRect
= GetAvailableClientSize();
536 if (pPage
->IsShown())
538 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
539 if ( pPage
->GetAutoLayout() )
548 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
550 // is it our tab control?
551 if ( event
.GetEventObject() == this )
553 if (event
.GetSelection() != m_nSelection
)
554 ChangePage(event
.GetOldSelection(), event
.GetSelection());
557 // we want to give others a chance to process this message as well
561 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
563 // set focus to the currently selected page if any
564 if ( m_nSelection
!= -1 )
565 m_pages
[m_nSelection
]->SetFocus();
570 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
572 if ( event
.IsWindowChange() ) {
574 AdvanceSelection(event
.GetDirection());
577 // pass to the parent
579 event
.SetCurrentFocus(this);
580 GetParent()->ProcessEvent(event
);
585 // ----------------------------------------------------------------------------
586 // wxNotebook base class virtuals
587 // ----------------------------------------------------------------------------
589 // override these 2 functions to do nothing: everything is done in OnSize
591 void wxNotebook::SetConstraintSizes(bool /* recurse */)
593 // don't set the sizes of the pages - their correct size is not yet known
594 wxControl::SetConstraintSizes(false);
597 bool wxNotebook::DoPhase(int /* nPhase */)
602 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
604 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
607 // ----------------------------------------------------------------------------
608 // wxNotebook helper functions
609 // ----------------------------------------------------------------------------
611 // hide the currently active panel and show the new one
612 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
614 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
615 wxASSERT( nOldSel
!= nSel
); // impossible
617 if ( nOldSel
!= -1 ) {
618 m_pages
[nOldSel
]->Show(false);
619 m_pages
[nOldSel
]->Lower();
622 wxNotebookPage
*pPage
= m_pages
[nSel
];
624 wxRect clientRect
= GetAvailableClientSize();
625 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
636 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
639 m_tabView
->OnEvent(event
);
642 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
649 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
651 // MBN: since the total tab height is really a function of the
652 // width, this should really call
653 // GetTotalTabHeightPretendingWidthIs(), but the current
654 // implementation will suffice, provided the wxNotebook has been
655 // created with a sensible initial width.
656 return wxSize( sizePage
.x
+ 12,
657 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
660 wxRect
wxNotebook::GetAvailableClientSize()
663 GetClientSize(& cw
, & ch
);
665 int tabHeight
= m_tabView
->GetTotalTabHeight();
667 // TODO: these margins should be configurable.
670 rect
.y
= tabHeight
+ 6;
671 rect
.width
= cw
- 12;
672 rect
.height
= ch
- 4 - rect
.y
;
681 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
683 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
684 : wxTabView(style
), m_nextid(1)
686 m_notebook
= notebook
;
688 m_notebook
->SetTabView(this);
690 SetWindow(m_notebook
);
693 wxNotebookTabView::~wxNotebookTabView(void)
697 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
699 int& id
= m_pageToId
[page
];
704 m_idToPage
[id
] = page
;
710 // Called when a tab is activated
711 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
716 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
718 // Translate from wxTabView's ids (which aren't position-dependent)
719 // to wxNotebook's (which are).
720 wxNotebookPage
* pActive
= GetPage(activateId
);
721 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
723 int activatePos
= m_notebook
->FindPagePosition(pActive
);
724 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
726 event
.SetEventObject(m_notebook
);
727 event
.SetSelection(activatePos
);
728 event
.SetOldSelection(deactivatePos
);
729 m_notebook
->GetEventHandler()->ProcessEvent(event
);
733 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
739 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
741 // Translate from wxTabView's ids (which aren't position-dependent)
742 // to wxNotebook's (which are).
743 wxNotebookPage
* pActive
= GetPage(activateId
);
744 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
746 int activatePos
= m_notebook
->FindPagePosition(pActive
);
747 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
749 event
.SetEventObject(m_notebook
);
750 event
.SetSelection(activatePos
);
751 event
.SetOldSelection(deactivatePos
);
752 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
754 retval
= event
.IsAllowed();
760 #endif // wxUSE_NOTEBOOK