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
)
59 // EVT_IDLE(wxNotebook::OnIdle)
62 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
63 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
65 // ============================================================================
67 // ============================================================================
69 // ============================================================================
71 // ============================================================================
73 // This reuses wxTabView to draw the tabs.
74 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
76 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
78 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
79 ~wxNotebookTabView(void);
81 // Called when a tab is activated
82 virtual void OnTabActivate(int activateId
, int deactivateId
);
84 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
87 wxNotebook
* m_notebook
;
90 // ----------------------------------------------------------------------------
91 // wxNotebook construction
92 // ----------------------------------------------------------------------------
94 // common part of all ctors
95 void wxNotebook::Init()
97 m_tabView
= (wxNotebookTabView
*) NULL
;
101 // default for dynamic class
102 wxNotebook::wxNotebook()
107 // the same arguments as for wxControl
108 wxNotebook::wxNotebook(wxWindow
*parent
,
113 const wxString
& name
)
117 Create(parent
, id
, pos
, size
, style
, name
);
121 bool wxNotebook::Create(wxWindow
*parent
,
126 const wxString
& name
)
131 m_windowId
= id
== -1 ? NewControlId() : id
;
133 // It's like a normal window...
134 if (!wxWindow::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, name
))
137 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
139 SetTabView(new wxNotebookTabView(this));
145 wxNotebook::~wxNotebook()
150 // ----------------------------------------------------------------------------
151 // wxNotebook accessors
152 // ----------------------------------------------------------------------------
153 int wxNotebook::GetRowCount() const
159 int wxNotebook::SetSelection(int nPage
)
164 wxASSERT( IS_VALID_PAGE(nPage
) );
166 #if defined (__WIN16__)
167 m_tabView
->SetTabSelection(nPage
);
169 wxNotebookPage
* pPage
= GetPage(nPage
);
171 m_tabView
->SetTabSelection((int) (long) pPage
);
178 void wxNotebook::AdvanceSelection(bool bForward
)
180 int nSel
= GetSelection();
181 int nMax
= GetPageCount() - 1;
183 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
185 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
189 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
191 wxASSERT( IS_VALID_PAGE(nPage
) );
192 #if defined (__WIN16__)
193 m_tabView
->SetTabText(nPage
, strText
);
197 wxNotebookPage
* page
= GetPage(nPage
);
200 m_tabView
->SetTabText((int) (long) page
, strText
);
208 wxString
wxNotebook::GetPageText(int nPage
) const
210 wxASSERT( IS_VALID_PAGE(nPage
) );
212 #if defined (__WIN16__)
213 return m_tabView
->GetTabText(nPage
);
215 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
217 return m_tabView
->GetTabText((int) (long) page
);
219 return wxEmptyString
;
223 int wxNotebook::GetPageImage(int nPage
) const
225 wxASSERT( IS_VALID_PAGE(nPage
) );
231 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
233 wxASSERT( IS_VALID_PAGE(nPage
) );
239 // set the size (the same for all pages)
240 void wxNotebook::SetPageSize(const wxSize
& size
)
245 // set the padding between tabs (in pixels)
246 void wxNotebook::SetPadding(const wxSize
& padding
)
251 // set the size of the tabs for wxNB_FIXEDWIDTH controls
252 void wxNotebook::SetTabSize(const wxSize
& sz
)
257 // ----------------------------------------------------------------------------
258 // wxNotebook operations
259 // ----------------------------------------------------------------------------
261 // remove one page from the notebook and delete it
262 bool wxNotebook::DeletePage(int nPage
)
264 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
266 if (m_nSelection
!= -1)
268 m_pages
[m_nSelection
]->Show(FALSE
);
269 m_pages
[m_nSelection
]->Lower();
272 wxNotebookPage
* pPage
= GetPage(nPage
);
273 #if defined (__WIN16__)
274 m_tabView
->RemoveTab(nPage
);
276 m_tabView
->RemoveTab((int) (long) pPage
);
279 m_pages
.Remove(pPage
);
282 if (m_pages
.GetCount() == 0)
285 m_tabView
->SetTabSelection(-1, FALSE
);
287 else if (m_nSelection
> -1)
290 #if defined (__WIN16__)
291 m_tabView
->SetTabSelection(0, FALSE
);
293 m_tabView
->SetTabSelection((int) (long) GetPage(0), FALSE
);
295 if (m_nSelection
!= 0)
299 RefreshLayout(FALSE
);
304 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
306 int pagePos
= FindPagePosition(page
);
308 return DeletePage(pagePos
);
313 // remove one page from the notebook
314 bool wxNotebook::RemovePage(int nPage
)
316 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
318 m_pages
[nPage
]->Show(FALSE
);
319 // m_pages[nPage]->Lower();
321 wxNotebookPage
* pPage
= GetPage(nPage
);
322 #if defined (__WIN16__)
323 m_tabView
->RemoveTab(nPage
);
325 m_tabView
->RemoveTab((int) (long) pPage
);
328 m_pages
.Remove(pPage
);
330 if (m_pages
.GetCount() == 0)
333 m_tabView
->SetTabSelection(-1, TRUE
);
335 else if (m_nSelection
> -1)
337 // Only change the selection if the page we
338 // deleted was the selection.
339 if (nPage
== m_nSelection
)
342 // Select the first tab. Generates a ChangePage.
343 m_tabView
->SetTabSelection((int) (long) GetPage(0), TRUE
);
347 // We must adjust which tab we think is selected.
348 // If greater than the page we deleted, it must be moved down
350 if (m_nSelection
> nPage
)
355 RefreshLayout(FALSE
);
360 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
362 int pagePos
= FindPagePosition(page
);
364 return RemovePage(pagePos
);
369 // Find the position of the wxNotebookPage, -1 if not found.
370 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
372 int nPageCount
= GetPageCount();
374 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
375 if (m_pages
[nPage
] == page
)
381 bool wxNotebook::DeleteAllPages()
383 m_tabView
->ClearTabs(TRUE
);
385 int nPageCount
= GetPageCount();
387 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
388 delete m_pages
[nPage
];
395 // same as AddPage() but does it at given position
396 bool wxNotebook::InsertPage(int nPage
,
397 wxNotebookPage
*pPage
,
398 const wxString
& strText
,
402 wxASSERT( pPage
!= NULL
);
403 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
405 // For 16 bit integers (tabs limited to 32768)
406 #if defined (__WIN16__)
407 m_tabView
->AddTab(nPage
, strText
);
409 m_tabView
->AddTab((int) (long) pPage
, strText
);
414 // save the pointer to the page
415 m_pages
.Insert(pPage
, nPage
);
419 // This will cause ChangePage to be called, via OnSelPage
420 #if defined (__WIN16__)
421 m_tabView
->SetTabSelection(nPage
, TRUE
);
423 m_tabView
->SetTabSelection((int) (long) pPage
, TRUE
);
427 // some page must be selected: either this one or the first one if there is
428 // still no selection
429 if ( m_nSelection
== -1 )
432 RefreshLayout(FALSE
);
437 // ----------------------------------------------------------------------------
438 // wxNotebook callbacks
439 // ----------------------------------------------------------------------------
441 // @@@ OnSize() is used for setting the font when it's called for the first
442 // time because doing it in ::Create() doesn't work (for unknown reasons)
443 void wxNotebook::OnSize(wxSizeEvent
& event
)
445 static bool s_bFirstTime
= TRUE
;
446 if ( s_bFirstTime
) {
447 // TODO: any first-time-size processing.
448 s_bFirstTime
= FALSE
;
453 // Processing continues to next OnSize
457 // This was supposed to cure the non-display of the notebook
458 // until the user resizes the window.
460 void wxNotebook::OnIdle(wxIdleEvent
& event
)
462 static bool s_bFirstTime
= TRUE
;
463 if ( s_bFirstTime
) {
465 wxSize sz(GetSize());
473 wxSize sz(GetSize());
474 wxSizeEvent sizeEvent(sz, GetId());
475 sizeEvent.SetEventObject(this);
476 GetEventHandler()->ProcessEvent(sizeEvent);
479 s_bFirstTime
= FALSE
;
484 // Implementation: calculate the layout of the view rect
485 // and resize the children if required
486 bool wxNotebook::RefreshLayout(bool force
)
490 wxRect oldRect
= m_tabView
->GetViewRect();
493 GetClientSize(& cw
, & ch
);
495 int tabHeight
= m_tabView
->GetTotalTabHeight();
498 rect
.y
= tabHeight
+ 4;
500 rect
.height
= ch
- 4 - rect
.y
;
502 m_tabView
->SetViewRect(rect
);
504 m_tabView
->LayoutTabs();
506 // Need to do it a 2nd time to get the tab height with
507 // the new view width, since changing the view width changes the
509 tabHeight
= m_tabView
->GetTotalTabHeight();
511 rect
.y
= tabHeight
+ 4;
513 rect
.height
= ch
- 4 - rect
.y
;
515 m_tabView
->SetViewRect(rect
);
517 m_tabView
->LayoutTabs();
519 if (!force
&& (rect
== oldRect
))
522 // fit the notebook page to the tab control's display area
524 unsigned int nCount
= m_pages
.Count();
525 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
526 wxNotebookPage
*pPage
= m_pages
[nPage
];
527 wxRect clientRect
= GetAvailableClientSize();
528 if (pPage
->IsShown())
530 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
531 if ( pPage
->GetAutoLayout() )
534 // MBN: this is probably just hiding a problem under the carpet,
535 // but: with OpenMotif 2.2 (not Lesstif), not moving the window
536 // may cause the tabs to be not clickable.
539 pPage
->Move(clientRect
.x
, clientRect
.y
);
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();