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
:
111 case wxWINDOW_VARIANT_SMALL
:
112 tabsize
= kControlTabSizeSmall
;
116 tabsize
= kControlTabSizeLarge
;
120 m_peer
= new wxMacControl( this );
121 OSStatus err
= CreateTabsControl(
122 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
123 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() );
126 MacPostControlCreate( pos
, size
);
132 wxNotebook::~wxNotebook()
136 // ----------------------------------------------------------------------------
137 // wxNotebook accessors
138 // ----------------------------------------------------------------------------
140 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
145 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
150 void wxNotebook::SetPageSize(const wxSize
& size
)
152 SetSize( CalcSizeFromPage( size
) );
155 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
157 return DoGetSizeFromClientSize( sizePage
);
160 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
162 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
164 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
166 if ( flags
& SetSelection_SendEvent
)
168 if ( !SendPageChangingEvent(nPage
) )
173 //else: program allows the page change
175 SendPageChangedEvent(m_nSelection
, nPage
);
178 ChangePage(m_nSelection
, nPage
);
185 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
187 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
189 wxNotebookPage
*page
= m_pages
[nPage
];
190 page
->SetLabel(strText
);
196 wxString
wxNotebook::GetPageText(size_t nPage
) const
198 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
200 wxNotebookPage
*page
= m_pages
[nPage
];
202 return page
->GetLabel();
205 int wxNotebook::GetPageImage(size_t nPage
) const
207 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
209 return m_images
[nPage
];
212 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
214 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
215 wxT("SetPageImage: invalid notebook page") );
216 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
217 wxT("SetPageImage: invalid image index") );
219 if ( nImage
!= m_images
[nPage
] )
221 // if the item didn't have an icon before or, on the contrary, did have
222 // it but has lost it now, its size will change - but if the icon just
224 m_images
[nPage
] = nImage
;
232 // ----------------------------------------------------------------------------
233 // wxNotebook operations
234 // ----------------------------------------------------------------------------
236 // remove one page from the notebook, without deleting the window
237 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
239 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
240 wxT("DoRemovePage: invalid notebook page") );
242 wxNotebookPage
* page
= m_pages
[nPage
] ;
243 m_pages
.RemoveAt(nPage
);
247 if (m_nSelection
>= (int)GetPageCount())
248 m_nSelection
= GetPageCount() - 1;
250 if (m_nSelection
>= 0)
251 m_pages
[m_nSelection
]->Show(true);
253 InvalidateBestSize();
259 bool wxNotebook::DeleteAllPages()
261 WX_CLEAR_ARRAY(m_pages
) ;
264 InvalidateBestSize();
269 // same as AddPage() but does it at given position
270 bool wxNotebook::InsertPage(size_t nPage
,
271 wxNotebookPage
*pPage
,
272 const wxString
& strText
,
276 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
279 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
281 // don't show pages by default (we'll need to adjust their size first)
282 pPage
->Show( false ) ;
284 pPage
->SetLabel( strText
);
286 m_images
.Insert( imageId
, nPage
);
290 wxRect rect
= GetPageRect() ;
291 pPage
->SetSize( rect
);
292 if ( pPage
->GetAutoLayout() )
295 // now deal with the selection
296 // ---------------------------
298 // if the inserted page is before the selected one, we must update the
299 // index of the selected page
301 if ( int(nPage
) <= m_nSelection
)
305 // while this still is the same page showing, we need to update the tabs
306 m_peer
->SetValue( m_nSelection
+ 1 ) ;
309 // some page should be selected: either this one or the first one if there
310 // is still no selection
314 else if ( m_nSelection
== -1 )
318 SetSelection( selNew
);
320 InvalidateBestSize();
325 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
327 int resultV
= wxNOT_FOUND
;
329 #if TARGET_API_MAC_OSX
330 const int countPages
= GetPageCount();
332 // we have to convert from Client to Window relative coordinates
333 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
334 // and now to HIView native ones
335 adjustedPt
.x
-= MacGetLeftBorderSize() ;
336 adjustedPt
.y
-= MacGetTopBorderSize() ;
338 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
339 HIViewPartCode outPart
= 0 ;
340 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
342 int max
= m_peer
->GetMaximum() ;
343 if ( outPart
== 0 && max
> 0 )
345 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
346 // so we have to go some extra miles to make sure we select something different
348 int val
= m_peer
->GetValue() ;
352 m_peer
->SetMaximum( 2 ) ;
357 m_peer
->SetValue( maxval
) ;
359 m_peer
->SetValue( 1 ) ;
361 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
363 m_peer
->SetValue( val
) ;
365 m_peer
->SetMaximum( 1 ) ;
368 if ( outPart
>= 1 && outPart
<= countPages
)
369 resultV
= outPart
- 1 ;
370 #endif // TARGET_API_MAC_OSX
376 // we cannot differentiate better
378 *flags
|= wxBK_HITTEST_ONLABEL
;
380 *flags
|= wxBK_HITTEST_NOWHERE
;
386 // Added by Mark Newsam
387 // When a page is added or deleted to the notebook this function updates
388 // information held in the control so that it matches the order
389 // the user would expect.
391 void wxNotebook::MacSetupTabs()
393 m_peer
->SetMaximum( GetPageCount() ) ;
395 wxNotebookPage
*page
;
396 ControlTabInfoRecV1 info
;
398 const size_t countPages
= GetPageCount();
399 for (size_t ii
= 0; ii
< countPages
; ii
++)
402 info
.version
= kControlTabInfoVersionOne
;
403 info
.iconSuiteID
= 0;
404 wxMacCFStringHolder
cflabel( page
->GetLabel(), m_font
.GetEncoding() ) ;
405 info
.name
= cflabel
;
406 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
408 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
410 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage( ii
) ) ;
413 ControlButtonContentInfo info
;
415 wxMacCreateBitmapButton( &info
, bmap
) ;
417 OSStatus err
= m_peer
->SetData
<ControlButtonContentInfo
>( ii
+ 1, kControlTabImageContentTag
, &info
);
418 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
420 wxMacReleaseBitmapButton( &info
) ;
424 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
427 #if wxMAC_USE_CORE_GRAPHICS
431 m_peer
->GetRectInWindowCoords( &bounds
) ;
432 InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
436 wxRect
wxNotebook::GetPageRect() const
438 wxSize size
= GetClientSize() ;
440 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
443 // ----------------------------------------------------------------------------
444 // wxNotebook callbacks
445 // ----------------------------------------------------------------------------
447 // @@@ OnSize() is used for setting the font when it's called for the first
448 // time because doing it in ::Create() doesn't work (for unknown reasons)
449 void wxNotebook::OnSize(wxSizeEvent
& event
)
451 unsigned int nCount
= m_pages
.Count();
452 wxRect rect
= GetPageRect() ;
454 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
456 wxNotebookPage
*pPage
= m_pages
[nPage
];
457 pPage
->SetSize(rect
);
458 if ( pPage
->GetAutoLayout() )
462 // Processing continues to next OnSize
466 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
468 // is it our tab control?
469 if ( event
.GetEventObject() == this )
470 ChangePage(event
.GetOldSelection(), event
.GetSelection());
472 // we want to give others a chance to process this message as well
476 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
478 // set focus to the currently selected page if any
479 if ( m_nSelection
!= -1 )
480 m_pages
[m_nSelection
]->SetFocus();
485 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
487 if ( event
.IsWindowChange() )
490 AdvanceSelection( event
.GetDirection() );
494 // we get this event in 2 cases
496 // a) one of our pages might have generated it because the user TABbed
497 // out from it in which case we should propagate the event upwards and
498 // our parent will take care of setting the focus to prev/next sibling
502 // b) the parent panel wants to give the focus to us so that we
503 // forward it to our selected page. We can't deal with this in
504 // OnSetFocus() because we don't know which direction the focus came
505 // from in this case and so can't choose between setting the focus to
506 // first or last panel child
507 wxWindow
*parent
= GetParent();
509 // the cast is here to fix a GCC ICE
510 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
512 // no, it doesn't come from child, case (b): forward to a page
513 if ( m_nSelection
!= -1 )
515 // so that the page knows that the event comes from it's parent
516 // and is being propagated downwards
517 event
.SetEventObject( this );
519 wxWindow
*page
= m_pages
[m_nSelection
];
520 if ( !page
->GetEventHandler()->ProcessEvent( event
) )
524 //else: page manages focus inside it itself
528 // we have no pages - still have to give focus to _something_
534 // it comes from our child, case (a), pass to the parent
537 event
.SetCurrentFocus( this );
538 parent
->GetEventHandler()->ProcessEvent( event
);
544 // ----------------------------------------------------------------------------
545 // wxNotebook base class virtuals
546 // ----------------------------------------------------------------------------
548 #if wxUSE_CONSTRAINTS
550 // override these 2 functions to do nothing: everything is done in OnSize
552 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
554 // don't set the sizes of the pages - their correct size is not yet known
555 wxControl::SetConstraintSizes( false );
558 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
563 #endif // wxUSE_CONSTRAINTS
565 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
567 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
570 // ----------------------------------------------------------------------------
571 // wxNotebook helper functions
572 // ----------------------------------------------------------------------------
574 // hide the currently active panel and show the new one
575 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
581 m_pages
[nOldSel
]->Show( false );
585 wxNotebookPage
*pPage
= m_pages
[nSel
];
591 m_peer
->SetValue( m_nSelection
+ 1 ) ;
594 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
596 OSStatus status
= eventNotHandledErr
;
598 SInt32 newSel
= m_peer
->GetValue() - 1 ;
599 if ( newSel
!= m_nSelection
)
601 wxNotebookEvent
changing(
602 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
603 newSel
, m_nSelection
);
604 changing
.SetEventObject( this );
605 GetEventHandler()->ProcessEvent( changing
);
607 if ( changing
.IsAllowed() )
609 wxNotebookEvent
event(
610 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
611 newSel
, m_nSelection
);
612 event
.SetEventObject( this );
613 GetEventHandler()->ProcessEvent( event
);
617 m_peer
->SetValue( m_nSelection
+ 1 ) ;
623 return (wxInt32
)status
;