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
;
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::GetRowCount() const
137 int wxNotebook::SetSelection(int nPage
)
142 wxASSERT( IS_VALID_PAGE(nPage
) );
144 #if defined (__WIN16__)
145 m_tabView
->SetTabSelection(nPage
);
147 wxNotebookPage
* pPage
= GetPage(nPage
);
149 m_tabView
->SetTabSelection((int) (long) pPage
);
156 void wxNotebook::AdvanceSelection(bool bForward
)
158 int nSel
= GetSelection();
159 int nMax
= GetPageCount() - 1;
161 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
163 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
167 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
169 wxASSERT( IS_VALID_PAGE(nPage
) );
170 #if defined (__WIN16__)
171 m_tabView
->SetTabText(nPage
, strText
);
175 wxNotebookPage
* page
= GetPage(nPage
);
178 m_tabView
->SetTabText((int) (long) page
, strText
);
186 wxString
wxNotebook::GetPageText(int nPage
) const
188 wxASSERT( IS_VALID_PAGE(nPage
) );
190 #if defined (__WIN16__)
191 return m_tabView
->GetTabText(nPage
);
193 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
195 return m_tabView
->GetTabText((int) (long) page
);
197 return wxEmptyString
;
201 int wxNotebook::GetPageImage(int nPage
) const
203 wxASSERT( IS_VALID_PAGE(nPage
) );
209 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
211 wxASSERT( IS_VALID_PAGE(nPage
) );
217 // ----------------------------------------------------------------------------
218 // wxNotebook operations
219 // ----------------------------------------------------------------------------
221 // remove one page from the notebook and delete it
222 bool wxNotebook::DeletePage(int nPage
)
224 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
226 if (m_nSelection
!= -1)
228 m_pages
[m_nSelection
]->Show(FALSE
);
229 m_pages
[m_nSelection
]->Lower();
232 wxNotebookPage
* pPage
= GetPage(nPage
);
233 #if defined (__WIN16__)
234 m_tabView
->RemoveTab(nPage
);
236 m_tabView
->RemoveTab((int) (long) pPage
);
239 m_pages
.Remove(pPage
);
242 if (m_pages
.GetCount() == 0)
245 m_tabView
->SetTabSelection(-1, FALSE
);
247 else if (m_nSelection
> -1)
250 #if defined (__WIN16__)
251 m_tabView
->SetTabSelection(0, FALSE
);
253 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
255 if (m_nSelection
!= 0)
259 RefreshLayout(FALSE
);
264 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
266 int pagePos
= FindPagePosition(page
);
268 return DeletePage(pagePos
);
273 // remove one page from the notebook
274 bool wxNotebook::RemovePage(int nPage
)
276 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
278 m_pages
[nPage
]->Show(FALSE
);
279 // m_pages[nPage]->Lower();
281 wxNotebookPage
* pPage
= GetPage(nPage
);
282 #if defined (__WIN16__)
283 m_tabView
->RemoveTab(nPage
);
285 m_tabView
->RemoveTab((int) (long) pPage
);
288 m_pages
.Remove(pPage
);
290 if (m_pages
.GetCount() == 0)
293 m_tabView
->SetTabSelection(-1, TRUE
);
295 else if (m_nSelection
> -1)
297 // Only change the selection if the page we
298 // deleted was the selection.
299 if (nPage
== m_nSelection
)
302 // Select the first tab. Generates a ChangePage.
303 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
307 // We must adjust which tab we think is selected.
308 // If greater than the page we deleted, it must be moved down
310 if (m_nSelection
> nPage
)
315 RefreshLayout(FALSE
);
320 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
322 int pagePos
= FindPagePosition(page
);
324 return RemovePage(pagePos
);
329 // Find the position of the wxNotebookPage, -1 if not found.
330 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
332 int nPageCount
= GetPageCount();
334 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
335 if (m_pages
[nPage
] == page
)
341 bool wxNotebook::DeleteAllPages()
343 m_tabView
->ClearTabs(TRUE
);
345 int nPageCount
= GetPageCount();
347 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
348 delete m_pages
[nPage
];
355 // same as AddPage() but does it at given position
356 bool wxNotebook::InsertPage(int nPage
,
357 wxNotebookPage
*pPage
,
358 const wxString
& strText
,
362 wxASSERT( pPage
!= NULL
);
363 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
365 // For 16 bit integers (tabs limited to 32768)
366 #if defined (__WIN16__)
367 m_tabView
->AddTab(nPage
, strText
);
369 m_tabView
->AddTab((int) (long) pPage
, strText
);
374 // save the pointer to the page
375 m_pages
.Insert(pPage
, nPage
);
379 // This will cause ChangePage to be called, via OnSelPage
380 #if defined (__WIN16__)
381 m_tabView
->SetTabSelection(nPage
, TRUE
);
383 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
387 // some page must be selected: either this one or the first one if there is
388 // still no selection
389 if ( m_nSelection
== -1 )
392 RefreshLayout(FALSE
);
397 // ----------------------------------------------------------------------------
398 // wxNotebook callbacks
399 // ----------------------------------------------------------------------------
401 // @@@ OnSize() is used for setting the font when it's called for the first
402 // time because doing it in ::Create() doesn't work (for unknown reasons)
403 void wxNotebook::OnSize(wxSizeEvent
& event
)
405 static bool s_bFirstTime
= TRUE
;
406 if ( s_bFirstTime
) {
407 // TODO: any first-time-size processing.
408 s_bFirstTime
= FALSE
;
413 // Processing continues to next OnSize
417 // This was supposed to cure the non-display of the notebook
418 // until the user resizes the window.
420 void wxNotebook::OnIdle(wxIdleEvent
& event
)
422 static bool s_bFirstTime
= TRUE
;
423 if ( s_bFirstTime
) {
425 wxSize sz(GetSize());
433 wxSize sz(GetSize());
434 wxSizeEvent sizeEvent(sz, GetId());
435 sizeEvent.SetEventObject(this);
436 GetEventHandler()->ProcessEvent(sizeEvent);
439 s_bFirstTime
= FALSE
;
444 // Implementation: calculate the layout of the view rect
445 // and resize the children if required
446 bool wxNotebook::RefreshLayout(bool force
)
450 wxRect oldRect
= m_tabView
->GetViewRect();
453 GetClientSize(& cw
, & ch
);
455 int tabHeight
= m_tabView
->GetTotalTabHeight();
458 rect
.y
= tabHeight
+ 4;
460 rect
.height
= ch
- 4 - rect
.y
;
462 m_tabView
->SetViewRect(rect
);
464 m_tabView
->LayoutTabs();
466 // Need to do it a 2nd time to get the tab height with
467 // the new view width, since changing the view width changes the
469 tabHeight
= m_tabView
->GetTotalTabHeight();
471 rect
.y
= tabHeight
+ 4;
473 rect
.height
= ch
- 4 - rect
.y
;
475 m_tabView
->SetViewRect(rect
);
477 m_tabView
->LayoutTabs();
479 if (!force
&& (rect
== oldRect
))
482 // fit the notebook page to the tab control's display area
484 unsigned int nCount
= m_pages
.Count();
485 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
486 wxNotebookPage
*pPage
= m_pages
[nPage
];
487 if (pPage
->IsShown())
489 wxRect clientRect
= GetAvailableClientSize();
490 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
491 if ( pPage
->GetAutoLayout() )
500 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
502 // is it our tab control?
503 if ( event
.GetEventObject() == this )
505 if (event
.GetSelection() != m_nSelection
)
506 ChangePage(event
.GetOldSelection(), event
.GetSelection());
509 // we want to give others a chance to process this message as well
513 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
515 // set focus to the currently selected page if any
516 if ( m_nSelection
!= -1 )
517 m_pages
[m_nSelection
]->SetFocus();
522 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
524 if ( event
.IsWindowChange() ) {
526 AdvanceSelection(event
.GetDirection());
529 // pass to the parent
531 event
.SetCurrentFocus(this);
532 GetParent()->ProcessEvent(event
);
537 // ----------------------------------------------------------------------------
538 // wxNotebook base class virtuals
539 // ----------------------------------------------------------------------------
541 // override these 2 functions to do nothing: everything is done in OnSize
543 void wxNotebook::SetConstraintSizes(bool /* recurse */)
545 // don't set the sizes of the pages - their correct size is not yet known
546 wxControl::SetConstraintSizes(FALSE
);
549 bool wxNotebook::DoPhase(int /* nPhase */)
554 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
556 wxFAIL_MSG("wxNotebook::Command not implemented");
559 // ----------------------------------------------------------------------------
560 // wxNotebook helper functions
561 // ----------------------------------------------------------------------------
563 // hide the currently active panel and show the new one
564 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
566 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
567 wxASSERT( nOldSel
!= nSel
); // impossible
569 if ( nOldSel
!= -1 ) {
570 m_pages
[nOldSel
]->Show(FALSE
);
571 m_pages
[nOldSel
]->Lower();
574 wxNotebookPage
*pPage
= m_pages
[nSel
];
576 wxRect clientRect
= GetAvailableClientSize();
577 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
588 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
591 m_tabView
->OnEvent(event
);
594 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
601 wxRect
wxNotebook::GetAvailableClientSize()
604 GetClientSize(& cw
, & ch
);
606 int tabHeight
= m_tabView
->GetTotalTabHeight();
608 // TODO: these margins should be configurable.
611 rect
.y
= tabHeight
+ 6;
612 rect
.width
= cw
- 12;
613 rect
.height
= ch
- 4 - rect
.y
;
622 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
624 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
626 m_notebook
= notebook
;
628 m_notebook
->SetTabView(this);
630 SetWindow(m_notebook
);
633 wxNotebookTabView::~wxNotebookTabView(void)
637 // Called when a tab is activated
638 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
643 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
645 #if defined (__WIN16__)
646 int activatePos
= activateId
;
647 int deactivatePos
= deactivateId
;
649 // Translate from wxTabView's ids (which aren't position-dependent)
650 // to wxNotebook's (which are).
651 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
652 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
654 int activatePos
= m_notebook
->FindPagePosition(pActive
);
655 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
658 event
.SetEventObject(m_notebook
);
659 event
.SetSelection(activatePos
);
660 event
.SetOldSelection(deactivatePos
);
661 m_notebook
->GetEventHandler()->ProcessEvent(event
);