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( m_imageList
&& nImage
< m_imageList
->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
);
179 if (m_selection
>= (int)GetPageCount())
180 m_selection
= GetPageCount() - 1;
182 if (m_selection
>= 0)
183 m_pages
[m_selection
]->Show(true);
185 InvalidateBestSize();
191 bool wxNotebook::DeleteAllPages()
193 WX_CLEAR_ARRAY(m_pages
) ;
195 m_selection
= wxNOT_FOUND
;
196 InvalidateBestSize();
201 // same as AddPage() but does it at given position
202 bool wxNotebook::InsertPage(size_t nPage
,
203 wxNotebookPage
*pPage
,
204 const wxString
& strText
,
208 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
211 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
213 // don't show pages by default (we'll need to adjust their size first)
214 pPage
->Show( false ) ;
216 pPage
->SetLabel( wxStripMenuCodes(strText
) );
218 m_images
.Insert( imageId
, nPage
);
222 wxRect rect
= GetPageRect() ;
223 pPage
->SetSize( rect
);
224 if ( pPage
->GetAutoLayout() )
227 // now deal with the selection
228 // ---------------------------
230 // if the inserted page is before the selected one, we must update the
231 // index of the selected page
233 if ( int(nPage
) <= m_selection
)
237 // while this still is the same page showing, we need to update the tabs
238 GetPeer()->SetValue( m_selection
+ 1 ) ;
241 DoSetSelectionAfterInsertion(nPage
, bSelect
);
243 InvalidateBestSize();
248 int wxNotebook::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(flags
)) const
250 int resultV
= wxNOT_FOUND
;
252 const int countPages
= GetPageCount();
254 // we have to convert from Client to Window relative coordinates
255 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
256 // and now to HIView native ones
257 adjustedPt
.x
-= MacGetLeftBorderSize() ;
258 adjustedPt
.y
-= MacGetTopBorderSize() ;
260 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
261 HIViewPartCode outPart
= 0 ;
262 OSStatus err
= HIViewGetPartHit( GetPeer()->GetControlRef(), &hipoint
, &outPart
);
264 int max
= GetPeer()->GetMaximum() ;
265 if ( outPart
== 0 && max
> 0 )
267 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
268 // so we have to go some extra miles to make sure we select something different
270 int val
= GetPeer()->GetValue() ;
274 GetPeer()->SetMaximum( 2 ) ;
279 GetPeer()->SetValue( maxval
) ;
281 GetPeer()->SetValue( 1 ) ;
283 err
= HIViewGetPartHit( GetPeer()->GetControlRef(), &hipoint
, &outPart
);
285 GetPeer()->SetValue( val
) ;
287 GetPeer()->SetMaximum( 1 ) ;
290 if ( outPart
>= 1 && outPart
<= countPages
)
291 resultV
= outPart
- 1 ;
297 // we cannot differentiate better
299 *flags
|= wxBK_HITTEST_ONLABEL
;
301 *flags
|= wxBK_HITTEST_NOWHERE
;
307 // Added by Mark Newsam
308 // When a page is added or deleted to the notebook this function updates
309 // information held in the control so that it matches the order
310 // the user would expect.
312 void wxNotebook::MacSetupTabs()
314 GetPeer()->SetupTabs(*this);
318 wxRect
wxNotebook::GetPageRect() const
320 wxSize size
= GetClientSize() ;
322 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
325 // ----------------------------------------------------------------------------
326 // wxNotebook callbacks
327 // ----------------------------------------------------------------------------
329 // @@@ OnSize() is used for setting the font when it's called for the first
330 // time because doing it in ::Create() doesn't work (for unknown reasons)
331 void wxNotebook::OnSize(wxSizeEvent
& event
)
333 unsigned int nCount
= m_pages
.Count();
334 wxRect rect
= GetPageRect() ;
336 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
338 wxNotebookPage
*pPage
= m_pages
[nPage
];
339 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
342 // If the selected page is hidden at this point, the notebook
343 // has become visible for the first time after creation, and
344 // we postponed showing the page in ChangePage().
345 // So show the selected page now.
346 if ( m_selection
!= wxNOT_FOUND
)
348 wxNotebookPage
*pPage
= m_pages
[m_selection
];
349 if ( !pPage
->IsShown() )
356 // Processing continues to next OnSize
360 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
362 // is it our tab control?
363 if ( event
.GetEventObject() == this )
364 ChangePage(event
.GetOldSelection(), event
.GetSelection());
366 // we want to give others a chance to process this message as well
370 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
372 // set focus to the currently selected page if any
373 if ( m_selection
!= wxNOT_FOUND
)
374 m_pages
[m_selection
]->SetFocus();
379 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
381 if ( event
.IsWindowChange() )
384 AdvanceSelection( event
.GetDirection() );
388 // we get this event in 2 cases
390 // a) one of our pages might have generated it because the user TABbed
391 // out from it in which case we should propagate the event upwards and
392 // our parent will take care of setting the focus to prev/next sibling
396 // b) the parent panel wants to give the focus to us so that we
397 // forward it to our selected page. We can't deal with this in
398 // OnSetFocus() because we don't know which direction the focus came
399 // from in this case and so can't choose between setting the focus to
400 // first or last panel child
401 wxWindow
*parent
= GetParent();
403 // the cast is here to fix a GCC ICE
404 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
406 // no, it doesn't come from child, case (b): forward to a page
407 if ( m_selection
!= wxNOT_FOUND
)
409 // so that the page knows that the event comes from it's parent
410 // and is being propagated downwards
411 event
.SetEventObject( this );
413 wxWindow
*page
= m_pages
[m_selection
];
414 if ( !page
->HandleWindowEvent( event
) )
418 //else: page manages focus inside it itself
422 // we have no pages - still have to give focus to _something_
428 // it comes from our child, case (a), pass to the parent
431 event
.SetCurrentFocus( this );
432 parent
->HandleWindowEvent( event
);
438 // ----------------------------------------------------------------------------
439 // wxNotebook base class virtuals
440 // ----------------------------------------------------------------------------
442 #if wxUSE_CONSTRAINTS
444 // override these 2 functions to do nothing: everything is done in OnSize
446 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
448 // don't set the sizes of the pages - their correct size is not yet known
449 wxControl::SetConstraintSizes( false );
452 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
457 #endif // wxUSE_CONSTRAINTS
459 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
461 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
464 // ----------------------------------------------------------------------------
465 // wxNotebook helper functions
466 // ----------------------------------------------------------------------------
468 // hide the currently active panel and show the new one
469 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
474 if ( nOldSel
!= wxNOT_FOUND
)
475 m_pages
[nOldSel
]->Show( false );
477 if ( nSel
!= wxNOT_FOUND
)
479 wxNotebookPage
*pPage
= m_pages
[nSel
];
480 if ( IsShownOnScreen() )
487 // Postpone Show() until the control is actually shown.
488 // Otherwise this forces the containing toplevel window
489 // to show, even if it's just being created and called
490 // AddPage() without intent to show the window yet.
491 // We Show() the selected page in our OnSize handler,
492 // unless it already is shown.
497 GetPeer()->SetValue( m_selection
+ 1 ) ;
500 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
502 bool status
= false ;
504 SInt32 newSel
= GetPeer()->GetValue() - 1 ;
505 if ( newSel
!= m_selection
)
507 wxBookCtrlEvent
changing(
508 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
509 newSel
, m_selection
);
510 changing
.SetEventObject( this );
511 HandleWindowEvent( changing
);
513 if ( changing
.IsAllowed() )
515 wxBookCtrlEvent
event(
516 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
517 newSel
, m_selection
);
518 event
.SetEventObject( this );
519 HandleWindowEvent( event
);
523 GetPeer()->SetValue( m_selection
+ 1 ) ;