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
= 0)
165 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
167 if ( int(nPage
) != m_nSelection
)
169 if (flags
& SetSelection_SendEvent
)
171 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
172 event
.SetSelection(nPage
);
173 event
.SetOldSelection(m_nSelection
);
174 event
.SetEventObject(this);
175 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
177 // program allows the page change
178 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
179 (void)GetEventHandler()->ProcessEvent(event
);
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
;
233 ChangePage(m_nSelection
, nPage
);
240 // ----------------------------------------------------------------------------
241 // wxNotebook operations
242 // ----------------------------------------------------------------------------
244 // remove one page from the notebook, without deleting the window
245 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
247 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
248 wxT("DoRemovePage: invalid notebook page") );
250 wxNotebookPage
* page
= m_pages
[nPage
] ;
251 m_pages
.RemoveAt(nPage
);
255 if (m_nSelection
>= (int)GetPageCount())
256 m_nSelection
= GetPageCount() - 1;
258 if (m_nSelection
>= 0)
259 m_pages
[m_nSelection
]->Show(true);
261 InvalidateBestSize();
267 bool wxNotebook::DeleteAllPages()
269 WX_CLEAR_ARRAY(m_pages
) ;
272 InvalidateBestSize();
277 // same as AddPage() but does it at given position
278 bool wxNotebook::InsertPage(size_t nPage
,
279 wxNotebookPage
*pPage
,
280 const wxString
& strText
,
284 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
287 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
289 // don't show pages by default (we'll need to adjust their size first)
290 pPage
->Show( false ) ;
292 pPage
->SetLabel( strText
);
294 m_images
.Insert( imageId
, nPage
);
298 wxRect rect
= GetPageRect() ;
299 pPage
->SetSize( rect
);
300 if ( pPage
->GetAutoLayout() )
303 // now deal with the selection
304 // ---------------------------
306 // if the inserted page is before the selected one, we must update the
307 // index of the selected page
309 if ( int(nPage
) <= m_nSelection
)
313 // while this still is the same page showing, we need to update the tabs
314 m_peer
->SetValue( m_nSelection
+ 1 ) ;
317 // some page should be selected: either this one or the first one if there
318 // is still no selection
322 else if ( m_nSelection
== -1 )
326 SetSelection( selNew
);
328 InvalidateBestSize();
333 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
335 int resultV
= wxNOT_FOUND
;
337 #if TARGET_API_MAC_OSX
338 const int countPages
= GetPageCount();
340 // we have to convert from Client to Window relative coordinates
341 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
342 // and now to HIView native ones
343 adjustedPt
.x
-= MacGetLeftBorderSize() ;
344 adjustedPt
.y
-= MacGetTopBorderSize() ;
346 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
347 HIViewPartCode outPart
= 0 ;
348 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
350 int max
= m_peer
->GetMaximum() ;
351 if ( outPart
== 0 && max
> 0 )
353 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
354 // so we have to go some extra miles to make sure we select something different
356 int val
= m_peer
->GetValue() ;
360 m_peer
->SetMaximum( 2 ) ;
365 m_peer
->SetValue( maxval
) ;
367 m_peer
->SetValue( 1 ) ;
369 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
371 m_peer
->SetValue( val
) ;
373 m_peer
->SetMaximum( 1 ) ;
376 if ( outPart
>= 1 && outPart
<= countPages
)
377 resultV
= outPart
- 1 ;
378 #endif // TARGET_API_MAC_OSX
384 // we cannot differentiate better
386 *flags
|= wxBK_HITTEST_ONLABEL
;
388 *flags
|= wxBK_HITTEST_NOWHERE
;
394 // Added by Mark Newsam
395 // When a page is added or deleted to the notebook this function updates
396 // information held in the control so that it matches the order
397 // the user would expect.
399 void wxNotebook::MacSetupTabs()
401 m_peer
->SetMaximum( GetPageCount() ) ;
403 wxNotebookPage
*page
;
404 ControlTabInfoRecV1 info
;
406 const size_t countPages
= GetPageCount();
407 for (size_t ii
= 0; ii
< countPages
; ii
++)
410 info
.version
= kControlTabInfoVersionOne
;
411 info
.iconSuiteID
= 0;
412 wxMacCFStringHolder
cflabel( page
->GetLabel(), m_font
.GetEncoding() ) ;
413 info
.name
= cflabel
;
414 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+ 1, kControlTabInfoTag
, &info
) ;
416 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
418 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage( ii
) ) ;
421 ControlButtonContentInfo info
;
423 wxMacCreateBitmapButton( &info
, bmap
) ;
425 OSStatus err
= m_peer
->SetData
<ControlButtonContentInfo
>( ii
+ 1, kControlTabImageContentTag
, &info
);
426 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
428 wxMacReleaseBitmapButton( &info
) ;
432 m_peer
->SetTabEnabled( ii
+ 1, true ) ;
436 m_peer
->GetRectInWindowCoords( &bounds
) ;
437 InvalWindowRect( (WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
440 wxRect
wxNotebook::GetPageRect() const
442 wxSize size
= GetClientSize() ;
444 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
447 // ----------------------------------------------------------------------------
448 // wxNotebook callbacks
449 // ----------------------------------------------------------------------------
451 // @@@ OnSize() is used for setting the font when it's called for the first
452 // time because doing it in ::Create() doesn't work (for unknown reasons)
453 void wxNotebook::OnSize(wxSizeEvent
& event
)
455 unsigned int nCount
= m_pages
.Count();
456 wxRect rect
= GetPageRect() ;
458 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
460 wxNotebookPage
*pPage
= m_pages
[nPage
];
461 pPage
->SetSize(rect
);
462 if ( pPage
->GetAutoLayout() )
466 // Processing continues to next OnSize
470 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
472 // is it our tab control?
473 if ( event
.GetEventObject() == this )
474 ChangePage(event
.GetOldSelection(), event
.GetSelection());
476 // we want to give others a chance to process this message as well
480 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
482 // set focus to the currently selected page if any
483 if ( m_nSelection
!= -1 )
484 m_pages
[m_nSelection
]->SetFocus();
489 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
491 if ( event
.IsWindowChange() )
494 AdvanceSelection( event
.GetDirection() );
498 // we get this event in 2 cases
500 // a) one of our pages might have generated it because the user TABbed
501 // out from it in which case we should propagate the event upwards and
502 // our parent will take care of setting the focus to prev/next sibling
506 // b) the parent panel wants to give the focus to us so that we
507 // forward it to our selected page. We can't deal with this in
508 // OnSetFocus() because we don't know which direction the focus came
509 // from in this case and so can't choose between setting the focus to
510 // first or last panel child
511 wxWindow
*parent
= GetParent();
513 // the cast is here to fix a GCC ICE
514 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
516 // no, it doesn't come from child, case (b): forward to a page
517 if ( m_nSelection
!= -1 )
519 // so that the page knows that the event comes from it's parent
520 // and is being propagated downwards
521 event
.SetEventObject( this );
523 wxWindow
*page
= m_pages
[m_nSelection
];
524 if ( !page
->GetEventHandler()->ProcessEvent( event
) )
528 //else: page manages focus inside it itself
532 // we have no pages - still have to give focus to _something_
538 // it comes from our child, case (a), pass to the parent
541 event
.SetCurrentFocus( this );
542 parent
->GetEventHandler()->ProcessEvent( event
);
548 // ----------------------------------------------------------------------------
549 // wxNotebook base class virtuals
550 // ----------------------------------------------------------------------------
552 #if wxUSE_CONSTRAINTS
554 // override these 2 functions to do nothing: everything is done in OnSize
556 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
558 // don't set the sizes of the pages - their correct size is not yet known
559 wxControl::SetConstraintSizes( false );
562 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
567 #endif // wxUSE_CONSTRAINTS
569 void wxNotebook::Command(wxCommandEvent
& event
)
571 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
574 // ----------------------------------------------------------------------------
575 // wxNotebook helper functions
576 // ----------------------------------------------------------------------------
578 // hide the currently active panel and show the new one
579 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
585 m_pages
[nOldSel
]->Show( false );
589 wxNotebookPage
*pPage
= m_pages
[nSel
];
595 m_peer
->SetValue( m_nSelection
+ 1 ) ;
598 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
600 OSStatus status
= eventNotHandledErr
;
602 SInt32 newSel
= m_peer
->GetValue() - 1 ;
603 if ( newSel
!= m_nSelection
)
605 wxNotebookEvent
changing(
606 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
607 newSel
, m_nSelection
);
608 changing
.SetEventObject( this );
609 GetEventHandler()->ProcessEvent( changing
);
611 if ( changing
.IsAllowed() )
613 wxNotebookEvent
event(
614 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
615 newSel
, m_nSelection
);
616 event
.SetEventObject( this );
617 GetEventHandler()->ProcessEvent( event
);
621 m_peer
->SetValue( m_nSelection
+ 1 ) ;
627 return (wxInt32
)status
;