1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/notebook_osx.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: notebmac.cpp 55079 2008-08-13 14:56:42Z PC $
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
)
44 // common part of all ctors
45 void wxNotebook::Init()
50 // default for dynamic class
51 wxNotebook::wxNotebook()
56 // the same arguments as for wxControl
57 wxNotebook::wxNotebook( wxWindow
*parent
,
62 const wxString
& name
)
66 Create( parent
, id
, pos
, size
, style
, name
);
69 bool wxNotebook::Create( wxWindow
*parent
,
74 const wxString
& name
)
76 m_macIsUserPane
= false ;
78 if (! (style
& wxBK_ALIGN_MASK
))
81 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
84 m_peer
= wxWidgetImpl::CreateTabView(this,parent
, id
, pos
, size
, style
, GetExtraStyle() );
86 MacPostControlCreate( pos
, size
);
92 wxNotebook::~wxNotebook()
96 // ----------------------------------------------------------------------------
97 // wxNotebook accessors
98 // ----------------------------------------------------------------------------
100 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
105 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
110 void wxNotebook::SetPageSize(const wxSize
& size
)
112 SetSize( CalcSizeFromPage( size
) );
115 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
117 return DoGetSizeFromClientSize( sizePage
);
120 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
122 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
124 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
126 if ( flags
& SetSelection_SendEvent
)
128 if ( !SendPageChangingEvent(nPage
) )
133 //else: program allows the page change
135 SendPageChangedEvent(m_nSelection
, nPage
);
138 ChangePage(m_nSelection
, nPage
);
145 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
147 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
149 wxNotebookPage
*page
= m_pages
[nPage
];
150 page
->SetLabel(wxStripMenuCodes(strText
));
156 wxString
wxNotebook::GetPageText(size_t nPage
) const
158 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
160 wxNotebookPage
*page
= m_pages
[nPage
];
162 return page
->GetLabel();
165 int wxNotebook::GetPageImage(size_t nPage
) const
167 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
169 return m_images
[nPage
];
172 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
174 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
175 wxT("SetPageImage: invalid notebook page") );
176 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
177 wxT("SetPageImage: invalid image index") );
179 if ( nImage
!= m_images
[nPage
] )
181 // if the item didn't have an icon before or, on the contrary, did have
182 // it but has lost it now, its size will change - but if the icon just
184 m_images
[nPage
] = nImage
;
192 // ----------------------------------------------------------------------------
193 // wxNotebook operations
194 // ----------------------------------------------------------------------------
196 // remove one page from the notebook, without deleting the window
197 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
199 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
200 wxT("DoRemovePage: invalid notebook page") );
202 wxNotebookPage
* page
= m_pages
[nPage
] ;
203 m_pages
.RemoveAt(nPage
);
207 if (m_nSelection
>= (int)GetPageCount())
208 m_nSelection
= GetPageCount() - 1;
210 if (m_nSelection
>= 0)
211 m_pages
[m_nSelection
]->Show(true);
213 InvalidateBestSize();
219 bool wxNotebook::DeleteAllPages()
221 WX_CLEAR_ARRAY(m_pages
) ;
224 InvalidateBestSize();
229 // same as AddPage() but does it at given position
230 bool wxNotebook::InsertPage(size_t nPage
,
231 wxNotebookPage
*pPage
,
232 const wxString
& strText
,
236 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
239 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
241 // don't show pages by default (we'll need to adjust their size first)
242 pPage
->Show( false ) ;
244 pPage
->SetLabel( wxStripMenuCodes(strText
) );
246 m_images
.Insert( imageId
, nPage
);
250 wxRect rect
= GetPageRect() ;
251 pPage
->SetSize( rect
);
252 if ( pPage
->GetAutoLayout() )
255 // now deal with the selection
256 // ---------------------------
258 // if the inserted page is before the selected one, we must update the
259 // index of the selected page
261 if ( int(nPage
) <= m_nSelection
)
265 // while this still is the same page showing, we need to update the tabs
266 m_peer
->SetValue( m_nSelection
+ 1 ) ;
269 // some page should be selected: either this one or the first one if there
270 // is still no selection
274 else if ( m_nSelection
== -1 )
278 SetSelection( selNew
);
280 InvalidateBestSize();
285 int wxNotebook::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(flags
)) const
287 int resultV
= wxNOT_FOUND
;
289 const int countPages
= GetPageCount();
291 // we have to convert from Client to Window relative coordinates
292 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
293 // and now to HIView native ones
294 adjustedPt
.x
-= MacGetLeftBorderSize() ;
295 adjustedPt
.y
-= MacGetTopBorderSize() ;
297 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
298 HIViewPartCode outPart
= 0 ;
299 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
301 int max
= m_peer
->GetMaximum() ;
302 if ( outPart
== 0 && max
> 0 )
304 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
305 // so we have to go some extra miles to make sure we select something different
307 int val
= m_peer
->GetValue() ;
311 m_peer
->SetMaximum( 2 ) ;
316 m_peer
->SetValue( maxval
) ;
318 m_peer
->SetValue( 1 ) ;
320 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
322 m_peer
->SetValue( val
) ;
324 m_peer
->SetMaximum( 1 ) ;
327 if ( outPart
>= 1 && outPart
<= countPages
)
328 resultV
= outPart
- 1 ;
334 // we cannot differentiate better
336 *flags
|= wxBK_HITTEST_ONLABEL
;
338 *flags
|= wxBK_HITTEST_NOWHERE
;
344 // Added by Mark Newsam
345 // When a page is added or deleted to the notebook this function updates
346 // information held in the control so that it matches the order
347 // the user would expect.
349 void wxNotebook::MacSetupTabs()
351 m_peer
->SetupTabs(*this);
355 wxRect
wxNotebook::GetPageRect() const
357 wxSize size
= GetClientSize() ;
359 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
362 // ----------------------------------------------------------------------------
363 // wxNotebook callbacks
364 // ----------------------------------------------------------------------------
366 // @@@ OnSize() is used for setting the font when it's called for the first
367 // time because doing it in ::Create() doesn't work (for unknown reasons)
368 void wxNotebook::OnSize(wxSizeEvent
& event
)
370 unsigned int nCount
= m_pages
.Count();
371 wxRect rect
= GetPageRect() ;
373 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
375 wxNotebookPage
*pPage
= m_pages
[nPage
];
376 pPage
->SetSize(rect
);
377 if ( pPage
->GetAutoLayout() )
381 // If the selected page is hidden at this point, the notebook
382 // has become visible for the first time after creation, and
383 // we postponed showing the page in ChangePage().
384 // So show the selected page now.
385 if ( m_nSelection
!= -1 )
387 wxNotebookPage
*pPage
= m_pages
[m_nSelection
];
388 if ( !pPage
->IsShown() )
395 // Processing continues to next OnSize
399 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
401 // is it our tab control?
402 if ( event
.GetEventObject() == this )
403 ChangePage(event
.GetOldSelection(), event
.GetSelection());
405 // we want to give others a chance to process this message as well
409 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
411 // set focus to the currently selected page if any
412 if ( m_nSelection
!= -1 )
413 m_pages
[m_nSelection
]->SetFocus();
418 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
420 if ( event
.IsWindowChange() )
423 AdvanceSelection( event
.GetDirection() );
427 // we get this event in 2 cases
429 // a) one of our pages might have generated it because the user TABbed
430 // out from it in which case we should propagate the event upwards and
431 // our parent will take care of setting the focus to prev/next sibling
435 // b) the parent panel wants to give the focus to us so that we
436 // forward it to our selected page. We can't deal with this in
437 // OnSetFocus() because we don't know which direction the focus came
438 // from in this case and so can't choose between setting the focus to
439 // first or last panel child
440 wxWindow
*parent
= GetParent();
442 // the cast is here to fix a GCC ICE
443 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
445 // no, it doesn't come from child, case (b): forward to a page
446 if ( m_nSelection
!= -1 )
448 // so that the page knows that the event comes from it's parent
449 // and is being propagated downwards
450 event
.SetEventObject( this );
452 wxWindow
*page
= m_pages
[m_nSelection
];
453 if ( !page
->HandleWindowEvent( event
) )
457 //else: page manages focus inside it itself
461 // we have no pages - still have to give focus to _something_
467 // it comes from our child, case (a), pass to the parent
470 event
.SetCurrentFocus( this );
471 parent
->HandleWindowEvent( event
);
477 // ----------------------------------------------------------------------------
478 // wxNotebook base class virtuals
479 // ----------------------------------------------------------------------------
481 #if wxUSE_CONSTRAINTS
483 // override these 2 functions to do nothing: everything is done in OnSize
485 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
487 // don't set the sizes of the pages - their correct size is not yet known
488 wxControl::SetConstraintSizes( false );
491 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
496 #endif // wxUSE_CONSTRAINTS
498 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
500 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
503 // ----------------------------------------------------------------------------
504 // wxNotebook helper functions
505 // ----------------------------------------------------------------------------
507 // hide the currently active panel and show the new one
508 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
514 m_pages
[nOldSel
]->Show( false );
518 wxNotebookPage
*pPage
= m_pages
[nSel
];
519 if ( IsShownOnScreen() )
526 // Postpone Show() until the control is actually shown.
527 // Otherwise this forces the containing toplevel window
528 // to show, even if it's just being created and called
529 // AddPage() without intent to show the window yet.
530 // We Show() the selected page in our OnSize handler,
531 // unless it already is shown.
536 m_peer
->SetValue( m_nSelection
+ 1 ) ;
539 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
541 bool status
= false ;
543 SInt32 newSel
= m_peer
->GetValue() - 1 ;
544 if ( newSel
!= m_nSelection
)
546 wxBookCtrlEvent
changing(
547 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
548 newSel
, m_nSelection
);
549 changing
.SetEventObject( this );
550 HandleWindowEvent( changing
);
552 if ( changing
.IsAllowed() )
554 wxBookCtrlEvent
event(
555 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
556 newSel
, m_nSelection
);
557 event
.SetEventObject( this );
558 HandleWindowEvent( event
);
562 m_peer
->SetValue( m_nSelection
+ 1 ) ;