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 // ----------------------------------------------------------------------------
21 #pragma message disable unscomzer
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
35 #include "wx/settings.h"
36 #include "wx/generic/imaglist.h"
37 #include "wx/notebook.h"
38 #include "wx/dcclient.h"
39 #include "wx/generic/tabg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
55 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
56 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
57 EVT_SIZE(wxNotebook::OnSize
)
58 EVT_PAINT(wxNotebook::OnPaint
)
59 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
67 // ============================================================================
69 // ============================================================================
71 // ============================================================================
73 // ============================================================================
75 // This reuses wxTabView to draw the tabs.
76 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
78 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
80 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
81 ~wxNotebookTabView(void);
83 // Called when a tab is activated
84 virtual void OnTabActivate(int activateId
, int deactivateId
);
86 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
89 wxNotebook
* m_notebook
;
92 // ----------------------------------------------------------------------------
93 // wxNotebook construction
94 // ----------------------------------------------------------------------------
96 // common part of all ctors
97 void wxNotebook::Init()
99 m_tabView
= (wxNotebookTabView
*) NULL
;
103 // default for dynamic class
104 wxNotebook::wxNotebook()
109 // the same arguments as for wxControl
110 wxNotebook::wxNotebook(wxWindow
*parent
,
115 const wxString
& name
)
119 Create(parent
, id
, pos
, size
, style
, name
);
123 bool wxNotebook::Create(wxWindow
*parent
,
128 const wxString
& name
)
133 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
135 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
138 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
140 SetTabView(new wxNotebookTabView(this));
146 wxNotebook::~wxNotebook()
151 // ----------------------------------------------------------------------------
152 // wxNotebook accessors
153 // ----------------------------------------------------------------------------
154 int wxNotebook::GetRowCount() const
160 int wxNotebook::SetSelection(size_t nPage
)
162 wxASSERT( IS_VALID_PAGE(nPage
) );
164 wxNotebookPage
* pPage
= GetPage(nPage
);
166 m_tabView
->SetTabSelection((int) (long) pPage
);
173 void wxNotebook::AdvanceSelection(bool bForward
)
175 int nSel
= GetSelection();
176 int nMax
= GetPageCount() - 1;
178 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
180 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
184 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
186 wxASSERT( IS_VALID_PAGE(nPage
) );
188 wxNotebookPage
* page
= GetPage(nPage
);
191 m_tabView
->SetTabText((int) (long) page
, strText
);
199 wxString
wxNotebook::GetPageText(size_t nPage
) const
201 wxASSERT( IS_VALID_PAGE(nPage
) );
203 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
205 return m_tabView
->GetTabText((int) (long) page
);
207 return wxEmptyString
;
210 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
)) const
212 wxASSERT( IS_VALID_PAGE(nPage
) );
218 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage
),
219 int WXUNUSED(nImage
))
221 wxASSERT( IS_VALID_PAGE(nPage
) );
227 // set the size (the same for all pages)
228 void wxNotebook::SetPageSize(const wxSize
& WXUNUSED(size
))
233 // set the padding between tabs (in pixels)
234 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
239 // set the size of the tabs for wxNB_FIXEDWIDTH controls
240 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
245 // ----------------------------------------------------------------------------
246 // wxNotebook operations
247 // ----------------------------------------------------------------------------
249 // remove one page from the notebook and delete it
250 bool wxNotebook::DeletePage(size_t nPage
)
252 wxCHECK( IS_VALID_PAGE(nPage
), false );
254 if (m_nSelection
!= -1)
256 m_pages
[m_nSelection
]->Show(false);
257 m_pages
[m_nSelection
]->Lower();
260 wxNotebookPage
* pPage
= GetPage(nPage
);
262 m_tabView
->RemoveTab((int) (long) pPage
);
264 m_pages
.Remove(pPage
);
267 if (m_pages
.GetCount() == 0)
270 m_tabView
->SetTabSelection(-1, false);
272 else if (m_nSelection
> -1)
276 m_tabView
->SetTabSelection((int) (long) GetPage(0), false);
278 if (m_nSelection
!= 0)
282 RefreshLayout(false);
287 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
289 int pagePos
= FindPagePosition(page
);
291 return DeletePage(pagePos
);
296 bool wxNotebook::RemovePage(size_t nPage
)
298 return DoRemovePage(nPage
) != NULL
;
301 // remove one page from the notebook
302 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
304 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
306 m_pages
[nPage
]->Show(false);
307 // m_pages[nPage]->Lower();
309 wxNotebookPage
* pPage
= GetPage(nPage
);
311 m_tabView
->RemoveTab((int) (long) pPage
);
313 m_pages
.Remove(pPage
);
315 if (m_pages
.GetCount() == 0)
318 m_tabView
->SetTabSelection(-1, true);
320 else if (m_nSelection
> -1)
322 // Only change the selection if the page we
323 // deleted was the selection.
324 if (nPage
== (size_t)m_nSelection
)
327 // Select the first tab. Generates a ChangePage.
328 m_tabView
->SetTabSelection(0, true);
332 // We must adjust which tab we think is selected.
333 // If greater than the page we deleted, it must be moved down
335 if (size_t(m_nSelection
) > nPage
)
340 RefreshLayout(false);
345 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
347 int pagePos
= FindPagePosition(page
);
349 return RemovePage(pagePos
);
354 // Find the position of the wxNotebookPage, -1 if not found.
355 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
357 size_t nPageCount
= GetPageCount();
359 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
360 if (m_pages
[nPage
] == page
)
366 bool wxNotebook::DeleteAllPages()
368 m_tabView
->ClearTabs(true);
370 size_t nPageCount
= GetPageCount();
372 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
373 delete m_pages
[nPage
];
380 // same as AddPage() but does it at given position
381 bool wxNotebook::InsertPage(size_t nPage
,
382 wxNotebookPage
*pPage
,
383 const wxString
& strText
,
385 int WXUNUSED(imageId
))
387 wxASSERT( pPage
!= NULL
);
388 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
390 m_tabView
->AddTab((int) (long) pPage
, strText
);
395 // save the pointer to the page
396 m_pages
.Insert(pPage
, nPage
);
400 // This will cause ChangePage to be called, via OnSelPage
402 m_tabView
->SetTabSelection((int) (long) pPage
, true);
405 // some page must be selected: either this one or the first one if there is
406 // still no selection
407 if ( m_nSelection
== -1 )
410 RefreshLayout(false);
415 // ----------------------------------------------------------------------------
416 // wxNotebook callbacks
417 // ----------------------------------------------------------------------------
419 // @@@ OnSize() is used for setting the font when it's called for the first
420 // time because doing it in ::Create() doesn't work (for unknown reasons)
421 void wxNotebook::OnSize(wxSizeEvent
& event
)
423 static bool s_bFirstTime
= true;
424 if ( s_bFirstTime
) {
425 // TODO: any first-time-size processing.
426 s_bFirstTime
= false;
431 // Processing continues to next OnSize
435 // This was supposed to cure the non-display of the notebook
436 // until the user resizes the window.
438 void wxNotebook::OnInternalIdle()
440 wxWindow::OnInternalIdle();
443 static bool s_bFirstTime
= true;
444 if ( s_bFirstTime
) {
446 wxSize sz(GetSize());
454 wxSize sz(GetSize());
455 wxSizeEvent sizeEvent(sz, GetId());
456 sizeEvent.SetEventObject(this);
457 GetEventHandler()->ProcessEvent(sizeEvent);
460 s_bFirstTime
= false;
465 // Implementation: calculate the layout of the view rect
466 // and resize the children if required
467 bool wxNotebook::RefreshLayout(bool force
)
471 wxRect oldRect
= m_tabView
->GetViewRect();
474 GetClientSize(& cw
, & ch
);
476 int tabHeight
= m_tabView
->GetTotalTabHeight();
479 rect
.y
= tabHeight
+ 4;
481 rect
.height
= ch
- 4 - rect
.y
;
483 m_tabView
->SetViewRect(rect
);
485 m_tabView
->LayoutTabs();
487 // Need to do it a 2nd time to get the tab height with
488 // the new view width, since changing the view width changes the
490 tabHeight
= m_tabView
->GetTotalTabHeight();
492 rect
.y
= tabHeight
+ 4;
494 rect
.height
= ch
- 4 - rect
.y
;
496 m_tabView
->SetViewRect(rect
);
498 m_tabView
->LayoutTabs();
500 if (!force
&& (rect
== oldRect
))
503 // fit the notebook page to the tab control's display area
505 size_t nCount
= m_pages
.Count();
506 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
507 wxNotebookPage
*pPage
= m_pages
[nPage
];
508 wxRect clientRect
= GetAvailableClientSize();
509 if (pPage
->IsShown())
511 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
512 if ( pPage
->GetAutoLayout() )
521 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
523 // is it our tab control?
524 if ( event
.GetEventObject() == this )
526 if (event
.GetSelection() != m_nSelection
)
527 ChangePage(event
.GetOldSelection(), event
.GetSelection());
530 // we want to give others a chance to process this message as well
534 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
536 // set focus to the currently selected page if any
537 if ( m_nSelection
!= -1 )
538 m_pages
[m_nSelection
]->SetFocus();
543 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
545 if ( event
.IsWindowChange() ) {
547 AdvanceSelection(event
.GetDirection());
550 // pass to the parent
552 event
.SetCurrentFocus(this);
553 GetParent()->ProcessEvent(event
);
558 // ----------------------------------------------------------------------------
559 // wxNotebook base class virtuals
560 // ----------------------------------------------------------------------------
562 // override these 2 functions to do nothing: everything is done in OnSize
564 void wxNotebook::SetConstraintSizes(bool /* recurse */)
566 // don't set the sizes of the pages - their correct size is not yet known
567 wxControl::SetConstraintSizes(false);
570 bool wxNotebook::DoPhase(int /* nPhase */)
575 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
577 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
580 // ----------------------------------------------------------------------------
581 // wxNotebook helper functions
582 // ----------------------------------------------------------------------------
584 // hide the currently active panel and show the new one
585 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
587 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
588 wxASSERT( nOldSel
!= nSel
); // impossible
590 if ( nOldSel
!= -1 ) {
591 m_pages
[nOldSel
]->Show(false);
592 m_pages
[nOldSel
]->Lower();
595 wxNotebookPage
*pPage
= m_pages
[nSel
];
597 wxRect clientRect
= GetAvailableClientSize();
598 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
609 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
612 m_tabView
->OnEvent(event
);
615 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
622 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
624 // MBN: since the total tab height is really a function of the
625 // width, this should really call
626 // GetTotalTabHeightPretendingWidthIs(), but the current
627 // implementation will suffice, provided the wxNotebook has been
628 // created with a sensible initial width.
629 return wxSize( sizePage
.x
+ 12,
630 sizePage
.y
+ m_tabView
->GetTotalTabHeight() + 6 + 4 );
633 wxRect
wxNotebook::GetAvailableClientSize()
636 GetClientSize(& cw
, & ch
);
638 int tabHeight
= m_tabView
->GetTotalTabHeight();
640 // TODO: these margins should be configurable.
643 rect
.y
= tabHeight
+ 6;
644 rect
.width
= cw
- 12;
645 rect
.height
= ch
- 4 - rect
.y
;
654 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
656 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
658 m_notebook
= notebook
;
660 m_notebook
->SetTabView(this);
662 SetWindow(m_notebook
);
665 wxNotebookTabView::~wxNotebookTabView(void)
669 // Called when a tab is activated
670 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
675 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
677 // Translate from wxTabView's ids (which aren't position-dependent)
678 // to wxNotebook's (which are).
679 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
680 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
682 int activatePos
= m_notebook
->FindPagePosition(pActive
);
683 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
685 event
.SetEventObject(m_notebook
);
686 event
.SetSelection(activatePos
);
687 event
.SetOldSelection(deactivatePos
);
688 m_notebook
->GetEventHandler()->ProcessEvent(event
);
692 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
698 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
700 // Translate from wxTabView's ids (which aren't position-dependent)
701 // to wxNotebook's (which are).
702 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
703 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
705 int activatePos
= m_notebook
->FindPagePosition(pActive
);
706 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
708 event
.SetEventObject(m_notebook
);
709 event
.SetSelection(activatePos
);
710 event
.SetOldSelection(deactivatePos
);
711 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
713 retval
= event
.IsAllowed();
719 #endif // __WXPALMOS__