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())
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
37 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
38 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
40 EVT_SIZE(wxNotebook::OnSize
)
41 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
42 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
45 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
48 // common part of all ctors
49 void wxNotebook::Init()
54 // default for dynamic class
55 wxNotebook::wxNotebook()
60 // the same arguments as for wxControl
61 wxNotebook::wxNotebook( wxWindow
*parent
,
66 const wxString
& name
)
70 Create( parent
, id
, pos
, size
, style
, name
);
73 bool wxNotebook::Create( wxWindow
*parent
,
78 const wxString
& name
)
80 m_macIsUserPane
= false ;
82 if (! (style
& wxBK_ALIGN_MASK
))
85 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
88 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
90 if ( bounds
.right
<= bounds
.left
)
91 bounds
.right
= bounds
.left
+ 100;
92 if ( bounds
.bottom
<= bounds
.top
)
93 bounds
.bottom
= bounds
.top
+ 100;
95 UInt16 tabstyle
= kControlTabDirectionNorth
;
96 if ( HasFlag(wxBK_LEFT
) )
97 tabstyle
= kControlTabDirectionWest
;
98 else if ( HasFlag( wxBK_RIGHT
) )
99 tabstyle
= kControlTabDirectionEast
;
100 else if ( HasFlag( wxBK_BOTTOM
) )
101 tabstyle
= kControlTabDirectionSouth
;
103 ControlTabSize tabsize
;
104 switch (GetWindowVariant())
106 case wxWINDOW_VARIANT_MINI
:
110 case wxWINDOW_VARIANT_SMALL
:
111 tabsize
= kControlTabSizeSmall
;
115 tabsize
= kControlTabSizeLarge
;
119 m_peer
= new wxMacControl( this );
120 OSStatus err
= CreateTabsControl(
121 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
122 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() );
125 MacPostControlCreate( pos
, size
);
131 wxNotebook::~wxNotebook()
135 // ----------------------------------------------------------------------------
136 // wxNotebook accessors
137 // ----------------------------------------------------------------------------
139 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
144 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
149 void wxNotebook::SetPageSize(const wxSize
& size
)
151 SetSize( CalcSizeFromPage( size
) );
154 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
156 return DoGetSizeFromClientSize( sizePage
);
159 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
161 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
163 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
165 if ( flags
& SetSelection_SendEvent
)
167 if ( !SendPageChangingEvent(nPage
) )
172 //else: program allows the page change
174 SendPageChangedEvent(m_nSelection
, nPage
);
177 ChangePage(m_nSelection
, nPage
);
184 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
186 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
188 wxNotebookPage
*page
= m_pages
[nPage
];
189 page
->SetLabel(wxStripMenuCodes(strText
));
195 wxString
wxNotebook::GetPageText(size_t nPage
) const
197 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
199 wxNotebookPage
*page
= m_pages
[nPage
];
201 return page
->GetLabel();
204 int wxNotebook::GetPageImage(size_t nPage
) const
206 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
208 return m_images
[nPage
];
211 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
214 wxT("SetPageImage: invalid notebook page") );
215 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
216 wxT("SetPageImage: invalid image index") );
218 if ( nImage
!= m_images
[nPage
] )
220 // if the item didn't have an icon before or, on the contrary, did have
221 // it but has lost it now, its size will change - but if the icon just
223 m_images
[nPage
] = nImage
;
231 // ----------------------------------------------------------------------------
232 // wxNotebook operations
233 // ----------------------------------------------------------------------------
235 // remove one page from the notebook, without deleting the window
236 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
238 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
239 wxT("DoRemovePage: invalid notebook page") );
241 wxNotebookPage
* page
= m_pages
[nPage
] ;
242 m_pages
.RemoveAt(nPage
);
246 if (m_nSelection
>= (int)GetPageCount())
247 m_nSelection
= GetPageCount() - 1;
249 if (m_nSelection
>= 0)
250 m_pages
[m_nSelection
]->Show(true);
252 InvalidateBestSize();
258 bool wxNotebook::DeleteAllPages()
260 WX_CLEAR_ARRAY(m_pages
) ;
263 InvalidateBestSize();
268 // same as AddPage() but does it at given position
269 bool wxNotebook::InsertPage(size_t nPage
,
270 wxNotebookPage
*pPage
,
271 const wxString
& strText
,
275 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
278 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
280 // don't show pages by default (we'll need to adjust their size first)
281 pPage
->Show( false ) ;
283 pPage
->SetLabel( wxStripMenuCodes(strText
) );
285 m_images
.Insert( imageId
, nPage
);
289 wxRect rect
= GetPageRect() ;
290 pPage
->SetSize( rect
);
291 if ( pPage
->GetAutoLayout() )
294 // now deal with the selection
295 // ---------------------------
297 // if the inserted page is before the selected one, we must update the
298 // index of the selected page
300 if ( int(nPage
) <= m_nSelection
)
304 // while this still is the same page showing, we need to update the tabs
305 m_peer
->SetValue( m_nSelection
+ 1 ) ;
308 // some page should be selected: either this one or the first one if there
309 // is still no selection
313 else if ( m_nSelection
== -1 )
317 SetSelection( selNew
);
319 InvalidateBestSize();
324 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
326 int resultV
= wxNOT_FOUND
;
328 const int countPages
= GetPageCount();
330 // we have to convert from Client to Window relative coordinates
331 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
332 // and now to HIView native ones
333 adjustedPt
.x
-= MacGetLeftBorderSize() ;
334 adjustedPt
.y
-= MacGetTopBorderSize() ;
336 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
337 HIViewPartCode outPart
= 0 ;
338 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
340 int max
= m_peer
->GetMaximum() ;
341 if ( outPart
== 0 && max
> 0 )
343 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
344 // so we have to go some extra miles to make sure we select something different
346 int val
= m_peer
->GetValue() ;
350 m_peer
->SetMaximum( 2 ) ;
355 m_peer
->SetValue( maxval
) ;
357 m_peer
->SetValue( 1 ) ;
359 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
361 m_peer
->SetValue( val
) ;
363 m_peer
->SetMaximum( 1 ) ;
366 if ( outPart
>= 1 && outPart
<= countPages
)
367 resultV
= outPart
- 1 ;
373 // we cannot differentiate better
375 *flags
|= wxBK_HITTEST_ONLABEL
;
377 *flags
|= wxBK_HITTEST_NOWHERE
;
383 // Added by Mark Newsam
384 // When a page is added or deleted to the notebook this function updates
385 // information held in the control so that it matches the order
386 // the user would expect.
388 void wxNotebook::MacSetupTabs()
390 m_peer
->SetMaximum( GetPageCount() ) ;
392 wxNotebookPage
*page
;
393 ControlTabInfoRecV1 info
;
395 const size_t countPages
= GetPageCount();
396 for (size_t ii
= 0; ii
< countPages
; ii
++)
399 info
.version
= kControlTabInfoVersionOne
;
400 info
.iconSuiteID
= 0;
401 wxCFStringRef
cflabel( page
->GetLabel(), GetFont().GetEncoding() ) ;
402 info
.name
= cflabel
;
403 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
405 if ( GetImageList() && GetPageImage(ii
) >= 0 )
407 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage( ii
) ) ;
410 ControlButtonContentInfo info
;
412 wxMacCreateBitmapButton( &info
, bmap
) ;
414 OSStatus err
= m_peer
->SetData
<ControlButtonContentInfo
>( ii
+ 1, kControlTabImageContentTag
, &info
);
417 wxFAIL_MSG("Error when setting icon on tab");
420 wxMacReleaseBitmapButton( &info
) ;
424 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
430 wxRect
wxNotebook::GetPageRect() const
432 wxSize size
= GetClientSize() ;
434 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
437 // ----------------------------------------------------------------------------
438 // wxNotebook callbacks
439 // ----------------------------------------------------------------------------
441 // @@@ OnSize() is used for setting the font when it's called for the first
442 // time because doing it in ::Create() doesn't work (for unknown reasons)
443 void wxNotebook::OnSize(wxSizeEvent
& event
)
445 unsigned int nCount
= m_pages
.Count();
446 wxRect rect
= GetPageRect() ;
448 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
450 wxNotebookPage
*pPage
= m_pages
[nPage
];
451 pPage
->SetSize(rect
);
452 if ( pPage
->GetAutoLayout() )
456 // Processing continues to next OnSize
460 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
462 // is it our tab control?
463 if ( event
.GetEventObject() == this )
464 ChangePage(event
.GetOldSelection(), event
.GetSelection());
466 // we want to give others a chance to process this message as well
470 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
472 // set focus to the currently selected page if any
473 if ( m_nSelection
!= -1 )
474 m_pages
[m_nSelection
]->SetFocus();
479 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
481 if ( event
.IsWindowChange() )
484 AdvanceSelection( event
.GetDirection() );
488 // we get this event in 2 cases
490 // a) one of our pages might have generated it because the user TABbed
491 // out from it in which case we should propagate the event upwards and
492 // our parent will take care of setting the focus to prev/next sibling
496 // b) the parent panel wants to give the focus to us so that we
497 // forward it to our selected page. We can't deal with this in
498 // OnSetFocus() because we don't know which direction the focus came
499 // from in this case and so can't choose between setting the focus to
500 // first or last panel child
501 wxWindow
*parent
= GetParent();
503 // the cast is here to fix a GCC ICE
504 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
506 // no, it doesn't come from child, case (b): forward to a page
507 if ( m_nSelection
!= -1 )
509 // so that the page knows that the event comes from it's parent
510 // and is being propagated downwards
511 event
.SetEventObject( this );
513 wxWindow
*page
= m_pages
[m_nSelection
];
514 if ( !page
->HandleWindowEvent( event
) )
518 //else: page manages focus inside it itself
522 // we have no pages - still have to give focus to _something_
528 // it comes from our child, case (a), pass to the parent
531 event
.SetCurrentFocus( this );
532 parent
->HandleWindowEvent( event
);
538 // ----------------------------------------------------------------------------
539 // wxNotebook base class virtuals
540 // ----------------------------------------------------------------------------
542 #if wxUSE_CONSTRAINTS
544 // override these 2 functions to do nothing: everything is done in OnSize
546 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
548 // don't set the sizes of the pages - their correct size is not yet known
549 wxControl::SetConstraintSizes( false );
552 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
557 #endif // wxUSE_CONSTRAINTS
559 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
561 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
564 // ----------------------------------------------------------------------------
565 // wxNotebook helper functions
566 // ----------------------------------------------------------------------------
568 // hide the currently active panel and show the new one
569 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
575 m_pages
[nOldSel
]->Show( false );
579 wxNotebookPage
*pPage
= m_pages
[nSel
];
585 m_peer
->SetValue( m_nSelection
+ 1 ) ;
588 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
590 OSStatus status
= eventNotHandledErr
;
592 SInt32 newSel
= m_peer
->GetValue() - 1 ;
593 if ( newSel
!= m_nSelection
)
595 wxBookCtrlEvent
changing(
596 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
597 newSel
, m_nSelection
);
598 changing
.SetEventObject( this );
599 HandleWindowEvent( changing
);
601 if ( changing
.IsAllowed() )
603 wxBookCtrlEvent
event(
604 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
605 newSel
, m_nSelection
);
606 event
.SetEventObject( this );
607 HandleWindowEvent( event
);
611 m_peer
->SetValue( m_nSelection
+ 1 ) ;
617 return (wxInt32
)status
;