1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma message disable unscomzer
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
35 #include "wx/settings.h"
36 #include "wx/generic/imaglist.h"
37 #include "wx/notebook.h"
38 #include "wx/dcclient.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
, wxControl
)
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
, wxControl
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
67 // ============================================================================
69 // ============================================================================
71 // ============================================================================
73 // ============================================================================
75 // This reuses wxTabView to draw the tabs.
76 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
78 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
80 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
81 ~wxNotebookTabView(void);
83 // Called when a tab is activated
84 virtual void OnTabActivate(int activateId
, int deactivateId
);
86 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
89 wxNotebook
* m_notebook
;
92 // ----------------------------------------------------------------------------
93 // wxNotebook construction
94 // ----------------------------------------------------------------------------
96 // common part of all ctors
97 void wxNotebook::Init()
99 m_tabView
= (wxNotebookTabView
*) NULL
;
103 // default for dynamic class
104 wxNotebook::wxNotebook()
109 // the same arguments as for wxControl
110 wxNotebook::wxNotebook(wxWindow
*parent
,
115 const wxString
& name
)
119 Create(parent
, id
, pos
, size
, style
, name
);
123 bool wxNotebook::Create(wxWindow
*parent
,
128 const wxString
& name
)
133 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
135 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
138 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
140 SetTabView(new wxNotebookTabView(this));
146 wxNotebook::~wxNotebook()
151 // ----------------------------------------------------------------------------
152 // wxNotebook accessors
153 // ----------------------------------------------------------------------------
154 int wxNotebook::GetRowCount() const
160 int wxNotebook::SetSelection(size_t nPage
)
162 wxASSERT( IS_VALID_PAGE(nPage
) );
164 wxNotebookPage
* pPage
= GetPage(nPage
);
166 m_tabView
->SetTabSelection((int) (long) pPage
);
173 void wxNotebook::AdvanceSelection(bool bForward
)
175 int nSel
= GetSelection();
176 int nMax
= GetPageCount() - 1;
178 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
180 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
184 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
186 wxASSERT( IS_VALID_PAGE(nPage
) );
188 wxNotebookPage
* page
= GetPage(nPage
);
191 m_tabView
->SetTabText((int) (long) page
, strText
);
199 wxString
wxNotebook::GetPageText(size_t nPage
) const
201 wxASSERT( IS_VALID_PAGE(nPage
) );
203 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
205 return m_tabView
->GetTabText((int) (long) page
);
207 return wxEmptyString
;
210 int wxNotebook::GetPageImage(size_t nPage
) const
212 wxASSERT( IS_VALID_PAGE(nPage
) );
218 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
220 wxASSERT( IS_VALID_PAGE(nPage
) );
226 // set the size (the same for all pages)
227 void wxNotebook::SetPageSize(const wxSize
& size
)
232 // set the padding between tabs (in pixels)
233 void wxNotebook::SetPadding(const wxSize
& padding
)
238 // set the size of the tabs for wxNB_FIXEDWIDTH controls
239 void wxNotebook::SetTabSize(const wxSize
& sz
)
244 // ----------------------------------------------------------------------------
245 // wxNotebook operations
246 // ----------------------------------------------------------------------------
248 // remove one page from the notebook and delete it
249 bool wxNotebook::DeletePage(size_t nPage
)
251 wxCHECK( IS_VALID_PAGE(nPage
), false );
253 if (m_nSelection
!= -1)
255 m_pages
[m_nSelection
]->Show(false);
256 m_pages
[m_nSelection
]->Lower();
259 wxNotebookPage
* pPage
= GetPage(nPage
);
261 m_tabView
->RemoveTab((int) (long) pPage
);
263 m_pages
.Remove(pPage
);
266 if (m_pages
.GetCount() == 0)
269 m_tabView
->SetTabSelection(-1, false);
271 else if (m_nSelection
> -1)
275 m_tabView
->SetTabSelection((int) (long) GetPage(0), false);
277 if (m_nSelection
!= 0)
281 RefreshLayout(false);
286 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
288 int pagePos
= FindPagePosition(page
);
290 return DeletePage(pagePos
);
295 bool wxNotebook::RemovePage(size_t nPage
)
297 return DoRemovePage(nPage
) != NULL
;
300 // remove one page from the notebook
301 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
303 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
305 m_pages
[nPage
]->Show(false);
306 // m_pages[nPage]->Lower();
308 wxNotebookPage
* pPage
= GetPage(nPage
);
310 m_tabView
->RemoveTab((int) (long) pPage
);
312 m_pages
.Remove(pPage
);
314 if (m_pages
.GetCount() == 0)
317 m_tabView
->SetTabSelection(-1, true);
319 else if (m_nSelection
> -1)
321 // Only change the selection if the page we
322 // deleted was the selection.
323 if (nPage
== (size_t)m_nSelection
)
326 // Select the first tab. Generates a ChangePage.
327 m_tabView
->SetTabSelection(0, true);
331 // We must adjust which tab we think is selected.
332 // If greater than the page we deleted, it must be moved down
334 if (size_t(m_nSelection
) > nPage
)
339 RefreshLayout(false);
344 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
346 int pagePos
= FindPagePosition(page
);
348 return RemovePage(pagePos
);
353 // Find the position of the wxNotebookPage, -1 if not found.
354 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
356 size_t nPageCount
= GetPageCount();
358 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
359 if (m_pages
[nPage
] == page
)
365 bool wxNotebook::DeleteAllPages()
367 m_tabView
->ClearTabs(true);
369 size_t nPageCount
= GetPageCount();
371 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
372 delete m_pages
[nPage
];
379 // same as AddPage() but does it at given position
380 bool wxNotebook::InsertPage(size_t nPage
,
381 wxNotebookPage
*pPage
,
382 const wxString
& strText
,
386 wxASSERT( pPage
!= NULL
);
387 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
389 m_tabView
->AddTab((int) (long) pPage
, strText
);
394 // save the pointer to the page
395 m_pages
.Insert(pPage
, nPage
);
399 // This will cause ChangePage to be called, via OnSelPage
401 m_tabView
->SetTabSelection((int) (long) pPage
, true);
404 // some page must be selected: either this one or the first one if there is
405 // still no selection
406 if ( m_nSelection
== -1 )
409 RefreshLayout(false);
414 // ----------------------------------------------------------------------------
415 // wxNotebook callbacks
416 // ----------------------------------------------------------------------------
418 // @@@ OnSize() is used for setting the font when it's called for the first
419 // time because doing it in ::Create() doesn't work (for unknown reasons)
420 void wxNotebook::OnSize(wxSizeEvent
& event
)
422 static bool s_bFirstTime
= true;
423 if ( s_bFirstTime
) {
424 // TODO: any first-time-size processing.
425 s_bFirstTime
= false;
430 // Processing continues to next OnSize
434 // This was supposed to cure the non-display of the notebook
435 // until the user resizes the window.
437 void wxNotebook::OnInternalIdle()
439 wxWindow::OnInternalIdle();
442 static bool s_bFirstTime
= true;
443 if ( s_bFirstTime
) {
445 wxSize sz(GetSize());
453 wxSize sz(GetSize());
454 wxSizeEvent sizeEvent(sz, GetId());
455 sizeEvent.SetEventObject(this);
456 GetEventHandler()->ProcessEvent(sizeEvent);
459 s_bFirstTime
= false;
464 // Implementation: calculate the layout of the view rect
465 // and resize the children if required
466 bool wxNotebook::RefreshLayout(bool force
)
470 wxRect oldRect
= m_tabView
->GetViewRect();
473 GetClientSize(& cw
, & ch
);
475 int tabHeight
= m_tabView
->GetTotalTabHeight();
478 rect
.y
= tabHeight
+ 4;
480 rect
.height
= ch
- 4 - rect
.y
;
482 m_tabView
->SetViewRect(rect
);
484 m_tabView
->LayoutTabs();
486 // Need to do it a 2nd time to get the tab height with
487 // the new view width, since changing the view width changes the
489 tabHeight
= m_tabView
->GetTotalTabHeight();
491 rect
.y
= tabHeight
+ 4;
493 rect
.height
= ch
- 4 - rect
.y
;
495 m_tabView
->SetViewRect(rect
);
497 m_tabView
->LayoutTabs();
499 if (!force
&& (rect
== oldRect
))
502 // fit the notebook page to the tab control's display area
504 size_t nCount
= m_pages
.Count();
505 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
506 wxNotebookPage
*pPage
= m_pages
[nPage
];
507 wxRect clientRect
= GetAvailableClientSize();
508 if (pPage
->IsShown())
510 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
511 if ( pPage
->GetAutoLayout() )
520 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
522 // is it our tab control?
523 if ( event
.GetEventObject() == this )
525 if (event
.GetSelection() != m_nSelection
)
526 ChangePage(event
.GetOldSelection(), event
.GetSelection());
529 // we want to give others a chance to process this message as well
533 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
535 // set focus to the currently selected page if any
536 if ( m_nSelection
!= -1 )
537 m_pages
[m_nSelection
]->SetFocus();
542 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
544 if ( event
.IsWindowChange() ) {
546 AdvanceSelection(event
.GetDirection());
549 // pass to the parent
551 event
.SetCurrentFocus(this);
552 GetParent()->ProcessEvent(event
);
557 // ----------------------------------------------------------------------------
558 // wxNotebook base class virtuals
559 // ----------------------------------------------------------------------------
561 // override these 2 functions to do nothing: everything is done in OnSize
563 void wxNotebook::SetConstraintSizes(bool /* recurse */)
565 // don't set the sizes of the pages - their correct size is not yet known
566 wxControl::SetConstraintSizes(false);
569 bool wxNotebook::DoPhase(int /* nPhase */)
574 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
576 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
579 // ----------------------------------------------------------------------------
580 // wxNotebook helper functions
581 // ----------------------------------------------------------------------------
583 // hide the currently active panel and show the new one
584 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
586 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
587 wxASSERT( nOldSel
!= nSel
); // impossible
589 if ( nOldSel
!= -1 ) {
590 m_pages
[nOldSel
]->Show(false);
591 m_pages
[nOldSel
]->Lower();
594 wxNotebookPage
*pPage
= m_pages
[nSel
];
596 wxRect clientRect
= GetAvailableClientSize();
597 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
608 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
611 m_tabView
->OnEvent(event
);
614 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
621 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
623 // MBN: since the total tab height is really a function of the
624 // width, this should really call
625 // GetTotalTabHeightPretendingWidthIs(), but the current
626 // implementation will suffice, provided the wxNotebook has been
627 // created with a sensible initial width.
628 return wxSize( sizePage
.x
+ 12,
629 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
632 wxRect
wxNotebook::GetAvailableClientSize()
635 GetClientSize(& cw
, & ch
);
637 int tabHeight
= m_tabView
->GetTotalTabHeight();
639 // TODO: these margins should be configurable.
642 rect
.y
= tabHeight
+ 6;
643 rect
.width
= cw
- 12;
644 rect
.height
= ch
- 4 - rect
.y
;
653 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
655 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
657 m_notebook
= notebook
;
659 m_notebook
->SetTabView(this);
661 SetWindow(m_notebook
);
664 wxNotebookTabView::~wxNotebookTabView(void)
668 // Called when a tab is activated
669 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
674 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
676 // Translate from wxTabView's ids (which aren't position-dependent)
677 // to wxNotebook's (which are).
678 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
679 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
681 int activatePos
= m_notebook
->FindPagePosition(pActive
);
682 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
684 event
.SetEventObject(m_notebook
);
685 event
.SetSelection(activatePos
);
686 event
.SetOldSelection(deactivatePos
);
687 m_notebook
->GetEventHandler()->ProcessEvent(event
);
691 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
697 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
699 // Translate from wxTabView's ids (which aren't position-dependent)
700 // to wxNotebook's (which are).
701 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
702 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
704 int activatePos
= m_notebook
->FindPagePosition(pActive
);
705 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
707 event
.SetEventObject(m_notebook
);
708 event
.SetSelection(activatePos
);
709 event
.SetOldSelection(deactivatePos
);
710 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
712 retval
= event
.IsAllowed();
718 #endif // __WXPALMOS__