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/mac/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
, wxControl
)
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
, wxControl
)
46 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
49 // common part of all ctors
50 void wxNotebook::Init()
55 // default for dynamic class
56 wxNotebook::wxNotebook()
61 // the same arguments as for wxControl
62 wxNotebook::wxNotebook( wxWindow
*parent
,
67 const wxString
& name
)
71 Create( parent
, id
, pos
, size
, style
, name
);
74 bool wxNotebook::Create( wxWindow
*parent
,
79 const wxString
& name
)
81 m_macIsUserPane
= false ;
83 if (! (style
& wxBK_ALIGN_MASK
))
86 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
89 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
91 if ( bounds
.right
<= bounds
.left
)
92 bounds
.right
= bounds
.left
+ 100;
93 if ( bounds
.bottom
<= bounds
.top
)
94 bounds
.bottom
= bounds
.top
+ 100;
96 UInt16 tabstyle
= kControlTabDirectionNorth
;
97 if ( HasFlag(wxBK_LEFT
) )
98 tabstyle
= kControlTabDirectionWest
;
99 else if ( HasFlag( wxBK_RIGHT
) )
100 tabstyle
= kControlTabDirectionEast
;
101 else if ( HasFlag( wxBK_BOTTOM
) )
102 tabstyle
= kControlTabDirectionSouth
;
104 ControlTabSize tabsize
;
105 switch (GetWindowVariant())
107 case wxWINDOW_VARIANT_MINI
:
108 if ( UMAGetSystemVersion() >= 0x1030 )
111 tabsize
= kControlSizeSmall
;
114 case wxWINDOW_VARIANT_SMALL
:
115 tabsize
= kControlTabSizeSmall
;
119 tabsize
= kControlTabSizeLarge
;
123 m_peer
= new wxMacControl( this );
124 OSStatus err
= CreateTabsControl(
125 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
126 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() );
129 MacPostControlCreate( pos
, size
);
135 wxNotebook::~wxNotebook()
139 // ----------------------------------------------------------------------------
140 // wxNotebook accessors
141 // ----------------------------------------------------------------------------
143 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
148 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
153 void wxNotebook::SetPageSize(const wxSize
& size
)
155 SetSize( CalcSizeFromPage( size
) );
158 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
160 return DoGetSizeFromClientSize( sizePage
);
163 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
165 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
167 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
169 if ( flags
& SetSelection_SendEvent
)
171 if ( !SendPageChangingEvent(nPage
) )
176 //else: program allows the page change
178 SendPageChangedEvent(m_nSelection
, nPage
);
181 ChangePage(m_nSelection
, nPage
);
188 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
190 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
192 wxNotebookPage
*page
= m_pages
[nPage
];
193 page
->SetLabel(strText
);
199 wxString
wxNotebook::GetPageText(size_t nPage
) const
201 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
203 wxNotebookPage
*page
= m_pages
[nPage
];
205 return page
->GetLabel();
208 int wxNotebook::GetPageImage(size_t nPage
) const
210 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
212 return m_images
[nPage
];
215 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
217 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
218 wxT("SetPageImage: invalid notebook page") );
219 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
220 wxT("SetPageImage: invalid image index") );
222 if ( nImage
!= m_images
[nPage
] )
224 // if the item didn't have an icon before or, on the contrary, did have
225 // it but has lost it now, its size will change - but if the icon just
227 m_images
[nPage
] = nImage
;
235 // ----------------------------------------------------------------------------
236 // wxNotebook operations
237 // ----------------------------------------------------------------------------
239 // remove one page from the notebook, without deleting the window
240 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
242 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
243 wxT("DoRemovePage: invalid notebook page") );
245 wxNotebookPage
* page
= m_pages
[nPage
] ;
246 m_pages
.RemoveAt(nPage
);
250 if (m_nSelection
>= (int)GetPageCount())
251 m_nSelection
= GetPageCount() - 1;
253 if (m_nSelection
>= 0)
254 m_pages
[m_nSelection
]->Show(true);
256 InvalidateBestSize();
262 bool wxNotebook::DeleteAllPages()
264 WX_CLEAR_ARRAY(m_pages
) ;
267 InvalidateBestSize();
272 // same as AddPage() but does it at given position
273 bool wxNotebook::InsertPage(size_t nPage
,
274 wxNotebookPage
*pPage
,
275 const wxString
& strText
,
279 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
282 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
284 // don't show pages by default (we'll need to adjust their size first)
285 pPage
->Show( false ) ;
287 pPage
->SetLabel( strText
);
289 m_images
.Insert( imageId
, nPage
);
293 wxRect rect
= GetPageRect() ;
294 pPage
->SetSize( rect
);
295 if ( pPage
->GetAutoLayout() )
298 // now deal with the selection
299 // ---------------------------
301 // if the inserted page is before the selected one, we must update the
302 // index of the selected page
304 if ( int(nPage
) <= m_nSelection
)
308 // while this still is the same page showing, we need to update the tabs
309 m_peer
->SetValue( m_nSelection
+ 1 ) ;
312 // some page should be selected: either this one or the first one if there
313 // is still no selection
317 else if ( m_nSelection
== -1 )
321 SetSelection( selNew
);
323 InvalidateBestSize();
328 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
330 int resultV
= wxNOT_FOUND
;
332 #if TARGET_API_MAC_OSX
333 const int countPages
= GetPageCount();
335 // we have to convert from Client to Window relative coordinates
336 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
337 // and now to HIView native ones
338 adjustedPt
.x
-= MacGetLeftBorderSize() ;
339 adjustedPt
.y
-= MacGetTopBorderSize() ;
341 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
342 HIViewPartCode outPart
= 0 ;
343 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
345 int max
= m_peer
->GetMaximum() ;
346 if ( outPart
== 0 && max
> 0 )
348 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
349 // so we have to go some extra miles to make sure we select something different
351 int val
= m_peer
->GetValue() ;
355 m_peer
->SetMaximum( 2 ) ;
360 m_peer
->SetValue( maxval
) ;
362 m_peer
->SetValue( 1 ) ;
364 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
366 m_peer
->SetValue( val
) ;
368 m_peer
->SetMaximum( 1 ) ;
371 if ( outPart
>= 1 && outPart
<= countPages
)
372 resultV
= outPart
- 1 ;
373 #endif // TARGET_API_MAC_OSX
379 // we cannot differentiate better
381 *flags
|= wxBK_HITTEST_ONLABEL
;
383 *flags
|= wxBK_HITTEST_NOWHERE
;
389 // Added by Mark Newsam
390 // When a page is added or deleted to the notebook this function updates
391 // information held in the control so that it matches the order
392 // the user would expect.
394 void wxNotebook::MacSetupTabs()
396 m_peer
->SetMaximum( GetPageCount() ) ;
398 wxNotebookPage
*page
;
399 ControlTabInfoRecV1 info
;
401 const size_t countPages
= GetPageCount();
402 for (size_t ii
= 0; ii
< countPages
; ii
++)
405 info
.version
= kControlTabInfoVersionOne
;
406 info
.iconSuiteID
= 0;
407 wxMacCFStringHolder
cflabel( page
->GetLabel(), m_font
.GetEncoding() ) ;
408 info
.name
= cflabel
;
409 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
411 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
413 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage( ii
) ) ;
416 ControlButtonContentInfo info
;
418 wxMacCreateBitmapButton( &info
, bmap
) ;
420 OSStatus err
= m_peer
->SetData
<ControlButtonContentInfo
>( ii
+ 1, kControlTabImageContentTag
, &info
);
421 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
423 wxMacReleaseBitmapButton( &info
) ;
427 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
430 #if wxMAC_USE_CORE_GRAPHICS
434 m_peer
->GetRectInWindowCoords( &bounds
) ;
435 InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
439 wxRect
wxNotebook::GetPageRect() const
441 wxSize size
= GetClientSize() ;
443 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
446 // ----------------------------------------------------------------------------
447 // wxNotebook callbacks
448 // ----------------------------------------------------------------------------
450 // @@@ OnSize() is used for setting the font when it's called for the first
451 // time because doing it in ::Create() doesn't work (for unknown reasons)
452 void wxNotebook::OnSize(wxSizeEvent
& event
)
454 unsigned int nCount
= m_pages
.Count();
455 wxRect rect
= GetPageRect() ;
457 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
459 wxNotebookPage
*pPage
= m_pages
[nPage
];
460 pPage
->SetSize(rect
);
461 if ( pPage
->GetAutoLayout() )
465 // Processing continues to next OnSize
469 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
471 // is it our tab control?
472 if ( event
.GetEventObject() == this )
473 ChangePage(event
.GetOldSelection(), event
.GetSelection());
475 // we want to give others a chance to process this message as well
479 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
481 // set focus to the currently selected page if any
482 if ( m_nSelection
!= -1 )
483 m_pages
[m_nSelection
]->SetFocus();
488 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
490 if ( event
.IsWindowChange() )
493 AdvanceSelection( event
.GetDirection() );
497 // we get this event in 2 cases
499 // a) one of our pages might have generated it because the user TABbed
500 // out from it in which case we should propagate the event upwards and
501 // our parent will take care of setting the focus to prev/next sibling
505 // b) the parent panel wants to give the focus to us so that we
506 // forward it to our selected page. We can't deal with this in
507 // OnSetFocus() because we don't know which direction the focus came
508 // from in this case and so can't choose between setting the focus to
509 // first or last panel child
510 wxWindow
*parent
= GetParent();
512 // the cast is here to fix a GCC ICE
513 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
515 // no, it doesn't come from child, case (b): forward to a page
516 if ( m_nSelection
!= -1 )
518 // so that the page knows that the event comes from it's parent
519 // and is being propagated downwards
520 event
.SetEventObject( this );
522 wxWindow
*page
= m_pages
[m_nSelection
];
523 if ( !page
->GetEventHandler()->ProcessEvent( event
) )
527 //else: page manages focus inside it itself
531 // we have no pages - still have to give focus to _something_
537 // it comes from our child, case (a), pass to the parent
540 event
.SetCurrentFocus( this );
541 parent
->GetEventHandler()->ProcessEvent( event
);
547 // ----------------------------------------------------------------------------
548 // wxNotebook base class virtuals
549 // ----------------------------------------------------------------------------
551 #if wxUSE_CONSTRAINTS
553 // override these 2 functions to do nothing: everything is done in OnSize
555 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
557 // don't set the sizes of the pages - their correct size is not yet known
558 wxControl::SetConstraintSizes( false );
561 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
566 #endif // wxUSE_CONSTRAINTS
568 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
570 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
573 // ----------------------------------------------------------------------------
574 // wxNotebook helper functions
575 // ----------------------------------------------------------------------------
577 // hide the currently active panel and show the new one
578 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
584 m_pages
[nOldSel
]->Show( false );
588 wxNotebookPage
*pPage
= m_pages
[nSel
];
594 m_peer
->SetValue( m_nSelection
+ 1 ) ;
597 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
599 OSStatus status
= eventNotHandledErr
;
601 SInt32 newSel
= m_peer
->GetValue() - 1 ;
602 if ( newSel
!= m_nSelection
)
604 wxNotebookEvent
changing(
605 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
606 newSel
, m_nSelection
);
607 changing
.SetEventObject( this );
608 GetEventHandler()->ProcessEvent( changing
);
610 if ( changing
.IsAllowed() )
612 wxNotebookEvent
event(
613 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
614 newSel
, m_nSelection
);
615 event
.SetEventObject( this );
616 GetEventHandler()->ProcessEvent( event
);
620 m_peer
->SetValue( m_nSelection
+ 1 ) ;
626 return (wxInt32
)status
;