1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/notebook_osx.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/notebook.h"
19 #include "wx/string.h"
25 #include "wx/string.h"
26 #include "wx/imaglist.h"
27 #include "wx/osx/private.h"
30 // check that the page index is valid
31 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
33 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
34 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
36 EVT_SIZE(wxNotebook::OnSize
)
37 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
38 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
41 bool wxNotebook::Create( wxWindow
*parent
,
46 const wxString
& name
)
50 if (! (style
& wxBK_ALIGN_MASK
))
53 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
56 SetPeer(wxWidgetImpl::CreateTabView(this,parent
, id
, pos
, size
, style
, GetExtraStyle() ));
58 MacPostControlCreate( pos
, size
);
64 wxNotebook::~wxNotebook()
68 // ----------------------------------------------------------------------------
69 // wxNotebook accessors
70 // ----------------------------------------------------------------------------
72 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
77 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
82 void wxNotebook::SetPageSize(const wxSize
& size
)
84 SetSize( CalcSizeFromPage( size
) );
87 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
89 return DoGetSizeFromClientSize( sizePage
);
92 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
94 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
96 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
98 if ( flags
& SetSelection_SendEvent
)
100 if ( !SendPageChangingEvent(nPage
) )
105 //else: program allows the page change
107 SendPageChangedEvent(m_selection
, nPage
);
110 ChangePage(m_selection
, nPage
);
117 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
119 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
121 wxNotebookPage
*page
= m_pages
[nPage
];
122 page
->SetLabel(wxStripMenuCodes(strText
));
128 wxString
wxNotebook::GetPageText(size_t nPage
) const
130 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
132 wxNotebookPage
*page
= m_pages
[nPage
];
134 return page
->GetLabel();
137 int wxNotebook::GetPageImage(size_t nPage
) const
139 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
141 return m_images
[nPage
];
144 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
146 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
147 wxT("SetPageImage: invalid notebook page") );
148 wxCHECK_MSG( HasImageList() && nImage
< GetImageList()->GetImageCount(), false,
149 wxT("SetPageImage: invalid image index") );
151 if ( nImage
!= m_images
[nPage
] )
153 // if the item didn't have an icon before or, on the contrary, did have
154 // it but has lost it now, its size will change - but if the icon just
156 m_images
[nPage
] = nImage
;
164 // ----------------------------------------------------------------------------
165 // wxNotebook operations
166 // ----------------------------------------------------------------------------
168 // remove one page from the notebook, without deleting the window
169 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
171 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
172 wxT("DoRemovePage: invalid notebook page") );
174 wxNotebookPage
* page
= m_pages
[nPage
] ;
175 m_pages
.RemoveAt(nPage
);
176 m_images
.RemoveAt(nPage
);
180 if ( m_selection
>= (int)nPage
)
182 if ( GetPageCount() == 0 )
183 m_selection
= wxNOT_FOUND
;
185 m_selection
= m_selection
? m_selection
- 1 : 0;
187 GetPeer()->SetValue( m_selection
+ 1 ) ;
190 if (m_selection
>= 0)
191 m_pages
[m_selection
]->Show(true);
193 InvalidateBestSize();
199 bool wxNotebook::DeleteAllPages()
201 WX_CLEAR_ARRAY(m_pages
);
204 m_selection
= wxNOT_FOUND
;
205 InvalidateBestSize();
210 // same as AddPage() but does it at given position
211 bool wxNotebook::InsertPage(size_t nPage
,
212 wxNotebookPage
*pPage
,
213 const wxString
& strText
,
217 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
220 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
222 // don't show pages by default (we'll need to adjust their size first)
223 pPage
->Show( false ) ;
225 pPage
->SetLabel( wxStripMenuCodes(strText
) );
227 m_images
.Insert( imageId
, nPage
);
231 wxRect rect
= GetPageRect() ;
232 pPage
->SetSize( rect
);
233 if ( pPage
->GetAutoLayout() )
236 // now deal with the selection
237 // ---------------------------
239 // if the inserted page is before the selected one, we must update the
240 // index of the selected page
242 if ( int(nPage
) <= m_selection
)
246 // while this still is the same page showing, we need to update the tabs
247 GetPeer()->SetValue( m_selection
+ 1 ) ;
250 DoSetSelectionAfterInsertion(nPage
, bSelect
);
252 InvalidateBestSize();
257 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
259 return GetPeer()->TabHitTest(pt
,flags
);
262 // Added by Mark Newsam
263 // When a page is added or deleted to the notebook this function updates
264 // information held in the control so that it matches the order
265 // the user would expect.
267 void wxNotebook::MacSetupTabs()
269 GetPeer()->SetupTabs(*this);
273 wxRect
wxNotebook::GetPageRect() const
275 wxSize size
= GetClientSize() ;
277 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
280 // ----------------------------------------------------------------------------
281 // wxNotebook callbacks
282 // ----------------------------------------------------------------------------
284 // @@@ OnSize() is used for setting the font when it's called for the first
285 // time because doing it in ::Create() doesn't work (for unknown reasons)
286 void wxNotebook::OnSize(wxSizeEvent
& event
)
288 unsigned int nCount
= m_pages
.Count();
289 wxRect rect
= GetPageRect() ;
291 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
293 wxNotebookPage
*pPage
= m_pages
[nPage
];
294 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
297 #if 0 // deactivate r65078 for the moment
298 // If the selected page is hidden at this point, the notebook
299 // has become visible for the first time after creation, and
300 // we postponed showing the page in ChangePage().
301 // So show the selected page now.
302 if ( m_selection
!= wxNOT_FOUND
)
304 wxNotebookPage
*pPage
= m_pages
[m_selection
];
305 if ( !pPage
->IsShown() )
313 // Processing continues to next OnSize
317 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
319 // is it our tab control?
320 if ( event
.GetEventObject() == this )
321 ChangePage(event
.GetOldSelection(), event
.GetSelection());
323 // we want to give others a chance to process this message as well
327 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
329 // set focus to the currently selected page if any
330 if ( m_selection
!= wxNOT_FOUND
)
331 m_pages
[m_selection
]->SetFocus();
336 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
338 if ( event
.IsWindowChange() )
341 AdvanceSelection( event
.GetDirection() );
345 // we get this event in 2 cases
347 // a) one of our pages might have generated it because the user TABbed
348 // out from it in which case we should propagate the event upwards and
349 // our parent will take care of setting the focus to prev/next sibling
353 // b) the parent panel wants to give the focus to us so that we
354 // forward it to our selected page. We can't deal with this in
355 // OnSetFocus() because we don't know which direction the focus came
356 // from in this case and so can't choose between setting the focus to
357 // first or last panel child
358 wxWindow
*parent
= GetParent();
360 // the cast is here to fix a GCC ICE
361 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
363 // no, it doesn't come from child, case (b): forward to a page
364 if ( m_selection
!= wxNOT_FOUND
)
366 // so that the page knows that the event comes from it's parent
367 // and is being propagated downwards
368 event
.SetEventObject( this );
370 wxWindow
*page
= m_pages
[m_selection
];
371 if ( !page
->HandleWindowEvent( event
) )
375 //else: page manages focus inside it itself
379 // we have no pages - still have to give focus to _something_
385 // it comes from our child, case (a), pass to the parent
388 event
.SetCurrentFocus( this );
389 parent
->HandleWindowEvent( event
);
395 // ----------------------------------------------------------------------------
396 // wxNotebook base class virtuals
397 // ----------------------------------------------------------------------------
399 #if wxUSE_CONSTRAINTS
401 // override these 2 functions to do nothing: everything is done in OnSize
403 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
405 // don't set the sizes of the pages - their correct size is not yet known
406 wxControl::SetConstraintSizes( false );
409 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
414 #endif // wxUSE_CONSTRAINTS
416 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
418 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
421 // ----------------------------------------------------------------------------
422 // wxNotebook helper functions
423 // ----------------------------------------------------------------------------
425 // hide the currently active panel and show the new one
426 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
431 if ( nOldSel
!= wxNOT_FOUND
)
432 m_pages
[nOldSel
]->Show( false );
434 if ( nSel
!= wxNOT_FOUND
)
436 wxNotebookPage
*pPage
= m_pages
[nSel
];
437 #if 0 // deactivate r65078 for the moment
438 if ( IsShownOnScreen() )
445 // Postpone Show() until the control is actually shown.
446 // Otherwise this forces the containing toplevel window
447 // to show, even if it's just being created and called
448 // AddPage() without intent to show the window yet.
449 // We Show() the selected page in our OnSize handler,
450 // unless it already is shown.
459 GetPeer()->SetValue( m_selection
+ 1 ) ;
462 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
464 bool status
= false ;
466 SInt32 newSel
= GetPeer()->GetValue() - 1 ;
467 if ( newSel
!= m_selection
)
469 wxBookCtrlEvent
changing(
470 wxEVT_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
471 newSel
, m_selection
);
472 changing
.SetEventObject( this );
473 HandleWindowEvent( changing
);
475 if ( changing
.IsAllowed() )
477 wxBookCtrlEvent
event(
478 wxEVT_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
479 newSel
, m_selection
);
480 event
.SetEventObject( this );
481 HandleWindowEvent( event
);
483 m_selection
= newSel
;
487 GetPeer()->SetValue( m_selection
+ 1 ) ;