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/string.h"
31 #include "wx/settings.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/notebook.h"
34 #include "wx/dcclient.h"
35 #include "wx/generic/tabg.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // check that the page index is valid
42 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
51 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
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 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
63 // ============================================================================
65 // ============================================================================
67 // ============================================================================
69 // ============================================================================
71 // This reuses wxTabView to draw the tabs.
72 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
74 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
76 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
77 ~wxNotebookTabView(void);
79 // Called when a tab is activated
80 virtual void OnTabActivate(int activateId
, int deactivateId
);
82 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
85 wxNotebook
* m_notebook
;
88 // ----------------------------------------------------------------------------
89 // wxNotebook construction
90 // ----------------------------------------------------------------------------
92 // common part of all ctors
93 void wxNotebook::Init()
95 m_tabView
= (wxNotebookTabView
*) NULL
;
99 // default for dynamic class
100 wxNotebook::wxNotebook()
105 // the same arguments as for wxControl
106 wxNotebook::wxNotebook(wxWindow
*parent
,
111 const wxString
& name
)
115 Create(parent
, id
, pos
, size
, style
, name
);
119 bool wxNotebook::Create(wxWindow
*parent
,
124 const wxString
& name
)
129 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
131 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
134 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
136 SetTabView(new wxNotebookTabView(this));
142 wxNotebook::~wxNotebook()
147 // ----------------------------------------------------------------------------
148 // wxNotebook accessors
149 // ----------------------------------------------------------------------------
150 int wxNotebook::GetRowCount() const
156 int wxNotebook::SetSelection(size_t nPage
)
158 wxASSERT( IS_VALID_PAGE(nPage
) );
160 wxNotebookPage
* pPage
= GetPage(nPage
);
162 m_tabView
->SetTabSelection((int) (long) pPage
);
169 void wxNotebook::AdvanceSelection(bool bForward
)
171 int nSel
= GetSelection();
172 int nMax
= GetPageCount() - 1;
174 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
176 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
180 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
182 wxASSERT( IS_VALID_PAGE(nPage
) );
184 wxNotebookPage
* page
= GetPage(nPage
);
187 m_tabView
->SetTabText((int) (long) page
, strText
);
195 wxString
wxNotebook::GetPageText(size_t nPage
) const
197 wxASSERT( IS_VALID_PAGE(nPage
) );
199 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
201 return m_tabView
->GetTabText((int) (long) page
);
203 return wxEmptyString
;
206 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
208 wxASSERT( IS_VALID_PAGE(nPage
) );
214 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
215 int WXUNUSED(nImage
))
217 wxASSERT( IS_VALID_PAGE(nPage
) );
223 // set the size (the same for all pages)
224 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
229 // set the padding between tabs (in pixels)
230 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
235 // set the size of the tabs for wxNB_FIXEDWIDTH controls
236 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
241 // ----------------------------------------------------------------------------
242 // wxNotebook operations
243 // ----------------------------------------------------------------------------
245 // remove one page from the notebook and delete it
246 bool wxNotebook::DeletePage(size_t nPage
)
248 wxCHECK( IS_VALID_PAGE(nPage
), false );
250 if (m_nSelection
!= -1)
252 m_pages
[m_nSelection
]->Show(false);
253 m_pages
[m_nSelection
]->Lower();
256 wxNotebookPage
* pPage
= GetPage(nPage
);
258 m_tabView
->RemoveTab((int) (long) pPage
);
260 m_pages
.Remove(pPage
);
263 if (m_pages
.GetCount() == 0)
266 m_tabView
->SetTabSelection(-1, false);
268 else if (m_nSelection
> -1)
272 m_tabView
->SetTabSelection((int) (long) GetPage(0), false);
274 if (m_nSelection
!= 0)
278 RefreshLayout(false);
283 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
285 int pagePos
= FindPagePosition(page
);
287 return DeletePage(pagePos
);
292 bool wxNotebook::RemovePage(size_t nPage
)
294 return DoRemovePage(nPage
) != NULL
;
297 // remove one page from the notebook
298 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
300 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
302 m_pages
[nPage
]->Show(false);
303 // m_pages[nPage]->Lower();
305 wxNotebookPage
* pPage
= GetPage(nPage
);
307 m_tabView
->RemoveTab((int) (long) pPage
);
309 m_pages
.Remove(pPage
);
311 if (m_pages
.GetCount() == 0)
314 m_tabView
->SetTabSelection(-1, true);
316 else if (m_nSelection
> -1)
318 // Only change the selection if the page we
319 // deleted was the selection.
320 if (nPage
== (size_t)m_nSelection
)
323 // Select the first tab. Generates a ChangePage.
324 m_tabView
->SetTabSelection(0, true);
328 // We must adjust which tab we think is selected.
329 // If greater than the page we deleted, it must be moved down
331 if (size_t(m_nSelection
) > nPage
)
336 RefreshLayout(false);
341 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
343 int pagePos
= FindPagePosition(page
);
345 return RemovePage(pagePos
);
350 // Find the position of the wxNotebookPage, -1 if not found.
351 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
353 size_t nPageCount
= GetPageCount();
355 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
356 if (m_pages
[nPage
] == page
)
362 bool wxNotebook::DeleteAllPages()
364 m_tabView
->ClearTabs(true);
366 size_t nPageCount
= GetPageCount();
368 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
369 delete m_pages
[nPage
];
376 // same as AddPage() but does it at given position
377 bool wxNotebook::InsertPage(size_t nPage
,
378 wxNotebookPage
*pPage
,
379 const wxString
& strText
,
381 int WXUNUSED(imageId
))
383 wxASSERT( pPage
!= NULL
);
384 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
386 m_tabView
->AddTab((int) (long) pPage
, strText
);
391 // save the pointer to the page
392 m_pages
.Insert(pPage
, nPage
);
396 // This will cause ChangePage to be called, via OnSelPage
398 m_tabView
->SetTabSelection((int) (long) pPage
, true);
401 // some page must be selected: either this one or the first one if there is
402 // still no selection
403 if ( m_nSelection
== -1 )
406 RefreshLayout(false);
411 // ----------------------------------------------------------------------------
412 // wxNotebook callbacks
413 // ----------------------------------------------------------------------------
415 // @@@ OnSize() is used for setting the font when it's called for the first
416 // time because doing it in ::Create() doesn't work (for unknown reasons)
417 void wxNotebook::OnSize(wxSizeEvent
& event
)
419 static bool s_bFirstTime
= true;
420 if ( s_bFirstTime
) {
421 // TODO: any first-time-size processing.
422 s_bFirstTime
= false;
427 // Processing continues to next OnSize
431 // This was supposed to cure the non-display of the notebook
432 // until the user resizes the window.
434 void wxNotebook::OnInternalIdle()
436 wxWindow::OnInternalIdle();
439 static bool s_bFirstTime
= true;
440 if ( s_bFirstTime
) {
442 wxSize sz(GetSize());
450 wxSize sz(GetSize());
451 wxSizeEvent sizeEvent(sz, GetId());
452 sizeEvent.SetEventObject(this);
453 GetEventHandler()->ProcessEvent(sizeEvent);
456 s_bFirstTime
= false;
461 // Implementation: calculate the layout of the view rect
462 // and resize the children if required
463 bool wxNotebook::RefreshLayout(bool force
)
467 wxRect oldRect
= m_tabView
->GetViewRect();
470 GetClientSize(& cw
, & ch
);
472 int tabHeight
= m_tabView
->GetTotalTabHeight();
475 rect
.y
= tabHeight
+ 4;
477 rect
.height
= ch
- 4 - rect
.y
;
479 m_tabView
->SetViewRect(rect
);
481 m_tabView
->LayoutTabs();
483 // Need to do it a 2nd time to get the tab height with
484 // the new view width, since changing the view width changes the
486 tabHeight
= m_tabView
->GetTotalTabHeight();
488 rect
.y
= tabHeight
+ 4;
490 rect
.height
= ch
- 4 - rect
.y
;
492 m_tabView
->SetViewRect(rect
);
494 m_tabView
->LayoutTabs();
496 if (!force
&& (rect
== oldRect
))
499 // fit the notebook page to the tab control's display area
501 size_t nCount
= m_pages
.Count();
502 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
503 wxNotebookPage
*pPage
= m_pages
[nPage
];
504 wxRect clientRect
= GetAvailableClientSize();
505 if (pPage
->IsShown())
507 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
508 if ( pPage
->GetAutoLayout() )
517 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
519 // is it our tab control?
520 if ( event
.GetEventObject() == this )
522 if (event
.GetSelection() != m_nSelection
)
523 ChangePage(event
.GetOldSelection(), event
.GetSelection());
526 // we want to give others a chance to process this message as well
530 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
532 // set focus to the currently selected page if any
533 if ( m_nSelection
!= -1 )
534 m_pages
[m_nSelection
]->SetFocus();
539 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
541 if ( event
.IsWindowChange() ) {
543 AdvanceSelection(event
.GetDirection());
546 // pass to the parent
548 event
.SetCurrentFocus(this);
549 GetParent()->ProcessEvent(event
);
554 // ----------------------------------------------------------------------------
555 // wxNotebook base class virtuals
556 // ----------------------------------------------------------------------------
558 // override these 2 functions to do nothing: everything is done in OnSize
560 void wxNotebook::SetConstraintSizes(bool /* recurse */)
562 // don't set the sizes of the pages - their correct size is not yet known
563 wxControl::SetConstraintSizes(false);
566 bool wxNotebook::DoPhase(int /* nPhase */)
571 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
573 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
576 // ----------------------------------------------------------------------------
577 // wxNotebook helper functions
578 // ----------------------------------------------------------------------------
580 // hide the currently active panel and show the new one
581 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
583 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
584 wxASSERT( nOldSel
!= nSel
); // impossible
586 if ( nOldSel
!= -1 ) {
587 m_pages
[nOldSel
]->Show(false);
588 m_pages
[nOldSel
]->Lower();
591 wxNotebookPage
*pPage
= m_pages
[nSel
];
593 wxRect clientRect
= GetAvailableClientSize();
594 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
605 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
608 m_tabView
->OnEvent(event
);
611 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
618 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
620 // MBN: since the total tab height is really a function of the
621 // width, this should really call
622 // GetTotalTabHeightPretendingWidthIs(), but the current
623 // implementation will suffice, provided the wxNotebook has been
624 // created with a sensible initial width.
625 return wxSize( sizePage
.x
+ 12,
626 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
629 wxRect
wxNotebook::GetAvailableClientSize()
632 GetClientSize(& cw
, & ch
);
634 int tabHeight
= m_tabView
->GetTotalTabHeight();
636 // TODO: these margins should be configurable.
639 rect
.y
= tabHeight
+ 6;
640 rect
.width
= cw
- 12;
641 rect
.height
= ch
- 4 - rect
.y
;
650 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
652 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
654 m_notebook
= notebook
;
656 m_notebook
->SetTabView(this);
658 SetWindow(m_notebook
);
661 wxNotebookTabView::~wxNotebookTabView(void)
665 // Called when a tab is activated
666 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
671 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
673 // Translate from wxTabView's ids (which aren't position-dependent)
674 // to wxNotebook's (which are).
675 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
676 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
678 int activatePos
= m_notebook
->FindPagePosition(pActive
);
679 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
681 event
.SetEventObject(m_notebook
);
682 event
.SetSelection(activatePos
);
683 event
.SetOldSelection(deactivatePos
);
684 m_notebook
->GetEventHandler()->ProcessEvent(event
);
688 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
694 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
696 // Translate from wxTabView's ids (which aren't position-dependent)
697 // to wxNotebook's (which are).
698 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
699 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
701 int activatePos
= m_notebook
->FindPagePosition(pActive
);
702 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
704 event
.SetEventObject(m_notebook
);
705 event
.SetSelection(activatePos
);
706 event
.SetOldSelection(deactivatePos
);
707 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
709 retval
= event
.IsAllowed();
715 #endif // __WXPALMOS__