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 #include <wx/string.h>
25 #include <wx/imaglist.h>
26 #include <wx/notebook.h>
27 #include <wx/dcclient.h>
30 #include <wx/motif/private.h>
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // check that the page index is valid
37 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if !USE_SHARED_LIBRARIES
44 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
45 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
46 EVT_SIZE(wxNotebook::OnSize
)
47 EVT_PAINT(wxNotebook::OnPaint
)
48 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxNotebook construction
63 // ----------------------------------------------------------------------------
65 // common part of all ctors
66 void wxNotebook::Init()
68 m_tabView
= (wxNotebookTabView
*) NULL
;
73 // default for dynamic class
74 wxNotebook::wxNotebook()
79 // the same arguments as for wxControl
80 wxNotebook::wxNotebook(wxWindow
*parent
,
89 Create(parent
, id
, pos
, size
, style
, name
);
93 bool wxNotebook::Create(wxWindow
*parent
,
103 m_windowId
= id
== -1 ? NewControlId() : id
;
105 // It's like a normal window...
106 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
109 SetTabView(new wxNotebookTabView(this));
115 wxNotebook::~wxNotebook()
120 // ----------------------------------------------------------------------------
121 // wxNotebook accessors
122 // ----------------------------------------------------------------------------
123 int wxNotebook::GetPageCount() const
125 return m_aPages
.Count();
128 int wxNotebook::GetRowCount() const
134 int wxNotebook::SetSelection(int nPage
)
139 wxASSERT( IS_VALID_PAGE(nPage
) );
141 ChangePage(m_nSelection
, nPage
);
147 void wxNotebook::AdvanceSelection(bool bForward
)
149 int nSel
= GetSelection();
150 int nMax
= GetPageCount() - 1;
152 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
154 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
157 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
159 wxASSERT( IS_VALID_PAGE(nPage
) );
165 wxString
wxNotebook::GetPageText(int nPage
) const
167 wxASSERT( IS_VALID_PAGE(nPage
) );
173 int wxNotebook::GetPageImage(int nPage
) const
175 wxASSERT( IS_VALID_PAGE(nPage
) );
181 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
183 wxASSERT( IS_VALID_PAGE(nPage
) );
189 void wxNotebook::SetImageList(wxImageList
* imageList
)
191 m_pImageList
= imageList
;
195 // ----------------------------------------------------------------------------
196 // wxNotebook operations
197 // ----------------------------------------------------------------------------
199 // remove one page from the notebook
200 bool wxNotebook::DeletePage(int nPage
)
202 wxFAIL_MSG("Sorry, DeletePage not implemented for Motif wxNotebook because wxTabView doesn't support it.");
206 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
208 // TODO: delete native widget page
210 delete m_aPages[nPage];
211 m_aPages.Remove(nPage);
218 bool wxNotebook::DeleteAllPages()
220 m_tabView
->ClearTabs(TRUE
);
222 int nPageCount
= GetPageCount();
224 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
225 delete m_aPages
[nPage
];
232 // add a page to the notebook
233 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
234 const wxString
& strText
,
238 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
241 // same as AddPage() but does it at given position
242 bool wxNotebook::InsertPage(int nPage
,
243 wxNotebookPage
*pPage
,
244 const wxString
& strText
,
248 wxASSERT( pPage
!= NULL
);
249 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
251 m_tabView
->AddTab(nPage
, strText
);
256 m_tabView->SetTabSelection(nPage, TRUE);
259 // save the pointer to the page
260 m_aPages
.Insert(pPage
, nPage
);
262 // some page must be selected: either this one or the first one if there is
263 // still no selection
265 m_nSelection
= nPage
;
266 else if ( m_nSelection
== -1 )
272 // ----------------------------------------------------------------------------
273 // wxNotebook callbacks
274 // ----------------------------------------------------------------------------
276 // @@@ OnSize() is used for setting the font when it's called for the first
277 // time because doing it in ::Create() doesn't work (for unknown reasons)
278 void wxNotebook::OnSize(wxSizeEvent
& event
)
280 static bool s_bFirstTime
= TRUE
;
281 if ( s_bFirstTime
) {
282 // TODO: any first-time-size processing.
283 s_bFirstTime
= FALSE
;
289 GetClientSize(& cw
, & ch
);
291 int tabHeight
= m_tabView
->GetTotalTabHeight();
294 rect
.y
= tabHeight
+ 4;
296 rect
.height
= ch
- 4 - rect
.y
;
298 m_tabView
->SetViewRect(rect
);
302 // Need to do it a 2nd time to get the tab height with
303 // the new view width, since changing the view width changes the
305 tabHeight
= m_tabView
->GetTotalTabHeight();
307 rect
.y
= tabHeight
+ 4;
309 rect
.height
= ch
- 4 - rect
.y
;
311 m_tabView
->SetViewRect(rect
);
315 // emulate page change (it's esp. important to do it first time because
316 // otherwise our page would stay invisible)
317 int nSel
= m_nSelection
;
321 // fit the notebook page to the tab control's display area
323 unsigned int nCount
= m_aPages
.Count();
324 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
325 wxNotebookPage
*pPage
= m_aPages
[nPage
];
326 if (pPage
->IsShown())
328 wxRect clientRect
= GetAvailableClientSize();
329 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
330 if ( pPage
->GetAutoLayout() )
337 // Processing continues to next OnSize
341 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
343 // is it our tab control?
344 if ( event
.GetEventObject() == this )
345 ChangePage(event
.GetOldSelection(), event
.GetSelection());
347 // we want to give others a chance to process this message as well
351 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
353 // set focus to the currently selected page if any
354 if ( m_nSelection
!= -1 )
355 m_aPages
[m_nSelection
]->SetFocus();
360 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
362 if ( event
.IsWindowChange() ) {
364 AdvanceSelection(event
.GetDirection());
367 // pass to the parent
369 event
.SetCurrentFocus(this);
370 GetParent()->ProcessEvent(event
);
375 // ----------------------------------------------------------------------------
376 // wxNotebook base class virtuals
377 // ----------------------------------------------------------------------------
379 // override these 2 functions to do nothing: everything is done in OnSize
381 void wxNotebook::SetConstraintSizes(bool /* recurse */)
383 // don't set the sizes of the pages - their correct size is not yet known
384 wxControl::SetConstraintSizes(FALSE
);
387 bool wxNotebook::DoPhase(int /* nPhase */)
392 void wxNotebook::Command(wxCommandEvent
& event
)
394 wxFAIL_MSG("wxNotebook::Command not implemented");
397 // ----------------------------------------------------------------------------
398 // wxNotebook helper functions
399 // ----------------------------------------------------------------------------
401 // hide the currently active panel and show the new one
402 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
404 wxASSERT( nOldSel
!= nSel
); // impossible
406 if ( nOldSel
!= -1 ) {
407 m_aPages
[nOldSel
]->Show(FALSE
);
408 m_aPages
[nOldSel
]->Lower();
411 wxNotebookPage
*pPage
= m_aPages
[nSel
];
413 wxRect clientRect
= GetAvailableClientSize();
414 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
425 void wxNotebook::ChangeFont(bool keepOriginalSize
)
427 wxWindow::ChangeFont(keepOriginalSize
);
430 void wxNotebook::ChangeBackgroundColour()
432 wxWindow::ChangeBackgroundColour();
435 void wxNotebook::ChangeForegroundColour()
437 wxWindow::ChangeForegroundColour();
440 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
443 m_tabView
->OnEvent(event
);
446 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
453 wxRect
wxNotebook::GetAvailableClientSize()
456 GetClientSize(& cw
, & ch
);
458 int tabHeight
= m_tabView
->GetTotalTabHeight();
460 // TODO: these margins should be configurable.
463 rect
.y
= tabHeight
+ 6;
464 rect
.width
= cw
- 12;
465 rect
.height
= ch
- 4 - rect
.y
;
474 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
476 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
478 m_notebook
= notebook
;
480 m_notebook
->SetTabView(this);
482 SetWindow(m_notebook
);
485 wxNotebookTabView::~wxNotebookTabView(void)
489 // Called when a tab is activated
490 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
495 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
496 event
.SetEventObject(m_notebook
);
497 event
.SetSelection(activateId
);
498 event
.SetOldSelection(deactivateId
);
499 m_notebook
->GetEventHandler()->ProcessEvent(event
);
502 wxWindow *oldWindow = ((deactivateId == -1) ? 0 : m_notebook->GetPage(deactivateId));
503 wxWindow *newWindow = m_notebook->GetPage(activateId);
507 oldWindow->Show(FALSE);
512 newWindow->Show(TRUE);
516 m_notebook->GetClientSize(& cw, & ch);
518 int tabHeight = GetTotalTabHeight();
521 rect.y = tabHeight + 4;
523 rect.height = ch - 4 - rect.y ;
525 newWindow->SetSize(rect.x + 2, rect.y + 2, rect.width - 2, rect.height - 2);
526 newWindow->Refresh();
529 // TODO: only refresh the tab area.
530 m_notebook->Refresh();
535 void wxNotebookTabView::AddTabWindow(int id
, wxWindow
*window
)
537 m_tabWindows
.Append((long)id
, window
);
541 wxWindow
*wxNotebookTabView::GetTabWindow(int id
) const
543 wxNode
*node
= m_tabWindows
.Find((long)id
);
545 return (wxWindow
*) NULL
;
546 return (wxWindow
*)node
->Data();
549 void wxNotebookTabView::ClearWindows(bool deleteWindows
)
552 m_tabWindows
.DeleteContents(TRUE
);
553 m_tabWindows
.Clear();
554 m_tabWindows
.DeleteContents(FALSE
);
557 void wxNotebookTabView::ShowWindowForTab(int id
)
559 wxWindow
*newWindow
= GetTabWindow(id
);
560 if (newWindow
== m_currentWindow
)
563 m_currentWindow
->Show(FALSE
);
564 newWindow
->Show(TRUE
);
565 newWindow
->Refresh();