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" 
  24 #include "wx/string.h" 
  25 #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
& padding
) 
 148 void wxNotebook::SetTabSize(const wxSize
& 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::SetSelection(size_t nPage
) 
 165     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("SetSelection: invalid notebook page") ); 
 167     if ( int(nPage
) != m_nSelection 
) 
 169         wxNotebookEvent 
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
); 
 170         event
.SetSelection(nPage
); 
 171         event
.SetOldSelection(m_nSelection
); 
 172         event
.SetEventObject(this); 
 173         if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() ) 
 175             // program allows the page change 
 176             event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 177             (void)GetEventHandler()->ProcessEvent(event
); 
 179             ChangePage(m_nSelection
, nPage
); 
 186 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
) 
 188     wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") ); 
 190     wxNotebookPage 
*page 
= m_pages
[nPage
]; 
 191     page
->SetLabel(strText
); 
 197 wxString 
wxNotebook::GetPageText(size_t nPage
) const 
 199     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") ); 
 201     wxNotebookPage 
*page 
= m_pages
[nPage
]; 
 203     return page
->GetLabel(); 
 206 int wxNotebook::GetPageImage(size_t nPage
) const 
 208     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") ); 
 210     return m_images
[nPage
]; 
 213 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
) 
 215     wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, 
 216         wxT("SetPageImage: invalid notebook page") ); 
 217     wxCHECK_MSG( m_imageList 
&& nImage 
< m_imageList
->GetImageCount(), false, 
 218         wxT("SetPageImage: invalid image index") ); 
 220     if ( nImage 
!= m_images
[nPage
] ) 
 222         // if the item didn't have an icon before or, on the contrary, did have 
 223         // it but has lost it now, its size will change - but if the icon just 
 225         m_images
[nPage
] = nImage
; 
 233 // ---------------------------------------------------------------------------- 
 234 // wxNotebook operations 
 235 // ---------------------------------------------------------------------------- 
 237 // remove one page from the notebook, without deleting the window 
 238 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
) 
 240     wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, 
 241         wxT("DoRemovePage: invalid notebook page") ); 
 243     wxNotebookPage
* page 
= m_pages
[nPage
] ; 
 244     m_pages
.RemoveAt(nPage
); 
 248     if (m_nSelection 
>= (int)GetPageCount()) 
 249         m_nSelection 
= GetPageCount() - 1; 
 251     if (m_nSelection 
>= 0) 
 252         m_pages
[m_nSelection
]->Show(true); 
 254     InvalidateBestSize(); 
 260 bool wxNotebook::DeleteAllPages() 
 262     WX_CLEAR_ARRAY(m_pages
) ; 
 265     InvalidateBestSize(); 
 270 // same as AddPage() but does it at given position 
 271 bool wxNotebook::InsertPage(size_t nPage
, 
 272     wxNotebookPage 
*pPage
, 
 273     const wxString
& strText
, 
 277     if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId 
) ) 
 280     wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") ); 
 282     // don't show pages by default (we'll need to adjust their size first) 
 283     pPage
->Show( false ) ; 
 285     pPage
->SetLabel( strText 
); 
 287     m_images
.Insert( imageId
, nPage 
); 
 291     wxRect rect 
= GetPageRect() ; 
 292     pPage
->SetSize( rect 
); 
 293     if ( pPage
->GetAutoLayout() ) 
 296     // now deal with the selection 
 297     // --------------------------- 
 299     // if the inserted page is before the selected one, we must update the 
 300     // index of the selected page 
 302     if ( int(nPage
) <= m_nSelection 
) 
 306         // while this still is the same page showing, we need to update the tabs 
 307         m_peer
->SetValue( m_nSelection 
+ 1 ) ; 
 310     // some page should be selected: either this one or the first one if there 
 311     // is still no selection 
 315     else if ( m_nSelection 
== -1 ) 
 319         SetSelection( selNew 
); 
 321     InvalidateBestSize(); 
 326 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const 
 328     int resultV 
= wxNOT_FOUND
; 
 330 #if TARGET_API_MAC_OSX 
 331     const int countPages 
= GetPageCount(); 
 333     // we have to convert from Client to Window relative coordinates 
 334     wxPoint adjustedPt 
= pt 
+ GetClientAreaOrigin(); 
 335     // and now to HIView native ones 
 336     adjustedPt
.x 
-= MacGetLeftBorderSize() ; 
 337     adjustedPt
.y 
-= MacGetTopBorderSize() ; 
 339     HIPoint hipoint
= { adjustedPt
.x 
, adjustedPt
.y 
} ; 
 340     HIViewPartCode outPart 
= 0 ; 
 341     OSStatus err 
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart 
); 
 343     int max 
= m_peer
->GetMaximum() ; 
 344     if ( outPart 
== 0 && max 
> 0 ) 
 346         // this is a hack, as unfortunately a hit on an already selected tab returns 0, 
 347         // so we have to go some extra miles to make sure we select something different 
 349         int val 
= m_peer
->GetValue() ; 
 353             m_peer
->SetMaximum( 2 ) ; 
 358             m_peer
->SetValue( maxval 
) ; 
 360              m_peer
->SetValue( 1 ) ; 
 362         err 
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart 
); 
 364         m_peer
->SetValue( val 
) ; 
 366             m_peer
->SetMaximum( 1 ) ; 
 369     if ( outPart 
>= 1 && outPart 
<= countPages 
) 
 370         resultV 
= outPart 
- 1 ; 
 371 #endif // TARGET_API_MAC_OSX 
 377         // we cannot differentiate better 
 379             *flags 
|= wxNB_HITTEST_ONLABEL
; 
 381             *flags 
|= wxNB_HITTEST_NOWHERE
; 
 387 // Added by Mark Newsam 
 388 // When a page is added or deleted to the notebook this function updates 
 389 // information held in the control so that it matches the order 
 390 // the user would expect. 
 392 void wxNotebook::MacSetupTabs() 
 394     m_peer
->SetMaximum( GetPageCount() ) ; 
 396     wxNotebookPage 
*page
; 
 397     ControlTabInfoRecV1 info
; 
 399     const size_t countPages 
= GetPageCount(); 
 400     for (size_t ii 
= 0; ii 
< countPages
; ii
++) 
 403         info
.version 
= kControlTabInfoVersionOne
; 
 404         info
.iconSuiteID 
= 0; 
 405         wxMacCFStringHolder 
cflabel( page
->GetLabel(), m_font
.GetEncoding() ) ; 
 406         info
.name 
= cflabel 
; 
 407         m_peer
->SetData
<ControlTabInfoRecV1
>( ii 
+ 1, kControlTabInfoTag
, &info 
) ; 
 409         if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 ) 
 411             const wxBitmap bmap 
= GetImageList()->GetBitmap( GetPageImage( ii 
) ) ; 
 414                 ControlButtonContentInfo info 
; 
 416                 wxMacCreateBitmapButton( &info
, bmap 
) ; 
 418                 OSStatus err 
= m_peer
->SetData
<ControlButtonContentInfo
>( ii 
+ 1, kControlTabImageContentTag
, &info 
); 
 419                 wxASSERT_MSG( err 
== noErr 
, wxT("Error when setting icon on tab") ) ; 
 421                 wxMacReleaseBitmapButton( &info 
) ; 
 425         m_peer
->SetTabEnabled( ii 
+ 1, true ) ; 
 429     m_peer
->GetRectInWindowCoords( &bounds 
) ; 
 430     InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds 
); 
 433 wxRect 
wxNotebook::GetPageRect() const 
 435     wxSize size 
= GetClientSize() ; 
 437     return wxRect( 0 , 0 , size
.x 
, size
.y 
) ; 
 440 // ---------------------------------------------------------------------------- 
 441 // wxNotebook callbacks 
 442 // ---------------------------------------------------------------------------- 
 444 // @@@ OnSize() is used for setting the font when it's called for the first 
 445 //     time because doing it in ::Create() doesn't work (for unknown reasons) 
 446 void wxNotebook::OnSize(wxSizeEvent
& event
) 
 448     unsigned int nCount 
= m_pages
.Count(); 
 449     wxRect rect 
= GetPageRect() ; 
 451     for ( unsigned int nPage 
= 0; nPage 
< nCount
; nPage
++ ) 
 453         wxNotebookPage 
*pPage 
= m_pages
[nPage
]; 
 454         pPage
->SetSize(rect
); 
 455         if ( pPage
->GetAutoLayout() ) 
 459     // Processing continues to next OnSize 
 463 void wxNotebook::OnSelChange(wxNotebookEvent
& event
) 
 465     // is it our tab control? 
 466     if ( event
.GetEventObject() == this ) 
 467         ChangePage(event
.GetOldSelection(), event
.GetSelection()); 
 469     // we want to give others a chance to process this message as well 
 473 void wxNotebook::OnSetFocus(wxFocusEvent
& event
) 
 475     // set focus to the currently selected page if any 
 476     if ( m_nSelection 
!= -1 ) 
 477         m_pages
[m_nSelection
]->SetFocus(); 
 482 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
) 
 484     if ( event
.IsWindowChange() ) 
 487         AdvanceSelection( event
.GetDirection() ); 
 491         // we get this event in 2 cases 
 493         // a) one of our pages might have generated it because the user TABbed 
 494         // out from it in which case we should propagate the event upwards and 
 495         // our parent will take care of setting the focus to prev/next sibling 
 499         // b) the parent panel wants to give the focus to us so that we 
 500         // forward it to our selected page. We can't deal with this in 
 501         // OnSetFocus() because we don't know which direction the focus came 
 502         // from in this case and so can't choose between setting the focus to 
 503         // first or last panel child 
 504         wxWindow 
*parent 
= GetParent(); 
 506         // the cast is here to fix a GCC ICE 
 507         if ( ((wxWindow
*)event
.GetEventObject()) == parent 
) 
 509             // no, it doesn't come from child, case (b): forward to a page 
 510             if ( m_nSelection 
!= -1 ) 
 512                 // so that the page knows that the event comes from it's parent 
 513                 // and is being propagated downwards 
 514                 event
.SetEventObject( this ); 
 516                 wxWindow 
*page 
= m_pages
[m_nSelection
]; 
 517                 if ( !page
->GetEventHandler()->ProcessEvent( event 
) ) 
 521                 //else: page manages focus inside it itself 
 525                 // we have no pages - still have to give focus to _something_ 
 531             // it comes from our child, case (a), pass to the parent 
 534                 event
.SetCurrentFocus( this ); 
 535                 parent
->GetEventHandler()->ProcessEvent( event 
); 
 541 // ---------------------------------------------------------------------------- 
 542 // wxNotebook base class virtuals 
 543 // ---------------------------------------------------------------------------- 
 545 #if wxUSE_CONSTRAINTS 
 547 // override these 2 functions to do nothing: everything is done in OnSize 
 549 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
)) 
 551     // don't set the sizes of the pages - their correct size is not yet known 
 552     wxControl::SetConstraintSizes( false ); 
 555 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
)) 
 560 #endif // wxUSE_CONSTRAINTS 
 562 void wxNotebook::Command(wxCommandEvent
& event
) 
 564     wxFAIL_MSG(wxT("wxNotebook::Command not implemented")); 
 567 // ---------------------------------------------------------------------------- 
 568 // wxNotebook helper functions 
 569 // ---------------------------------------------------------------------------- 
 571 // hide the currently active panel and show the new one 
 572 void wxNotebook::ChangePage(int nOldSel
, int nSel
) 
 578         m_pages
[nOldSel
]->Show( false ); 
 582         wxNotebookPage 
*pPage 
= m_pages
[nSel
]; 
 588     m_peer
->SetValue( m_nSelection 
+ 1 ) ; 
 591 wxInt32 
wxNotebook::MacControlHit(WXEVENTHANDLERREF 
WXUNUSED(handler
) , WXEVENTREF 
WXUNUSED(event
) ) 
 593     OSStatus status 
= eventNotHandledErr 
; 
 595     SInt32 newSel 
= m_peer
->GetValue() - 1 ; 
 596     if ( newSel 
!= m_nSelection 
) 
 598         wxNotebookEvent 
changing( 
 599             wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
, 
 600             newSel 
, m_nSelection 
); 
 601         changing
.SetEventObject( this ); 
 602         GetEventHandler()->ProcessEvent( changing 
); 
 604         if ( changing
.IsAllowed() ) 
 606             wxNotebookEvent 
event( 
 607                 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, 
 608                 newSel
, m_nSelection 
); 
 609             event
.SetEventObject( this ); 
 610             GetEventHandler()->ProcessEvent( event 
); 
 614             m_peer
->SetValue( m_nSelection 
+ 1 ) ; 
 620     return (wxInt32
)status 
;