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"
24 #pragma message disable unscomzer
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
34 #include "wx/string.h"
36 #include "wx/settings.h"
37 #include "wx/generic/imaglist.h"
38 #include "wx/notebook.h"
39 #include "wx/dcclient.h"
40 #include "wx/generic/tabg.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // check that the page index is valid
47 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
56 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
57 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
58 EVT_SIZE(wxNotebook::OnSize
)
59 EVT_PAINT(wxNotebook::OnPaint
)
60 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
61 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
62 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
68 // ============================================================================
70 // ============================================================================
72 // ============================================================================
74 // ============================================================================
76 // This reuses wxTabView to draw the tabs.
77 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
79 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
81 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
82 ~wxNotebookTabView(void);
84 // Called when a tab is activated
85 virtual void OnTabActivate(int activateId
, int deactivateId
);
87 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
90 wxNotebook
* m_notebook
;
93 // ----------------------------------------------------------------------------
94 // wxNotebook construction
95 // ----------------------------------------------------------------------------
97 // common part of all ctors
98 void wxNotebook::Init()
100 m_tabView
= (wxNotebookTabView
*) NULL
;
104 // default for dynamic class
105 wxNotebook::wxNotebook()
110 // the same arguments as for wxControl
111 wxNotebook::wxNotebook(wxWindow
*parent
,
116 const wxString
& name
)
120 Create(parent
, id
, pos
, size
, style
, name
);
124 bool wxNotebook::Create(wxWindow
*parent
,
129 const wxString
& name
)
134 m_windowId
= id
== -1 ? NewControlId() : id
;
136 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
139 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
141 SetTabView(new wxNotebookTabView(this));
147 wxNotebook::~wxNotebook()
152 // ----------------------------------------------------------------------------
153 // wxNotebook accessors
154 // ----------------------------------------------------------------------------
155 int wxNotebook::GetRowCount() const
161 int wxNotebook::SetSelection(size_t nPage
)
163 wxASSERT( IS_VALID_PAGE(nPage
) );
165 #if defined (__WIN16__)
166 m_tabView
->SetTabSelection(nPage
);
168 wxNotebookPage
* pPage
= GetPage(nPage
);
170 m_tabView
->SetTabSelection((int) (long) pPage
);
177 void wxNotebook::AdvanceSelection(bool bForward
)
179 int nSel
= GetSelection();
180 int nMax
= GetPageCount() - 1;
182 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
184 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
188 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
190 wxASSERT( IS_VALID_PAGE(nPage
) );
191 #if defined (__WIN16__)
192 m_tabView
->SetTabText(nPage
, strText
);
196 wxNotebookPage
* page
= GetPage(nPage
);
199 m_tabView
->SetTabText((int) (long) page
, strText
);
207 wxString
wxNotebook::GetPageText(size_t nPage
) const
209 wxASSERT( IS_VALID_PAGE(nPage
) );
211 #if defined (__WIN16__)
212 return m_tabView
->GetTabText(nPage
);
214 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
216 return m_tabView
->GetTabText((int) (long) page
);
218 return wxEmptyString
;
222 int wxNotebook::GetPageImage(size_t nPage
) const
224 wxASSERT( IS_VALID_PAGE(nPage
) );
230 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
232 wxASSERT( IS_VALID_PAGE(nPage
) );
238 // set the size (the same for all pages)
239 void wxNotebook::SetPageSize(const wxSize
& size
)
244 // set the padding between tabs (in pixels)
245 void wxNotebook::SetPadding(const wxSize
& padding
)
250 // set the size of the tabs for wxNB_FIXEDWIDTH controls
251 void wxNotebook::SetTabSize(const wxSize
& sz
)
256 // ----------------------------------------------------------------------------
257 // wxNotebook operations
258 // ----------------------------------------------------------------------------
260 // remove one page from the notebook and delete it
261 bool wxNotebook::DeletePage(size_t nPage
)
263 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
265 if (m_nSelection
!= -1)
267 m_pages
[m_nSelection
]->Show(FALSE
);
268 m_pages
[m_nSelection
]->Lower();
271 wxNotebookPage
* pPage
= GetPage(nPage
);
272 #if defined (__WIN16__)
273 m_tabView
->RemoveTab(nPage
);
275 m_tabView
->RemoveTab((int) (long) pPage
);
278 m_pages
.Remove(pPage
);
281 if (m_pages
.GetCount() == 0)
284 m_tabView
->SetTabSelection(-1, FALSE
);
286 else if (m_nSelection
> -1)
289 #if defined (__WIN16__)
290 m_tabView
->SetTabSelection(0, FALSE
);
292 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
294 if (m_nSelection
!= 0)
298 RefreshLayout(FALSE
);
303 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
305 int pagePos
= FindPagePosition(page
);
307 return DeletePage(pagePos
);
312 bool wxNotebook::RemovePage(size_t nPage
)
314 return DoRemovePage(nPage
) != NULL
;
317 // remove one page from the notebook
318 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
320 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
322 m_pages
[nPage
]->Show(FALSE
);
323 // m_pages[nPage]->Lower();
325 wxNotebookPage
* pPage
= GetPage(nPage
);
326 #if defined (__WIN16__)
327 m_tabView
->RemoveTab(nPage
);
329 m_tabView
->RemoveTab((int) (long) pPage
);
332 m_pages
.Remove(pPage
);
334 if (m_pages
.GetCount() == 0)
337 m_tabView
->SetTabSelection(-1, TRUE
);
339 else if (m_nSelection
> -1)
341 // Only change the selection if the page we
342 // deleted was the selection.
343 if (nPage
== (size_t)m_nSelection
)
346 // Select the first tab. Generates a ChangePage.
347 m_tabView
->SetTabSelection(0, TRUE
);
351 // We must adjust which tab we think is selected.
352 // If greater than the page we deleted, it must be moved down
354 if (size_t(m_nSelection
) > nPage
)
359 RefreshLayout(FALSE
);
364 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
366 int pagePos
= FindPagePosition(page
);
368 return RemovePage(pagePos
);
373 // Find the position of the wxNotebookPage, -1 if not found.
374 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
376 size_t nPageCount
= GetPageCount();
378 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
379 if (m_pages
[nPage
] == page
)
385 bool wxNotebook::DeleteAllPages()
387 m_tabView
->ClearTabs(TRUE
);
389 size_t nPageCount
= GetPageCount();
391 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
392 delete m_pages
[nPage
];
399 // same as AddPage() but does it at given position
400 bool wxNotebook::InsertPage(size_t nPage
,
401 wxNotebookPage
*pPage
,
402 const wxString
& strText
,
406 wxASSERT( pPage
!= NULL
);
407 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
409 // For 16 bit integers (tabs limited to 32768)
410 #if defined (__WIN16__)
411 m_tabView
->AddTab(nPage
, strText
);
413 m_tabView
->AddTab((int) (long) pPage
, strText
);
418 // save the pointer to the page
419 m_pages
.Insert(pPage
, nPage
);
423 // This will cause ChangePage to be called, via OnSelPage
424 #if defined (__WIN16__)
425 m_tabView
->SetTabSelection(nPage
, TRUE
);
427 m_tabView
->SetTabSelection((int) (long) 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(wxNotebookEvent
& 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("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 wxRect
wxNotebook::GetAvailableClientSize()
651 GetClientSize(& cw
, & ch
);
653 int tabHeight
= m_tabView
->GetTotalTabHeight();
655 // TODO: these margins should be configurable.
658 rect
.y
= tabHeight
+ 6;
659 rect
.width
= cw
- 12;
660 rect
.height
= ch
- 4 - rect
.y
;
669 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
671 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
673 m_notebook
= notebook
;
675 m_notebook
->SetTabView(this);
677 SetWindow(m_notebook
);
680 wxNotebookTabView::~wxNotebookTabView(void)
684 // Called when a tab is activated
685 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
690 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
692 #if defined (__WIN16__)
693 int activatePos
= activateId
;
694 int deactivatePos
= deactivateId
;
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
);
705 event
.SetEventObject(m_notebook
);
706 event
.SetSelection(activatePos
);
707 event
.SetOldSelection(deactivatePos
);
708 m_notebook
->GetEventHandler()->ProcessEvent(event
);
712 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
718 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
720 #if defined (__WIN16__)
721 int activatePos
= activateId
;
722 int deactivatePos
= deactivateId
;
724 // Translate from wxTabView's ids (which aren't position-dependent)
725 // to wxNotebook's (which are).
726 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
727 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
729 int activatePos
= m_notebook
->FindPagePosition(pActive
);
730 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
733 event
.SetEventObject(m_notebook
);
734 event
.SetSelection(activatePos
);
735 event
.SetOldSelection(deactivatePos
);
736 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
738 retval
= event
.IsAllowed();