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::GetColour(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 // set the size (the same for all pages)
218 void wxNotebook::SetPageSize(const wxSize
& size
)
223 // set the padding between tabs (in pixels)
224 void wxNotebook::SetPadding(const wxSize
& padding
)
229 // set the size of the tabs for wxNB_FIXEDWIDTH controls
230 void wxNotebook::SetTabSize(const wxSize
& sz
)
235 // ----------------------------------------------------------------------------
236 // wxNotebook operations
237 // ----------------------------------------------------------------------------
239 // remove one page from the notebook and delete it
240 bool wxNotebook::DeletePage(int nPage
)
242 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
244 if (m_nSelection
!= -1)
246 m_pages
[m_nSelection
]->Show(FALSE
);
247 m_pages
[m_nSelection
]->Lower();
250 wxNotebookPage
* pPage
= GetPage(nPage
);
251 #if defined (__WIN16__)
252 m_tabView
->RemoveTab(nPage
);
254 m_tabView
->RemoveTab((int) (long) pPage
);
257 m_pages
.Remove(pPage
);
260 if (m_pages
.GetCount() == 0)
263 m_tabView
->SetTabSelection(-1, FALSE
);
265 else if (m_nSelection
> -1)
268 #if defined (__WIN16__)
269 m_tabView
->SetTabSelection(0, FALSE
);
271 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
273 if (m_nSelection
!= 0)
277 RefreshLayout(FALSE
);
282 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
284 int pagePos
= FindPagePosition(page
);
286 return DeletePage(pagePos
);
291 // remove one page from the notebook
292 bool wxNotebook::RemovePage(int nPage
)
294 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
296 m_pages
[nPage
]->Show(FALSE
);
297 // m_pages[nPage]->Lower();
299 wxNotebookPage
* pPage
= GetPage(nPage
);
300 #if defined (__WIN16__)
301 m_tabView
->RemoveTab(nPage
);
303 m_tabView
->RemoveTab((int) (long) pPage
);
306 m_pages
.Remove(pPage
);
308 if (m_pages
.GetCount() == 0)
311 m_tabView
->SetTabSelection(-1, TRUE
);
313 else if (m_nSelection
> -1)
315 // Only change the selection if the page we
316 // deleted was the selection.
317 if (nPage
== m_nSelection
)
320 // Select the first tab. Generates a ChangePage.
321 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
325 // We must adjust which tab we think is selected.
326 // If greater than the page we deleted, it must be moved down
328 if (m_nSelection
> nPage
)
333 RefreshLayout(FALSE
);
338 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
340 int pagePos
= FindPagePosition(page
);
342 return RemovePage(pagePos
);
347 // Find the position of the wxNotebookPage, -1 if not found.
348 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
350 int nPageCount
= GetPageCount();
352 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
353 if (m_pages
[nPage
] == page
)
359 bool wxNotebook::DeleteAllPages()
361 m_tabView
->ClearTabs(TRUE
);
363 int nPageCount
= GetPageCount();
365 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
366 delete m_pages
[nPage
];
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_pages
.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_pages
.Count();
503 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
504 wxNotebookPage
*pPage
= m_pages
[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_pages
[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
& WXUNUSED(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_pages
[nOldSel
]->Show(FALSE
);
589 m_pages
[nOldSel
]->Lower();
592 wxNotebookPage
*pPage
= m_pages
[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 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
663 #if defined (__WIN16__)
664 int activatePos
= activateId
;
665 int deactivatePos
= deactivateId
;
667 // Translate from wxTabView's ids (which aren't position-dependent)
668 // to wxNotebook's (which are).
669 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
670 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
672 int activatePos
= m_notebook
->FindPagePosition(pActive
);
673 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
676 event
.SetEventObject(m_notebook
);
677 event
.SetSelection(activatePos
);
678 event
.SetOldSelection(deactivatePos
);
679 m_notebook
->GetEventHandler()->ProcessEvent(event
);