1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notebook.cpp
3 // Purpose: generic implementation of wxNotebook
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/notebook.h"
31 #include "wx/string.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
37 #include "wx/imaglist.h"
38 #include "wx/generic/tabg.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // check that the page index is valid
45 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
52 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, 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
)
60 // ============================================================================
62 // ============================================================================
64 // ============================================================================
66 // ============================================================================
68 WX_DECLARE_HASH_MAP(int, wxNotebookPage
*, wxIntegerHash
, wxIntegerEqual
,
69 wxIntToNotebookPageHashMap
);
71 WX_DECLARE_HASH_MAP(wxNotebookPage
*, int, wxPointerHash
, wxPointerEqual
,
72 wxNotebookPageToIntHashMap
);
74 // This reuses wxTabView to draw the tabs.
75 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
77 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
79 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
80 virtual ~wxNotebookTabView(void);
82 // Called when a tab is activated
83 virtual void OnTabActivate(int activateId
, int deactivateId
);
85 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
87 // map integer ids used by wxTabView to wxNotebookPage pointers
88 int GetId(wxNotebookPage
*page
);
89 wxNotebookPage
*GetPage(int id
) { return m_idToPage
[id
]; }
92 wxNotebook
* m_notebook
;
95 wxIntToNotebookPageHashMap m_idToPage
;
96 wxNotebookPageToIntHashMap m_pageToId
;
100 static int GetPageId(wxTabView
*tabview
, wxNotebookPage
*page
)
102 return static_cast<wxNotebookTabView
*>(tabview
)->GetId(page
);
105 // ----------------------------------------------------------------------------
106 // wxNotebook construction
107 // ----------------------------------------------------------------------------
109 // common part of all ctors
110 void wxNotebook::Init()
116 // default for dynamic class
117 wxNotebook::wxNotebook()
122 // the same arguments as for wxControl
123 wxNotebook::wxNotebook(wxWindow
*parent
,
128 const wxString
& name
)
132 Create(parent
, id
, pos
, size
, style
, name
);
136 bool wxNotebook::Create(wxWindow
*parent
,
141 const wxString
& name
)
146 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
149 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
151 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
154 SetTabView(new wxNotebookTabView(this));
160 wxNotebook::~wxNotebook()
165 // ----------------------------------------------------------------------------
166 // wxNotebook accessors
167 // ----------------------------------------------------------------------------
168 int wxNotebook::GetRowCount() const
174 int wxNotebook::SetSelection(size_t nPage
)
176 wxASSERT( IS_VALID_PAGE(nPage
) );
178 wxNotebookPage
* pPage
= GetPage(nPage
);
180 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
));
186 int wxNotebook::ChangeSelection(size_t nPage
)
188 // FIXME: currently it does generate events too
189 return SetSelection(nPage
);
193 void wxNotebook::AdvanceSelection(bool bForward
)
195 int nSel
= GetSelection();
196 int nMax
= GetPageCount() - 1;
198 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
200 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
204 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
206 wxASSERT( IS_VALID_PAGE(nPage
) );
208 wxNotebookPage
* page
= GetPage(nPage
);
211 m_tabView
->SetTabText(GetPageId(m_tabView
, page
), strText
);
219 wxString
wxNotebook::GetPageText(size_t nPage
) const
221 wxASSERT( IS_VALID_PAGE(nPage
) );
223 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
225 return m_tabView
->GetTabText(GetPageId(m_tabView
, page
));
227 return wxEmptyString
;
230 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
232 wxASSERT( IS_VALID_PAGE(nPage
) );
238 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
239 int WXUNUSED(nImage
))
241 wxASSERT( IS_VALID_PAGE(nPage
) );
247 // set the size (the same for all pages)
248 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
253 // set the padding between tabs (in pixels)
254 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
259 // set the size of the tabs for wxNB_FIXEDWIDTH controls
260 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
265 // ----------------------------------------------------------------------------
266 // wxNotebook operations
267 // ----------------------------------------------------------------------------
269 // remove one page from the notebook and delete it
270 bool wxNotebook::DeletePage(size_t nPage
)
272 wxCHECK( IS_VALID_PAGE(nPage
), false );
274 if (m_selection
!= -1)
276 m_pages
[m_selection
]->Show(false);
277 m_pages
[m_selection
]->Lower();
280 wxNotebookPage
* pPage
= GetPage(nPage
);
282 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
284 m_pages
.Remove(pPage
);
287 if (m_pages
.GetCount() == 0)
290 m_tabView
->SetTabSelection(-1, false);
292 else if (m_selection
> -1)
296 m_tabView
->SetTabSelection(GetPageId(m_tabView
, GetPage(0)), false);
298 if (m_selection
!= 0)
302 RefreshLayout(false);
307 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
309 int pagePos
= FindPagePosition(page
);
311 return DeletePage(pagePos
);
316 bool wxNotebook::RemovePage(size_t nPage
)
318 return DoRemovePage(nPage
) != NULL
;
321 // remove one page from the notebook
322 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
324 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
326 m_pages
[nPage
]->Show(false);
327 // m_pages[nPage]->Lower();
329 wxNotebookPage
* pPage
= GetPage(nPage
);
331 m_tabView
->RemoveTab(GetPageId(m_tabView
, pPage
));
333 m_pages
.Remove(pPage
);
335 if (m_pages
.GetCount() == 0)
338 m_tabView
->SetTabSelection(-1, true);
340 else if (m_selection
> -1)
342 // Only change the selection if the page we
343 // deleted was the selection.
344 if (nPage
== (size_t)m_selection
)
347 // Select the first tab. Generates a ChangePage.
348 m_tabView
->SetTabSelection(0, true);
352 // We must adjust which tab we think is selected.
353 // If greater than the page we deleted, it must be moved down
355 if (size_t(m_selection
) > nPage
)
360 RefreshLayout(false);
365 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
367 int pagePos
= FindPagePosition(page
);
369 return RemovePage(pagePos
);
374 // Find the position of the wxNotebookPage, -1 if not found.
375 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
377 size_t nPageCount
= GetPageCount();
379 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
380 if (m_pages
[nPage
] == page
)
386 bool wxNotebook::DeleteAllPages()
388 m_tabView
->ClearTabs(true);
390 size_t nPageCount
= GetPageCount();
392 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
393 delete m_pages
[nPage
];
400 // same as AddPage() but does it at given position
401 bool wxNotebook::InsertPage(size_t nPage
,
402 wxNotebookPage
*pPage
,
403 const wxString
& strText
,
405 int WXUNUSED(imageId
))
407 wxASSERT( pPage
!= NULL
);
408 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
410 m_tabView
->AddTab(GetPageId(m_tabView
, pPage
), strText
);
415 // save the pointer to the page
416 m_pages
.Insert(pPage
, nPage
);
420 // This will cause ChangePage to be called, via OnSelPage
422 m_tabView
->SetTabSelection(GetPageId(m_tabView
, pPage
), true);
425 // some page must be selected: either this one or the first one if there is
426 // still no selection
427 if ( m_selection
== -1 )
430 RefreshLayout(false);
435 // ----------------------------------------------------------------------------
436 // wxNotebook callbacks
437 // ----------------------------------------------------------------------------
439 // @@@ OnSize() is used for setting the font when it's called for the first
440 // time because doing it in ::Create() doesn't work (for unknown reasons)
441 void wxNotebook::OnSize(wxSizeEvent
& event
)
443 static bool s_bFirstTime
= true;
444 if ( s_bFirstTime
) {
445 // TODO: any first-time-size processing.
446 s_bFirstTime
= false;
451 // Processing continues to next OnSize
455 // This was supposed to cure the non-display of the notebook
456 // until the user resizes the window.
458 void wxNotebook::OnInternalIdle()
460 wxWindow::OnInternalIdle();
463 static bool s_bFirstTime
= true;
464 if ( s_bFirstTime
) {
466 wxSize sz(GetSize());
474 wxSize sz(GetSize());
475 wxSizeEvent sizeEvent(sz, GetId());
476 sizeEvent.SetEventObject(this);
477 GetEventHandler()->ProcessEvent(sizeEvent);
480 s_bFirstTime
= false;
485 // Implementation: calculate the layout of the view rect
486 // and resize the children if required
487 bool wxNotebook::RefreshLayout(bool force
)
491 wxRect oldRect
= m_tabView
->GetViewRect();
494 GetClientSize(& cw
, & ch
);
496 int tabHeight
= m_tabView
->GetTotalTabHeight();
499 rect
.y
= tabHeight
+ 4;
501 rect
.height
= ch
- 4 - rect
.y
;
503 m_tabView
->SetViewRect(rect
);
505 m_tabView
->LayoutTabs();
507 // Need to do it a 2nd time to get the tab height with
508 // the new view width, since changing the view width changes the
510 tabHeight
= m_tabView
->GetTotalTabHeight();
512 rect
.y
= tabHeight
+ 4;
514 rect
.height
= ch
- 4 - rect
.y
;
516 m_tabView
->SetViewRect(rect
);
518 m_tabView
->LayoutTabs();
520 if (!force
&& (rect
== oldRect
))
523 // fit the notebook page to the tab control's display area
525 size_t nCount
= m_pages
.Count();
526 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
527 wxNotebookPage
*pPage
= m_pages
[nPage
];
528 wxRect clientRect
= GetAvailableClientSize();
529 if (pPage
->IsShown())
531 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
532 if ( pPage
->GetAutoLayout() )
541 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
543 // is it our tab control?
544 if ( event
.GetEventObject() == this )
546 if (event
.GetSelection() != m_selection
)
547 ChangePage(event
.GetOldSelection(), event
.GetSelection());
550 // we want to give others a chance to process this message as well
554 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
556 // set focus to the currently selected page if any
557 if ( m_selection
!= -1 )
558 m_pages
[m_selection
]->SetFocus();
563 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
565 if ( event
.IsWindowChange() )
568 AdvanceSelection(event
.GetDirection());
571 // pass to the parent
574 event
.SetCurrentFocus(this);
575 GetParent()->ProcessWindowEvent(event
);
580 // ----------------------------------------------------------------------------
581 // wxNotebook base class virtuals
582 // ----------------------------------------------------------------------------
584 // override these 2 functions to do nothing: everything is done in OnSize
586 void wxNotebook::SetConstraintSizes(bool /* recurse */)
588 // don't set the sizes of the pages - their correct size is not yet known
589 wxControl::SetConstraintSizes(false);
592 bool wxNotebook::DoPhase(int /* nPhase */)
597 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
599 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
602 // ----------------------------------------------------------------------------
603 // wxNotebook helper functions
604 // ----------------------------------------------------------------------------
606 // hide the currently active panel and show the new one
607 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
609 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
610 wxASSERT( nOldSel
!= nSel
); // impossible
612 if ( nOldSel
!= -1 ) {
613 m_pages
[nOldSel
]->Show(false);
614 m_pages
[nOldSel
]->Lower();
617 wxNotebookPage
*pPage
= m_pages
[nSel
];
619 wxRect clientRect
= GetAvailableClientSize();
620 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
631 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
634 m_tabView
->OnEvent(event
);
637 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
644 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
646 // MBN: since the total tab height is really a function of the
647 // width, this should really call
648 // GetTotalTabHeightPretendingWidthIs(), but the current
649 // implementation will suffice, provided the wxNotebook has been
650 // created with a sensible initial width.
651 return wxSize( sizePage
.x
+ 12,
652 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
655 wxRect
wxNotebook::GetAvailableClientSize()
658 GetClientSize(& cw
, & ch
);
660 int tabHeight
= m_tabView
->GetTotalTabHeight();
662 // TODO: these margins should be configurable.
665 rect
.y
= tabHeight
+ 6;
666 rect
.width
= cw
- 12;
667 rect
.height
= ch
- 4 - rect
.y
;
676 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
678 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
)
679 : wxTabView(style
), m_nextid(1)
681 m_notebook
= notebook
;
683 m_notebook
->SetTabView(this);
685 SetWindow(m_notebook
);
688 wxNotebookTabView::~wxNotebookTabView(void)
692 int wxNotebookTabView::GetId(wxNotebookPage
*page
)
694 int& id
= m_pageToId
[page
];
699 m_idToPage
[id
] = page
;
705 // Called when a tab is activated
706 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
711 wxBookCtrlEvent
event(wxEVT_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
713 // Translate from wxTabView's ids (which aren't position-dependent)
714 // to wxNotebook's (which are).
715 wxNotebookPage
* pActive
= GetPage(activateId
);
716 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
718 int activatePos
= m_notebook
->FindPagePosition(pActive
);
719 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
721 event
.SetEventObject(m_notebook
);
722 event
.SetSelection(activatePos
);
723 event
.SetOldSelection(deactivatePos
);
724 m_notebook
->GetEventHandler()->ProcessEvent(event
);
728 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
734 wxBookCtrlEvent
event(wxEVT_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
736 // Translate from wxTabView's ids (which aren't position-dependent)
737 // to wxNotebook's (which are).
738 wxNotebookPage
* pActive
= GetPage(activateId
);
739 wxNotebookPage
* pDeactive
= GetPage(deactivateId
);
741 int activatePos
= m_notebook
->FindPagePosition(pActive
);
742 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
744 event
.SetEventObject(m_notebook
);
745 event
.SetSelection(activatePos
);
746 event
.SetOldSelection(deactivatePos
);
747 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
749 retval
= event
.IsAllowed();
755 #endif // wxUSE_NOTEBOOK