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"
29 #include "wx/notebook.h"
32 #include "wx/string.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
38 #include "wx/imaglist.h"
39 #include "wx/generic/tabg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
55 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
56 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
57 EVT_SIZE(wxNotebook::OnSize
)
58 EVT_PAINT(wxNotebook::OnPaint
)
59 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
66 // ============================================================================
68 // ============================================================================
70 // ============================================================================
72 // ============================================================================
74 WX_DECLARE_HASH_MAP(int, wxNotebookPage
*, wxIntegerHash
, wxIntegerEqual
,
75 wxIntToNotebookPageHashMap
);
77 WX_DECLARE_HASH_MAP(wxNotebookPage
*, int, wxPointerHash
, wxPointerEqual
,
78 wxNotebookPageToIntHashMap
);
80 // This reuses wxTabView to draw the tabs.
81 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
83 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
85 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
86 virtual ~wxNotebookTabView(void);
88 // Called when a tab is activated
89 virtual void OnTabActivate(int activateId
, int deactivateId
);
91 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
93 // map integer ids used by wxTabView to wxNotebookPage pointers
94 int GetId(wxNotebookPage
*page
);
95 wxNotebookPage
*GetPage(int id
) { return m_idToPage
[id
]; }
98 wxNotebook
* m_notebook
;
101 wxIntToNotebookPageHashMap m_idToPage
;
102 wxNotebookPageToIntHashMap m_pageToId
;
106 static int GetPageId(wxTabView
*tabview
, wxNotebookPage
*page
)
108 return wx_static_cast(wxNotebookTabView
*, tabview
)->GetId(page
);
111 // ----------------------------------------------------------------------------
112 // wxNotebook construction
113 // ----------------------------------------------------------------------------
115 // common part of all ctors
116 void wxNotebook::Init()
118 m_tabView
= (wxNotebookTabView
*) NULL
;
122 // default for dynamic class
123 wxNotebook::wxNotebook()
128 // the same arguments as for wxControl
129 wxNotebook::wxNotebook(wxWindow
*parent
,
134 const wxString
& name
)
138 Create(parent
, id
, pos
, size
, style
, name
);
142 bool wxNotebook::Create(wxWindow
*parent
,
147 const wxString
& name
)
152 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
155 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
157 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
160 SetTabView(new wxNotebookTabView(this));
166 wxNotebook::~wxNotebook()
171 // ----------------------------------------------------------------------------
172 // wxNotebook accessors
173 // ----------------------------------------------------------------------------
174 int wxNotebook::GetRowCount() const
180 int wxNotebook::SetSelection(size_t nPage
)
182 wxASSERT( IS_VALID_PAGE(nPage
) );
184 wxNotebookPage
* pPage
= GetPage(nPage
);
186 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
192 int wxNotebook::ChangeSelection(size_t nPage
)
194 // FIXME: currently it does generate events too
195 return SetSelection(nPage
);
199 void wxNotebook::AdvanceSelection(bool bForward
)
201 int nSel
= GetSelection();
202 int nMax
= GetPageCount() - 1;
204 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
206 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
210 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
212 wxASSERT( IS_VALID_PAGE(nPage
) );
214 wxNotebookPage
* page
= GetPage(nPage
);
217 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
225 wxString
wxNotebook::GetPageText(size_t nPage
) const
227 wxASSERT( IS_VALID_PAGE(nPage
) );
229 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
231 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
233 return wxEmptyString
;
236 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
238 wxASSERT( IS_VALID_PAGE(nPage
) );
244 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
245 int WXUNUSED(nImage
))
247 wxASSERT( IS_VALID_PAGE(nPage
) );
253 // set the size (the same for all pages)
254 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
259 // set the padding between tabs (in pixels)
260 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
265 // set the size of the tabs for wxNB_FIXEDWIDTH controls
266 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
271 // ----------------------------------------------------------------------------
272 // wxNotebook operations
273 // ----------------------------------------------------------------------------
275 // remove one page from the notebook and delete it
276 bool wxNotebook::DeletePage(size_t nPage
)
278 wxCHECK( IS_VALID_PAGE(nPage
), false );
280 if (m_nSelection
!= -1)
282 m_pages
[m_nSelection
]->Show(false);
283 m_pages
[m_nSelection
]->Lower();
286 wxNotebookPage
* pPage
= GetPage(nPage
);
288 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
290 m_pages
.Remove(pPage
);
293 if (m_pages
.GetCount() == 0)
296 m_tabView
->SetTabSelection(-1, false);
298 else if (m_nSelection
> -1)
302 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
304 if (m_nSelection
!= 0)
308 RefreshLayout(false);
313 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
315 int pagePos
= FindPagePosition(page
);
317 return DeletePage(pagePos
);
322 bool wxNotebook::RemovePage(size_t nPage
)
324 return DoRemovePage(nPage
) != NULL
;
327 // remove one page from the notebook
328 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
330 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
332 m_pages
[nPage
]->Show(false);
333 // m_pages[nPage]->Lower();
335 wxNotebookPage
* pPage
= GetPage(nPage
);
337 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
339 m_pages
.Remove(pPage
);
341 if (m_pages
.GetCount() == 0)
344 m_tabView
->SetTabSelection(-1, true);
346 else if (m_nSelection
> -1)
348 // Only change the selection if the page we
349 // deleted was the selection.
350 if (nPage
== (size_t)m_nSelection
)
353 // Select the first tab. Generates a ChangePage.
354 m_tabView
->SetTabSelection(0, true);
358 // We must adjust which tab we think is selected.
359 // If greater than the page we deleted, it must be moved down
361 if (size_t(m_nSelection
) > nPage
)
366 RefreshLayout(false);
371 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
373 int pagePos
= FindPagePosition(page
);
375 return RemovePage(pagePos
);
380 // Find the position of the wxNotebookPage, -1 if not found.
381 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
383 size_t nPageCount
= GetPageCount();
385 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
386 if (m_pages
[nPage
] == page
)
392 bool wxNotebook::DeleteAllPages()
394 m_tabView
->ClearTabs(true);
396 size_t nPageCount
= GetPageCount();
398 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
399 delete m_pages
[nPage
];
406 // same as AddPage() but does it at given position
407 bool wxNotebook::InsertPage(size_t nPage
,
408 wxNotebookPage
*pPage
,
409 const wxString
& strText
,
411 int WXUNUSED(imageId
))
413 wxASSERT( pPage
!= NULL
);
414 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
416 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
421 // save the pointer to the page
422 m_pages
.Insert(pPage
, nPage
);
426 // This will cause ChangePage to be called, via OnSelPage
428 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
431 // some page must be selected: either this one or the first one if there is
432 // still no selection
433 if ( m_nSelection
== -1 )
436 RefreshLayout(false);
441 // ----------------------------------------------------------------------------
442 // wxNotebook callbacks
443 // ----------------------------------------------------------------------------
445 // @@@ OnSize() is used for setting the font when it's called for the first
446 // time because doing it in ::Create() doesn't work (for unknown reasons)
447 void wxNotebook::OnSize(wxSizeEvent
& event
)
449 static bool s_bFirstTime
= true;
450 if ( s_bFirstTime
) {
451 // TODO: any first-time-size processing.
452 s_bFirstTime
= false;
457 // Processing continues to next OnSize
461 // This was supposed to cure the non-display of the notebook
462 // until the user resizes the window.
464 void wxNotebook::OnInternalIdle()
466 wxWindow::OnInternalIdle();
469 static bool s_bFirstTime
= true;
470 if ( s_bFirstTime
) {
472 wxSize sz(GetSize());
480 wxSize sz(GetSize());
481 wxSizeEvent sizeEvent(sz, GetId());
482 sizeEvent.SetEventObject(this);
483 GetEventHandler()->ProcessEvent(sizeEvent);
486 s_bFirstTime
= false;
491 // Implementation: calculate the layout of the view rect
492 // and resize the children if required
493 bool wxNotebook::RefreshLayout(bool force
)
497 wxRect oldRect
= m_tabView
->GetViewRect();
500 GetClientSize(& cw
, & ch
);
502 int tabHeight
= m_tabView
->GetTotalTabHeight();
505 rect
.y
= tabHeight
+ 4;
507 rect
.height
= ch
- 4 - rect
.y
;
509 m_tabView
->SetViewRect(rect
);
511 m_tabView
->LayoutTabs();
513 // Need to do it a 2nd time to get the tab height with
514 // the new view width, since changing the view width changes the
516 tabHeight
= m_tabView
->GetTotalTabHeight();
518 rect
.y
= tabHeight
+ 4;
520 rect
.height
= ch
- 4 - rect
.y
;
522 m_tabView
->SetViewRect(rect
);
524 m_tabView
->LayoutTabs();
526 if (!force
&& (rect
== oldRect
))
529 // fit the notebook page to the tab control's display area
531 size_t nCount
= m_pages
.Count();
532 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
533 wxNotebookPage
*pPage
= m_pages
[nPage
];
534 wxRect clientRect
= GetAvailableClientSize();
535 if (pPage
->IsShown())
537 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
538 if ( pPage
->GetAutoLayout() )
547 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
549 // is it our tab control?
550 if ( event
.GetEventObject() == this )
552 if (event
.GetSelection() != m_nSelection
)
553 ChangePage(event
.GetOldSelection(), event
.GetSelection());
556 // we want to give others a chance to process this message as well
560 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
562 // set focus to the currently selected page if any
563 if ( m_nSelection
!= -1 )
564 m_pages
[m_nSelection
]->SetFocus();
569 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
571 if ( event
.IsWindowChange() ) {
573 AdvanceSelection(event
.GetDirection());
576 // pass to the parent
578 event
.SetCurrentFocus(this);
579 GetParent()->ProcessEvent(event
);
584 // ----------------------------------------------------------------------------
585 // wxNotebook base class virtuals
586 // ----------------------------------------------------------------------------
588 // override these 2 functions to do nothing: everything is done in OnSize
590 void wxNotebook::SetConstraintSizes(bool /* recurse */)
592 // don't set the sizes of the pages - their correct size is not yet known
593 wxControl::SetConstraintSizes(false);
596 bool wxNotebook::DoPhase(int /* nPhase */)
601 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
603 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
606 // ----------------------------------------------------------------------------
607 // wxNotebook helper functions
608 // ----------------------------------------------------------------------------
610 // hide the currently active panel and show the new one
611 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
613 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
614 wxASSERT( nOldSel
!= nSel
); // impossible
616 if ( nOldSel
!= -1 ) {
617 m_pages
[nOldSel
]->Show(false);
618 m_pages
[nOldSel
]->Lower();
621 wxNotebookPage
*pPage
= m_pages
[nSel
];
623 wxRect clientRect
= GetAvailableClientSize();
624 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
635 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
638 m_tabView
->OnEvent(event
);
641 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
648 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
650 // MBN: since the total tab height is really a function of the
651 // width, this should really call
652 // GetTotalTabHeightPretendingWidthIs(), but the current
653 // implementation will suffice, provided the wxNotebook has been
654 // created with a sensible initial width.
655 return wxSize( sizePage
.x
+ 12,
656 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
659 wxRect
wxNotebook::GetAvailableClientSize()
662 GetClientSize(& cw
, & ch
);
664 int tabHeight
= m_tabView
->GetTotalTabHeight();
666 // TODO: these margins should be configurable.
669 rect
.y
= tabHeight
+ 6;
670 rect
.width
= cw
- 12;
671 rect
.height
= ch
- 4 - rect
.y
;
680 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
682 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
683 : wxTabView(style
), m_nextid(1)
685 m_notebook
= notebook
;
687 m_notebook
->SetTabView(this);
689 SetWindow(m_notebook
);
692 wxNotebookTabView::~wxNotebookTabView(void)
696 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
698 int& id
= m_pageToId
[page
];
703 m_idToPage
[id
] = page
;
709 // Called when a tab is activated
710 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
715 wxBookCtrlEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
717 // Translate from wxTabView's ids (which aren't position-dependent)
718 // to wxNotebook's (which are).
719 wxNotebookPage
* pActive
= GetPage(activateId
);
720 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
722 int activatePos
= m_notebook
->FindPagePosition(pActive
);
723 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
725 event
.SetEventObject(m_notebook
);
726 event
.SetSelection(activatePos
);
727 event
.SetOldSelection(deactivatePos
);
728 m_notebook
->GetEventHandler()->ProcessEvent(event
);
732 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
738 wxBookCtrlEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
740 // Translate from wxTabView's ids (which aren't position-dependent)
741 // to wxNotebook's (which are).
742 wxNotebookPage
* pActive
= GetPage(activateId
);
743 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
745 int activatePos
= m_notebook
->FindPagePosition(pActive
);
746 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
748 event
.SetEventObject(m_notebook
);
749 event
.SetSelection(activatePos
);
750 event
.SetOldSelection(deactivatePos
);
751 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
753 retval
= event
.IsAllowed();
759 #endif // wxUSE_NOTEBOOK