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"
17 #include "wx/string.h"
19 #include "wx/imaglist.h"
21 #include "wx/notebook.h"
22 #include "wx/mac/uma.h"
25 // check that the page index is valid
26 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
29 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
30 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
32 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
33 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
35 EVT_SIZE(wxNotebook::OnSize
)
36 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
37 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
40 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
41 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
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 ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
81 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
83 if ( bounds
.right
<= bounds
.left
)
84 bounds
.right
= bounds
.left
+ 100;
85 if ( bounds
.bottom
<= bounds
.top
)
86 bounds
.bottom
= bounds
.top
+ 100;
88 UInt16 tabstyle
= kControlTabDirectionNorth
;
89 if ( HasFlag(wxBK_LEFT
) )
90 tabstyle
= kControlTabDirectionWest
;
91 else if ( HasFlag( wxBK_RIGHT
) )
92 tabstyle
= kControlTabDirectionEast
;
93 else if ( HasFlag( wxBK_BOTTOM
) )
94 tabstyle
= kControlTabDirectionSouth
;
96 ControlTabSize tabsize
;
97 switch (GetWindowVariant())
99 case wxWINDOW_VARIANT_MINI
:
100 if ( UMAGetSystemVersion() >= 0x1030 )
103 tabsize
= kControlSizeSmall
;
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
& padding
)
140 void wxNotebook::SetTabSize(const wxSize
& 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::SetSelection(size_t nPage
)
157 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("SetSelection: invalid notebook page") );
159 if ( int(nPage
) != m_nSelection
)
161 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
162 event
.SetSelection(nPage
);
163 event
.SetOldSelection(m_nSelection
);
164 event
.SetEventObject(this);
165 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
167 // program allows the page change
168 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
169 (void)GetEventHandler()->ProcessEvent(event
);
171 ChangePage(m_nSelection
, nPage
);
178 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
180 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
182 wxNotebookPage
*page
= m_pages
[nPage
];
183 page
->SetLabel(strText
);
189 wxString
wxNotebook::GetPageText(size_t nPage
) const
191 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
193 wxNotebookPage
*page
= m_pages
[nPage
];
195 return page
->GetLabel();
198 int wxNotebook::GetPageImage(size_t nPage
) const
200 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
202 return m_images
[nPage
];
205 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
207 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
208 wxT("SetPageImage: invalid notebook page") );
209 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
210 wxT("SetPageImage: invalid image index") );
212 if ( nImage
!= m_images
[nPage
] )
214 // if the item didn't have an icon before or, on the contrary, did have
215 // it but has lost it now, its size will change - but if the icon just
217 m_images
[nPage
] = nImage
;
225 // ----------------------------------------------------------------------------
226 // wxNotebook operations
227 // ----------------------------------------------------------------------------
229 // remove one page from the notebook, without deleting the window
230 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
232 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
233 wxT("DoRemovePage: invalid notebook page") );
235 wxNotebookPage
* page
= m_pages
[nPage
] ;
236 m_pages
.RemoveAt(nPage
);
240 if (m_nSelection
>= (int)GetPageCount())
241 m_nSelection
= GetPageCount() - 1;
243 if (m_nSelection
>= 0)
244 m_pages
[m_nSelection
]->Show(true);
246 InvalidateBestSize();
252 bool wxNotebook::DeleteAllPages()
254 WX_CLEAR_ARRAY(m_pages
) ;
257 InvalidateBestSize();
262 // same as AddPage() but does it at given position
263 bool wxNotebook::InsertPage(size_t nPage
,
264 wxNotebookPage
*pPage
,
265 const wxString
& strText
,
269 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
272 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
274 // don't show pages by default (we'll need to adjust their size first)
275 pPage
->Show( false ) ;
277 pPage
->SetLabel( strText
);
279 m_images
.Insert( imageId
, nPage
);
283 wxRect rect
= GetPageRect() ;
284 pPage
->SetSize( rect
);
285 if ( pPage
->GetAutoLayout() )
288 // now deal with the selection
289 // ---------------------------
291 // if the inserted page is before the selected one, we must update the
292 // index of the selected page
294 if ( int(nPage
) <= m_nSelection
)
298 // while this still is the same page showing, we need to update the tabs
299 m_peer
->SetValue( m_nSelection
+ 1 ) ;
302 // some page should be selected: either this one or the first one if there
303 // is still no selection
307 else if ( m_nSelection
== -1 )
311 SetSelection( selNew
);
313 InvalidateBestSize();
318 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
320 int resultV
= wxNOT_FOUND
;
322 #if TARGET_API_MAC_OSX
323 const int countPages
= GetPageCount();
325 // we have to convert from Client to Window relative coordinates
326 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
327 // and now to HIView native ones
328 adjustedPt
.x
-= MacGetLeftBorderSize() ;
329 adjustedPt
.y
-= MacGetTopBorderSize() ;
331 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
332 HIViewPartCode outPart
= 0 ;
333 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
335 int max
= m_peer
->GetMaximum() ;
336 if ( outPart
== 0 && max
> 0 )
338 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
339 // so we have to go some extra miles to make sure we select something different
341 int val
= m_peer
->GetValue() ;
345 m_peer
->SetMaximum( 2 ) ;
350 m_peer
->SetValue( maxval
) ;
352 m_peer
->SetValue( 1 ) ;
354 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
356 m_peer
->SetValue( val
) ;
358 m_peer
->SetMaximum( 1 ) ;
361 if ( outPart
>= 1 && outPart
<= countPages
)
362 resultV
= outPart
- 1 ;
363 #endif // TARGET_API_MAC_OSX
369 // we cannot differentiate better
371 *flags
|= wxNB_HITTEST_ONLABEL
;
373 *flags
|= wxNB_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 wxMacCFStringHolder
cflabel( page
->GetLabel(), m_font
.GetEncoding() ) ;
398 info
.name
= cflabel
;
399 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
401 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
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
);
411 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
413 wxMacReleaseBitmapButton( &info
) ;
417 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
421 m_peer
->GetRectInWindowCoords( &bounds
) ;
422 InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
425 wxRect
wxNotebook::GetPageRect() const
427 wxSize size
= GetClientSize() ;
429 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
432 // ----------------------------------------------------------------------------
433 // wxNotebook callbacks
434 // ----------------------------------------------------------------------------
436 // @@@ OnSize() is used for setting the font when it's called for the first
437 // time because doing it in ::Create() doesn't work (for unknown reasons)
438 void wxNotebook::OnSize(wxSizeEvent
& event
)
440 unsigned int nCount
= m_pages
.Count();
441 wxRect rect
= GetPageRect() ;
443 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
445 wxNotebookPage
*pPage
= m_pages
[nPage
];
446 pPage
->SetSize(rect
);
447 if ( pPage
->GetAutoLayout() )
451 // Processing continues to next OnSize
455 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
457 // is it our tab control?
458 if ( event
.GetEventObject() == this )
459 ChangePage(event
.GetOldSelection(), event
.GetSelection());
461 // we want to give others a chance to process this message as well
465 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
467 // set focus to the currently selected page if any
468 if ( m_nSelection
!= -1 )
469 m_pages
[m_nSelection
]->SetFocus();
474 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
476 if ( event
.IsWindowChange() )
479 AdvanceSelection( event
.GetDirection() );
483 // we get this event in 2 cases
485 // a) one of our pages might have generated it because the user TABbed
486 // out from it in which case we should propagate the event upwards and
487 // our parent will take care of setting the focus to prev/next sibling
491 // b) the parent panel wants to give the focus to us so that we
492 // forward it to our selected page. We can't deal with this in
493 // OnSetFocus() because we don't know which direction the focus came
494 // from in this case and so can't choose between setting the focus to
495 // first or last panel child
496 wxWindow
*parent
= GetParent();
498 // the cast is here to fix a GCC ICE
499 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
501 // no, it doesn't come from child, case (b): forward to a page
502 if ( m_nSelection
!= -1 )
504 // so that the page knows that the event comes from it's parent
505 // and is being propagated downwards
506 event
.SetEventObject( this );
508 wxWindow
*page
= m_pages
[m_nSelection
];
509 if ( !page
->GetEventHandler()->ProcessEvent( event
) )
513 //else: page manages focus inside it itself
517 // we have no pages - still have to give focus to _something_
523 // it comes from our child, case (a), pass to the parent
526 event
.SetCurrentFocus( this );
527 parent
->GetEventHandler()->ProcessEvent( event
);
533 // ----------------------------------------------------------------------------
534 // wxNotebook base class virtuals
535 // ----------------------------------------------------------------------------
537 #if wxUSE_CONSTRAINTS
539 // override these 2 functions to do nothing: everything is done in OnSize
541 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
543 // don't set the sizes of the pages - their correct size is not yet known
544 wxControl::SetConstraintSizes( false );
547 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
552 #endif // wxUSE_CONSTRAINTS
554 void wxNotebook::Command(wxCommandEvent
& event
)
556 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
559 // ----------------------------------------------------------------------------
560 // wxNotebook helper functions
561 // ----------------------------------------------------------------------------
563 // hide the currently active panel and show the new one
564 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
570 m_pages
[nOldSel
]->Show( false );
574 wxNotebookPage
*pPage
= m_pages
[nSel
];
580 m_peer
->SetValue( m_nSelection
+ 1 ) ;
583 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
585 OSStatus status
= eventNotHandledErr
;
587 SInt32 newSel
= m_peer
->GetValue() - 1 ;
588 if ( newSel
!= m_nSelection
)
590 wxNotebookEvent
changing(
591 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
592 newSel
, m_nSelection
);
593 changing
.SetEventObject( this );
594 GetEventHandler()->ProcessEvent( changing
);
596 if ( changing
.IsAllowed() )
598 wxNotebookEvent
event(
599 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
600 newSel
, m_nSelection
);
601 event
.SetEventObject( this );
602 GetEventHandler()->ProcessEvent( event
);
606 m_peer
->SetValue( m_nSelection
+ 1 ) ;
612 return (wxInt32
)status
;