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 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
43 bool wxNotebook::Create( wxWindow
*parent
,
48 const wxString
& name
)
50 m_macIsUserPane
= false ;
52 if (! (style
& wxBK_ALIGN_MASK
))
55 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
58 m_peer
= wxWidgetImpl::CreateTabView(this,parent
, id
, pos
, size
, style
, GetExtraStyle() );
60 MacPostControlCreate( pos
, size
);
66 wxNotebook::~wxNotebook()
70 // ----------------------------------------------------------------------------
71 // wxNotebook accessors
72 // ----------------------------------------------------------------------------
74 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
79 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
84 void wxNotebook::SetPageSize(const wxSize
& size
)
86 SetSize( CalcSizeFromPage( size
) );
89 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
91 return DoGetSizeFromClientSize( sizePage
);
94 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
96 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
98 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
100 if ( flags
& SetSelection_SendEvent
)
102 if ( !SendPageChangingEvent(nPage
) )
107 //else: program allows the page change
109 SendPageChangedEvent(m_selection
, nPage
);
112 ChangePage(m_selection
, nPage
);
119 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
121 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
123 wxNotebookPage
*page
= m_pages
[nPage
];
124 page
->SetLabel(wxStripMenuCodes(strText
));
130 wxString
wxNotebook::GetPageText(size_t nPage
) const
132 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
134 wxNotebookPage
*page
= m_pages
[nPage
];
136 return page
->GetLabel();
139 int wxNotebook::GetPageImage(size_t nPage
) const
141 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
143 return m_images
[nPage
];
146 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
148 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
149 wxT("SetPageImage: invalid notebook page") );
150 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
151 wxT("SetPageImage: invalid image index") );
153 if ( nImage
!= m_images
[nPage
] )
155 // if the item didn't have an icon before or, on the contrary, did have
156 // it but has lost it now, its size will change - but if the icon just
158 m_images
[nPage
] = nImage
;
166 // ----------------------------------------------------------------------------
167 // wxNotebook operations
168 // ----------------------------------------------------------------------------
170 // remove one page from the notebook, without deleting the window
171 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
173 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
174 wxT("DoRemovePage: invalid notebook page") );
176 wxNotebookPage
* page
= m_pages
[nPage
] ;
177 m_pages
.RemoveAt(nPage
);
181 if (m_selection
>= (int)GetPageCount())
182 m_selection
= GetPageCount() - 1;
184 if (m_selection
>= 0)
185 m_pages
[m_selection
]->Show(true);
187 InvalidateBestSize();
193 bool wxNotebook::DeleteAllPages()
195 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 m_peer
->SetValue( m_selection
+ 1 ) ;
243 // some page should be selected: either this one or the first one if there
244 // is still no selection
245 int selNew
= wxNOT_FOUND
;
248 else if ( m_selection
== wxNOT_FOUND
)
251 if ( selNew
!= wxNOT_FOUND
)
252 SetSelection( selNew
);
254 InvalidateBestSize();
259 int wxNotebook::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(flags
)) const
261 int resultV
= wxNOT_FOUND
;
263 const int countPages
= GetPageCount();
265 // we have to convert from Client to Window relative coordinates
266 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
267 // and now to HIView native ones
268 adjustedPt
.x
-= MacGetLeftBorderSize() ;
269 adjustedPt
.y
-= MacGetTopBorderSize() ;
271 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
272 HIViewPartCode outPart
= 0 ;
273 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
275 int max
= m_peer
->GetMaximum() ;
276 if ( outPart
== 0 && max
> 0 )
278 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
279 // so we have to go some extra miles to make sure we select something different
281 int val
= m_peer
->GetValue() ;
285 m_peer
->SetMaximum( 2 ) ;
290 m_peer
->SetValue( maxval
) ;
292 m_peer
->SetValue( 1 ) ;
294 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
296 m_peer
->SetValue( val
) ;
298 m_peer
->SetMaximum( 1 ) ;
301 if ( outPart
>= 1 && outPart
<= countPages
)
302 resultV
= outPart
- 1 ;
308 // we cannot differentiate better
310 *flags
|= wxBK_HITTEST_ONLABEL
;
312 *flags
|= wxBK_HITTEST_NOWHERE
;
318 // Added by Mark Newsam
319 // When a page is added or deleted to the notebook this function updates
320 // information held in the control so that it matches the order
321 // the user would expect.
323 void wxNotebook::MacSetupTabs()
325 m_peer
->SetupTabs(*this);
329 wxRect
wxNotebook::GetPageRect() const
331 wxSize size
= GetClientSize() ;
333 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
336 // ----------------------------------------------------------------------------
337 // wxNotebook callbacks
338 // ----------------------------------------------------------------------------
340 // @@@ OnSize() is used for setting the font when it's called for the first
341 // time because doing it in ::Create() doesn't work (for unknown reasons)
342 void wxNotebook::OnSize(wxSizeEvent
& event
)
344 unsigned int nCount
= m_pages
.Count();
345 wxRect rect
= GetPageRect() ;
347 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
349 wxNotebookPage
*pPage
= m_pages
[nPage
];
350 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
353 // If the selected page is hidden at this point, the notebook
354 // has become visible for the first time after creation, and
355 // we postponed showing the page in ChangePage().
356 // So show the selected page now.
357 if ( m_selection
!= wxNOT_FOUND
)
359 wxNotebookPage
*pPage
= m_pages
[m_selection
];
360 if ( !pPage
->IsShown() )
367 // Processing continues to next OnSize
371 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
373 // is it our tab control?
374 if ( event
.GetEventObject() == this )
375 ChangePage(event
.GetOldSelection(), event
.GetSelection());
377 // we want to give others a chance to process this message as well
381 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
383 // set focus to the currently selected page if any
384 if ( m_selection
!= wxNOT_FOUND
)
385 m_pages
[m_selection
]->SetFocus();
390 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
392 if ( event
.IsWindowChange() )
395 AdvanceSelection( event
.GetDirection() );
399 // we get this event in 2 cases
401 // a) one of our pages might have generated it because the user TABbed
402 // out from it in which case we should propagate the event upwards and
403 // our parent will take care of setting the focus to prev/next sibling
407 // b) the parent panel wants to give the focus to us so that we
408 // forward it to our selected page. We can't deal with this in
409 // OnSetFocus() because we don't know which direction the focus came
410 // from in this case and so can't choose between setting the focus to
411 // first or last panel child
412 wxWindow
*parent
= GetParent();
414 // the cast is here to fix a GCC ICE
415 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
417 // no, it doesn't come from child, case (b): forward to a page
418 if ( m_selection
!= wxNOT_FOUND
)
420 // so that the page knows that the event comes from it's parent
421 // and is being propagated downwards
422 event
.SetEventObject( this );
424 wxWindow
*page
= m_pages
[m_selection
];
425 if ( !page
->HandleWindowEvent( event
) )
429 //else: page manages focus inside it itself
433 // we have no pages - still have to give focus to _something_
439 // it comes from our child, case (a), pass to the parent
442 event
.SetCurrentFocus( this );
443 parent
->HandleWindowEvent( event
);
449 // ----------------------------------------------------------------------------
450 // wxNotebook base class virtuals
451 // ----------------------------------------------------------------------------
453 #if wxUSE_CONSTRAINTS
455 // override these 2 functions to do nothing: everything is done in OnSize
457 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
459 // don't set the sizes of the pages - their correct size is not yet known
460 wxControl::SetConstraintSizes( false );
463 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
468 #endif // wxUSE_CONSTRAINTS
470 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
472 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
475 // ----------------------------------------------------------------------------
476 // wxNotebook helper functions
477 // ----------------------------------------------------------------------------
479 // hide the currently active panel and show the new one
480 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
485 if ( nOldSel
!= wxNOT_FOUND
)
486 m_pages
[nOldSel
]->Show( false );
488 if ( nSel
!= wxNOT_FOUND
)
490 wxNotebookPage
*pPage
= m_pages
[nSel
];
491 if ( IsShownOnScreen() )
498 // Postpone Show() until the control is actually shown.
499 // Otherwise this forces the containing toplevel window
500 // to show, even if it's just being created and called
501 // AddPage() without intent to show the window yet.
502 // We Show() the selected page in our OnSize handler,
503 // unless it already is shown.
508 m_peer
->SetValue( m_selection
+ 1 ) ;
511 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
513 bool status
= false ;
515 SInt32 newSel
= m_peer
->GetValue() - 1 ;
516 if ( newSel
!= m_selection
)
518 wxBookCtrlEvent
changing(
519 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
520 newSel
, m_selection
);
521 changing
.SetEventObject( this );
522 HandleWindowEvent( changing
);
524 if ( changing
.IsAllowed() )
526 wxBookCtrlEvent
event(
527 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
528 newSel
, m_selection
);
529 event
.SetEventObject( this );
530 HandleWindowEvent( event
);
534 m_peer
->SetValue( m_selection
+ 1 ) ;