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
& 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::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 ) ;
431 m_peer
->GetRectInWindowCoords( &bounds
) ;
432 InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
435 wxRect
wxNotebook::GetPageRect() const
437 wxSize size
= GetClientSize() ;
439 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
442 // ----------------------------------------------------------------------------
443 // wxNotebook callbacks
444 // ----------------------------------------------------------------------------
446 // @@@ OnSize() is used for setting the font when it's called for the first
447 // time because doing it in ::Create() doesn't work (for unknown reasons)
448 void wxNotebook::OnSize(wxSizeEvent
& event
)
450 unsigned int nCount
= m_pages
.Count();
451 wxRect rect
= GetPageRect() ;
453 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
455 wxNotebookPage
*pPage
= m_pages
[nPage
];
456 pPage
->SetSize(rect
);
457 if ( pPage
->GetAutoLayout() )
461 // Processing continues to next OnSize
465 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
467 // is it our tab control?
468 if ( event
.GetEventObject() == this )
469 ChangePage(event
.GetOldSelection(), event
.GetSelection());
471 // we want to give others a chance to process this message as well
475 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
477 // set focus to the currently selected page if any
478 if ( m_nSelection
!= -1 )
479 m_pages
[m_nSelection
]->SetFocus();
484 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
486 if ( event
.IsWindowChange() )
489 AdvanceSelection( event
.GetDirection() );
493 // we get this event in 2 cases
495 // a) one of our pages might have generated it because the user TABbed
496 // out from it in which case we should propagate the event upwards and
497 // our parent will take care of setting the focus to prev/next sibling
501 // b) the parent panel wants to give the focus to us so that we
502 // forward it to our selected page. We can't deal with this in
503 // OnSetFocus() because we don't know which direction the focus came
504 // from in this case and so can't choose between setting the focus to
505 // first or last panel child
506 wxWindow
*parent
= GetParent();
508 // the cast is here to fix a GCC ICE
509 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
511 // no, it doesn't come from child, case (b): forward to a page
512 if ( m_nSelection
!= -1 )
514 // so that the page knows that the event comes from it's parent
515 // and is being propagated downwards
516 event
.SetEventObject( this );
518 wxWindow
*page
= m_pages
[m_nSelection
];
519 if ( !page
->GetEventHandler()->ProcessEvent( event
) )
523 //else: page manages focus inside it itself
527 // we have no pages - still have to give focus to _something_
533 // it comes from our child, case (a), pass to the parent
536 event
.SetCurrentFocus( this );
537 parent
->GetEventHandler()->ProcessEvent( event
);
543 // ----------------------------------------------------------------------------
544 // wxNotebook base class virtuals
545 // ----------------------------------------------------------------------------
547 #if wxUSE_CONSTRAINTS
549 // override these 2 functions to do nothing: everything is done in OnSize
551 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
553 // don't set the sizes of the pages - their correct size is not yet known
554 wxControl::SetConstraintSizes( false );
557 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
562 #endif // wxUSE_CONSTRAINTS
564 void wxNotebook::Command(wxCommandEvent
& event
)
566 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
569 // ----------------------------------------------------------------------------
570 // wxNotebook helper functions
571 // ----------------------------------------------------------------------------
573 // hide the currently active panel and show the new one
574 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
580 m_pages
[nOldSel
]->Show( false );
584 wxNotebookPage
*pPage
= m_pages
[nSel
];
590 m_peer
->SetValue( m_nSelection
+ 1 ) ;
593 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
595 OSStatus status
= eventNotHandledErr
;
597 SInt32 newSel
= m_peer
->GetValue() - 1 ;
598 if ( newSel
!= m_nSelection
)
600 wxNotebookEvent
changing(
601 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
602 newSel
, m_nSelection
);
603 changing
.SetEventObject( this );
604 GetEventHandler()->ProcessEvent( changing
);
606 if ( changing
.IsAllowed() )
608 wxNotebookEvent
event(
609 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
610 newSel
, m_nSelection
);
611 event
.SetEventObject( this );
612 GetEventHandler()->ProcessEvent( event
);
616 m_peer
->SetValue( m_nSelection
+ 1 ) ;
622 return (wxInt32
)status
;