1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "notebook.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/string.h"
32 #include "wx/settings.h"
33 #include "wx/generic/imaglist.h"
34 #include "wx/notebook.h"
35 #include "wx/dcclient.h"
36 #include "wx/generic/tabg.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // check that the page index is valid
43 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
52 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
53 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
54 EVT_SIZE(wxNotebook::OnSize
)
55 EVT_PAINT(wxNotebook::OnPaint
)
56 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
57 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
58 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
62 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
64 // ============================================================================
66 // ============================================================================
68 // ============================================================================
70 // ============================================================================
72 // This reuses wxTabView to draw the tabs.
73 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
75 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
77 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
78 ~wxNotebookTabView(void);
80 // Called when a tab is activated
81 virtual void OnTabActivate(int activateId
, int deactivateId
);
83 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
86 wxNotebook
* m_notebook
;
89 // ----------------------------------------------------------------------------
90 // wxNotebook construction
91 // ----------------------------------------------------------------------------
93 // common part of all ctors
94 void wxNotebook::Init()
96 m_tabView
= (wxNotebookTabView
*) NULL
;
100 // default for dynamic class
101 wxNotebook::wxNotebook()
106 // the same arguments as for wxControl
107 wxNotebook::wxNotebook(wxWindow
*parent
,
112 const wxString
& name
)
116 Create(parent
, id
, pos
, size
, style
, name
);
120 bool wxNotebook::Create(wxWindow
*parent
,
125 const wxString
& name
)
130 m_windowId
= id
== -1 ? NewControlId() : id
;
132 // It's like a normal window...
133 if (!wxWindow::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, 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 #if defined (__WIN16__)
163 m_tabView
->SetTabSelection(nPage
);
165 wxNotebookPage
* pPage
= GetPage(nPage
);
167 m_tabView
->SetTabSelection((int) (long) pPage
);
174 void wxNotebook::AdvanceSelection(bool bForward
)
176 int nSel
= GetSelection();
177 int nMax
= GetPageCount() - 1;
179 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
181 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
185 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
187 wxASSERT( IS_VALID_PAGE(nPage
) );
188 #if defined (__WIN16__)
189 m_tabView
->SetTabText(nPage
, strText
);
193 wxNotebookPage
* page
= GetPage(nPage
);
196 m_tabView
->SetTabText((int) (long) page
, strText
);
204 wxString
wxNotebook::GetPageText(size_t nPage
) const
206 wxASSERT( IS_VALID_PAGE(nPage
) );
208 #if defined (__WIN16__)
209 return m_tabView
->GetTabText(nPage
);
211 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
213 return m_tabView
->GetTabText((int) (long) page
);
215 return wxEmptyString
;
219 int wxNotebook::GetPageImage(size_t nPage
) const
221 wxASSERT( IS_VALID_PAGE(nPage
) );
227 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
229 wxASSERT( IS_VALID_PAGE(nPage
) );
235 // set the size (the same for all pages)
236 void wxNotebook::SetPageSize(const wxSize
& size
)
241 // set the padding between tabs (in pixels)
242 void wxNotebook::SetPadding(const wxSize
& padding
)
247 // set the size of the tabs for wxNB_FIXEDWIDTH controls
248 void wxNotebook::SetTabSize(const wxSize
& sz
)
253 // ----------------------------------------------------------------------------
254 // wxNotebook operations
255 // ----------------------------------------------------------------------------
257 // remove one page from the notebook and delete it
258 bool wxNotebook::DeletePage(size_t nPage
)
260 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
262 if (m_nSelection
!= -1)
264 m_pages
[m_nSelection
]->Show(FALSE
);
265 m_pages
[m_nSelection
]->Lower();
268 wxNotebookPage
* pPage
= GetPage(nPage
);
269 #if defined (__WIN16__)
270 m_tabView
->RemoveTab(nPage
);
272 m_tabView
->RemoveTab((int) (long) pPage
);
275 m_pages
.Remove(pPage
);
278 if (m_pages
.GetCount() == 0)
281 m_tabView
->SetTabSelection(-1, FALSE
);
283 else if (m_nSelection
> -1)
286 #if defined (__WIN16__)
287 m_tabView
->SetTabSelection(0, FALSE
);
289 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
291 if (m_nSelection
!= 0)
295 RefreshLayout(FALSE
);
300 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
302 int pagePos
= FindPagePosition(page
);
304 return DeletePage(pagePos
);
309 bool wxNotebook::RemovePage(size_t nPage
)
311 return DoRemovePage(nPage
) != NULL
;
314 // remove one page from the notebook
315 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
317 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
319 m_pages
[nPage
]->Show(FALSE
);
320 // m_pages[nPage]->Lower();
322 wxNotebookPage
* pPage
= GetPage(nPage
);
323 #if defined (__WIN16__)
324 m_tabView
->RemoveTab(nPage
);
326 m_tabView
->RemoveTab((int) (long) pPage
);
329 m_pages
.Remove(pPage
);
331 if (m_pages
.GetCount() == 0)
334 m_tabView
->SetTabSelection(-1, TRUE
);
336 else if (m_nSelection
> -1)
338 // Only change the selection if the page we
339 // deleted was the selection.
340 if (nPage
== (size_t)m_nSelection
)
343 // Select the first tab. Generates a ChangePage.
344 m_tabView
->SetTabSelection(0, TRUE
);
348 // We must adjust which tab we think is selected.
349 // If greater than the page we deleted, it must be moved down
351 if (size_t(m_nSelection
) > nPage
)
356 RefreshLayout(FALSE
);
361 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
363 int pagePos
= FindPagePosition(page
);
365 return RemovePage(pagePos
);
370 // Find the position of the wxNotebookPage, -1 if not found.
371 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
373 size_t nPageCount
= GetPageCount();
375 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
376 if (m_pages
[nPage
] == page
)
382 bool wxNotebook::DeleteAllPages()
384 m_tabView
->ClearTabs(TRUE
);
386 size_t nPageCount
= GetPageCount();
388 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
389 delete m_pages
[nPage
];
396 // same as AddPage() but does it at given position
397 bool wxNotebook::InsertPage(size_t nPage
,
398 wxNotebookPage
*pPage
,
399 const wxString
& strText
,
403 wxASSERT( pPage
!= NULL
);
404 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
406 // For 16 bit integers (tabs limited to 32768)
407 #if defined (__WIN16__)
408 m_tabView
->AddTab(nPage
, strText
);
410 m_tabView
->AddTab((int) (long) 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
421 #if defined (__WIN16__)
422 m_tabView
->SetTabSelection(nPage
, TRUE
);
424 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
428 // some page must be selected: either this one or the first one if there is
429 // still no selection
430 if ( m_nSelection
== -1 )
433 RefreshLayout(FALSE
);
438 // ----------------------------------------------------------------------------
439 // wxNotebook callbacks
440 // ----------------------------------------------------------------------------
442 // @@@ OnSize() is used for setting the font when it's called for the first
443 // time because doing it in ::Create() doesn't work (for unknown reasons)
444 void wxNotebook::OnSize(wxSizeEvent
& event
)
446 static bool s_bFirstTime
= TRUE
;
447 if ( s_bFirstTime
) {
448 // TODO: any first-time-size processing.
449 s_bFirstTime
= FALSE
;
454 // Processing continues to next OnSize
458 // This was supposed to cure the non-display of the notebook
459 // until the user resizes the window.
461 void wxNotebook::OnInternalIdle()
463 wxWindow::OnInternalIdle();
466 static bool s_bFirstTime
= TRUE
;
467 if ( s_bFirstTime
) {
469 wxSize sz(GetSize());
477 wxSize sz(GetSize());
478 wxSizeEvent sizeEvent(sz, GetId());
479 sizeEvent.SetEventObject(this);
480 GetEventHandler()->ProcessEvent(sizeEvent);
483 s_bFirstTime
= FALSE
;
488 // Implementation: calculate the layout of the view rect
489 // and resize the children if required
490 bool wxNotebook::RefreshLayout(bool force
)
494 wxRect oldRect
= m_tabView
->GetViewRect();
497 GetClientSize(& cw
, & ch
);
499 int tabHeight
= m_tabView
->GetTotalTabHeight();
502 rect
.y
= tabHeight
+ 4;
504 rect
.height
= ch
- 4 - rect
.y
;
506 m_tabView
->SetViewRect(rect
);
508 m_tabView
->LayoutTabs();
510 // Need to do it a 2nd time to get the tab height with
511 // the new view width, since changing the view width changes the
513 tabHeight
= m_tabView
->GetTotalTabHeight();
515 rect
.y
= tabHeight
+ 4;
517 rect
.height
= ch
- 4 - rect
.y
;
519 m_tabView
->SetViewRect(rect
);
521 m_tabView
->LayoutTabs();
523 if (!force
&& (rect
== oldRect
))
526 // fit the notebook page to the tab control's display area
528 size_t nCount
= m_pages
.Count();
529 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
530 wxNotebookPage
*pPage
= m_pages
[nPage
];
531 wxRect clientRect
= GetAvailableClientSize();
532 if (pPage
->IsShown())
534 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
535 if ( pPage
->GetAutoLayout() )
544 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
546 // is it our tab control?
547 if ( event
.GetEventObject() == this )
549 if (event
.GetSelection() != m_nSelection
)
550 ChangePage(event
.GetOldSelection(), event
.GetSelection());
553 // we want to give others a chance to process this message as well
557 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
559 // set focus to the currently selected page if any
560 if ( m_nSelection
!= -1 )
561 m_pages
[m_nSelection
]->SetFocus();
566 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
568 if ( event
.IsWindowChange() ) {
570 AdvanceSelection(event
.GetDirection());
573 // pass to the parent
575 event
.SetCurrentFocus(this);
576 GetParent()->ProcessEvent(event
);
581 // ----------------------------------------------------------------------------
582 // wxNotebook base class virtuals
583 // ----------------------------------------------------------------------------
585 // override these 2 functions to do nothing: everything is done in OnSize
587 void wxNotebook::SetConstraintSizes(bool /* recurse */)
589 // don't set the sizes of the pages - their correct size is not yet known
590 wxControl::SetConstraintSizes(FALSE
);
593 bool wxNotebook::DoPhase(int /* nPhase */)
598 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
600 wxFAIL_MSG("wxNotebook::Command not implemented");
603 // ----------------------------------------------------------------------------
604 // wxNotebook helper functions
605 // ----------------------------------------------------------------------------
607 // hide the currently active panel and show the new one
608 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
610 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
611 wxASSERT( nOldSel
!= nSel
); // impossible
613 if ( nOldSel
!= -1 ) {
614 m_pages
[nOldSel
]->Show(FALSE
);
615 m_pages
[nOldSel
]->Lower();
618 wxNotebookPage
*pPage
= m_pages
[nSel
];
620 wxRect clientRect
= GetAvailableClientSize();
621 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
632 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
635 m_tabView
->OnEvent(event
);
638 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
645 wxRect
wxNotebook::GetAvailableClientSize()
648 GetClientSize(& cw
, & ch
);
650 int tabHeight
= m_tabView
->GetTotalTabHeight();
652 // TODO: these margins should be configurable.
655 rect
.y
= tabHeight
+ 6;
656 rect
.width
= cw
- 12;
657 rect
.height
= ch
- 4 - rect
.y
;
666 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
668 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
670 m_notebook
= notebook
;
672 m_notebook
->SetTabView(this);
674 SetWindow(m_notebook
);
677 wxNotebookTabView::~wxNotebookTabView(void)
681 // Called when a tab is activated
682 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
687 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
689 #if defined (__WIN16__)
690 int activatePos
= activateId
;
691 int deactivatePos
= deactivateId
;
693 // Translate from wxTabView's ids (which aren't position-dependent)
694 // to wxNotebook's (which are).
695 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
696 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
698 int activatePos
= m_notebook
->FindPagePosition(pActive
);
699 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
702 event
.SetEventObject(m_notebook
);
703 event
.SetSelection(activatePos
);
704 event
.SetOldSelection(deactivatePos
);
705 m_notebook
->GetEventHandler()->ProcessEvent(event
);
709 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
715 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
717 #if defined (__WIN16__)
718 int activatePos
= activateId
;
719 int deactivatePos
= deactivateId
;
721 // Translate from wxTabView's ids (which aren't position-dependent)
722 // to wxNotebook's (which are).
723 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
724 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
726 int activatePos
= m_notebook
->FindPagePosition(pActive
);
727 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
730 event
.SetEventObject(m_notebook
);
731 event
.SetSelection(activatePos
);
732 event
.SetOldSelection(deactivatePos
);
733 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
735 retval
= event
.IsAllowed();