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 #include <wx/string.h>
25 #include <wx/imaglist.h>
26 #include <wx/notebook.h>
27 #include <wx/dcclient.h>
30 #include <wx/motif/private.h>
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // check that the page index is valid
37 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if !USE_SHARED_LIBRARIES
44 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
45 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
46 EVT_SIZE(wxNotebook::OnSize
)
47 EVT_PAINT(wxNotebook::OnPaint
)
48 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxNotebook construction
63 // ----------------------------------------------------------------------------
65 // common part of all ctors
66 void wxNotebook::Init()
68 m_tabView
= (wxNotebookTabView
*) NULL
;
73 // default for dynamic class
74 wxNotebook::wxNotebook()
79 // the same arguments as for wxControl
80 wxNotebook::wxNotebook(wxWindow
*parent
,
89 Create(parent
, id
, pos
, size
, style
, name
);
93 bool wxNotebook::Create(wxWindow
*parent
,
103 m_windowId
= id
== -1 ? NewControlId() : id
;
105 // It's like a normal window...
106 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
109 SetTabView(new wxNotebookTabView(this));
115 wxNotebook::~wxNotebook()
120 // ----------------------------------------------------------------------------
121 // wxNotebook accessors
122 // ----------------------------------------------------------------------------
123 int wxNotebook::GetPageCount() const
125 return m_aPages
.Count();
128 int wxNotebook::GetRowCount() const
134 int wxNotebook::SetSelection(int nPage
)
139 wxASSERT( IS_VALID_PAGE(nPage
) );
141 wxNotebookPage
* pPage
= GetPage(nPage
);
143 m_tabView
->SetTabSelection((int) (long) pPage
);
149 void wxNotebook::AdvanceSelection(bool bForward
)
151 int nSel
= GetSelection();
152 int nMax
= GetPageCount() - 1;
154 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
156 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
159 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
161 wxASSERT( IS_VALID_PAGE(nPage
) );
163 wxNotebookPage
* page
= GetPage(nPage
);
166 m_tabView
->SetTabText((int) (long) page
, strText
);
174 wxString
wxNotebook::GetPageText(int nPage
) const
176 wxASSERT( IS_VALID_PAGE(nPage
) );
178 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
180 return m_tabView
->GetTabText((int) (long) page
);
182 return wxEmptyString
;
185 int wxNotebook::GetPageImage(int nPage
) const
187 wxASSERT( IS_VALID_PAGE(nPage
) );
193 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
195 wxASSERT( IS_VALID_PAGE(nPage
) );
201 void wxNotebook::SetImageList(wxImageList
* imageList
)
203 m_pImageList
= imageList
;
207 // ----------------------------------------------------------------------------
208 // wxNotebook operations
209 // ----------------------------------------------------------------------------
211 // remove one page from the notebook and delete it
212 bool wxNotebook::DeletePage(int nPage
)
214 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
216 if (m_nSelection
!= -1)
218 m_aPages
[m_nSelection
]->Show(FALSE
);
219 m_aPages
[m_nSelection
]->Lower();
222 wxNotebookPage
* pPage
= GetPage(nPage
);
223 m_tabView
->RemoveTab((int) (long) pPage
);
225 delete m_aPages
[nPage
];
226 m_aPages
.Remove(nPage
);
228 if (m_aPages
.GetCount() == 0)
231 m_tabView
->SetTabSelection(-1, FALSE
);
233 else if (m_nSelection
> -1)
236 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
237 if (m_nSelection
!= 0)
241 RefreshLayout(FALSE
);
246 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
248 int pagePos
= FindPagePosition(page
);
250 return DeletePage(pagePos
);
255 // remove one page from the notebook
256 bool wxNotebook::RemovePage(int nPage
)
258 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
260 m_aPages
[nPage
]->Show(FALSE
);
261 // m_aPages[nPage]->Lower();
263 wxNotebookPage
* pPage
= GetPage(nPage
);
264 m_tabView
->RemoveTab((int) (long) pPage
);
266 m_aPages
.Remove(nPage
);
268 if (m_aPages
.GetCount() == 0)
271 m_tabView
->SetTabSelection(-1, TRUE
);
273 else if (m_nSelection
> -1)
275 // Only change the selection if the page we
276 // deleted was the selection.
277 if (nPage
== m_nSelection
)
280 // Select the first tab. Generates a ChangePage.
281 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
285 // We must adjust which tab we think is selected.
286 // If greater than the page we deleted, it must be moved down
288 if (m_nSelection
> nPage
)
293 RefreshLayout(FALSE
);
298 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
300 int pagePos
= FindPagePosition(page
);
302 return RemovePage(pagePos
);
307 // Find the position of the wxNotebookPage, -1 if not found.
308 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
310 int nPageCount
= GetPageCount();
312 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
313 if (m_aPages
[nPage
] == page
)
319 bool wxNotebook::DeleteAllPages()
321 m_tabView
->ClearTabs(TRUE
);
323 int nPageCount
= GetPageCount();
325 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
326 delete m_aPages
[nPage
];
333 // add a page to the notebook
334 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
335 const wxString
& strText
,
339 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
342 // same as AddPage() but does it at given position
343 bool wxNotebook::InsertPage(int nPage
,
344 wxNotebookPage
*pPage
,
345 const wxString
& strText
,
349 wxASSERT( pPage
!= NULL
);
350 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
352 m_tabView
->AddTab((int) (long) pPage
, strText
);
356 // save the pointer to the page
357 m_aPages
.Insert(pPage
, nPage
);
361 // This will cause ChangePage to be called, via OnSelPage
362 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
365 // some page must be selected: either this one or the first one if there is
366 // still no selection
367 if ( m_nSelection
== -1 )
370 RefreshLayout(FALSE
);
375 // ----------------------------------------------------------------------------
376 // wxNotebook callbacks
377 // ----------------------------------------------------------------------------
379 // @@@ OnSize() is used for setting the font when it's called for the first
380 // time because doing it in ::Create() doesn't work (for unknown reasons)
381 void wxNotebook::OnSize(wxSizeEvent
& event
)
383 static bool s_bFirstTime
= TRUE
;
384 if ( s_bFirstTime
) {
385 // TODO: any first-time-size processing.
386 s_bFirstTime
= FALSE
;
391 // Processing continues to next OnSize
395 // Implementation: calculate the layout of the view rect
396 // and resize the children if required
397 bool wxNotebook::RefreshLayout(bool force
)
401 wxRect oldRect
= m_tabView
->GetViewRect();
404 GetClientSize(& cw
, & ch
);
406 int tabHeight
= m_tabView
->GetTotalTabHeight();
409 rect
.y
= tabHeight
+ 4;
411 rect
.height
= ch
- 4 - rect
.y
;
413 m_tabView
->SetViewRect(rect
);
417 // Need to do it a 2nd time to get the tab height with
418 // the new view width, since changing the view width changes the
420 tabHeight
= m_tabView
->GetTotalTabHeight();
422 rect
.y
= tabHeight
+ 4;
424 rect
.height
= ch
- 4 - rect
.y
;
426 m_tabView
->SetViewRect(rect
);
430 if (!force
&& (rect
== oldRect
))
433 // fit the notebook page to the tab control's display area
435 unsigned int nCount
= m_aPages
.Count();
436 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
437 wxNotebookPage
*pPage
= m_aPages
[nPage
];
438 if (pPage
->IsShown())
440 wxRect clientRect
= GetAvailableClientSize();
441 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
442 if ( pPage
->GetAutoLayout() )
451 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
453 // is it our tab control?
454 if ( event
.GetEventObject() == this )
456 if (event
.GetSelection() != m_nSelection
)
457 ChangePage(event
.GetOldSelection(), event
.GetSelection());
460 // we want to give others a chance to process this message as well
464 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
466 // set focus to the currently selected page if any
467 if ( m_nSelection
!= -1 )
468 m_aPages
[m_nSelection
]->SetFocus();
473 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
475 if ( event
.IsWindowChange() ) {
477 AdvanceSelection(event
.GetDirection());
480 // pass to the parent
482 event
.SetCurrentFocus(this);
483 GetParent()->ProcessEvent(event
);
488 // ----------------------------------------------------------------------------
489 // wxNotebook base class virtuals
490 // ----------------------------------------------------------------------------
492 // override these 2 functions to do nothing: everything is done in OnSize
494 void wxNotebook::SetConstraintSizes(bool /* recurse */)
496 // don't set the sizes of the pages - their correct size is not yet known
497 wxControl::SetConstraintSizes(FALSE
);
500 bool wxNotebook::DoPhase(int /* nPhase */)
505 void wxNotebook::Command(wxCommandEvent
& event
)
507 wxFAIL_MSG("wxNotebook::Command not implemented");
510 // ----------------------------------------------------------------------------
511 // wxNotebook helper functions
512 // ----------------------------------------------------------------------------
514 // hide the currently active panel and show the new one
515 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
517 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
518 wxASSERT( nOldSel
!= nSel
); // impossible
520 if ( nOldSel
!= -1 ) {
521 m_aPages
[nOldSel
]->Show(FALSE
);
522 m_aPages
[nOldSel
]->Lower();
525 wxNotebookPage
*pPage
= m_aPages
[nSel
];
527 wxRect clientRect
= GetAvailableClientSize();
528 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
539 void wxNotebook::ChangeFont(bool keepOriginalSize
)
541 wxWindow::ChangeFont(keepOriginalSize
);
544 void wxNotebook::ChangeBackgroundColour()
546 wxWindow::ChangeBackgroundColour();
549 void wxNotebook::ChangeForegroundColour()
551 wxWindow::ChangeForegroundColour();
554 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
557 m_tabView
->OnEvent(event
);
560 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
567 wxRect
wxNotebook::GetAvailableClientSize()
570 GetClientSize(& cw
, & ch
);
572 int tabHeight
= m_tabView
->GetTotalTabHeight();
574 // TODO: these margins should be configurable.
577 rect
.y
= tabHeight
+ 6;
578 rect
.width
= cw
- 12;
579 rect
.height
= ch
- 4 - rect
.y
;
588 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
590 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
592 m_notebook
= notebook
;
594 m_notebook
->SetTabView(this);
596 SetWindow(m_notebook
);
599 wxNotebookTabView::~wxNotebookTabView(void)
603 // Called when a tab is activated
604 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
609 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
611 // Translate from wxTabView's ids (which aren't position-dependent)
612 // to wxNotebook's (which are).
613 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
614 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
616 int activatePos
= m_notebook
->FindPagePosition(pActive
);
617 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
619 event
.SetEventObject(m_notebook
);
620 event
.SetSelection(activatePos
);
621 event
.SetOldSelection(deactivatePos
);
622 m_notebook
->GetEventHandler()->ProcessEvent(event
);