1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/notebook_osx.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/notebook.h"
18 #include "wx/string.h"
24 #include "wx/string.h"
25 #include "wx/imaglist.h"
26 #include "wx/osx/private.h"
29 // check that the page index is valid
30 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
32 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
33 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
35 EVT_SIZE(wxNotebook::OnSize
)
36 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
37 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
40 bool wxNotebook::Create( wxWindow
*parent
,
45 const wxString
& name
)
49 if (! (style
& wxBK_ALIGN_MASK
))
52 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
55 SetPeer(wxWidgetImpl::CreateTabView(this,parent
, id
, pos
, size
, style
, GetExtraStyle() ));
57 MacPostControlCreate( pos
, size
);
63 wxNotebook::~wxNotebook()
67 // ----------------------------------------------------------------------------
68 // wxNotebook accessors
69 // ----------------------------------------------------------------------------
71 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
76 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
81 void wxNotebook::SetPageSize(const wxSize
& size
)
83 SetSize( CalcSizeFromPage( size
) );
86 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
88 return DoGetSizeFromClientSize( sizePage
);
91 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
93 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
95 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
97 if ( flags
& SetSelection_SendEvent
)
99 if ( !SendPageChangingEvent(nPage
) )
104 //else: program allows the page change
106 SendPageChangedEvent(m_selection
, nPage
);
109 ChangePage(m_selection
, nPage
);
116 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
118 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
120 wxNotebookPage
*page
= m_pages
[nPage
];
121 page
->SetLabel(wxStripMenuCodes(strText
));
127 wxString
wxNotebook::GetPageText(size_t nPage
) const
129 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
131 wxNotebookPage
*page
= m_pages
[nPage
];
133 return page
->GetLabel();
136 int wxNotebook::GetPageImage(size_t nPage
) const
138 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
140 return m_images
[nPage
];
143 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
145 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
146 wxT("SetPageImage: invalid notebook page") );
147 wxCHECK_MSG( HasImageList() && nImage
< GetImageList()->GetImageCount(), false,
148 wxT("SetPageImage: invalid image index") );
150 if ( nImage
!= m_images
[nPage
] )
152 // if the item didn't have an icon before or, on the contrary, did have
153 // it but has lost it now, its size will change - but if the icon just
155 m_images
[nPage
] = nImage
;
163 // ----------------------------------------------------------------------------
164 // wxNotebook operations
165 // ----------------------------------------------------------------------------
167 // remove one page from the notebook, without deleting the window
168 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
170 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
171 wxT("DoRemovePage: invalid notebook page") );
173 wxNotebookPage
* page
= m_pages
[nPage
] ;
174 m_pages
.RemoveAt(nPage
);
175 m_images
.RemoveAt(nPage
);
179 if ( m_selection
>= (int)nPage
)
181 if ( GetPageCount() == 0 )
182 m_selection
= wxNOT_FOUND
;
184 m_selection
= m_selection
? m_selection
- 1 : 0;
186 GetPeer()->SetValue( m_selection
+ 1 ) ;
189 if (m_selection
>= 0)
190 m_pages
[m_selection
]->Show(true);
192 InvalidateBestSize();
198 bool wxNotebook::DeleteAllPages()
200 WX_CLEAR_ARRAY(m_pages
);
203 m_selection
= wxNOT_FOUND
;
204 InvalidateBestSize();
209 // same as AddPage() but does it at given position
210 bool wxNotebook::InsertPage(size_t nPage
,
211 wxNotebookPage
*pPage
,
212 const wxString
& strText
,
216 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
219 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
221 // don't show pages by default (we'll need to adjust their size first)
222 pPage
->Show( false ) ;
224 pPage
->SetLabel( wxStripMenuCodes(strText
) );
226 m_images
.Insert( imageId
, nPage
);
230 wxRect rect
= GetPageRect() ;
231 pPage
->SetSize( rect
);
232 if ( pPage
->GetAutoLayout() )
235 // now deal with the selection
236 // ---------------------------
238 // if the inserted page is before the selected one, we must update the
239 // index of the selected page
241 if ( int(nPage
) <= m_selection
)
245 // while this still is the same page showing, we need to update the tabs
246 GetPeer()->SetValue( m_selection
+ 1 ) ;
249 DoSetSelectionAfterInsertion(nPage
, bSelect
);
251 InvalidateBestSize();
256 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
258 return GetPeer()->TabHitTest(pt
,flags
);
261 // Added by Mark Newsam
262 // When a page is added or deleted to the notebook this function updates
263 // information held in the control so that it matches the order
264 // the user would expect.
266 void wxNotebook::MacSetupTabs()
268 GetPeer()->SetupTabs(*this);
272 wxRect
wxNotebook::GetPageRect() const
274 wxSize size
= GetClientSize() ;
276 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
279 // ----------------------------------------------------------------------------
280 // wxNotebook callbacks
281 // ----------------------------------------------------------------------------
283 // @@@ OnSize() is used for setting the font when it's called for the first
284 // time because doing it in ::Create() doesn't work (for unknown reasons)
285 void wxNotebook::OnSize(wxSizeEvent
& event
)
287 unsigned int nCount
= m_pages
.Count();
288 wxRect rect
= GetPageRect() ;
290 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
292 wxNotebookPage
*pPage
= m_pages
[nPage
];
293 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
296 #if 0 // deactivate r65078 for the moment
297 // If the selected page is hidden at this point, the notebook
298 // has become visible for the first time after creation, and
299 // we postponed showing the page in ChangePage().
300 // So show the selected page now.
301 if ( m_selection
!= wxNOT_FOUND
)
303 wxNotebookPage
*pPage
= m_pages
[m_selection
];
304 if ( !pPage
->IsShown() )
312 // Processing continues to next OnSize
316 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
318 // is it our tab control?
319 if ( event
.GetEventObject() == this )
320 ChangePage(event
.GetOldSelection(), event
.GetSelection());
322 // we want to give others a chance to process this message as well
326 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
328 // set focus to the currently selected page if any
329 if ( m_selection
!= wxNOT_FOUND
)
330 m_pages
[m_selection
]->SetFocus();
335 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
337 if ( event
.IsWindowChange() )
340 AdvanceSelection( event
.GetDirection() );
344 // we get this event in 2 cases
346 // a) one of our pages might have generated it because the user TABbed
347 // out from it in which case we should propagate the event upwards and
348 // our parent will take care of setting the focus to prev/next sibling
352 // b) the parent panel wants to give the focus to us so that we
353 // forward it to our selected page. We can't deal with this in
354 // OnSetFocus() because we don't know which direction the focus came
355 // from in this case and so can't choose between setting the focus to
356 // first or last panel child
357 wxWindow
*parent
= GetParent();
359 // the cast is here to fix a GCC ICE
360 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
362 // no, it doesn't come from child, case (b): forward to a page
363 if ( m_selection
!= wxNOT_FOUND
)
365 // so that the page knows that the event comes from it's parent
366 // and is being propagated downwards
367 event
.SetEventObject( this );
369 wxWindow
*page
= m_pages
[m_selection
];
370 if ( !page
->HandleWindowEvent( event
) )
374 //else: page manages focus inside it itself
378 // we have no pages - still have to give focus to _something_
384 // it comes from our child, case (a), pass to the parent
387 event
.SetCurrentFocus( this );
388 parent
->HandleWindowEvent( event
);
394 // ----------------------------------------------------------------------------
395 // wxNotebook base class virtuals
396 // ----------------------------------------------------------------------------
398 #if wxUSE_CONSTRAINTS
400 // override these 2 functions to do nothing: everything is done in OnSize
402 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
404 // don't set the sizes of the pages - their correct size is not yet known
405 wxControl::SetConstraintSizes( false );
408 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
413 #endif // wxUSE_CONSTRAINTS
415 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
417 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
420 // ----------------------------------------------------------------------------
421 // wxNotebook helper functions
422 // ----------------------------------------------------------------------------
424 // hide the currently active panel and show the new one
425 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
430 if ( nOldSel
!= wxNOT_FOUND
)
431 m_pages
[nOldSel
]->Show( false );
433 if ( nSel
!= wxNOT_FOUND
)
435 wxNotebookPage
*pPage
= m_pages
[nSel
];
436 #if 0 // deactivate r65078 for the moment
437 if ( IsShownOnScreen() )
444 // Postpone Show() until the control is actually shown.
445 // Otherwise this forces the containing toplevel window
446 // to show, even if it's just being created and called
447 // AddPage() without intent to show the window yet.
448 // We Show() the selected page in our OnSize handler,
449 // unless it already is shown.
458 GetPeer()->SetValue( m_selection
+ 1 ) ;
461 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
463 bool status
= false ;
465 SInt32 newSel
= GetPeer()->GetValue() - 1 ;
466 if ( newSel
!= m_selection
)
468 wxBookCtrlEvent
changing(
469 wxEVT_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
470 newSel
, m_selection
);
471 changing
.SetEventObject( this );
472 HandleWindowEvent( changing
);
474 if ( changing
.IsAllowed() )
476 wxBookCtrlEvent
event(
477 wxEVT_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
478 newSel
, m_selection
);
479 event
.SetEventObject( this );
480 HandleWindowEvent( event
);
482 m_selection
= newSel
;
486 GetPeer()->SetValue( m_selection
+ 1 ) ;