1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notebook.cpp
3 // Purpose: generic implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/notebook.h"
30 #include "wx/string.h"
34 #include "wx/settings.h"
35 #include "wx/generic/imaglist.h"
36 #include "wx/dcclient.h"
37 #include "wx/generic/tabg.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // check that the page index is valid
44 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
53 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
54 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
55 EVT_SIZE(wxNotebook::OnSize
)
56 EVT_PAINT(wxNotebook::OnPaint
)
57 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
58 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
59 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
62 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
63 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
65 // ============================================================================
67 // ============================================================================
69 // ============================================================================
71 // ============================================================================
73 // This reuses wxTabView to draw the tabs.
74 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
76 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
78 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
79 ~wxNotebookTabView(void);
81 // Called when a tab is activated
82 virtual void OnTabActivate(int activateId
, int deactivateId
);
84 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
87 wxNotebook
* m_notebook
;
90 // ----------------------------------------------------------------------------
91 // wxNotebook construction
92 // ----------------------------------------------------------------------------
94 // common part of all ctors
95 void wxNotebook::Init()
97 m_tabView
= (wxNotebookTabView
*) NULL
;
101 // default for dynamic class
102 wxNotebook::wxNotebook()
107 // the same arguments as for wxControl
108 wxNotebook::wxNotebook(wxWindow
*parent
,
113 const wxString
& name
)
117 Create(parent
, id
, pos
, size
, style
, name
);
121 bool wxNotebook::Create(wxWindow
*parent
,
126 const wxString
& name
)
131 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
133 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
136 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
138 SetTabView(new wxNotebookTabView(this));
144 wxNotebook::~wxNotebook()
149 // ----------------------------------------------------------------------------
150 // wxNotebook accessors
151 // ----------------------------------------------------------------------------
152 int wxNotebook::GetRowCount() const
158 int wxNotebook::SetSelection(size_t nPage
)
160 wxASSERT( IS_VALID_PAGE(nPage
) );
162 wxNotebookPage
* pPage
= GetPage(nPage
);
164 m_tabView
->SetTabSelection((int) (long) pPage
);
171 void wxNotebook::AdvanceSelection(bool bForward
)
173 int nSel
= GetSelection();
174 int nMax
= GetPageCount() - 1;
176 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
178 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
182 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
184 wxASSERT( IS_VALID_PAGE(nPage
) );
186 wxNotebookPage
* page
= GetPage(nPage
);
189 m_tabView
->SetTabText((int) (long) page
, strText
);
197 wxString
wxNotebook::GetPageText(size_t nPage
) const
199 wxASSERT( IS_VALID_PAGE(nPage
) );
201 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
203 return m_tabView
->GetTabText((int) (long) page
);
205 return wxEmptyString
;
208 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
210 wxASSERT( IS_VALID_PAGE(nPage
) );
216 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
217 int WXUNUSED(nImage
))
219 wxASSERT( IS_VALID_PAGE(nPage
) );
225 // set the size (the same for all pages)
226 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
231 // set the padding between tabs (in pixels)
232 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
237 // set the size of the tabs for wxNB_FIXEDWIDTH controls
238 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
243 // ----------------------------------------------------------------------------
244 // wxNotebook operations
245 // ----------------------------------------------------------------------------
247 // remove one page from the notebook and delete it
248 bool wxNotebook::DeletePage(size_t nPage
)
250 wxCHECK( IS_VALID_PAGE(nPage
), false );
252 if (m_nSelection
!= -1)
254 m_pages
[m_nSelection
]->Show(false);
255 m_pages
[m_nSelection
]->Lower();
258 wxNotebookPage
* pPage
= GetPage(nPage
);
260 m_tabView
->RemoveTab((int) (long) pPage
);
262 m_pages
.Remove(pPage
);
265 if (m_pages
.GetCount() == 0)
268 m_tabView
->SetTabSelection(-1, false);
270 else if (m_nSelection
> -1)
274 m_tabView
->SetTabSelection((int) (long) GetPage(0), false);
276 if (m_nSelection
!= 0)
280 RefreshLayout(false);
285 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
287 int pagePos
= FindPagePosition(page
);
289 return DeletePage(pagePos
);
294 bool wxNotebook::RemovePage(size_t nPage
)
296 return DoRemovePage(nPage
) != NULL
;
299 // remove one page from the notebook
300 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
302 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
304 m_pages
[nPage
]->Show(false);
305 // m_pages[nPage]->Lower();
307 wxNotebookPage
* pPage
= GetPage(nPage
);
309 m_tabView
->RemoveTab((int) (long) pPage
);
311 m_pages
.Remove(pPage
);
313 if (m_pages
.GetCount() == 0)
316 m_tabView
->SetTabSelection(-1, true);
318 else if (m_nSelection
> -1)
320 // Only change the selection if the page we
321 // deleted was the selection.
322 if (nPage
== (size_t)m_nSelection
)
325 // Select the first tab. Generates a ChangePage.
326 m_tabView
->SetTabSelection(0, true);
330 // We must adjust which tab we think is selected.
331 // If greater than the page we deleted, it must be moved down
333 if (size_t(m_nSelection
) > nPage
)
338 RefreshLayout(false);
343 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
345 int pagePos
= FindPagePosition(page
);
347 return RemovePage(pagePos
);
352 // Find the position of the wxNotebookPage, -1 if not found.
353 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
355 size_t nPageCount
= GetPageCount();
357 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
358 if (m_pages
[nPage
] == page
)
364 bool wxNotebook::DeleteAllPages()
366 m_tabView
->ClearTabs(true);
368 size_t nPageCount
= GetPageCount();
370 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
371 delete m_pages
[nPage
];
378 // same as AddPage() but does it at given position
379 bool wxNotebook::InsertPage(size_t nPage
,
380 wxNotebookPage
*pPage
,
381 const wxString
& strText
,
383 int WXUNUSED(imageId
))
385 wxASSERT( pPage
!= NULL
);
386 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
388 m_tabView
->AddTab((int) (long) pPage
, strText
);
393 // save the pointer to the page
394 m_pages
.Insert(pPage
, nPage
);
398 // This will cause ChangePage to be called, via OnSelPage
400 m_tabView
->SetTabSelection((int) (long) pPage
, true);
403 // some page must be selected: either this one or the first one if there is
404 // still no selection
405 if ( m_nSelection
== -1 )
408 RefreshLayout(false);
413 // ----------------------------------------------------------------------------
414 // wxNotebook callbacks
415 // ----------------------------------------------------------------------------
417 // @@@ OnSize() is used for setting the font when it's called for the first
418 // time because doing it in ::Create() doesn't work (for unknown reasons)
419 void wxNotebook::OnSize(wxSizeEvent
& event
)
421 static bool s_bFirstTime
= true;
422 if ( s_bFirstTime
) {
423 // TODO: any first-time-size processing.
424 s_bFirstTime
= false;
429 // Processing continues to next OnSize
433 // This was supposed to cure the non-display of the notebook
434 // until the user resizes the window.
436 void wxNotebook::OnInternalIdle()
438 wxWindow::OnInternalIdle();
441 static bool s_bFirstTime
= true;
442 if ( s_bFirstTime
) {
444 wxSize sz(GetSize());
452 wxSize sz(GetSize());
453 wxSizeEvent sizeEvent(sz, GetId());
454 sizeEvent.SetEventObject(this);
455 GetEventHandler()->ProcessEvent(sizeEvent);
458 s_bFirstTime
= false;
463 // Implementation: calculate the layout of the view rect
464 // and resize the children if required
465 bool wxNotebook::RefreshLayout(bool force
)
469 wxRect oldRect
= m_tabView
->GetViewRect();
472 GetClientSize(& cw
, & ch
);
474 int tabHeight
= m_tabView
->GetTotalTabHeight();
477 rect
.y
= tabHeight
+ 4;
479 rect
.height
= ch
- 4 - rect
.y
;
481 m_tabView
->SetViewRect(rect
);
483 m_tabView
->LayoutTabs();
485 // Need to do it a 2nd time to get the tab height with
486 // the new view width, since changing the view width changes the
488 tabHeight
= m_tabView
->GetTotalTabHeight();
490 rect
.y
= tabHeight
+ 4;
492 rect
.height
= ch
- 4 - rect
.y
;
494 m_tabView
->SetViewRect(rect
);
496 m_tabView
->LayoutTabs();
498 if (!force
&& (rect
== oldRect
))
501 // fit the notebook page to the tab control's display area
503 size_t nCount
= m_pages
.Count();
504 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
505 wxNotebookPage
*pPage
= m_pages
[nPage
];
506 wxRect clientRect
= GetAvailableClientSize();
507 if (pPage
->IsShown())
509 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
510 if ( pPage
->GetAutoLayout() )
519 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
521 // is it our tab control?
522 if ( event
.GetEventObject() == this )
524 if (event
.GetSelection() != m_nSelection
)
525 ChangePage(event
.GetOldSelection(), event
.GetSelection());
528 // we want to give others a chance to process this message as well
532 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
534 // set focus to the currently selected page if any
535 if ( m_nSelection
!= -1 )
536 m_pages
[m_nSelection
]->SetFocus();
541 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
543 if ( event
.IsWindowChange() ) {
545 AdvanceSelection(event
.GetDirection());
548 // pass to the parent
550 event
.SetCurrentFocus(this);
551 GetParent()->ProcessEvent(event
);
556 // ----------------------------------------------------------------------------
557 // wxNotebook base class virtuals
558 // ----------------------------------------------------------------------------
560 // override these 2 functions to do nothing: everything is done in OnSize
562 void wxNotebook::SetConstraintSizes(bool /* recurse */)
564 // don't set the sizes of the pages - their correct size is not yet known
565 wxControl::SetConstraintSizes(false);
568 bool wxNotebook::DoPhase(int /* nPhase */)
573 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
575 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
578 // ----------------------------------------------------------------------------
579 // wxNotebook helper functions
580 // ----------------------------------------------------------------------------
582 // hide the currently active panel and show the new one
583 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
585 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
586 wxASSERT( nOldSel
!= nSel
); // impossible
588 if ( nOldSel
!= -1 ) {
589 m_pages
[nOldSel
]->Show(false);
590 m_pages
[nOldSel
]->Lower();
593 wxNotebookPage
*pPage
= m_pages
[nSel
];
595 wxRect clientRect
= GetAvailableClientSize();
596 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
607 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
610 m_tabView
->OnEvent(event
);
613 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
620 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
622 // MBN: since the total tab height is really a function of the
623 // width, this should really call
624 // GetTotalTabHeightPretendingWidthIs(), but the current
625 // implementation will suffice, provided the wxNotebook has been
626 // created with a sensible initial width.
627 return wxSize( sizePage
.x
+ 12,
628 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
631 wxRect
wxNotebook::GetAvailableClientSize()
634 GetClientSize(& cw
, & ch
);
636 int tabHeight
= m_tabView
->GetTotalTabHeight();
638 // TODO: these margins should be configurable.
641 rect
.y
= tabHeight
+ 6;
642 rect
.width
= cw
- 12;
643 rect
.height
= ch
- 4 - rect
.y
;
652 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
654 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
656 m_notebook
= notebook
;
658 m_notebook
->SetTabView(this);
660 SetWindow(m_notebook
);
663 wxNotebookTabView::~wxNotebookTabView(void)
667 // Called when a tab is activated
668 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
673 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
675 // Translate from wxTabView's ids (which aren't position-dependent)
676 // to wxNotebook's (which are).
677 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
678 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
680 int activatePos
= m_notebook
->FindPagePosition(pActive
);
681 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
683 event
.SetEventObject(m_notebook
);
684 event
.SetSelection(activatePos
);
685 event
.SetOldSelection(deactivatePos
);
686 m_notebook
->GetEventHandler()->ProcessEvent(event
);
690 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
696 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
698 // Translate from wxTabView's ids (which aren't position-dependent)
699 // to wxNotebook's (which are).
700 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
701 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
703 int activatePos
= m_notebook
->FindPagePosition(pActive
);
704 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
706 event
.SetEventObject(m_notebook
);
707 event
.SetSelection(activatePos
);
708 event
.SetOldSelection(deactivatePos
);
709 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
711 retval
= event
.IsAllowed();