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 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
163 SetTabView(new wxNotebookTabView(this));
169 wxNotebook::~wxNotebook()
174 // ----------------------------------------------------------------------------
175 // wxNotebook accessors
176 // ----------------------------------------------------------------------------
177 int wxNotebook::GetRowCount() const
183 int wxNotebook::SetSelection(size_t nPage
)
185 wxASSERT( IS_VALID_PAGE(nPage
) );
187 wxNotebookPage
* pPage
= GetPage(nPage
);
189 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
195 int wxNotebook::ChangeSelection(size_t nPage
)
197 // FIXME: currently it does generate events too
198 return SetSelection(nPage
);
202 void wxNotebook::AdvanceSelection(bool bForward
)
204 int nSel
= GetSelection();
205 int nMax
= GetPageCount() - 1;
207 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
209 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
213 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
215 wxASSERT( IS_VALID_PAGE(nPage
) );
217 wxNotebookPage
* page
= GetPage(nPage
);
220 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
228 wxString
wxNotebook::GetPageText(size_t nPage
) const
230 wxASSERT( IS_VALID_PAGE(nPage
) );
232 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
234 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
236 return wxEmptyString
;
239 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
241 wxASSERT( IS_VALID_PAGE(nPage
) );
247 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
248 int WXUNUSED(nImage
))
250 wxASSERT( IS_VALID_PAGE(nPage
) );
256 // set the size (the same for all pages)
257 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
262 // set the padding between tabs (in pixels)
263 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
268 // set the size of the tabs for wxNB_FIXEDWIDTH controls
269 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
274 // ----------------------------------------------------------------------------
275 // wxNotebook operations
276 // ----------------------------------------------------------------------------
278 // remove one page from the notebook and delete it
279 bool wxNotebook::DeletePage(size_t nPage
)
281 wxCHECK( IS_VALID_PAGE(nPage
), false );
283 if (m_nSelection
!= -1)
285 m_pages
[m_nSelection
]->Show(false);
286 m_pages
[m_nSelection
]->Lower();
289 wxNotebookPage
* pPage
= GetPage(nPage
);
291 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
293 m_pages
.Remove(pPage
);
296 if (m_pages
.GetCount() == 0)
299 m_tabView
->SetTabSelection(-1, false);
301 else if (m_nSelection
> -1)
305 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
307 if (m_nSelection
!= 0)
311 RefreshLayout(false);
316 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
318 int pagePos
= FindPagePosition(page
);
320 return DeletePage(pagePos
);
325 bool wxNotebook::RemovePage(size_t nPage
)
327 return DoRemovePage(nPage
) != NULL
;
330 // remove one page from the notebook
331 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
333 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
335 m_pages
[nPage
]->Show(false);
336 // m_pages[nPage]->Lower();
338 wxNotebookPage
* pPage
= GetPage(nPage
);
340 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
342 m_pages
.Remove(pPage
);
344 if (m_pages
.GetCount() == 0)
347 m_tabView
->SetTabSelection(-1, true);
349 else if (m_nSelection
> -1)
351 // Only change the selection if the page we
352 // deleted was the selection.
353 if (nPage
== (size_t)m_nSelection
)
356 // Select the first tab. Generates a ChangePage.
357 m_tabView
->SetTabSelection(0, true);
361 // We must adjust which tab we think is selected.
362 // If greater than the page we deleted, it must be moved down
364 if (size_t(m_nSelection
) > nPage
)
369 RefreshLayout(false);
374 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
376 int pagePos
= FindPagePosition(page
);
378 return RemovePage(pagePos
);
383 // Find the position of the wxNotebookPage, -1 if not found.
384 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
386 size_t nPageCount
= GetPageCount();
388 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
389 if (m_pages
[nPage
] == page
)
395 bool wxNotebook::DeleteAllPages()
397 m_tabView
->ClearTabs(true);
399 size_t nPageCount
= GetPageCount();
401 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
402 delete m_pages
[nPage
];
409 // same as AddPage() but does it at given position
410 bool wxNotebook::InsertPage(size_t nPage
,
411 wxNotebookPage
*pPage
,
412 const wxString
& strText
,
414 int WXUNUSED(imageId
))
416 wxASSERT( pPage
!= NULL
);
417 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
419 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
424 // save the pointer to the page
425 m_pages
.Insert(pPage
, nPage
);
429 // This will cause ChangePage to be called, via OnSelPage
431 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
434 // some page must be selected: either this one or the first one if there is
435 // still no selection
436 if ( m_nSelection
== -1 )
439 RefreshLayout(false);
444 // ----------------------------------------------------------------------------
445 // wxNotebook callbacks
446 // ----------------------------------------------------------------------------
448 // @@@ OnSize() is used for setting the font when it's called for the first
449 // time because doing it in ::Create() doesn't work (for unknown reasons)
450 void wxNotebook::OnSize(wxSizeEvent
& event
)
452 static bool s_bFirstTime
= true;
453 if ( s_bFirstTime
) {
454 // TODO: any first-time-size processing.
455 s_bFirstTime
= false;
460 // Processing continues to next OnSize
464 // This was supposed to cure the non-display of the notebook
465 // until the user resizes the window.
467 void wxNotebook::OnInternalIdle()
469 wxWindow::OnInternalIdle();
472 static bool s_bFirstTime
= true;
473 if ( s_bFirstTime
) {
475 wxSize sz(GetSize());
483 wxSize sz(GetSize());
484 wxSizeEvent sizeEvent(sz, GetId());
485 sizeEvent.SetEventObject(this);
486 GetEventHandler()->ProcessEvent(sizeEvent);
489 s_bFirstTime
= false;
494 // Implementation: calculate the layout of the view rect
495 // and resize the children if required
496 bool wxNotebook::RefreshLayout(bool force
)
500 wxRect oldRect
= m_tabView
->GetViewRect();
503 GetClientSize(& cw
, & ch
);
505 int tabHeight
= m_tabView
->GetTotalTabHeight();
508 rect
.y
= tabHeight
+ 4;
510 rect
.height
= ch
- 4 - rect
.y
;
512 m_tabView
->SetViewRect(rect
);
514 m_tabView
->LayoutTabs();
516 // Need to do it a 2nd time to get the tab height with
517 // the new view width, since changing the view width changes the
519 tabHeight
= m_tabView
->GetTotalTabHeight();
521 rect
.y
= tabHeight
+ 4;
523 rect
.height
= ch
- 4 - rect
.y
;
525 m_tabView
->SetViewRect(rect
);
527 m_tabView
->LayoutTabs();
529 if (!force
&& (rect
== oldRect
))
532 // fit the notebook page to the tab control's display area
534 size_t nCount
= m_pages
.Count();
535 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
536 wxNotebookPage
*pPage
= m_pages
[nPage
];
537 wxRect clientRect
= GetAvailableClientSize();
538 if (pPage
->IsShown())
540 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
541 if ( pPage
->GetAutoLayout() )
550 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
552 // is it our tab control?
553 if ( event
.GetEventObject() == this )
555 if (event
.GetSelection() != m_nSelection
)
556 ChangePage(event
.GetOldSelection(), event
.GetSelection());
559 // we want to give others a chance to process this message as well
563 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
565 // set focus to the currently selected page if any
566 if ( m_nSelection
!= -1 )
567 m_pages
[m_nSelection
]->SetFocus();
572 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
574 if ( event
.IsWindowChange() ) {
576 AdvanceSelection(event
.GetDirection());
579 // pass to the parent
581 event
.SetCurrentFocus(this);
582 GetParent()->ProcessEvent(event
);
587 // ----------------------------------------------------------------------------
588 // wxNotebook base class virtuals
589 // ----------------------------------------------------------------------------
591 // override these 2 functions to do nothing: everything is done in OnSize
593 void wxNotebook::SetConstraintSizes(bool /* recurse */)
595 // don't set the sizes of the pages - their correct size is not yet known
596 wxControl::SetConstraintSizes(false);
599 bool wxNotebook::DoPhase(int /* nPhase */)
604 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
606 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
609 // ----------------------------------------------------------------------------
610 // wxNotebook helper functions
611 // ----------------------------------------------------------------------------
613 // hide the currently active panel and show the new one
614 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
616 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
617 wxASSERT( nOldSel
!= nSel
); // impossible
619 if ( nOldSel
!= -1 ) {
620 m_pages
[nOldSel
]->Show(false);
621 m_pages
[nOldSel
]->Lower();
624 wxNotebookPage
*pPage
= m_pages
[nSel
];
626 wxRect clientRect
= GetAvailableClientSize();
627 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
638 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
641 m_tabView
->OnEvent(event
);
644 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
651 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
653 // MBN: since the total tab height is really a function of the
654 // width, this should really call
655 // GetTotalTabHeightPretendingWidthIs(), but the current
656 // implementation will suffice, provided the wxNotebook has been
657 // created with a sensible initial width.
658 return wxSize( sizePage
.x
+ 12,
659 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
662 wxRect
wxNotebook::GetAvailableClientSize()
665 GetClientSize(& cw
, & ch
);
667 int tabHeight
= m_tabView
->GetTotalTabHeight();
669 // TODO: these margins should be configurable.
672 rect
.y
= tabHeight
+ 6;
673 rect
.width
= cw
- 12;
674 rect
.height
= ch
- 4 - rect
.y
;
683 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
685 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
686 : wxTabView(style
), m_nextid(1)
688 m_notebook
= notebook
;
690 m_notebook
->SetTabView(this);
692 SetWindow(m_notebook
);
695 wxNotebookTabView::~wxNotebookTabView(void)
699 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
701 int& id
= m_pageToId
[page
];
706 m_idToPage
[id
] = page
;
712 // Called when a tab is activated
713 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
718 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
720 // Translate from wxTabView's ids (which aren't position-dependent)
721 // to wxNotebook's (which are).
722 wxNotebookPage
* pActive
= GetPage(activateId
);
723 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
725 int activatePos
= m_notebook
->FindPagePosition(pActive
);
726 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
728 event
.SetEventObject(m_notebook
);
729 event
.SetSelection(activatePos
);
730 event
.SetOldSelection(deactivatePos
);
731 m_notebook
->GetEventHandler()->ProcessEvent(event
);
735 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
741 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
743 // Translate from wxTabView's ids (which aren't position-dependent)
744 // to wxNotebook's (which are).
745 wxNotebookPage
* pActive
= GetPage(activateId
);
746 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
748 int activatePos
= m_notebook
->FindPagePosition(pActive
);
749 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
751 event
.SetEventObject(m_notebook
);
752 event
.SetSelection(activatePos
);
753 event
.SetOldSelection(deactivatePos
);
754 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
756 retval
= event
.IsAllowed();
762 #endif // wxUSE_NOTEBOOK