1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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(int 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(int 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(int 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(int nPage
) const
224 wxASSERT( IS_VALID_PAGE(nPage
) );
230 bool wxNotebook::SetPageImage(int 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(int 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 // remove one page from the notebook
313 bool wxNotebook::RemovePage(int nPage
)
315 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
317 m_pages
[nPage
]->Show(FALSE
);
318 // m_pages[nPage]->Lower();
320 wxNotebookPage
* pPage
= GetPage(nPage
);
321 #if defined (__WIN16__)
322 m_tabView
->RemoveTab(nPage
);
324 m_tabView
->RemoveTab((int) (long) pPage
);
327 m_pages
.Remove(pPage
);
329 if (m_pages
.GetCount() == 0)
332 m_tabView
->SetTabSelection(-1, TRUE
);
334 else if (m_nSelection
> -1)
336 // Only change the selection if the page we
337 // deleted was the selection.
338 if (nPage
== m_nSelection
)
341 // Select the first tab. Generates a ChangePage.
342 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
346 // We must adjust which tab we think is selected.
347 // If greater than the page we deleted, it must be moved down
349 if (m_nSelection
> nPage
)
354 RefreshLayout(FALSE
);
359 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
361 int pagePos
= FindPagePosition(page
);
363 return RemovePage(pagePos
);
368 // Find the position of the wxNotebookPage, -1 if not found.
369 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
371 int nPageCount
= GetPageCount();
373 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
374 if (m_pages
[nPage
] == page
)
380 bool wxNotebook::DeleteAllPages()
382 m_tabView
->ClearTabs(TRUE
);
384 int nPageCount
= GetPageCount();
386 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
387 delete m_pages
[nPage
];
394 // same as AddPage() but does it at given position
395 bool wxNotebook::InsertPage(int nPage
,
396 wxNotebookPage
*pPage
,
397 const wxString
& strText
,
401 wxASSERT( pPage
!= NULL
);
402 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
404 // For 16 bit integers (tabs limited to 32768)
405 #if defined (__WIN16__)
406 m_tabView
->AddTab(nPage
, strText
);
408 m_tabView
->AddTab((int) (long) pPage
, strText
);
413 // save the pointer to the page
414 m_pages
.Insert(pPage
, nPage
);
418 // This will cause ChangePage to be called, via OnSelPage
419 #if defined (__WIN16__)
420 m_tabView
->SetTabSelection(nPage
, TRUE
);
422 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
426 // some page must be selected: either this one or the first one if there is
427 // still no selection
428 if ( m_nSelection
== -1 )
431 RefreshLayout(FALSE
);
436 // ----------------------------------------------------------------------------
437 // wxNotebook callbacks
438 // ----------------------------------------------------------------------------
440 // @@@ OnSize() is used for setting the font when it's called for the first
441 // time because doing it in ::Create() doesn't work (for unknown reasons)
442 void wxNotebook::OnSize(wxSizeEvent
& event
)
444 static bool s_bFirstTime
= TRUE
;
445 if ( s_bFirstTime
) {
446 // TODO: any first-time-size processing.
447 s_bFirstTime
= FALSE
;
452 // Processing continues to next OnSize
456 // This was supposed to cure the non-display of the notebook
457 // until the user resizes the window.
459 void wxNotebook::OnInternalIdle()
461 wxWindow::OnInternalIdle();
464 static bool s_bFirstTime
= TRUE
;
465 if ( s_bFirstTime
) {
467 wxSize sz(GetSize());
475 wxSize sz(GetSize());
476 wxSizeEvent sizeEvent(sz, GetId());
477 sizeEvent.SetEventObject(this);
478 GetEventHandler()->ProcessEvent(sizeEvent);
481 s_bFirstTime
= FALSE
;
486 // Implementation: calculate the layout of the view rect
487 // and resize the children if required
488 bool wxNotebook::RefreshLayout(bool force
)
492 wxRect oldRect
= m_tabView
->GetViewRect();
495 GetClientSize(& cw
, & ch
);
497 int tabHeight
= m_tabView
->GetTotalTabHeight();
500 rect
.y
= tabHeight
+ 4;
502 rect
.height
= ch
- 4 - rect
.y
;
504 m_tabView
->SetViewRect(rect
);
506 m_tabView
->LayoutTabs();
508 // Need to do it a 2nd time to get the tab height with
509 // the new view width, since changing the view width changes the
511 tabHeight
= m_tabView
->GetTotalTabHeight();
513 rect
.y
= tabHeight
+ 4;
515 rect
.height
= ch
- 4 - rect
.y
;
517 m_tabView
->SetViewRect(rect
);
519 m_tabView
->LayoutTabs();
521 if (!force
&& (rect
== oldRect
))
524 // fit the notebook page to the tab control's display area
526 unsigned int nCount
= m_pages
.Count();
527 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
528 wxNotebookPage
*pPage
= m_pages
[nPage
];
529 wxRect clientRect
= GetAvailableClientSize();
530 if (pPage
->IsShown())
532 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
533 if ( pPage
->GetAutoLayout() )
536 // MBN: this is probably just hiding a problem under the carpet,
537 // but: with OpenMotif 2.2 (not Lesstif), not moving the window
538 // may cause the tabs to be not clickable.
541 pPage
->Move(clientRect
.x
, clientRect
.y
);
549 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
551 // is it our tab control?
552 if ( event
.GetEventObject() == this )
554 if (event
.GetSelection() != m_nSelection
)
555 ChangePage(event
.GetOldSelection(), event
.GetSelection());
558 // we want to give others a chance to process this message as well
562 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
564 // set focus to the currently selected page if any
565 if ( m_nSelection
!= -1 )
566 m_pages
[m_nSelection
]->SetFocus();
571 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
573 if ( event
.IsWindowChange() ) {
575 AdvanceSelection(event
.GetDirection());
578 // pass to the parent
580 event
.SetCurrentFocus(this);
581 GetParent()->ProcessEvent(event
);
586 // ----------------------------------------------------------------------------
587 // wxNotebook base class virtuals
588 // ----------------------------------------------------------------------------
590 // override these 2 functions to do nothing: everything is done in OnSize
592 void wxNotebook::SetConstraintSizes(bool /* recurse */)
594 // don't set the sizes of the pages - their correct size is not yet known
595 wxControl::SetConstraintSizes(FALSE
);
598 bool wxNotebook::DoPhase(int /* nPhase */)
603 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
605 wxFAIL_MSG("wxNotebook::Command not implemented");
608 // ----------------------------------------------------------------------------
609 // wxNotebook helper functions
610 // ----------------------------------------------------------------------------
612 // hide the currently active panel and show the new one
613 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
615 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
616 wxASSERT( nOldSel
!= nSel
); // impossible
618 if ( nOldSel
!= -1 ) {
619 m_pages
[nOldSel
]->Show(FALSE
);
620 m_pages
[nOldSel
]->Lower();
623 wxNotebookPage
*pPage
= m_pages
[nSel
];
625 wxRect clientRect
= GetAvailableClientSize();
626 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
637 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
640 m_tabView
->OnEvent(event
);
643 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
650 wxRect
wxNotebook::GetAvailableClientSize()
653 GetClientSize(& cw
, & ch
);
655 int tabHeight
= m_tabView
->GetTotalTabHeight();
657 // TODO: these margins should be configurable.
660 rect
.y
= tabHeight
+ 6;
661 rect
.width
= cw
- 12;
662 rect
.height
= ch
- 4 - rect
.y
;
671 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
673 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
675 m_notebook
= notebook
;
677 m_notebook
->SetTabView(this);
679 SetWindow(m_notebook
);
682 wxNotebookTabView::~wxNotebookTabView(void)
686 // Called when a tab is activated
687 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
692 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
694 #if defined (__WIN16__)
695 int activatePos
= activateId
;
696 int deactivatePos
= deactivateId
;
698 // Translate from wxTabView's ids (which aren't position-dependent)
699 // to wxNotebook's (which are).
700 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
701 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
703 int activatePos
= m_notebook
->FindPagePosition(pActive
);
704 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
707 event
.SetEventObject(m_notebook
);
708 event
.SetSelection(activatePos
);
709 event
.SetOldSelection(deactivatePos
);
710 m_notebook
->GetEventHandler()->ProcessEvent(event
);
714 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
720 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
722 #if defined (__WIN16__)
723 int activatePos
= activateId
;
724 int deactivatePos
= deactivateId
;
726 // Translate from wxTabView's ids (which aren't position-dependent)
727 // to wxNotebook's (which are).
728 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
729 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
731 int activatePos
= m_notebook
->FindPagePosition(pActive
);
732 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
735 event
.SetEventObject(m_notebook
);
736 event
.SetSelection(activatePos
);
737 event
.SetOldSelection(deactivatePos
);
738 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
740 retval
= event
.IsAllowed();