1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/notebmac.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/uma.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 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
86 if ( bounds
.right
<= bounds
.left
)
87 bounds
.right
= bounds
.left
+ 100;
88 if ( bounds
.bottom
<= bounds
.top
)
89 bounds
.bottom
= bounds
.top
+ 100;
91 UInt16 tabstyle
= kControlTabDirectionNorth
;
92 if ( HasFlag(wxBK_LEFT
) )
93 tabstyle
= kControlTabDirectionWest
;
94 else if ( HasFlag( wxBK_RIGHT
) )
95 tabstyle
= kControlTabDirectionEast
;
96 else if ( HasFlag( wxBK_BOTTOM
) )
97 tabstyle
= kControlTabDirectionSouth
;
99 ControlTabSize tabsize
;
100 switch (GetWindowVariant())
102 case wxWINDOW_VARIANT_MINI
:
106 case wxWINDOW_VARIANT_SMALL
:
107 tabsize
= kControlTabSizeSmall
;
111 tabsize
= kControlTabSizeLarge
;
115 m_peer
= new wxMacControl( this );
116 OSStatus err
= CreateTabsControl(
117 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
118 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() );
121 MacPostControlCreate( pos
, size
);
127 wxNotebook::~wxNotebook()
131 // ----------------------------------------------------------------------------
132 // wxNotebook accessors
133 // ----------------------------------------------------------------------------
135 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
140 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
145 void wxNotebook::SetPageSize(const wxSize
& size
)
147 SetSize( CalcSizeFromPage( size
) );
150 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
152 return DoGetSizeFromClientSize( sizePage
);
155 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
157 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
159 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
161 if ( flags
& SetSelection_SendEvent
)
163 if ( !SendPageChangingEvent(nPage
) )
168 //else: program allows the page change
170 SendPageChangedEvent(m_nSelection
, nPage
);
173 ChangePage(m_nSelection
, nPage
);
180 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
182 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
184 wxNotebookPage
*page
= m_pages
[nPage
];
185 page
->SetLabel(wxStripMenuCodes(strText
));
191 wxString
wxNotebook::GetPageText(size_t nPage
) const
193 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
195 wxNotebookPage
*page
= m_pages
[nPage
];
197 return page
->GetLabel();
200 int wxNotebook::GetPageImage(size_t nPage
) const
202 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
204 return m_images
[nPage
];
207 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
209 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
210 wxT("SetPageImage: invalid notebook page") );
211 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
212 wxT("SetPageImage: invalid image index") );
214 if ( nImage
!= m_images
[nPage
] )
216 // if the item didn't have an icon before or, on the contrary, did have
217 // it but has lost it now, its size will change - but if the icon just
219 m_images
[nPage
] = nImage
;
227 // ----------------------------------------------------------------------------
228 // wxNotebook operations
229 // ----------------------------------------------------------------------------
231 // remove one page from the notebook, without deleting the window
232 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
235 wxT("DoRemovePage: invalid notebook page") );
237 wxNotebookPage
* page
= m_pages
[nPage
] ;
238 m_pages
.RemoveAt(nPage
);
242 if (m_nSelection
>= (int)GetPageCount())
243 m_nSelection
= GetPageCount() - 1;
245 if (m_nSelection
>= 0)
246 m_pages
[m_nSelection
]->Show(true);
248 InvalidateBestSize();
254 bool wxNotebook::DeleteAllPages()
256 WX_CLEAR_ARRAY(m_pages
) ;
259 InvalidateBestSize();
264 // same as AddPage() but does it at given position
265 bool wxNotebook::InsertPage(size_t nPage
,
266 wxNotebookPage
*pPage
,
267 const wxString
& strText
,
271 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
274 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
276 // don't show pages by default (we'll need to adjust their size first)
277 pPage
->Show( false ) ;
279 pPage
->SetLabel( wxStripMenuCodes(strText
) );
281 m_images
.Insert( imageId
, nPage
);
285 wxRect rect
= GetPageRect() ;
286 pPage
->SetSize( rect
);
287 if ( pPage
->GetAutoLayout() )
290 // now deal with the selection
291 // ---------------------------
293 // if the inserted page is before the selected one, we must update the
294 // index of the selected page
296 if ( int(nPage
) <= m_nSelection
)
300 // while this still is the same page showing, we need to update the tabs
301 m_peer
->SetValue( m_nSelection
+ 1 ) ;
304 // some page should be selected: either this one or the first one if there
305 // is still no selection
309 else if ( m_nSelection
== -1 )
313 SetSelection( selNew
);
315 InvalidateBestSize();
320 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
322 int resultV
= wxNOT_FOUND
;
324 const int countPages
= GetPageCount();
326 // we have to convert from Client to Window relative coordinates
327 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
328 // and now to HIView native ones
329 adjustedPt
.x
-= MacGetLeftBorderSize() ;
330 adjustedPt
.y
-= MacGetTopBorderSize() ;
332 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
333 HIViewPartCode outPart
= 0 ;
334 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
336 int max
= m_peer
->GetMaximum() ;
337 if ( outPart
== 0 && max
> 0 )
339 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
340 // so we have to go some extra miles to make sure we select something different
342 int val
= m_peer
->GetValue() ;
346 m_peer
->SetMaximum( 2 ) ;
351 m_peer
->SetValue( maxval
) ;
353 m_peer
->SetValue( 1 ) ;
355 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
357 m_peer
->SetValue( val
) ;
359 m_peer
->SetMaximum( 1 ) ;
362 if ( outPart
>= 1 && outPart
<= countPages
)
363 resultV
= outPart
- 1 ;
369 // we cannot differentiate better
371 *flags
|= wxBK_HITTEST_ONLABEL
;
373 *flags
|= wxBK_HITTEST_NOWHERE
;
379 // Added by Mark Newsam
380 // When a page is added or deleted to the notebook this function updates
381 // information held in the control so that it matches the order
382 // the user would expect.
384 void wxNotebook::MacSetupTabs()
386 m_peer
->SetMaximum( GetPageCount() ) ;
388 wxNotebookPage
*page
;
389 ControlTabInfoRecV1 info
;
391 const size_t countPages
= GetPageCount();
392 for (size_t ii
= 0; ii
< countPages
; ii
++)
395 info
.version
= kControlTabInfoVersionOne
;
396 info
.iconSuiteID
= 0;
397 wxCFStringRef
cflabel( page
->GetLabel(), GetFont().GetEncoding() ) ;
398 info
.name
= cflabel
;
399 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
401 if ( GetImageList() && GetPageImage(ii
) >= 0 )
403 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage( ii
) ) ;
406 ControlButtonContentInfo info
;
408 wxMacCreateBitmapButton( &info
, bmap
) ;
410 OSStatus err
= m_peer
->SetData
<ControlButtonContentInfo
>( ii
+ 1, kControlTabImageContentTag
, &info
);
413 wxFAIL_MSG("Error when setting icon on tab");
416 wxMacReleaseBitmapButton( &info
) ;
420 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
426 wxRect
wxNotebook::GetPageRect() const
428 wxSize size
= GetClientSize() ;
430 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
433 // ----------------------------------------------------------------------------
434 // wxNotebook callbacks
435 // ----------------------------------------------------------------------------
437 // @@@ OnSize() is used for setting the font when it's called for the first
438 // time because doing it in ::Create() doesn't work (for unknown reasons)
439 void wxNotebook::OnSize(wxSizeEvent
& event
)
441 unsigned int nCount
= m_pages
.Count();
442 wxRect rect
= GetPageRect() ;
444 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
446 wxNotebookPage
*pPage
= m_pages
[nPage
];
447 pPage
->SetSize(rect
);
448 if ( pPage
->GetAutoLayout() )
452 // Processing continues to next OnSize
456 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
458 // is it our tab control?
459 if ( event
.GetEventObject() == this )
460 ChangePage(event
.GetOldSelection(), event
.GetSelection());
462 // we want to give others a chance to process this message as well
466 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
468 // set focus to the currently selected page if any
469 if ( m_nSelection
!= -1 )
470 m_pages
[m_nSelection
]->SetFocus();
475 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
477 if ( event
.IsWindowChange() )
480 AdvanceSelection( event
.GetDirection() );
484 // we get this event in 2 cases
486 // a) one of our pages might have generated it because the user TABbed
487 // out from it in which case we should propagate the event upwards and
488 // our parent will take care of setting the focus to prev/next sibling
492 // b) the parent panel wants to give the focus to us so that we
493 // forward it to our selected page. We can't deal with this in
494 // OnSetFocus() because we don't know which direction the focus came
495 // from in this case and so can't choose between setting the focus to
496 // first or last panel child
497 wxWindow
*parent
= GetParent();
499 // the cast is here to fix a GCC ICE
500 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
502 // no, it doesn't come from child, case (b): forward to a page
503 if ( m_nSelection
!= -1 )
505 // so that the page knows that the event comes from it's parent
506 // and is being propagated downwards
507 event
.SetEventObject( this );
509 wxWindow
*page
= m_pages
[m_nSelection
];
510 if ( !page
->HandleWindowEvent( event
) )
514 //else: page manages focus inside it itself
518 // we have no pages - still have to give focus to _something_
524 // it comes from our child, case (a), pass to the parent
527 event
.SetCurrentFocus( this );
528 parent
->HandleWindowEvent( event
);
534 // ----------------------------------------------------------------------------
535 // wxNotebook base class virtuals
536 // ----------------------------------------------------------------------------
538 #if wxUSE_CONSTRAINTS
540 // override these 2 functions to do nothing: everything is done in OnSize
542 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
544 // don't set the sizes of the pages - their correct size is not yet known
545 wxControl::SetConstraintSizes( false );
548 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
553 #endif // wxUSE_CONSTRAINTS
555 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
557 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
560 // ----------------------------------------------------------------------------
561 // wxNotebook helper functions
562 // ----------------------------------------------------------------------------
564 // hide the currently active panel and show the new one
565 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
571 m_pages
[nOldSel
]->Show( false );
575 wxNotebookPage
*pPage
= m_pages
[nSel
];
581 m_peer
->SetValue( m_nSelection
+ 1 ) ;
584 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
586 OSStatus status
= eventNotHandledErr
;
588 SInt32 newSel
= m_peer
->GetValue() - 1 ;
589 if ( newSel
!= m_nSelection
)
591 wxBookCtrlEvent
changing(
592 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
593 newSel
, m_nSelection
);
594 changing
.SetEventObject( this );
595 HandleWindowEvent( changing
);
597 if ( changing
.IsAllowed() )
599 wxBookCtrlEvent
event(
600 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
601 newSel
, m_nSelection
);
602 event
.SetEventObject( this );
603 HandleWindowEvent( event
);
607 m_peer
->SetValue( m_nSelection
+ 1 ) ;
613 return (wxInt32
)status
;