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 #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 #if defined (__WIN16__)
150 m_tabView
->SetTabSelection(nPage
);
152 wxNotebookPage
* pPage
= GetPage(nPage
);
154 m_tabView
->SetTabSelection((int) (long) pPage
);
160 void wxNotebook::AdvanceSelection(bool bForward
)
162 int nSel
= GetSelection();
163 int nMax
= GetPageCount() - 1;
165 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
167 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
170 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
172 wxASSERT( IS_VALID_PAGE(nPage
) );
173 #if defined (__WIN16__)
174 m_tabView
->SetTabText(nPage
, strText
);
178 wxNotebookPage
* page
= GetPage(nPage
);
181 m_tabView
->SetTabText((int) (long) page
, strText
);
189 wxString
wxNotebook::GetPageText(int nPage
) const
191 wxASSERT( IS_VALID_PAGE(nPage
) );
193 #if defined (__WIN16__)
194 return m_tabView
->GetTabText(nPage
);
196 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
198 return m_tabView
->GetTabText((int) (long) page
);
200 return wxEmptyString
;
204 int wxNotebook::GetPageImage(int nPage
) const
206 wxASSERT( IS_VALID_PAGE(nPage
) );
212 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
214 wxASSERT( IS_VALID_PAGE(nPage
) );
220 void wxNotebook::SetImageList(wxImageList
* imageList
)
222 m_pImageList
= imageList
;
226 // ----------------------------------------------------------------------------
227 // wxNotebook operations
228 // ----------------------------------------------------------------------------
230 // remove one page from the notebook and delete it
231 bool wxNotebook::DeletePage(int nPage
)
233 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
235 if (m_nSelection
!= -1)
237 m_aPages
[m_nSelection
]->Show(FALSE
);
238 m_aPages
[m_nSelection
]->Lower();
241 wxNotebookPage
* pPage
= GetPage(nPage
);
242 #if defined (__WIN16__)
243 m_tabView
->RemoveTab(nPage
);
245 m_tabView
->RemoveTab((int) (long) pPage
);
248 delete m_aPages
[nPage
];
249 m_aPages
.Remove(nPage
);
251 if (m_aPages
.GetCount() == 0)
254 m_tabView
->SetTabSelection(-1, FALSE
);
256 else if (m_nSelection
> -1)
259 #if defined (__WIN16__)
260 m_tabView
->SetTabSelection(0, FALSE
);
262 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
264 if (m_nSelection
!= 0)
268 RefreshLayout(FALSE
);
273 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
275 int pagePos
= FindPagePosition(page
);
277 return DeletePage(pagePos
);
282 // remove one page from the notebook
283 bool wxNotebook::RemovePage(int nPage
)
285 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
287 m_aPages
[nPage
]->Show(FALSE
);
288 // m_aPages[nPage]->Lower();
290 wxNotebookPage
* pPage
= GetPage(nPage
);
291 #if defined (__WIN16__)
292 m_tabView
->RemoveTab(nPage
);
294 m_tabView
->RemoveTab((int) (long) pPage
);
297 m_aPages
.Remove(nPage
);
299 if (m_aPages
.GetCount() == 0)
302 m_tabView
->SetTabSelection(-1, TRUE
);
304 else if (m_nSelection
> -1)
306 // Only change the selection if the page we
307 // deleted was the selection.
308 if (nPage
== m_nSelection
)
311 // Select the first tab. Generates a ChangePage.
312 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
316 // We must adjust which tab we think is selected.
317 // If greater than the page we deleted, it must be moved down
319 if (m_nSelection
> nPage
)
324 RefreshLayout(FALSE
);
329 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
331 int pagePos
= FindPagePosition(page
);
333 return RemovePage(pagePos
);
338 // Find the position of the wxNotebookPage, -1 if not found.
339 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
341 int nPageCount
= GetPageCount();
343 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
344 if (m_aPages
[nPage
] == page
)
350 bool wxNotebook::DeleteAllPages()
352 m_tabView
->ClearTabs(TRUE
);
354 int nPageCount
= GetPageCount();
356 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
357 delete m_aPages
[nPage
];
364 // add a page to the notebook
365 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
366 const wxString
& strText
,
370 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
373 // same as AddPage() but does it at given position
374 bool wxNotebook::InsertPage(int nPage
,
375 wxNotebookPage
*pPage
,
376 const wxString
& strText
,
380 wxASSERT( pPage
!= NULL
);
381 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
383 // For 16 bit integers (tabs limited to 32768)
384 #if defined (__WIN16__)
385 m_tabView
->AddTab(nPage
, strText
);
387 m_tabView
->AddTab((int) (long) pPage
, strText
);
392 // save the pointer to the page
393 m_aPages
.Insert(pPage
, nPage
);
397 // This will cause ChangePage to be called, via OnSelPage
398 #if defined (__WIN16__)
399 m_tabView
->SetTabSelection(nPage
, TRUE
);
401 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
405 // some page must be selected: either this one or the first one if there is
406 // still no selection
407 if ( m_nSelection
== -1 )
410 RefreshLayout(FALSE
);
415 // ----------------------------------------------------------------------------
416 // wxNotebook callbacks
417 // ----------------------------------------------------------------------------
419 // @@@ OnSize() is used for setting the font when it's called for the first
420 // time because doing it in ::Create() doesn't work (for unknown reasons)
421 void wxNotebook::OnSize(wxSizeEvent
& event
)
423 static bool s_bFirstTime
= TRUE
;
424 if ( s_bFirstTime
) {
425 // TODO: any first-time-size processing.
426 s_bFirstTime
= FALSE
;
431 // Processing continues to next OnSize
435 // This was supposed to cure the non-display of the notebook
436 // until the user resizes the window.
438 void wxNotebook::OnIdle(wxIdleEvent
& event
)
440 static bool s_bFirstTime
= TRUE
;
441 if ( s_bFirstTime
) {
443 wxSize sz(GetSize());
451 wxSize sz(GetSize());
452 wxSizeEvent sizeEvent(sz, GetId());
453 sizeEvent.SetEventObject(this);
454 GetEventHandler()->ProcessEvent(sizeEvent);
457 s_bFirstTime
= FALSE
;
462 // Implementation: calculate the layout of the view rect
463 // and resize the children if required
464 bool wxNotebook::RefreshLayout(bool force
)
468 wxRect oldRect
= m_tabView
->GetViewRect();
471 GetClientSize(& cw
, & ch
);
473 int tabHeight
= m_tabView
->GetTotalTabHeight();
476 rect
.y
= tabHeight
+ 4;
478 rect
.height
= ch
- 4 - rect
.y
;
480 m_tabView
->SetViewRect(rect
);
482 m_tabView
->LayoutTabs();
484 // Need to do it a 2nd time to get the tab height with
485 // the new view width, since changing the view width changes the
487 tabHeight
= m_tabView
->GetTotalTabHeight();
489 rect
.y
= tabHeight
+ 4;
491 rect
.height
= ch
- 4 - rect
.y
;
493 m_tabView
->SetViewRect(rect
);
495 m_tabView
->LayoutTabs();
497 if (!force
&& (rect
== oldRect
))
500 // fit the notebook page to the tab control's display area
502 unsigned int nCount
= m_aPages
.Count();
503 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
504 wxNotebookPage
*pPage
= m_aPages
[nPage
];
505 if (pPage
->IsShown())
507 wxRect clientRect
= GetAvailableClientSize();
508 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
509 if ( pPage
->GetAutoLayout() )
518 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
520 // is it our tab control?
521 if ( event
.GetEventObject() == this )
523 if (event
.GetSelection() != m_nSelection
)
524 ChangePage(event
.GetOldSelection(), event
.GetSelection());
527 // we want to give others a chance to process this message as well
531 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
533 // set focus to the currently selected page if any
534 if ( m_nSelection
!= -1 )
535 m_aPages
[m_nSelection
]->SetFocus();
540 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
542 if ( event
.IsWindowChange() ) {
544 AdvanceSelection(event
.GetDirection());
547 // pass to the parent
549 event
.SetCurrentFocus(this);
550 GetParent()->ProcessEvent(event
);
555 // ----------------------------------------------------------------------------
556 // wxNotebook base class virtuals
557 // ----------------------------------------------------------------------------
559 // override these 2 functions to do nothing: everything is done in OnSize
561 void wxNotebook::SetConstraintSizes(bool /* recurse */)
563 // don't set the sizes of the pages - their correct size is not yet known
564 wxControl::SetConstraintSizes(FALSE
);
567 bool wxNotebook::DoPhase(int /* nPhase */)
572 void wxNotebook::Command(wxCommandEvent
& event
)
574 wxFAIL_MSG("wxNotebook::Command not implemented");
577 // ----------------------------------------------------------------------------
578 // wxNotebook helper functions
579 // ----------------------------------------------------------------------------
581 // hide the currently active panel and show the new one
582 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
584 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
585 wxASSERT( nOldSel
!= nSel
); // impossible
587 if ( nOldSel
!= -1 ) {
588 m_aPages
[nOldSel
]->Show(FALSE
);
589 m_aPages
[nOldSel
]->Lower();
592 wxNotebookPage
*pPage
= m_aPages
[nSel
];
594 wxRect clientRect
= GetAvailableClientSize();
595 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
606 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
609 m_tabView
->OnEvent(event
);
612 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
619 wxRect
wxNotebook::GetAvailableClientSize()
622 GetClientSize(& cw
, & ch
);
624 int tabHeight
= m_tabView
->GetTotalTabHeight();
626 // TODO: these margins should be configurable.
629 rect
.y
= tabHeight
+ 6;
630 rect
.width
= cw
- 12;
631 rect
.height
= ch
- 4 - rect
.y
;
640 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
642 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
644 m_notebook
= notebook
;
646 m_notebook
->SetTabView(this);
648 SetWindow(m_notebook
);
651 wxNotebookTabView::~wxNotebookTabView(void)
655 // Called when a tab is activated
656 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
661 // Because of name truncation!
662 #if defined(__BORLANDC__) && defined(__WIN16__)
663 wxNotebookEvent
event(wxEVT_COMMAND_NB_PAGE_CHANGED
, m_notebook
->GetId());
665 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
668 #if defined (__WIN16__)
669 int activatePos
= activateId
;
670 int deactivatePos
= deactivateId
;
672 // Translate from wxTabView's ids (which aren't position-dependent)
673 // to wxNotebook's (which are).
674 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
675 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
677 int activatePos
= m_notebook
->FindPagePosition(pActive
);
678 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
681 event
.SetEventObject(m_notebook
);
682 event
.SetSelection(activatePos
);
683 event
.SetOldSelection(deactivatePos
);
684 m_notebook
->GetEventHandler()->ProcessEvent(event
);