1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/notebook_osx.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/osx/private.h"
30 // check that the page index is valid
31 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
33 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
34 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
36 EVT_SIZE(wxNotebook::OnSize
)
37 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
38 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
41 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
43 bool wxNotebook::Create( wxWindow
*parent
,
48 const wxString
& name
)
50 m_macIsUserPane
= false ;
52 if (! (style
& wxBK_ALIGN_MASK
))
55 if ( !wxNotebookBase::Create( parent
, id
, pos
, size
, style
, name
) )
58 m_peer
= wxWidgetImpl::CreateTabView(this,parent
, id
, pos
, size
, style
, GetExtraStyle() );
60 MacPostControlCreate( pos
, size
);
66 wxNotebook::~wxNotebook()
70 // ----------------------------------------------------------------------------
71 // wxNotebook accessors
72 // ----------------------------------------------------------------------------
74 void wxNotebook::SetPadding(const wxSize
& WXUNUSED(padding
))
79 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
84 void wxNotebook::SetPageSize(const wxSize
& size
)
86 SetSize( CalcSizeFromPage( size
) );
89 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
91 return DoGetSizeFromClientSize( sizePage
);
94 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
96 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("DoSetSelection: invalid notebook page") );
98 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
100 if ( flags
& SetSelection_SendEvent
)
102 if ( !SendPageChangingEvent(nPage
) )
107 //else: program allows the page change
109 SendPageChangedEvent(m_selection
, nPage
);
112 ChangePage(m_selection
, nPage
);
119 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
121 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("SetPageText: invalid notebook page") );
123 wxNotebookPage
*page
= m_pages
[nPage
];
124 page
->SetLabel(wxStripMenuCodes(strText
));
130 wxString
wxNotebook::GetPageText(size_t nPage
) const
132 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("GetPageText: invalid notebook page") );
134 wxNotebookPage
*page
= m_pages
[nPage
];
136 return page
->GetLabel();
139 int wxNotebook::GetPageImage(size_t nPage
) const
141 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("GetPageImage: invalid notebook page") );
143 return m_images
[nPage
];
146 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
148 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false,
149 wxT("SetPageImage: invalid notebook page") );
150 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
151 wxT("SetPageImage: invalid image index") );
153 if ( nImage
!= m_images
[nPage
] )
155 // if the item didn't have an icon before or, on the contrary, did have
156 // it but has lost it now, its size will change - but if the icon just
158 m_images
[nPage
] = nImage
;
166 // ----------------------------------------------------------------------------
167 // wxNotebook operations
168 // ----------------------------------------------------------------------------
170 // remove one page from the notebook, without deleting the window
171 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
173 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
,
174 wxT("DoRemovePage: invalid notebook page") );
176 wxNotebookPage
* page
= m_pages
[nPage
] ;
177 m_pages
.RemoveAt(nPage
);
181 if (m_selection
>= (int)GetPageCount())
182 m_selection
= GetPageCount() - 1;
184 if (m_selection
>= 0)
185 m_pages
[m_selection
]->Show(true);
187 InvalidateBestSize();
193 bool wxNotebook::DeleteAllPages()
195 WX_CLEAR_ARRAY(m_pages
) ;
197 m_selection
= wxNOT_FOUND
;
198 InvalidateBestSize();
203 // same as AddPage() but does it at given position
204 bool wxNotebook::InsertPage(size_t nPage
,
205 wxNotebookPage
*pPage
,
206 const wxString
& strText
,
210 if ( !wxNotebookBase::InsertPage( nPage
, pPage
, strText
, bSelect
, imageId
) )
213 wxASSERT_MSG( pPage
->GetParent() == this, wxT("notebook pages must have notebook as parent") );
215 // don't show pages by default (we'll need to adjust their size first)
216 pPage
->Show( false ) ;
218 pPage
->SetLabel( wxStripMenuCodes(strText
) );
220 m_images
.Insert( imageId
, nPage
);
224 wxRect rect
= GetPageRect() ;
225 pPage
->SetSize( rect
);
226 if ( pPage
->GetAutoLayout() )
229 // now deal with the selection
230 // ---------------------------
232 // if the inserted page is before the selected one, we must update the
233 // index of the selected page
235 if ( int(nPage
) <= m_selection
)
239 // while this still is the same page showing, we need to update the tabs
240 m_peer
->SetValue( m_selection
+ 1 ) ;
243 DoSetSelectionAfterInsertion(nPage
, bSelect
);
245 InvalidateBestSize();
250 int wxNotebook::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(flags
)) const
252 int resultV
= wxNOT_FOUND
;
254 const int countPages
= GetPageCount();
256 // we have to convert from Client to Window relative coordinates
257 wxPoint adjustedPt
= pt
+ GetClientAreaOrigin();
258 // and now to HIView native ones
259 adjustedPt
.x
-= MacGetLeftBorderSize() ;
260 adjustedPt
.y
-= MacGetTopBorderSize() ;
262 HIPoint hipoint
= { adjustedPt
.x
, adjustedPt
.y
} ;
263 HIViewPartCode outPart
= 0 ;
264 OSStatus err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
266 int max
= m_peer
->GetMaximum() ;
267 if ( outPart
== 0 && max
> 0 )
269 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
270 // so we have to go some extra miles to make sure we select something different
272 int val
= m_peer
->GetValue() ;
276 m_peer
->SetMaximum( 2 ) ;
281 m_peer
->SetValue( maxval
) ;
283 m_peer
->SetValue( 1 ) ;
285 err
= HIViewGetPartHit( m_peer
->GetControlRef(), &hipoint
, &outPart
);
287 m_peer
->SetValue( val
) ;
289 m_peer
->SetMaximum( 1 ) ;
292 if ( outPart
>= 1 && outPart
<= countPages
)
293 resultV
= outPart
- 1 ;
299 // we cannot differentiate better
301 *flags
|= wxBK_HITTEST_ONLABEL
;
303 *flags
|= wxBK_HITTEST_NOWHERE
;
309 // Added by Mark Newsam
310 // When a page is added or deleted to the notebook this function updates
311 // information held in the control so that it matches the order
312 // the user would expect.
314 void wxNotebook::MacSetupTabs()
316 m_peer
->SetupTabs(*this);
320 wxRect
wxNotebook::GetPageRect() const
322 wxSize size
= GetClientSize() ;
324 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
327 // ----------------------------------------------------------------------------
328 // wxNotebook callbacks
329 // ----------------------------------------------------------------------------
331 // @@@ OnSize() is used for setting the font when it's called for the first
332 // time because doing it in ::Create() doesn't work (for unknown reasons)
333 void wxNotebook::OnSize(wxSizeEvent
& event
)
335 unsigned int nCount
= m_pages
.Count();
336 wxRect rect
= GetPageRect() ;
338 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ )
340 wxNotebookPage
*pPage
= m_pages
[nPage
];
341 pPage
->SetSize(rect
, wxSIZE_FORCE_EVENT
);
344 // If the selected page is hidden at this point, the notebook
345 // has become visible for the first time after creation, and
346 // we postponed showing the page in ChangePage().
347 // So show the selected page now.
348 if ( m_selection
!= wxNOT_FOUND
)
350 wxNotebookPage
*pPage
= m_pages
[m_selection
];
351 if ( !pPage
->IsShown() )
358 // Processing continues to next OnSize
362 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
364 // is it our tab control?
365 if ( event
.GetEventObject() == this )
366 ChangePage(event
.GetOldSelection(), event
.GetSelection());
368 // we want to give others a chance to process this message as well
372 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
374 // set focus to the currently selected page if any
375 if ( m_selection
!= wxNOT_FOUND
)
376 m_pages
[m_selection
]->SetFocus();
381 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
383 if ( event
.IsWindowChange() )
386 AdvanceSelection( event
.GetDirection() );
390 // we get this event in 2 cases
392 // a) one of our pages might have generated it because the user TABbed
393 // out from it in which case we should propagate the event upwards and
394 // our parent will take care of setting the focus to prev/next sibling
398 // b) the parent panel wants to give the focus to us so that we
399 // forward it to our selected page. We can't deal with this in
400 // OnSetFocus() because we don't know which direction the focus came
401 // from in this case and so can't choose between setting the focus to
402 // first or last panel child
403 wxWindow
*parent
= GetParent();
405 // the cast is here to fix a GCC ICE
406 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
408 // no, it doesn't come from child, case (b): forward to a page
409 if ( m_selection
!= wxNOT_FOUND
)
411 // so that the page knows that the event comes from it's parent
412 // and is being propagated downwards
413 event
.SetEventObject( this );
415 wxWindow
*page
= m_pages
[m_selection
];
416 if ( !page
->HandleWindowEvent( event
) )
420 //else: page manages focus inside it itself
424 // we have no pages - still have to give focus to _something_
430 // it comes from our child, case (a), pass to the parent
433 event
.SetCurrentFocus( this );
434 parent
->HandleWindowEvent( event
);
440 // ----------------------------------------------------------------------------
441 // wxNotebook base class virtuals
442 // ----------------------------------------------------------------------------
444 #if wxUSE_CONSTRAINTS
446 // override these 2 functions to do nothing: everything is done in OnSize
448 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
450 // don't set the sizes of the pages - their correct size is not yet known
451 wxControl::SetConstraintSizes( false );
454 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
459 #endif // wxUSE_CONSTRAINTS
461 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
463 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
466 // ----------------------------------------------------------------------------
467 // wxNotebook helper functions
468 // ----------------------------------------------------------------------------
470 // hide the currently active panel and show the new one
471 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
476 if ( nOldSel
!= wxNOT_FOUND
)
477 m_pages
[nOldSel
]->Show( false );
479 if ( nSel
!= wxNOT_FOUND
)
481 wxNotebookPage
*pPage
= m_pages
[nSel
];
482 if ( IsShownOnScreen() )
489 // Postpone Show() until the control is actually shown.
490 // Otherwise this forces the containing toplevel window
491 // to show, even if it's just being created and called
492 // AddPage() without intent to show the window yet.
493 // We Show() the selected page in our OnSize handler,
494 // unless it already is shown.
499 m_peer
->SetValue( m_selection
+ 1 ) ;
502 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec
) )
504 bool status
= false ;
506 SInt32 newSel
= m_peer
->GetValue() - 1 ;
507 if ( newSel
!= m_selection
)
509 wxBookCtrlEvent
changing(
510 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
511 newSel
, m_selection
);
512 changing
.SetEventObject( this );
513 HandleWindowEvent( changing
);
515 if ( changing
.IsAllowed() )
517 wxBookCtrlEvent
event(
518 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
519 newSel
, m_selection
);
520 event
.SetEventObject( this );
521 HandleWindowEvent( event
);
525 m_peer
->SetValue( m_selection
+ 1 ) ;