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/generic/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 #if !USE_SHARED_LIBRARIES
49 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
50 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
51 EVT_SIZE(wxNotebook::OnSize
)
52 EVT_PAINT(wxNotebook::OnPaint
)
53 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
54 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
55 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
56 // EVT_IDLE(wxNotebook::OnIdle)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
60 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxNotebook construction
69 // ----------------------------------------------------------------------------
71 // common part of all ctors
72 void wxNotebook::Init()
74 m_tabView
= (wxNotebookTabView
*) NULL
;
79 // default for dynamic class
80 wxNotebook::wxNotebook()
85 // the same arguments as for wxControl
86 wxNotebook::wxNotebook(wxWindow
*parent
,
95 Create(parent
, id
, pos
, size
, style
, name
);
99 bool wxNotebook::Create(wxWindow
*parent
,
104 const wxString
& name
)
109 m_windowId
= id
== -1 ? NewControlId() : id
;
111 // It's like a normal window...
112 if (!wxWindow::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, name
))
115 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
117 SetTabView(new wxNotebookTabView(this));
123 wxNotebook::~wxNotebook()
128 // ----------------------------------------------------------------------------
129 // wxNotebook accessors
130 // ----------------------------------------------------------------------------
131 int wxNotebook::GetPageCount() const
133 return m_aPages
.Count();
136 int wxNotebook::GetRowCount() const
142 int wxNotebook::SetSelection(int nPage
)
147 wxASSERT( IS_VALID_PAGE(nPage
) );
149 wxNotebookPage
* pPage
= GetPage(nPage
);
151 m_tabView
->SetTabSelection((int) (long) pPage
);
157 void wxNotebook::AdvanceSelection(bool bForward
)
159 int nSel
= GetSelection();
160 int nMax
= GetPageCount() - 1;
162 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
164 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
167 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
169 wxASSERT( IS_VALID_PAGE(nPage
) );
171 wxNotebookPage
* page
= GetPage(nPage
);
174 m_tabView
->SetTabText((int) (long) page
, strText
);
182 wxString
wxNotebook::GetPageText(int nPage
) const
184 wxASSERT( IS_VALID_PAGE(nPage
) );
186 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
188 return m_tabView
->GetTabText((int) (long) page
);
190 return wxEmptyString
;
193 int wxNotebook::GetPageImage(int nPage
) const
195 wxASSERT( IS_VALID_PAGE(nPage
) );
201 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
203 wxASSERT( IS_VALID_PAGE(nPage
) );
209 void wxNotebook::SetImageList(wxImageList
* imageList
)
211 m_pImageList
= imageList
;
215 // ----------------------------------------------------------------------------
216 // wxNotebook operations
217 // ----------------------------------------------------------------------------
219 // remove one page from the notebook and delete it
220 bool wxNotebook::DeletePage(int nPage
)
222 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
224 if (m_nSelection
!= -1)
226 m_aPages
[m_nSelection
]->Show(FALSE
);
227 m_aPages
[m_nSelection
]->Lower();
230 wxNotebookPage
* pPage
= GetPage(nPage
);
231 m_tabView
->RemoveTab((int) (long) pPage
);
233 delete m_aPages
[nPage
];
234 m_aPages
.Remove(nPage
);
236 if (m_aPages
.GetCount() == 0)
239 m_tabView
->SetTabSelection(-1, FALSE
);
241 else if (m_nSelection
> -1)
244 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
245 if (m_nSelection
!= 0)
249 RefreshLayout(FALSE
);
254 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
256 int pagePos
= FindPagePosition(page
);
258 return DeletePage(pagePos
);
263 // remove one page from the notebook
264 bool wxNotebook::RemovePage(int nPage
)
266 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
268 m_aPages
[nPage
]->Show(FALSE
);
269 // m_aPages[nPage]->Lower();
271 wxNotebookPage
* pPage
= GetPage(nPage
);
272 m_tabView
->RemoveTab((int) (long) pPage
);
274 m_aPages
.Remove(nPage
);
276 if (m_aPages
.GetCount() == 0)
279 m_tabView
->SetTabSelection(-1, TRUE
);
281 else if (m_nSelection
> -1)
283 // Only change the selection if the page we
284 // deleted was the selection.
285 if (nPage
== m_nSelection
)
288 // Select the first tab. Generates a ChangePage.
289 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
293 // We must adjust which tab we think is selected.
294 // If greater than the page we deleted, it must be moved down
296 if (m_nSelection
> nPage
)
301 RefreshLayout(FALSE
);
306 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
308 int pagePos
= FindPagePosition(page
);
310 return RemovePage(pagePos
);
315 // Find the position of the wxNotebookPage, -1 if not found.
316 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
318 int nPageCount
= GetPageCount();
320 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
321 if (m_aPages
[nPage
] == page
)
327 bool wxNotebook::DeleteAllPages()
329 m_tabView
->ClearTabs(TRUE
);
331 int nPageCount
= GetPageCount();
333 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
334 delete m_aPages
[nPage
];
341 // add a page to the notebook
342 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
343 const wxString
& strText
,
347 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
350 // same as AddPage() but does it at given position
351 bool wxNotebook::InsertPage(int nPage
,
352 wxNotebookPage
*pPage
,
353 const wxString
& strText
,
357 wxASSERT( pPage
!= NULL
);
358 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
360 m_tabView
->AddTab((int) (long) pPage
, strText
);
364 // save the pointer to the page
365 m_aPages
.Insert(pPage
, nPage
);
369 // This will cause ChangePage to be called, via OnSelPage
370 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
373 // some page must be selected: either this one or the first one if there is
374 // still no selection
375 if ( m_nSelection
== -1 )
378 RefreshLayout(FALSE
);
383 // ----------------------------------------------------------------------------
384 // wxNotebook callbacks
385 // ----------------------------------------------------------------------------
387 // @@@ OnSize() is used for setting the font when it's called for the first
388 // time because doing it in ::Create() doesn't work (for unknown reasons)
389 void wxNotebook::OnSize(wxSizeEvent
& event
)
391 static bool s_bFirstTime
= TRUE
;
392 if ( s_bFirstTime
) {
393 // TODO: any first-time-size processing.
394 s_bFirstTime
= FALSE
;
399 // Processing continues to next OnSize
403 // This was supposed to cure the non-display of the notebook
404 // until the user resizes the window.
406 void wxNotebook::OnIdle(wxIdleEvent
& event
)
408 static bool s_bFirstTime
= TRUE
;
409 if ( s_bFirstTime
) {
411 wxSize sz(GetSize());
419 wxSize sz(GetSize());
420 wxSizeEvent sizeEvent(sz, GetId());
421 sizeEvent.SetEventObject(this);
422 GetEventHandler()->ProcessEvent(sizeEvent);
425 s_bFirstTime
= FALSE
;
430 // Implementation: calculate the layout of the view rect
431 // and resize the children if required
432 bool wxNotebook::RefreshLayout(bool force
)
436 wxRect oldRect
= m_tabView
->GetViewRect();
439 GetClientSize(& cw
, & ch
);
441 int tabHeight
= m_tabView
->GetTotalTabHeight();
444 rect
.y
= tabHeight
+ 4;
446 rect
.height
= ch
- 4 - rect
.y
;
448 m_tabView
->SetViewRect(rect
);
452 // Need to do it a 2nd time to get the tab height with
453 // the new view width, since changing the view width changes the
455 tabHeight
= m_tabView
->GetTotalTabHeight();
457 rect
.y
= tabHeight
+ 4;
459 rect
.height
= ch
- 4 - rect
.y
;
461 m_tabView
->SetViewRect(rect
);
465 if (!force
&& (rect
== oldRect
))
468 // fit the notebook page to the tab control's display area
470 unsigned int nCount
= m_aPages
.Count();
471 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
472 wxNotebookPage
*pPage
= m_aPages
[nPage
];
473 if (pPage
->IsShown())
475 wxRect clientRect
= GetAvailableClientSize();
476 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
477 if ( pPage
->GetAutoLayout() )
486 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
488 // is it our tab control?
489 if ( event
.GetEventObject() == this )
491 if (event
.GetSelection() != m_nSelection
)
492 ChangePage(event
.GetOldSelection(), event
.GetSelection());
495 // we want to give others a chance to process this message as well
499 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
501 // set focus to the currently selected page if any
502 if ( m_nSelection
!= -1 )
503 m_aPages
[m_nSelection
]->SetFocus();
508 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
510 if ( event
.IsWindowChange() ) {
512 AdvanceSelection(event
.GetDirection());
515 // pass to the parent
517 event
.SetCurrentFocus(this);
518 GetParent()->ProcessEvent(event
);
523 // ----------------------------------------------------------------------------
524 // wxNotebook base class virtuals
525 // ----------------------------------------------------------------------------
527 // override these 2 functions to do nothing: everything is done in OnSize
529 void wxNotebook::SetConstraintSizes(bool /* recurse */)
531 // don't set the sizes of the pages - their correct size is not yet known
532 wxControl::SetConstraintSizes(FALSE
);
535 bool wxNotebook::DoPhase(int /* nPhase */)
540 void wxNotebook::Command(wxCommandEvent
& event
)
542 wxFAIL_MSG("wxNotebook::Command not implemented");
545 // ----------------------------------------------------------------------------
546 // wxNotebook helper functions
547 // ----------------------------------------------------------------------------
549 // hide the currently active panel and show the new one
550 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
552 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
553 wxASSERT( nOldSel
!= nSel
); // impossible
555 if ( nOldSel
!= -1 ) {
556 m_aPages
[nOldSel
]->Show(FALSE
);
557 m_aPages
[nOldSel
]->Lower();
560 wxNotebookPage
*pPage
= m_aPages
[nSel
];
562 wxRect clientRect
= GetAvailableClientSize();
563 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
574 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
577 m_tabView
->OnEvent(event
);
580 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
587 wxRect
wxNotebook::GetAvailableClientSize()
590 GetClientSize(& cw
, & ch
);
592 int tabHeight
= m_tabView
->GetTotalTabHeight();
594 // TODO: these margins should be configurable.
597 rect
.y
= tabHeight
+ 6;
598 rect
.width
= cw
- 12;
599 rect
.height
= ch
- 4 - rect
.y
;
608 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
610 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
612 m_notebook
= notebook
;
614 m_notebook
->SetTabView(this);
616 SetWindow(m_notebook
);
619 wxNotebookTabView::~wxNotebookTabView(void)
623 // Called when a tab is activated
624 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
629 // Because of name truncation!
630 #if defined(__BORLANDC__) && defined(__WIN16__)
631 wxNotebookEvent
event(wxEVT_COMMAND_NB_PAGE_CHANGED
, m_notebook
->GetId());
633 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
636 // Translate from wxTabView's ids (which aren't position-dependent)
637 // to wxNotebook's (which are).
638 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
639 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
641 int activatePos
= m_notebook
->FindPagePosition(pActive
);
642 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
644 event
.SetEventObject(m_notebook
);
645 event
.SetSelection(activatePos
);
646 event
.SetOldSelection(deactivatePos
);
647 m_notebook
->GetEventHandler()->ProcessEvent(event
);