1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/string.h"
32 #include "wx/settings.h"
33 #include "wx/generic/imaglist.h"
34 #include "wx/notebook.h"
35 #include "wx/dcclient.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // check that the page index is valid
42 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
49 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
50 EVT_SIZE(wxNotebook::OnSize
)
51 EVT_PAINT(wxNotebook::OnPaint
)
52 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
53 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
54 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
55 // EVT_IDLE(wxNotebook::OnIdle)
58 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxNotebook construction
67 // ----------------------------------------------------------------------------
69 // common part of all ctors
70 void wxNotebook::Init()
72 m_tabView
= (wxNotebookTabView
*) NULL
;
77 // default for dynamic class
78 wxNotebook::wxNotebook()
83 // the same arguments as for wxControl
84 wxNotebook::wxNotebook(wxWindow
*parent
,
93 Create(parent
, id
, pos
, size
, style
, name
);
97 bool wxNotebook::Create(wxWindow
*parent
,
102 const wxString
& name
)
107 m_windowId
= id
== -1 ? NewControlId() : id
;
109 // It's like a normal window...
110 if (!wxWindow::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, name
))
113 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
115 SetTabView(new wxNotebookTabView(this));
121 wxNotebook::~wxNotebook()
126 // ----------------------------------------------------------------------------
127 // wxNotebook accessors
128 // ----------------------------------------------------------------------------
129 int wxNotebook::GetPageCount() const
131 return m_aPages
.Count();
134 int wxNotebook::GetRowCount() const
140 int wxNotebook::SetSelection(int nPage
)
145 wxASSERT( IS_VALID_PAGE(nPage
) );
147 #if defined (__WIN16__)
148 m_tabView
->SetTabSelection(nPage
);
150 wxNotebookPage
* pPage
= GetPage(nPage
);
152 m_tabView
->SetTabSelection((int) (long) pPage
);
158 void wxNotebook::AdvanceSelection(bool bForward
)
160 int nSel
= GetSelection();
161 int nMax
= GetPageCount() - 1;
163 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
165 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
168 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
170 wxASSERT( IS_VALID_PAGE(nPage
) );
171 #if defined (__WIN16__)
172 m_tabView
->SetTabText(nPage
, strText
);
176 wxNotebookPage
* page
= GetPage(nPage
);
179 m_tabView
->SetTabText((int) (long) page
, strText
);
187 wxString
wxNotebook::GetPageText(int nPage
) const
189 wxASSERT( IS_VALID_PAGE(nPage
) );
191 #if defined (__WIN16__)
192 return m_tabView
->GetTabText(nPage
);
194 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
196 return m_tabView
->GetTabText((int) (long) page
);
198 return wxEmptyString
;
202 int wxNotebook::GetPageImage(int nPage
) const
204 wxASSERT( IS_VALID_PAGE(nPage
) );
210 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
212 wxASSERT( IS_VALID_PAGE(nPage
) );
218 void wxNotebook::SetImageList(wxImageList
* imageList
)
220 m_pImageList
= imageList
;
224 // ----------------------------------------------------------------------------
225 // wxNotebook operations
226 // ----------------------------------------------------------------------------
228 // remove one page from the notebook and delete it
229 bool wxNotebook::DeletePage(int nPage
)
231 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
233 if (m_nSelection
!= -1)
235 m_aPages
[m_nSelection
]->Show(FALSE
);
236 m_aPages
[m_nSelection
]->Lower();
239 wxNotebookPage
* pPage
= GetPage(nPage
);
240 #if defined (__WIN16__)
241 m_tabView
->RemoveTab(nPage
);
243 m_tabView
->RemoveTab((int) (long) pPage
);
246 delete m_aPages
[nPage
];
247 m_aPages
.Remove(nPage
);
249 if (m_aPages
.GetCount() == 0)
252 m_tabView
->SetTabSelection(-1, FALSE
);
254 else if (m_nSelection
> -1)
257 #if defined (__WIN16__)
258 m_tabView
->SetTabSelection(0, FALSE
);
260 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
262 if (m_nSelection
!= 0)
266 RefreshLayout(FALSE
);
271 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
273 int pagePos
= FindPagePosition(page
);
275 return DeletePage(pagePos
);
280 // remove one page from the notebook
281 bool wxNotebook::RemovePage(int nPage
)
283 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
285 m_aPages
[nPage
]->Show(FALSE
);
286 // m_aPages[nPage]->Lower();
288 wxNotebookPage
* pPage
= GetPage(nPage
);
289 #if defined (__WIN16__)
290 m_tabView
->RemoveTab(nPage
);
292 m_tabView
->RemoveTab((int) (long) pPage
);
295 m_aPages
.Remove(nPage
);
297 if (m_aPages
.GetCount() == 0)
300 m_tabView
->SetTabSelection(-1, TRUE
);
302 else if (m_nSelection
> -1)
304 // Only change the selection if the page we
305 // deleted was the selection.
306 if (nPage
== m_nSelection
)
309 // Select the first tab. Generates a ChangePage.
310 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
314 // We must adjust which tab we think is selected.
315 // If greater than the page we deleted, it must be moved down
317 if (m_nSelection
> nPage
)
322 RefreshLayout(FALSE
);
327 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
329 int pagePos
= FindPagePosition(page
);
331 return RemovePage(pagePos
);
336 // Find the position of the wxNotebookPage, -1 if not found.
337 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
339 int nPageCount
= GetPageCount();
341 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
342 if (m_aPages
[nPage
] == page
)
348 bool wxNotebook::DeleteAllPages()
350 m_tabView
->ClearTabs(TRUE
);
352 int nPageCount
= GetPageCount();
354 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
355 delete m_aPages
[nPage
];
362 // add a page to the notebook
363 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
364 const wxString
& strText
,
368 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
371 // same as AddPage() but does it at given position
372 bool wxNotebook::InsertPage(int nPage
,
373 wxNotebookPage
*pPage
,
374 const wxString
& strText
,
378 wxASSERT( pPage
!= NULL
);
379 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
381 // For 16 bit integers (tabs limited to 32768)
382 #if defined (__WIN16__)
383 m_tabView
->AddTab(nPage
, strText
);
385 m_tabView
->AddTab((int) (long) pPage
, strText
);
390 // save the pointer to the page
391 m_aPages
.Insert(pPage
, nPage
);
395 // This will cause ChangePage to be called, via OnSelPage
396 #if defined (__WIN16__)
397 m_tabView
->SetTabSelection(nPage
, TRUE
);
399 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
403 // some page must be selected: either this one or the first one if there is
404 // still no selection
405 if ( m_nSelection
== -1 )
408 RefreshLayout(FALSE
);
413 // ----------------------------------------------------------------------------
414 // wxNotebook callbacks
415 // ----------------------------------------------------------------------------
417 // @@@ OnSize() is used for setting the font when it's called for the first
418 // time because doing it in ::Create() doesn't work (for unknown reasons)
419 void wxNotebook::OnSize(wxSizeEvent
& event
)
421 static bool s_bFirstTime
= TRUE
;
422 if ( s_bFirstTime
) {
423 // TODO: any first-time-size processing.
424 s_bFirstTime
= FALSE
;
429 // Processing continues to next OnSize
433 // This was supposed to cure the non-display of the notebook
434 // until the user resizes the window.
436 void wxNotebook::OnIdle(wxIdleEvent
& event
)
438 static bool s_bFirstTime
= TRUE
;
439 if ( s_bFirstTime
) {
441 wxSize sz(GetSize());
449 wxSize sz(GetSize());
450 wxSizeEvent sizeEvent(sz, GetId());
451 sizeEvent.SetEventObject(this);
452 GetEventHandler()->ProcessEvent(sizeEvent);
455 s_bFirstTime
= FALSE
;
460 // Implementation: calculate the layout of the view rect
461 // and resize the children if required
462 bool wxNotebook::RefreshLayout(bool force
)
466 wxRect oldRect
= m_tabView
->GetViewRect();
469 GetClientSize(& cw
, & ch
);
471 int tabHeight
= m_tabView
->GetTotalTabHeight();
474 rect
.y
= tabHeight
+ 4;
476 rect
.height
= ch
- 4 - rect
.y
;
478 m_tabView
->SetViewRect(rect
);
480 m_tabView
->LayoutTabs();
482 // Need to do it a 2nd time to get the tab height with
483 // the new view width, since changing the view width changes the
485 tabHeight
= m_tabView
->GetTotalTabHeight();
487 rect
.y
= tabHeight
+ 4;
489 rect
.height
= ch
- 4 - rect
.y
;
491 m_tabView
->SetViewRect(rect
);
493 m_tabView
->LayoutTabs();
495 if (!force
&& (rect
== oldRect
))
498 // fit the notebook page to the tab control's display area
500 unsigned int nCount
= m_aPages
.Count();
501 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
502 wxNotebookPage
*pPage
= m_aPages
[nPage
];
503 if (pPage
->IsShown())
505 wxRect clientRect
= GetAvailableClientSize();
506 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
507 if ( pPage
->GetAutoLayout() )
516 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
518 // is it our tab control?
519 if ( event
.GetEventObject() == this )
521 if (event
.GetSelection() != m_nSelection
)
522 ChangePage(event
.GetOldSelection(), event
.GetSelection());
525 // we want to give others a chance to process this message as well
529 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
531 // set focus to the currently selected page if any
532 if ( m_nSelection
!= -1 )
533 m_aPages
[m_nSelection
]->SetFocus();
538 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
540 if ( event
.IsWindowChange() ) {
542 AdvanceSelection(event
.GetDirection());
545 // pass to the parent
547 event
.SetCurrentFocus(this);
548 GetParent()->ProcessEvent(event
);
553 // ----------------------------------------------------------------------------
554 // wxNotebook base class virtuals
555 // ----------------------------------------------------------------------------
557 // override these 2 functions to do nothing: everything is done in OnSize
559 void wxNotebook::SetConstraintSizes(bool /* recurse */)
561 // don't set the sizes of the pages - their correct size is not yet known
562 wxControl::SetConstraintSizes(FALSE
);
565 bool wxNotebook::DoPhase(int /* nPhase */)
570 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
572 wxFAIL_MSG("wxNotebook::Command not implemented");
575 // ----------------------------------------------------------------------------
576 // wxNotebook helper functions
577 // ----------------------------------------------------------------------------
579 // hide the currently active panel and show the new one
580 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
582 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
583 wxASSERT( nOldSel
!= nSel
); // impossible
585 if ( nOldSel
!= -1 ) {
586 m_aPages
[nOldSel
]->Show(FALSE
);
587 m_aPages
[nOldSel
]->Lower();
590 wxNotebookPage
*pPage
= m_aPages
[nSel
];
592 wxRect clientRect
= GetAvailableClientSize();
593 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
604 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
607 m_tabView
->OnEvent(event
);
610 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
617 wxRect
wxNotebook::GetAvailableClientSize()
620 GetClientSize(& cw
, & ch
);
622 int tabHeight
= m_tabView
->GetTotalTabHeight();
624 // TODO: these margins should be configurable.
627 rect
.y
= tabHeight
+ 6;
628 rect
.width
= cw
- 12;
629 rect
.height
= ch
- 4 - rect
.y
;
638 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
640 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
642 m_notebook
= notebook
;
644 m_notebook
->SetTabView(this);
646 SetWindow(m_notebook
);
649 wxNotebookTabView::~wxNotebookTabView(void)
653 // Called when a tab is activated
654 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
659 // Because of name truncation!
660 #if defined(__BORLANDC__) && defined(__WIN16__)
661 wxNotebookEvent
event(wxEVT_COMMAND_NB_PAGE_CHANGED
, m_notebook
->GetId());
663 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
666 #if defined (__WIN16__)
667 int activatePos
= activateId
;
668 int deactivatePos
= deactivateId
;
670 // Translate from wxTabView's ids (which aren't position-dependent)
671 // to wxNotebook's (which are).
672 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
673 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
675 int activatePos
= m_notebook
->FindPagePosition(pActive
);
676 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
679 event
.SetEventObject(m_notebook
);
680 event
.SetSelection(activatePos
);
681 event
.SetOldSelection(deactivatePos
);
682 m_notebook
->GetEventHandler()->ProcessEvent(event
);