1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "notebook.h"
24 #pragma message disable unscomzer
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
34 #include "wx/string.h"
36 #include "wx/settings.h"
37 #include "wx/generic/imaglist.h"
38 #include "wx/notebook.h"
39 #include "wx/dcclient.h"
40 #include "wx/generic/tabg.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // check that the page index is valid
47 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
56 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
57 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
58 EVT_SIZE(wxNotebook::OnSize
)
59 EVT_PAINT(wxNotebook::OnPaint
)
60 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
61 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
62 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
68 // ============================================================================
70 // ============================================================================
72 // ============================================================================
74 // ============================================================================
76 // This reuses wxTabView to draw the tabs.
77 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
79 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
81 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
82 ~wxNotebookTabView(void);
84 // Called when a tab is activated
85 virtual void OnTabActivate(int activateId
, int deactivateId
);
87 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
90 wxNotebook
* m_notebook
;
93 // ----------------------------------------------------------------------------
94 // wxNotebook construction
95 // ----------------------------------------------------------------------------
97 // common part of all ctors
98 void wxNotebook::Init()
100 m_tabView
= (wxNotebookTabView
*) NULL
;
104 // default for dynamic class
105 wxNotebook::wxNotebook()
110 // the same arguments as for wxControl
111 wxNotebook::wxNotebook(wxWindow
*parent
,
116 const wxString
& name
)
120 Create(parent
, id
, pos
, size
, style
, name
);
124 bool wxNotebook::Create(wxWindow
*parent
,
129 const wxString
& name
)
134 m_windowId
= id
== -1 ? NewControlId() : id
;
136 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
139 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
141 SetTabView(new wxNotebookTabView(this));
147 wxNotebook::~wxNotebook()
152 // ----------------------------------------------------------------------------
153 // wxNotebook accessors
154 // ----------------------------------------------------------------------------
155 int wxNotebook::GetRowCount() const
161 int wxNotebook::SetSelection(size_t nPage
)
163 wxASSERT( IS_VALID_PAGE(nPage
) );
165 wxNotebookPage
* pPage
= GetPage(nPage
);
167 m_tabView
->SetTabSelection((int) (long) pPage
);
174 void wxNotebook::AdvanceSelection(bool bForward
)
176 int nSel
= GetSelection();
177 int nMax
= GetPageCount() - 1;
179 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
181 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
185 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
187 wxASSERT( IS_VALID_PAGE(nPage
) );
189 wxNotebookPage
* page
= GetPage(nPage
);
192 m_tabView
->SetTabText((int) (long) page
, strText
);
200 wxString
wxNotebook::GetPageText(size_t nPage
) const
202 wxASSERT( IS_VALID_PAGE(nPage
) );
204 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
206 return m_tabView
->GetTabText((int) (long) page
);
208 return wxEmptyString
;
211 int wxNotebook::GetPageImage(size_t nPage
) const
213 wxASSERT( IS_VALID_PAGE(nPage
) );
219 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
221 wxASSERT( IS_VALID_PAGE(nPage
) );
227 // set the size (the same for all pages)
228 void wxNotebook::SetPageSize(const wxSize
& size
)
233 // set the padding between tabs (in pixels)
234 void wxNotebook::SetPadding(const wxSize
& padding
)
239 // set the size of the tabs for wxNB_FIXEDWIDTH controls
240 void wxNotebook::SetTabSize(const wxSize
& sz
)
245 // ----------------------------------------------------------------------------
246 // wxNotebook operations
247 // ----------------------------------------------------------------------------
249 // remove one page from the notebook and delete it
250 bool wxNotebook::DeletePage(size_t nPage
)
252 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
254 if (m_nSelection
!= -1)
256 m_pages
[m_nSelection
]->Show(FALSE
);
257 m_pages
[m_nSelection
]->Lower();
260 wxNotebookPage
* pPage
= GetPage(nPage
);
262 m_tabView
->RemoveTab((int) (long) pPage
);
264 m_pages
.Remove(pPage
);
267 if (m_pages
.GetCount() == 0)
270 m_tabView
->SetTabSelection(-1, FALSE
);
272 else if (m_nSelection
> -1)
276 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
278 if (m_nSelection
!= 0)
282 RefreshLayout(FALSE
);
287 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
289 int pagePos
= FindPagePosition(page
);
291 return DeletePage(pagePos
);
296 bool wxNotebook::RemovePage(size_t nPage
)
298 return DoRemovePage(nPage
) != NULL
;
301 // remove one page from the notebook
302 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
304 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
306 m_pages
[nPage
]->Show(FALSE
);
307 // m_pages[nPage]->Lower();
309 wxNotebookPage
* pPage
= GetPage(nPage
);
311 m_tabView
->RemoveTab((int) (long) pPage
);
313 m_pages
.Remove(pPage
);
315 if (m_pages
.GetCount() == 0)
318 m_tabView
->SetTabSelection(-1, TRUE
);
320 else if (m_nSelection
> -1)
322 // Only change the selection if the page we
323 // deleted was the selection.
324 if (nPage
== (size_t)m_nSelection
)
327 // Select the first tab. Generates a ChangePage.
328 m_tabView
->SetTabSelection(0, TRUE
);
332 // We must adjust which tab we think is selected.
333 // If greater than the page we deleted, it must be moved down
335 if (size_t(m_nSelection
) > nPage
)
340 RefreshLayout(FALSE
);
345 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
347 int pagePos
= FindPagePosition(page
);
349 return RemovePage(pagePos
);
354 // Find the position of the wxNotebookPage, -1 if not found.
355 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
357 size_t nPageCount
= GetPageCount();
359 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
360 if (m_pages
[nPage
] == page
)
366 bool wxNotebook::DeleteAllPages()
368 m_tabView
->ClearTabs(TRUE
);
370 size_t nPageCount
= GetPageCount();
372 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
373 delete m_pages
[nPage
];
380 // same as AddPage() but does it at given position
381 bool wxNotebook::InsertPage(size_t nPage
,
382 wxNotebookPage
*pPage
,
383 const wxString
& strText
,
387 wxASSERT( pPage
!= NULL
);
388 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
390 m_tabView
->AddTab((int) (long) pPage
, strText
);
395 // save the pointer to the page
396 m_pages
.Insert(pPage
, nPage
);
400 // This will cause ChangePage to be called, via OnSelPage
402 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::OnInternalIdle()
440 wxWindow::OnInternalIdle();
443 static bool s_bFirstTime
= TRUE
;
444 if ( s_bFirstTime
) {
446 wxSize sz(GetSize());
454 wxSize sz(GetSize());
455 wxSizeEvent sizeEvent(sz, GetId());
456 sizeEvent.SetEventObject(this);
457 GetEventHandler()->ProcessEvent(sizeEvent);
460 s_bFirstTime
= FALSE
;
465 // Implementation: calculate the layout of the view rect
466 // and resize the children if required
467 bool wxNotebook::RefreshLayout(bool force
)
471 wxRect oldRect
= m_tabView
->GetViewRect();
474 GetClientSize(& cw
, & ch
);
476 int tabHeight
= m_tabView
->GetTotalTabHeight();
479 rect
.y
= tabHeight
+ 4;
481 rect
.height
= ch
- 4 - rect
.y
;
483 m_tabView
->SetViewRect(rect
);
485 m_tabView
->LayoutTabs();
487 // Need to do it a 2nd time to get the tab height with
488 // the new view width, since changing the view width changes the
490 tabHeight
= m_tabView
->GetTotalTabHeight();
492 rect
.y
= tabHeight
+ 4;
494 rect
.height
= ch
- 4 - rect
.y
;
496 m_tabView
->SetViewRect(rect
);
498 m_tabView
->LayoutTabs();
500 if (!force
&& (rect
== oldRect
))
503 // fit the notebook page to the tab control's display area
505 size_t nCount
= m_pages
.Count();
506 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
507 wxNotebookPage
*pPage
= m_pages
[nPage
];
508 wxRect clientRect
= GetAvailableClientSize();
509 if (pPage
->IsShown())
511 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
512 if ( pPage
->GetAutoLayout() )
521 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
523 // is it our tab control?
524 if ( event
.GetEventObject() == this )
526 if (event
.GetSelection() != m_nSelection
)
527 ChangePage(event
.GetOldSelection(), event
.GetSelection());
530 // we want to give others a chance to process this message as well
534 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
536 // set focus to the currently selected page if any
537 if ( m_nSelection
!= -1 )
538 m_pages
[m_nSelection
]->SetFocus();
543 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
545 if ( event
.IsWindowChange() ) {
547 AdvanceSelection(event
.GetDirection());
550 // pass to the parent
552 event
.SetCurrentFocus(this);
553 GetParent()->ProcessEvent(event
);
558 // ----------------------------------------------------------------------------
559 // wxNotebook base class virtuals
560 // ----------------------------------------------------------------------------
562 // override these 2 functions to do nothing: everything is done in OnSize
564 void wxNotebook::SetConstraintSizes(bool /* recurse */)
566 // don't set the sizes of the pages - their correct size is not yet known
567 wxControl::SetConstraintSizes(FALSE
);
570 bool wxNotebook::DoPhase(int /* nPhase */)
575 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
577 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
580 // ----------------------------------------------------------------------------
581 // wxNotebook helper functions
582 // ----------------------------------------------------------------------------
584 // hide the currently active panel and show the new one
585 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
587 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
588 wxASSERT( nOldSel
!= nSel
); // impossible
590 if ( nOldSel
!= -1 ) {
591 m_pages
[nOldSel
]->Show(FALSE
);
592 m_pages
[nOldSel
]->Lower();
595 wxNotebookPage
*pPage
= m_pages
[nSel
];
597 wxRect clientRect
= GetAvailableClientSize();
598 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
609 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
612 m_tabView
->OnEvent(event
);
615 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
622 wxRect
wxNotebook::GetAvailableClientSize()
625 GetClientSize(& cw
, & ch
);
627 int tabHeight
= m_tabView
->GetTotalTabHeight();
629 // TODO: these margins should be configurable.
632 rect
.y
= tabHeight
+ 6;
633 rect
.width
= cw
- 12;
634 rect
.height
= ch
- 4 - rect
.y
;
643 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
645 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
647 m_notebook
= notebook
;
649 m_notebook
->SetTabView(this);
651 SetWindow(m_notebook
);
654 wxNotebookTabView::~wxNotebookTabView(void)
658 // Called when a tab is activated
659 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
664 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
666 // Translate from wxTabView's ids (which aren't position-dependent)
667 // to wxNotebook's (which are).
668 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
669 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
671 int activatePos
= m_notebook
->FindPagePosition(pActive
);
672 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
674 event
.SetEventObject(m_notebook
);
675 event
.SetSelection(activatePos
);
676 event
.SetOldSelection(deactivatePos
);
677 m_notebook
->GetEventHandler()->ProcessEvent(event
);
681 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
687 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
689 // Translate from wxTabView's ids (which aren't position-dependent)
690 // to wxNotebook's (which are).
691 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
692 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
694 int activatePos
= m_notebook
->FindPagePosition(pActive
);
695 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
697 event
.SetEventObject(m_notebook
);
698 event
.SetSelection(activatePos
);
699 event
.SetOldSelection(deactivatePos
);
700 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
702 retval
= event
.IsAllowed();