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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
51 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
52 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
53 EVT_SIZE(wxNotebook::OnSize
)
54 EVT_PAINT(wxNotebook::OnPaint
)
55 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
56 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
57 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
58 // EVT_IDLE(wxNotebook::OnIdle)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
62 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxNotebook construction
70 // ----------------------------------------------------------------------------
72 // common part of all ctors
73 void wxNotebook::Init()
75 m_tabView
= (wxNotebookTabView
*) NULL
;
80 // default for dynamic class
81 wxNotebook::wxNotebook()
86 // the same arguments as for wxControl
87 wxNotebook::wxNotebook(wxWindow
*parent
,
96 Create(parent
, id
, pos
, size
, style
, name
);
100 bool wxNotebook::Create(wxWindow
*parent
,
105 const wxString
& name
)
110 m_windowId
= id
== -1 ? NewControlId() : id
;
112 // It's like a normal window...
113 if (!wxWindow::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, name
))
116 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
118 SetTabView(new wxNotebookTabView(this));
124 wxNotebook::~wxNotebook()
129 // ----------------------------------------------------------------------------
130 // wxNotebook accessors
131 // ----------------------------------------------------------------------------
132 int wxNotebook::GetPageCount() const
134 return m_aPages
.Count();
137 int wxNotebook::GetRowCount() const
143 int wxNotebook::SetSelection(int nPage
)
148 wxASSERT( IS_VALID_PAGE(nPage
) );
150 #if defined (__WIN16__)
151 m_tabView
->SetTabSelection(nPage
);
153 wxNotebookPage
* pPage
= GetPage(nPage
);
155 m_tabView
->SetTabSelection((int) (long) pPage
);
161 void wxNotebook::AdvanceSelection(bool bForward
)
163 int nSel
= GetSelection();
164 int nMax
= GetPageCount() - 1;
166 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
168 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
171 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
173 wxASSERT( IS_VALID_PAGE(nPage
) );
174 #if defined (__WIN16__)
175 m_tabView
->SetTabText(nPage
, strText
);
179 wxNotebookPage
* page
= GetPage(nPage
);
182 m_tabView
->SetTabText((int) (long) page
, strText
);
190 wxString
wxNotebook::GetPageText(int nPage
) const
192 wxASSERT( IS_VALID_PAGE(nPage
) );
194 #if defined (__WIN16__)
195 return m_tabView
->GetTabText(nPage
);
197 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
199 return m_tabView
->GetTabText((int) (long) page
);
201 return wxEmptyString
;
205 int wxNotebook::GetPageImage(int nPage
) const
207 wxASSERT( IS_VALID_PAGE(nPage
) );
213 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
215 wxASSERT( IS_VALID_PAGE(nPage
) );
221 void wxNotebook::SetImageList(wxImageList
* imageList
)
223 m_pImageList
= imageList
;
227 // ----------------------------------------------------------------------------
228 // wxNotebook operations
229 // ----------------------------------------------------------------------------
231 // remove one page from the notebook and delete it
232 bool wxNotebook::DeletePage(int nPage
)
234 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
236 if (m_nSelection
!= -1)
238 m_aPages
[m_nSelection
]->Show(FALSE
);
239 m_aPages
[m_nSelection
]->Lower();
242 wxNotebookPage
* pPage
= GetPage(nPage
);
243 #if defined (__WIN16__)
244 m_tabView
->RemoveTab(nPage
);
246 m_tabView
->RemoveTab((int) (long) pPage
);
249 delete m_aPages
[nPage
];
250 m_aPages
.Remove(nPage
);
252 if (m_aPages
.GetCount() == 0)
255 m_tabView
->SetTabSelection(-1, FALSE
);
257 else if (m_nSelection
> -1)
260 #if defined (__WIN16__)
261 m_tabView
->SetTabSelection(0, FALSE
);
263 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
265 if (m_nSelection
!= 0)
269 RefreshLayout(FALSE
);
274 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
276 int pagePos
= FindPagePosition(page
);
278 return DeletePage(pagePos
);
283 // remove one page from the notebook
284 bool wxNotebook::RemovePage(int nPage
)
286 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
288 m_aPages
[nPage
]->Show(FALSE
);
289 // m_aPages[nPage]->Lower();
291 wxNotebookPage
* pPage
= GetPage(nPage
);
292 #if defined (__WIN16__)
293 m_tabView
->RemoveTab(nPage
);
295 m_tabView
->RemoveTab((int) (long) pPage
);
298 m_aPages
.Remove(nPage
);
300 if (m_aPages
.GetCount() == 0)
303 m_tabView
->SetTabSelection(-1, TRUE
);
305 else if (m_nSelection
> -1)
307 // Only change the selection if the page we
308 // deleted was the selection.
309 if (nPage
== m_nSelection
)
312 // Select the first tab. Generates a ChangePage.
313 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
317 // We must adjust which tab we think is selected.
318 // If greater than the page we deleted, it must be moved down
320 if (m_nSelection
> nPage
)
325 RefreshLayout(FALSE
);
330 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
332 int pagePos
= FindPagePosition(page
);
334 return RemovePage(pagePos
);
339 // Find the position of the wxNotebookPage, -1 if not found.
340 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
342 int nPageCount
= GetPageCount();
344 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
345 if (m_aPages
[nPage
] == page
)
351 bool wxNotebook::DeleteAllPages()
353 m_tabView
->ClearTabs(TRUE
);
355 int nPageCount
= GetPageCount();
357 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
358 delete m_aPages
[nPage
];
365 // add a page to the notebook
366 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
367 const wxString
& strText
,
371 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
374 // same as AddPage() but does it at given position
375 bool wxNotebook::InsertPage(int nPage
,
376 wxNotebookPage
*pPage
,
377 const wxString
& strText
,
381 wxASSERT( pPage
!= NULL
);
382 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
384 // For 16 bit integers (tabs limited to 32768)
385 #if defined (__WIN16__)
386 m_tabView
->AddTab(nPage
, strText
);
388 m_tabView
->AddTab((int) (long) pPage
, strText
);
393 // save the pointer to the page
394 m_aPages
.Insert(pPage
, nPage
);
398 // This will cause ChangePage to be called, via OnSelPage
399 #if defined (__WIN16__)
400 m_tabView
->SetTabSelection(nPage
, TRUE
);
402 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
406 // some page must be selected: either this one or the first one if there is
407 // still no selection
408 if ( m_nSelection
== -1 )
411 RefreshLayout(FALSE
);
416 // ----------------------------------------------------------------------------
417 // wxNotebook callbacks
418 // ----------------------------------------------------------------------------
420 // @@@ OnSize() is used for setting the font when it's called for the first
421 // time because doing it in ::Create() doesn't work (for unknown reasons)
422 void wxNotebook::OnSize(wxSizeEvent
& event
)
424 static bool s_bFirstTime
= TRUE
;
425 if ( s_bFirstTime
) {
426 // TODO: any first-time-size processing.
427 s_bFirstTime
= FALSE
;
432 // Processing continues to next OnSize
436 // This was supposed to cure the non-display of the notebook
437 // until the user resizes the window.
439 void wxNotebook::OnIdle(wxIdleEvent
& event
)
441 static bool s_bFirstTime
= TRUE
;
442 if ( s_bFirstTime
) {
444 wxSize sz(GetSize());
452 wxSize sz(GetSize());
453 wxSizeEvent sizeEvent(sz, GetId());
454 sizeEvent.SetEventObject(this);
455 GetEventHandler()->ProcessEvent(sizeEvent);
458 s_bFirstTime
= FALSE
;
463 // Implementation: calculate the layout of the view rect
464 // and resize the children if required
465 bool wxNotebook::RefreshLayout(bool force
)
469 wxRect oldRect
= m_tabView
->GetViewRect();
472 GetClientSize(& cw
, & ch
);
474 int tabHeight
= m_tabView
->GetTotalTabHeight();
477 rect
.y
= tabHeight
+ 4;
479 rect
.height
= ch
- 4 - rect
.y
;
481 m_tabView
->SetViewRect(rect
);
483 m_tabView
->LayoutTabs();
485 // Need to do it a 2nd time to get the tab height with
486 // the new view width, since changing the view width changes the
488 tabHeight
= m_tabView
->GetTotalTabHeight();
490 rect
.y
= tabHeight
+ 4;
492 rect
.height
= ch
- 4 - rect
.y
;
494 m_tabView
->SetViewRect(rect
);
496 m_tabView
->LayoutTabs();
498 if (!force
&& (rect
== oldRect
))
501 // fit the notebook page to the tab control's display area
503 unsigned int nCount
= m_aPages
.Count();
504 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
505 wxNotebookPage
*pPage
= m_aPages
[nPage
];
506 if (pPage
->IsShown())
508 wxRect clientRect
= GetAvailableClientSize();
509 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
510 if ( pPage
->GetAutoLayout() )
519 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
521 // is it our tab control?
522 if ( event
.GetEventObject() == this )
524 if (event
.GetSelection() != m_nSelection
)
525 ChangePage(event
.GetOldSelection(), event
.GetSelection());
528 // we want to give others a chance to process this message as well
532 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
534 // set focus to the currently selected page if any
535 if ( m_nSelection
!= -1 )
536 m_aPages
[m_nSelection
]->SetFocus();
541 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
543 if ( event
.IsWindowChange() ) {
545 AdvanceSelection(event
.GetDirection());
548 // pass to the parent
550 event
.SetCurrentFocus(this);
551 GetParent()->ProcessEvent(event
);
556 // ----------------------------------------------------------------------------
557 // wxNotebook base class virtuals
558 // ----------------------------------------------------------------------------
560 // override these 2 functions to do nothing: everything is done in OnSize
562 void wxNotebook::SetConstraintSizes(bool /* recurse */)
564 // don't set the sizes of the pages - their correct size is not yet known
565 wxControl::SetConstraintSizes(FALSE
);
568 bool wxNotebook::DoPhase(int /* nPhase */)
573 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
575 wxFAIL_MSG("wxNotebook::Command not implemented");
578 // ----------------------------------------------------------------------------
579 // wxNotebook helper functions
580 // ----------------------------------------------------------------------------
582 // hide the currently active panel and show the new one
583 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
585 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
586 wxASSERT( nOldSel
!= nSel
); // impossible
588 if ( nOldSel
!= -1 ) {
589 m_aPages
[nOldSel
]->Show(FALSE
);
590 m_aPages
[nOldSel
]->Lower();
593 wxNotebookPage
*pPage
= m_aPages
[nSel
];
595 wxRect clientRect
= GetAvailableClientSize();
596 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
607 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
610 m_tabView
->OnEvent(event
);
613 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
620 wxRect
wxNotebook::GetAvailableClientSize()
623 GetClientSize(& cw
, & ch
);
625 int tabHeight
= m_tabView
->GetTotalTabHeight();
627 // TODO: these margins should be configurable.
630 rect
.y
= tabHeight
+ 6;
631 rect
.width
= cw
- 12;
632 rect
.height
= ch
- 4 - rect
.y
;
641 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
643 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
645 m_notebook
= notebook
;
647 m_notebook
->SetTabView(this);
649 SetWindow(m_notebook
);
652 wxNotebookTabView::~wxNotebookTabView(void)
656 // Called when a tab is activated
657 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
662 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
664 #if defined (__WIN16__)
665 int activatePos
= activateId
;
666 int deactivatePos
= deactivateId
;
668 // Translate from wxTabView's ids (which aren't position-dependent)
669 // to wxNotebook's (which are).
670 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
671 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
673 int activatePos
= m_notebook
->FindPagePosition(pActive
);
674 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
677 event
.SetEventObject(m_notebook
);
678 event
.SetSelection(activatePos
);
679 event
.SetOldSelection(deactivatePos
);
680 m_notebook
->GetEventHandler()->ProcessEvent(event
);