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)GetPageCount())
181 m_selection
= GetPageCount() - 1;
183 if (m_selection
>= 0)
184 m_pages
[m_selection
]->Show(true);
186 InvalidateBestSize();
192 bool wxNotebook::DeleteAllPages()
194 WX_CLEAR_ARRAY(m_pages
);
197 m_selection
= wxNOT_FOUND
;
198 InvalidateBestSize();
203 // same as AddPage() but does it at given position
204 bool wxNotebook::InsertPage(size_t nPage
,
205 wxNotebookPage
*pPage
,
206 const wxString
& strText
,
210 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
213 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
215 // don't show pages by default (we'll need to adjust their size first)
216 pPage
->Show( false ) ;
218 pPage
->SetLabel( wxStripMenuCodes(strText
) );
220 m_images
.Insert( imageId
, nPage
);
224 wxRect rect
= GetPageRect() ;
225 pPage
->SetSize( rect
);
226 if ( pPage
->GetAutoLayout() )
229 // now deal with the selection
230 // ---------------------------
232 // if the inserted page is before the selected one, we must update the
233 // index of the selected page
235 if ( int(nPage
) <= m_selection
)
239 // while this still is the same page showing, we need to update the tabs
240 GetPeer()->SetValue( m_selection
+ 1 ) ;
243 DoSetSelectionAfterInsertion(nPage
, bSelect
);
245 InvalidateBestSize();
250 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
252 return GetPeer()->TabHitTest(pt
,flags
);
255 // Added by Mark Newsam
256 // When a page is added or deleted to the notebook this function updates
257 // information held in the control so that it matches the order
258 // the user would expect.
260 void wxNotebook::MacSetupTabs()
262 GetPeer()->SetupTabs(*this);
266 wxRect
wxNotebook::GetPageRect() const
268 wxSize size
= GetClientSize() ;
270 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
273 // ----------------------------------------------------------------------------
274 // wxNotebook callbacks
275 // ----------------------------------------------------------------------------
277 // @@@ OnSize() is used for setting the font when it's called for the first
278 // time because doing it in ::Create() doesn't work (for unknown reasons)
279 void wxNotebook::OnSize(wxSizeEvent
& event
)
281 unsigned int nCount
= m_pages
.Count();
282 wxRect rect
= GetPageRect() ;
284 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
286 wxNotebookPage
*pPage
= m_pages
[nPage
];
287 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
290 #if 0 // deactivate r65078 for the moment
291 // If the selected page is hidden at this point, the notebook
292 // has become visible for the first time after creation, and
293 // we postponed showing the page in ChangePage().
294 // So show the selected page now.
295 if ( m_selection
!= wxNOT_FOUND
)
297 wxNotebookPage
*pPage
= m_pages
[m_selection
];
298 if ( !pPage
->IsShown() )
306 // Processing continues to next OnSize
310 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
312 // is it our tab control?
313 if ( event
.GetEventObject() == this )
314 ChangePage(event
.GetOldSelection(), event
.GetSelection());
316 // we want to give others a chance to process this message as well
320 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
322 // set focus to the currently selected page if any
323 if ( m_selection
!= wxNOT_FOUND
)
324 m_pages
[m_selection
]->SetFocus();
329 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
331 if ( event
.IsWindowChange() )
334 AdvanceSelection( event
.GetDirection() );
338 // we get this event in 2 cases
340 // a) one of our pages might have generated it because the user TABbed
341 // out from it in which case we should propagate the event upwards and
342 // our parent will take care of setting the focus to prev/next sibling
346 // b) the parent panel wants to give the focus to us so that we
347 // forward it to our selected page. We can't deal with this in
348 // OnSetFocus() because we don't know which direction the focus came
349 // from in this case and so can't choose between setting the focus to
350 // first or last panel child
351 wxWindow
*parent
= GetParent();
353 // the cast is here to fix a GCC ICE
354 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
356 // no, it doesn't come from child, case (b): forward to a page
357 if ( m_selection
!= wxNOT_FOUND
)
359 // so that the page knows that the event comes from it's parent
360 // and is being propagated downwards
361 event
.SetEventObject( this );
363 wxWindow
*page
= m_pages
[m_selection
];
364 if ( !page
->HandleWindowEvent( event
) )
368 //else: page manages focus inside it itself
372 // we have no pages - still have to give focus to _something_
378 // it comes from our child, case (a), pass to the parent
381 event
.SetCurrentFocus( this );
382 parent
->HandleWindowEvent( event
);
388 // ----------------------------------------------------------------------------
389 // wxNotebook base class virtuals
390 // ----------------------------------------------------------------------------
392 #if wxUSE_CONSTRAINTS
394 // override these 2 functions to do nothing: everything is done in OnSize
396 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
398 // don't set the sizes of the pages - their correct size is not yet known
399 wxControl::SetConstraintSizes( false );
402 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
407 #endif // wxUSE_CONSTRAINTS
409 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
411 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
414 // ----------------------------------------------------------------------------
415 // wxNotebook helper functions
416 // ----------------------------------------------------------------------------
418 // hide the currently active panel and show the new one
419 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
424 if ( nOldSel
!= wxNOT_FOUND
)
425 m_pages
[nOldSel
]->Show( false );
427 if ( nSel
!= wxNOT_FOUND
)
429 wxNotebookPage
*pPage
= m_pages
[nSel
];
430 #if 0 // deactivate r65078 for the moment
431 if ( IsShownOnScreen() )
438 // Postpone Show() until the control is actually shown.
439 // Otherwise this forces the containing toplevel window
440 // to show, even if it's just being created and called
441 // AddPage() without intent to show the window yet.
442 // We Show() the selected page in our OnSize handler,
443 // unless it already is shown.
452 GetPeer()->SetValue( m_selection
+ 1 ) ;
455 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
457 bool status
= false ;
459 SInt32 newSel
= GetPeer()->GetValue() - 1 ;
460 if ( newSel
!= m_selection
)
462 wxBookCtrlEvent
changing(
463 wxEVT_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
464 newSel
, m_selection
);
465 changing
.SetEventObject( this );
466 HandleWindowEvent( changing
);
468 if ( changing
.IsAllowed() )
470 wxBookCtrlEvent
event(
471 wxEVT_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
472 newSel
, m_selection
);
473 event
.SetEventObject( this );
474 HandleWindowEvent( event
);
476 m_selection
= newSel
;
480 GetPeer()->SetValue( m_selection
+ 1 ) ;