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 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
24 #include "wx/string.h"
26 #include "wx/imaglist.h"
28 #include "wx/notebook.h"
29 #include "wx/mac/uma.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // check that the page index is valid
35 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
45 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
46 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
48 EVT_SIZE(wxNotebook::OnSize
)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
56 // ============================================================================
58 // ============================================================================
60 // ----------------------------------------------------------------------------
61 // wxNotebook construction
62 // ----------------------------------------------------------------------------
64 // common part of all ctors
65 void wxNotebook::Init()
70 // default for dynamic class
71 wxNotebook::wxNotebook()
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow
*parent
,
86 Create(parent
, id
, pos
, size
, style
, name
);
90 bool wxNotebook::Create(wxWindow
*parent
,
97 m_macIsUserPane
= false ;
99 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
102 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
104 if( bounds
.right
<= bounds
.left
)
105 bounds
.right
= bounds
.left
+ 100 ;
106 if ( bounds
.bottom
<= bounds
.top
)
107 bounds
.bottom
= bounds
.top
+ 100 ;
109 UInt16 tabstyle
= kControlTabDirectionNorth
;
110 if ( HasFlag(wxBK_LEFT
) )
111 tabstyle
= kControlTabDirectionWest
;
112 else if ( HasFlag( wxBK_RIGHT
) )
113 tabstyle
= kControlTabDirectionEast
;
114 else if ( HasFlag( wxBK_BOTTOM
) )
115 tabstyle
= kControlTabDirectionSouth
;
117 ControlTabSize tabsize
= kControlTabSizeLarge
;
118 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL
)
119 tabsize
= kControlTabSizeSmall
;
120 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
122 if (UMAGetSystemVersion() >= 0x1030 )
125 tabsize
= kControlSizeSmall
;
128 m_peer
= new wxMacControl(this) ;
129 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
130 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() ) );
133 MacPostControlCreate(pos
,size
) ;
138 wxNotebook::~wxNotebook()
142 // ----------------------------------------------------------------------------
143 // wxNotebook accessors
144 // ----------------------------------------------------------------------------
146 void wxNotebook::SetPadding(const wxSize
& padding
)
151 void wxNotebook::SetTabSize(const wxSize
& sz
)
156 void wxNotebook::SetPageSize(const wxSize
& size
)
158 SetSize( CalcSizeFromPage( size
) );
161 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
163 return DoGetSizeFromClientSize( sizePage
) ;
166 int wxNotebook::SetSelection(size_t nPage
)
168 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
170 if ( int(nPage
) != m_nSelection
)
172 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
173 event
.SetSelection(nPage
);
174 event
.SetOldSelection(m_nSelection
);
175 event
.SetEventObject(this);
176 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
178 // program allows the page change
179 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
180 (void)GetEventHandler()->ProcessEvent(event
);
182 ChangePage(m_nSelection
, nPage
);
189 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
191 wxASSERT( IS_VALID_PAGE(nPage
) );
193 wxNotebookPage
*page
= m_pages
[nPage
];
194 page
->SetLabel(strText
);
200 wxString
wxNotebook::GetPageText(size_t nPage
) const
202 wxASSERT( IS_VALID_PAGE(nPage
) );
204 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
, _T("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, _T("invalid notebook page") );
219 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
220 _T("invalid image index in SetPageImage()") );
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( IS_VALID_PAGE(nPage
), NULL
);
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();
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,
280 _T("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() ) {
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
)
307 // while this still is the same page showing, we need to update the tabs
308 m_peer
->SetValue( m_nSelection
+ 1 ) ;
311 // some page should be selected: either this one or the first one if there
312 // is still no selection
316 else if ( m_nSelection
== -1 )
320 SetSelection(selNew
);
322 InvalidateBestSize();
326 int wxNotebook::HitTest(const wxPoint
& pt
, long * flags
) const
328 int resultV
= wxNOT_FOUND
;
329 #if TARGET_API_MAC_OSX
330 const int countPages
= GetPageCount();
332 HIPoint hipoint
= { pt
.x
, pt
.y
} ;
333 HIViewPartCode outPart
= 0 ;
334 OSStatus err
= HIViewGetPartHit (
335 m_peer
->GetControlRef() ,
340 int max
= m_peer
->GetMaximum() ;
341 if ( outPart
== 0 && max
> 0 )
343 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
344 // so we have to go some extra miles to make sure we select something different
346 int val
= m_peer
->GetValue() ;
350 m_peer
->SetMaximum( 2 ) ;
355 m_peer
->SetValue( maxval
) ;
357 m_peer
->SetValue( 1 ) ;
359 err
= HIViewGetPartHit (
360 m_peer
->GetControlRef() ,
365 m_peer
->SetValue( val
) ;
368 m_peer
->SetMaximum( 1 ) ;
372 if ( outPart
>= 1 && outPart
<= countPages
)
376 #endif // TARGET_API_MAC_OSX
382 // we cannot differentiate better
384 *flags
|= wxNB_HITTEST_ONLABEL
;
386 *flags
|= wxNB_HITTEST_NOWHERE
;
391 /* Added by Mark Newsam
392 * When a page is added or deleted to the notebook this function updates
393 * information held in the control so that it matches the order
394 * the user would expect.
396 void wxNotebook::MacSetupTabs()
398 m_peer
->SetMaximum( GetPageCount() ) ;
400 wxNotebookPage
*page
;
401 ControlTabInfoRecV1 info
;
403 const size_t countPages
= GetPageCount();
404 for(size_t ii
= 0; ii
< countPages
; ii
++)
407 info
.version
= kControlTabInfoVersionOne
;
408 info
.iconSuiteID
= 0;
409 wxMacCFStringHolder
cflabel( page
->GetLabel() , m_font
.GetEncoding() ) ;
410 info
.name
= cflabel
;
411 m_peer
->SetData
<ControlTabInfoRecV1
>( ii
+1, kControlTabInfoTag
, &info
) ;
413 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
415 const wxBitmap bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
418 ControlButtonContentInfo info
;
420 wxMacCreateBitmapButton( &info
, bmap
) ;
423 #endif // __WXDEBUG__
424 m_peer
->SetData
<ControlButtonContentInfo
>( ii
+1,kControlTabImageContentTag
, &info
);
425 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
426 wxMacReleaseBitmapButton( &info
) ;
429 m_peer
->SetTabEnabled( ii
+ 1 , true ) ;
432 m_peer
->GetRectInWindowCoords( &bounds
) ;
433 InvalWindowRect((WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
436 wxRect
wxNotebook::GetPageRect() const
438 wxSize size
= GetClientSize() ;
439 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
441 // ----------------------------------------------------------------------------
442 // wxNotebook callbacks
443 // ----------------------------------------------------------------------------
445 // @@@ OnSize() is used for setting the font when it's called for the first
446 // time because doing it in ::Create() doesn't work (for unknown reasons)
447 void wxNotebook::OnSize(wxSizeEvent
& event
)
450 unsigned int nCount
= m_pages
.Count();
451 wxRect rect
= GetPageRect() ;
452 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
453 wxNotebookPage
*pPage
= m_pages
[nPage
];
454 pPage
->SetSize(rect
);
455 if ( pPage
->GetAutoLayout() ) {
460 // Processing continues to next OnSize
464 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
466 // is it our tab control?
467 if ( event
.GetEventObject() == this )
468 ChangePage(event
.GetOldSelection(), event
.GetSelection());
470 // we want to give others a chance to process this message as well
474 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
476 // set focus to the currently selected page if any
477 if ( m_nSelection
!= -1 )
478 m_pages
[m_nSelection
]->SetFocus();
483 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
485 if ( event
.IsWindowChange() ) {
487 AdvanceSelection(event
.GetDirection());
490 // we get this event in 2 cases
492 // a) one of our pages might have generated it because the user TABbed
493 // out from it in which case we should propagate the event upwards and
494 // our parent will take care of setting the focus to prev/next sibling
498 // b) the parent panel wants to give the focus to us so that we
499 // forward it to our selected page. We can't deal with this in
500 // OnSetFocus() because we don't know which direction the focus came
501 // from in this case and so can't choose between setting the focus to
502 // first or last panel child
503 wxWindow
*parent
= GetParent();
504 // the cast is here to fic a GCC ICE
505 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
507 // no, it doesn't come from child, case (b): forward to a page
508 if ( m_nSelection
!= -1 )
510 // so that the page knows that the event comes from it's parent
511 // and is being propagated downwards
512 event
.SetEventObject(this);
514 wxWindow
*page
= m_pages
[m_nSelection
];
515 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
519 //else: page manages focus inside it itself
523 // we have no pages - still have to give focus to _something_
529 // it comes from our child, case (a), pass to the parent
531 event
.SetCurrentFocus(this);
532 parent
->GetEventHandler()->ProcessEvent(event
);
538 // ----------------------------------------------------------------------------
539 // wxNotebook base class virtuals
540 // ----------------------------------------------------------------------------
542 #if wxUSE_CONSTRAINTS
544 // override these 2 functions to do nothing: everything is done in OnSize
546 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
548 // don't set the sizes of the pages - their correct size is not yet known
549 wxControl::SetConstraintSizes(false);
552 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
557 #endif // wxUSE_CONSTRAINTS
559 void wxNotebook::Command(wxCommandEvent
& event
)
561 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
564 // ----------------------------------------------------------------------------
565 // wxNotebook helper functions
566 // ----------------------------------------------------------------------------
568 // hide the currently active panel and show the new one
569 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
573 m_pages
[nOldSel
]->Show(false);
578 wxNotebookPage
*pPage
= m_pages
[nSel
];
584 m_peer
->SetValue( m_nSelection
+ 1 ) ;
587 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
589 OSStatus status
= eventNotHandledErr
;
591 SInt32 newSel
= m_peer
->GetValue() - 1 ;
592 if ( newSel
!= m_nSelection
)
594 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
595 newSel
, m_nSelection
);
596 changing
.SetEventObject(this);
597 GetEventHandler()->ProcessEvent(changing
);
599 if(changing
.IsAllowed())
601 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
602 newSel
, m_nSelection
);
603 event
.SetEventObject(this);
605 GetEventHandler()->ProcessEvent(event
);
609 m_peer
->SetValue( m_nSelection
+ 1 ) ;