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
)
51 EVT_IDLE(wxNotebook::OnIdle
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxNotebook construction
64 // ----------------------------------------------------------------------------
66 // common part of all ctors
67 void wxNotebook::Init()
69 m_tabView
= (wxNotebookTabView
*) NULL
;
74 // default for dynamic class
75 wxNotebook::wxNotebook()
80 // the same arguments as for wxControl
81 wxNotebook::wxNotebook(wxWindow
*parent
,
90 Create(parent
, id
, pos
, size
, style
, name
);
94 bool wxNotebook::Create(wxWindow
*parent
,
104 m_windowId
= id
== -1 ? NewControlId() : id
;
106 // It's like a normal window...
107 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
110 SetTabView(new wxNotebookTabView(this));
116 wxNotebook::~wxNotebook()
121 // ----------------------------------------------------------------------------
122 // wxNotebook accessors
123 // ----------------------------------------------------------------------------
124 int wxNotebook::GetPageCount() const
126 return m_aPages
.Count();
129 int wxNotebook::GetRowCount() const
135 int wxNotebook::SetSelection(int nPage
)
140 wxASSERT( IS_VALID_PAGE(nPage
) );
142 wxNotebookPage
* pPage
= GetPage(nPage
);
144 m_tabView
->SetTabSelection((int) (long) pPage
);
150 void wxNotebook::AdvanceSelection(bool bForward
)
152 int nSel
= GetSelection();
153 int nMax
= GetPageCount() - 1;
155 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
157 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
160 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
162 wxASSERT( IS_VALID_PAGE(nPage
) );
164 wxNotebookPage
* page
= GetPage(nPage
);
167 m_tabView
->SetTabText((int) (long) page
, strText
);
175 wxString
wxNotebook::GetPageText(int nPage
) const
177 wxASSERT( IS_VALID_PAGE(nPage
) );
179 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
181 return m_tabView
->GetTabText((int) (long) page
);
183 return wxEmptyString
;
186 int wxNotebook::GetPageImage(int nPage
) const
188 wxASSERT( IS_VALID_PAGE(nPage
) );
194 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
196 wxASSERT( IS_VALID_PAGE(nPage
) );
202 void wxNotebook::SetImageList(wxImageList
* imageList
)
204 m_pImageList
= imageList
;
208 // ----------------------------------------------------------------------------
209 // wxNotebook operations
210 // ----------------------------------------------------------------------------
212 // remove one page from the notebook and delete it
213 bool wxNotebook::DeletePage(int nPage
)
215 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
217 if (m_nSelection
!= -1)
219 m_aPages
[m_nSelection
]->Show(FALSE
);
220 m_aPages
[m_nSelection
]->Lower();
223 wxNotebookPage
* pPage
= GetPage(nPage
);
224 m_tabView
->RemoveTab((int) (long) pPage
);
226 delete m_aPages
[nPage
];
227 m_aPages
.Remove(nPage
);
229 if (m_aPages
.GetCount() == 0)
232 m_tabView
->SetTabSelection(-1, FALSE
);
234 else if (m_nSelection
> -1)
237 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
238 if (m_nSelection
!= 0)
242 RefreshLayout(FALSE
);
247 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
249 int pagePos
= FindPagePosition(page
);
251 return DeletePage(pagePos
);
256 // remove one page from the notebook
257 bool wxNotebook::RemovePage(int nPage
)
259 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
261 m_aPages
[nPage
]->Show(FALSE
);
262 // m_aPages[nPage]->Lower();
264 wxNotebookPage
* pPage
= GetPage(nPage
);
265 m_tabView
->RemoveTab((int) (long) pPage
);
267 m_aPages
.Remove(nPage
);
269 if (m_aPages
.GetCount() == 0)
272 m_tabView
->SetTabSelection(-1, TRUE
);
274 else if (m_nSelection
> -1)
276 // Only change the selection if the page we
277 // deleted was the selection.
278 if (nPage
== m_nSelection
)
281 // Select the first tab. Generates a ChangePage.
282 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
286 // We must adjust which tab we think is selected.
287 // If greater than the page we deleted, it must be moved down
289 if (m_nSelection
> nPage
)
294 RefreshLayout(FALSE
);
299 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
301 int pagePos
= FindPagePosition(page
);
303 return RemovePage(pagePos
);
308 // Find the position of the wxNotebookPage, -1 if not found.
309 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
311 int nPageCount
= GetPageCount();
313 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
314 if (m_aPages
[nPage
] == page
)
320 bool wxNotebook::DeleteAllPages()
322 m_tabView
->ClearTabs(TRUE
);
324 int nPageCount
= GetPageCount();
326 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
327 delete m_aPages
[nPage
];
334 // add a page to the notebook
335 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
336 const wxString
& strText
,
340 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
343 // same as AddPage() but does it at given position
344 bool wxNotebook::InsertPage(int nPage
,
345 wxNotebookPage
*pPage
,
346 const wxString
& strText
,
350 wxASSERT( pPage
!= NULL
);
351 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
353 m_tabView
->AddTab((int) (long) pPage
, strText
);
357 // save the pointer to the page
358 m_aPages
.Insert(pPage
, nPage
);
362 // This will cause ChangePage to be called, via OnSelPage
363 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
366 // some page must be selected: either this one or the first one if there is
367 // still no selection
368 if ( m_nSelection
== -1 )
371 RefreshLayout(FALSE
);
376 // ----------------------------------------------------------------------------
377 // wxNotebook callbacks
378 // ----------------------------------------------------------------------------
380 // @@@ OnSize() is used for setting the font when it's called for the first
381 // time because doing it in ::Create() doesn't work (for unknown reasons)
382 void wxNotebook::OnSize(wxSizeEvent
& event
)
384 static bool s_bFirstTime
= TRUE
;
385 if ( s_bFirstTime
) {
386 // TODO: any first-time-size processing.
387 s_bFirstTime
= FALSE
;
392 // Processing continues to next OnSize
396 // This was supposed to cure the non-display of the notebook
397 // until the user resizes the window.
399 void wxNotebook::OnIdle(wxIdleEvent
& event
)
401 static bool s_bFirstTime
= TRUE
;
402 if ( s_bFirstTime
) {
404 wxSize sz(GetSize());
412 wxSize sz(GetSize());
413 wxSizeEvent sizeEvent(sz, GetId());
414 sizeEvent.SetEventObject(this);
415 GetEventHandler()->ProcessEvent(sizeEvent);
418 s_bFirstTime
= FALSE
;
423 // Implementation: calculate the layout of the view rect
424 // and resize the children if required
425 bool wxNotebook::RefreshLayout(bool force
)
429 wxRect oldRect
= m_tabView
->GetViewRect();
432 GetClientSize(& cw
, & ch
);
434 int tabHeight
= m_tabView
->GetTotalTabHeight();
437 rect
.y
= tabHeight
+ 4;
439 rect
.height
= ch
- 4 - rect
.y
;
441 m_tabView
->SetViewRect(rect
);
445 // Need to do it a 2nd time to get the tab height with
446 // the new view width, since changing the view width changes the
448 tabHeight
= m_tabView
->GetTotalTabHeight();
450 rect
.y
= tabHeight
+ 4;
452 rect
.height
= ch
- 4 - rect
.y
;
454 m_tabView
->SetViewRect(rect
);
458 if (!force
&& (rect
== oldRect
))
461 // fit the notebook page to the tab control's display area
463 unsigned int nCount
= m_aPages
.Count();
464 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
465 wxNotebookPage
*pPage
= m_aPages
[nPage
];
466 if (pPage
->IsShown())
468 wxRect clientRect
= GetAvailableClientSize();
469 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
470 if ( pPage
->GetAutoLayout() )
479 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
481 // is it our tab control?
482 if ( event
.GetEventObject() == this )
484 if (event
.GetSelection() != m_nSelection
)
485 ChangePage(event
.GetOldSelection(), event
.GetSelection());
488 // we want to give others a chance to process this message as well
492 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
494 // set focus to the currently selected page if any
495 if ( m_nSelection
!= -1 )
496 m_aPages
[m_nSelection
]->SetFocus();
501 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
503 if ( event
.IsWindowChange() ) {
505 AdvanceSelection(event
.GetDirection());
508 // pass to the parent
510 event
.SetCurrentFocus(this);
511 GetParent()->ProcessEvent(event
);
516 // ----------------------------------------------------------------------------
517 // wxNotebook base class virtuals
518 // ----------------------------------------------------------------------------
520 // override these 2 functions to do nothing: everything is done in OnSize
522 void wxNotebook::SetConstraintSizes(bool /* recurse */)
524 // don't set the sizes of the pages - their correct size is not yet known
525 wxControl::SetConstraintSizes(FALSE
);
528 bool wxNotebook::DoPhase(int /* nPhase */)
533 void wxNotebook::Command(wxCommandEvent
& event
)
535 wxFAIL_MSG("wxNotebook::Command not implemented");
538 // ----------------------------------------------------------------------------
539 // wxNotebook helper functions
540 // ----------------------------------------------------------------------------
542 // hide the currently active panel and show the new one
543 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
545 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
546 wxASSERT( nOldSel
!= nSel
); // impossible
548 if ( nOldSel
!= -1 ) {
549 m_aPages
[nOldSel
]->Show(FALSE
);
550 m_aPages
[nOldSel
]->Lower();
553 wxNotebookPage
*pPage
= m_aPages
[nSel
];
555 wxRect clientRect
= GetAvailableClientSize();
556 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
567 void wxNotebook::ChangeFont(bool keepOriginalSize
)
569 wxWindow::ChangeFont(keepOriginalSize
);
572 void wxNotebook::ChangeBackgroundColour()
574 wxWindow::ChangeBackgroundColour();
577 void wxNotebook::ChangeForegroundColour()
579 wxWindow::ChangeForegroundColour();
582 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
585 m_tabView
->OnEvent(event
);
588 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
595 wxRect
wxNotebook::GetAvailableClientSize()
598 GetClientSize(& cw
, & ch
);
600 int tabHeight
= m_tabView
->GetTotalTabHeight();
602 // TODO: these margins should be configurable.
605 rect
.y
= tabHeight
+ 6;
606 rect
.width
= cw
- 12;
607 rect
.height
= ch
- 4 - rect
.y
;
616 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
618 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
620 m_notebook
= notebook
;
622 m_notebook
->SetTabView(this);
624 SetWindow(m_notebook
);
627 wxNotebookTabView::~wxNotebookTabView(void)
631 // Called when a tab is activated
632 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
637 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
639 // Translate from wxTabView's ids (which aren't position-dependent)
640 // to wxNotebook's (which are).
641 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
642 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
644 int activatePos
= m_notebook
->FindPagePosition(pActive
);
645 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
647 event
.SetEventObject(m_notebook
);
648 event
.SetSelection(activatePos
);
649 event
.SetOldSelection(deactivatePos
);
650 m_notebook
->GetEventHandler()->ProcessEvent(event
);