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/imaglist.h>
33 #include <wx/generic/notebook.h>
34 #include <wx/dcclient.h>
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // check that the page index is valid
41 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 #if !USE_SHARED_LIBRARIES
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
)
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxNotebook construction
68 // ----------------------------------------------------------------------------
70 // common part of all ctors
71 void wxNotebook::Init()
73 m_tabView
= (wxNotebookTabView
*) NULL
;
78 // default for dynamic class
79 wxNotebook::wxNotebook()
84 // the same arguments as for wxControl
85 wxNotebook::wxNotebook(wxWindow
*parent
,
94 Create(parent
, id
, pos
, size
, style
, name
);
98 bool wxNotebook::Create(wxWindow
*parent
,
103 const wxString
& name
)
108 m_windowId
= id
== -1 ? NewControlId() : id
;
110 // It's like a normal window...
111 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
114 SetTabView(new wxNotebookTabView(this));
120 wxNotebook::~wxNotebook()
125 // ----------------------------------------------------------------------------
126 // wxNotebook accessors
127 // ----------------------------------------------------------------------------
128 int wxNotebook::GetPageCount() const
130 return m_aPages
.Count();
133 int wxNotebook::GetRowCount() const
139 int wxNotebook::SetSelection(int nPage
)
144 wxASSERT( IS_VALID_PAGE(nPage
) );
146 wxNotebookPage
* pPage
= GetPage(nPage
);
148 m_tabView
->SetTabSelection((int) (long) pPage
);
154 void wxNotebook::AdvanceSelection(bool bForward
)
156 int nSel
= GetSelection();
157 int nMax
= GetPageCount() - 1;
159 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
161 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
164 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
166 wxASSERT( IS_VALID_PAGE(nPage
) );
168 wxNotebookPage
* page
= GetPage(nPage
);
171 m_tabView
->SetTabText((int) (long) page
, strText
);
179 wxString
wxNotebook::GetPageText(int nPage
) const
181 wxASSERT( IS_VALID_PAGE(nPage
) );
183 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
185 return m_tabView
->GetTabText((int) (long) page
);
187 return wxEmptyString
;
190 int wxNotebook::GetPageImage(int nPage
) const
192 wxASSERT( IS_VALID_PAGE(nPage
) );
198 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
200 wxASSERT( IS_VALID_PAGE(nPage
) );
206 void wxNotebook::SetImageList(wxImageList
* imageList
)
208 m_pImageList
= imageList
;
212 // ----------------------------------------------------------------------------
213 // wxNotebook operations
214 // ----------------------------------------------------------------------------
216 // remove one page from the notebook and delete it
217 bool wxNotebook::DeletePage(int nPage
)
219 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
221 if (m_nSelection
!= -1)
223 m_aPages
[m_nSelection
]->Show(FALSE
);
224 m_aPages
[m_nSelection
]->Lower();
227 wxNotebookPage
* pPage
= GetPage(nPage
);
228 m_tabView
->RemoveTab((int) (long) pPage
);
230 delete m_aPages
[nPage
];
231 m_aPages
.Remove(nPage
);
233 if (m_aPages
.GetCount() == 0)
236 m_tabView
->SetTabSelection(-1, FALSE
);
238 else if (m_nSelection
> -1)
241 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
242 if (m_nSelection
!= 0)
246 RefreshLayout(FALSE
);
251 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
253 int pagePos
= FindPagePosition(page
);
255 return DeletePage(pagePos
);
260 // remove one page from the notebook
261 bool wxNotebook::RemovePage(int nPage
)
263 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
265 m_aPages
[nPage
]->Show(FALSE
);
266 // m_aPages[nPage]->Lower();
268 wxNotebookPage
* pPage
= GetPage(nPage
);
269 m_tabView
->RemoveTab((int) (long) pPage
);
271 m_aPages
.Remove(nPage
);
273 if (m_aPages
.GetCount() == 0)
276 m_tabView
->SetTabSelection(-1, TRUE
);
278 else if (m_nSelection
> -1)
280 // Only change the selection if the page we
281 // deleted was the selection.
282 if (nPage
== m_nSelection
)
285 // Select the first tab. Generates a ChangePage.
286 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
290 // We must adjust which tab we think is selected.
291 // If greater than the page we deleted, it must be moved down
293 if (m_nSelection
> nPage
)
298 RefreshLayout(FALSE
);
303 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
305 int pagePos
= FindPagePosition(page
);
307 return RemovePage(pagePos
);
312 // Find the position of the wxNotebookPage, -1 if not found.
313 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
315 int nPageCount
= GetPageCount();
317 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
318 if (m_aPages
[nPage
] == page
)
324 bool wxNotebook::DeleteAllPages()
326 m_tabView
->ClearTabs(TRUE
);
328 int nPageCount
= GetPageCount();
330 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
331 delete m_aPages
[nPage
];
338 // add a page to the notebook
339 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
340 const wxString
& strText
,
344 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
347 // same as AddPage() but does it at given position
348 bool wxNotebook::InsertPage(int nPage
,
349 wxNotebookPage
*pPage
,
350 const wxString
& strText
,
354 wxASSERT( pPage
!= NULL
);
355 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
357 m_tabView
->AddTab((int) (long) pPage
, strText
);
361 // save the pointer to the page
362 m_aPages
.Insert(pPage
, nPage
);
366 // This will cause ChangePage to be called, via OnSelPage
367 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
370 // some page must be selected: either this one or the first one if there is
371 // still no selection
372 if ( m_nSelection
== -1 )
375 RefreshLayout(FALSE
);
380 // ----------------------------------------------------------------------------
381 // wxNotebook callbacks
382 // ----------------------------------------------------------------------------
384 // @@@ OnSize() is used for setting the font when it's called for the first
385 // time because doing it in ::Create() doesn't work (for unknown reasons)
386 void wxNotebook::OnSize(wxSizeEvent
& event
)
388 static bool s_bFirstTime
= TRUE
;
389 if ( s_bFirstTime
) {
390 // TODO: any first-time-size processing.
391 s_bFirstTime
= FALSE
;
396 // Processing continues to next OnSize
400 // This was supposed to cure the non-display of the notebook
401 // until the user resizes the window.
403 void wxNotebook::OnIdle(wxIdleEvent
& event
)
405 static bool s_bFirstTime
= TRUE
;
406 if ( s_bFirstTime
) {
408 wxSize sz(GetSize());
416 wxSize sz(GetSize());
417 wxSizeEvent sizeEvent(sz, GetId());
418 sizeEvent.SetEventObject(this);
419 GetEventHandler()->ProcessEvent(sizeEvent);
422 s_bFirstTime
= FALSE
;
427 // Implementation: calculate the layout of the view rect
428 // and resize the children if required
429 bool wxNotebook::RefreshLayout(bool force
)
433 wxRect oldRect
= m_tabView
->GetViewRect();
436 GetClientSize(& cw
, & ch
);
438 int tabHeight
= m_tabView
->GetTotalTabHeight();
441 rect
.y
= tabHeight
+ 4;
443 rect
.height
= ch
- 4 - rect
.y
;
445 m_tabView
->SetViewRect(rect
);
449 // Need to do it a 2nd time to get the tab height with
450 // the new view width, since changing the view width changes the
452 tabHeight
= m_tabView
->GetTotalTabHeight();
454 rect
.y
= tabHeight
+ 4;
456 rect
.height
= ch
- 4 - rect
.y
;
458 m_tabView
->SetViewRect(rect
);
462 if (!force
&& (rect
== oldRect
))
465 // fit the notebook page to the tab control's display area
467 unsigned int nCount
= m_aPages
.Count();
468 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
469 wxNotebookPage
*pPage
= m_aPages
[nPage
];
470 if (pPage
->IsShown())
472 wxRect clientRect
= GetAvailableClientSize();
473 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
474 if ( pPage
->GetAutoLayout() )
483 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
485 // is it our tab control?
486 if ( event
.GetEventObject() == this )
488 if (event
.GetSelection() != m_nSelection
)
489 ChangePage(event
.GetOldSelection(), event
.GetSelection());
492 // we want to give others a chance to process this message as well
496 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
498 // set focus to the currently selected page if any
499 if ( m_nSelection
!= -1 )
500 m_aPages
[m_nSelection
]->SetFocus();
505 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
507 if ( event
.IsWindowChange() ) {
509 AdvanceSelection(event
.GetDirection());
512 // pass to the parent
514 event
.SetCurrentFocus(this);
515 GetParent()->ProcessEvent(event
);
520 // ----------------------------------------------------------------------------
521 // wxNotebook base class virtuals
522 // ----------------------------------------------------------------------------
524 // override these 2 functions to do nothing: everything is done in OnSize
526 void wxNotebook::SetConstraintSizes(bool /* recurse */)
528 // don't set the sizes of the pages - their correct size is not yet known
529 wxControl::SetConstraintSizes(FALSE
);
532 bool wxNotebook::DoPhase(int /* nPhase */)
537 void wxNotebook::Command(wxCommandEvent
& event
)
539 wxFAIL_MSG("wxNotebook::Command not implemented");
542 // ----------------------------------------------------------------------------
543 // wxNotebook helper functions
544 // ----------------------------------------------------------------------------
546 // hide the currently active panel and show the new one
547 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
549 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
550 wxASSERT( nOldSel
!= nSel
); // impossible
552 if ( nOldSel
!= -1 ) {
553 m_aPages
[nOldSel
]->Show(FALSE
);
554 m_aPages
[nOldSel
]->Lower();
557 wxNotebookPage
*pPage
= m_aPages
[nSel
];
559 wxRect clientRect
= GetAvailableClientSize();
560 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
571 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
574 m_tabView
->OnEvent(event
);
577 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
584 wxRect
wxNotebook::GetAvailableClientSize()
587 GetClientSize(& cw
, & ch
);
589 int tabHeight
= m_tabView
->GetTotalTabHeight();
591 // TODO: these margins should be configurable.
594 rect
.y
= tabHeight
+ 6;
595 rect
.width
= cw
- 12;
596 rect
.height
= ch
- 4 - rect
.y
;
605 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
607 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
609 m_notebook
= notebook
;
611 m_notebook
->SetTabView(this);
613 SetWindow(m_notebook
);
616 wxNotebookTabView::~wxNotebookTabView(void)
620 // Called when a tab is activated
621 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
626 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
628 // Translate from wxTabView's ids (which aren't position-dependent)
629 // to wxNotebook's (which are).
630 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
631 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
633 int activatePos
= m_notebook
->FindPagePosition(pActive
);
634 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
636 event
.SetEventObject(m_notebook
);
637 event
.SetSelection(activatePos
);
638 event
.SetOldSelection(deactivatePos
);
639 m_notebook
->GetEventHandler()->ProcessEvent(event
);