1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "notebook.h"
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
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(-1, 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(wxNB_LEFT
) )
111 tabstyle
= kControlTabDirectionWest
;
112 else if ( HasFlag( wxNB_RIGHT
) )
113 tabstyle
= kControlTabDirectionEast
;
114 else if ( HasFlag( wxNB_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() ;
129 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
130 tabsize
, tabstyle
, 0, NULL
, *m_peer
) );
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 wxSize
wxNotebook::DoGetBestSize() const
168 // calculate the max page size
171 size_t count
= GetPageCount();
174 for ( size_t n
= 0; n
< count
; n
++ )
176 wxSize sizePage
= m_pages
[n
]->GetSize();
178 if ( size
.x
< sizePage
.x
)
180 if ( size
.y
< sizePage
.y
)
186 // use some arbitrary default size
191 return CalcSizeFromPage(size
);
194 int wxNotebook::SetSelection(size_t nPage
)
196 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
198 if ( int(nPage
) != m_nSelection
)
200 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
201 event
.SetSelection(nPage
);
202 event
.SetOldSelection(m_nSelection
);
203 event
.SetEventObject(this);
204 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
206 // program allows the page change
207 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
208 (void)GetEventHandler()->ProcessEvent(event
);
210 ChangePage(m_nSelection
, nPage
);
217 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
219 wxASSERT( IS_VALID_PAGE(nPage
) );
221 wxNotebookPage
*page
= m_pages
[nPage
];
222 page
->SetLabel(strText
);
228 wxString
wxNotebook::GetPageText(size_t nPage
) const
230 wxASSERT( IS_VALID_PAGE(nPage
) );
232 wxNotebookPage
*page
= m_pages
[nPage
];
233 return page
->GetLabel();
236 int wxNotebook::GetPageImage(size_t nPage
) const
238 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
240 return m_images
[nPage
];
243 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
245 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
247 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
248 _T("invalid image index in SetPageImage()") );
250 if ( nImage
!= m_images
[nPage
] )
252 // if the item didn't have an icon before or, on the contrary, did have
253 // it but has lost it now, its size will change - but if the icon just
255 m_images
[nPage
] = nImage
;
263 // ----------------------------------------------------------------------------
264 // wxNotebook operations
265 // ----------------------------------------------------------------------------
267 // remove one page from the notebook, without deleting the window
268 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
270 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
271 wxNotebookPage
* page
= m_pages
[nPage
] ;
272 m_pages
.RemoveAt(nPage
);
276 if(m_nSelection
>= (int)GetPageCount()) {
277 m_nSelection
= GetPageCount() - 1;
279 if(m_nSelection
>= 0) {
280 m_pages
[m_nSelection
]->Show(true);
286 bool wxNotebook::DeleteAllPages()
288 WX_CLEAR_ARRAY(m_pages
) ;
295 // same as AddPage() but does it at given position
296 bool wxNotebook::InsertPage(size_t nPage
,
297 wxNotebookPage
*pPage
,
298 const wxString
& strText
,
302 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
305 wxASSERT_MSG( pPage
->GetParent() == this,
306 _T("notebook pages must have notebook as parent") );
308 // don't show pages by default (we'll need to adjust their size first)
309 pPage
->Show( false ) ;
311 pPage
->SetLabel(strText
);
313 m_images
.Insert(imageId
, nPage
);
317 wxRect rect
= GetPageRect() ;
318 pPage
->SetSize(rect
);
319 if ( pPage
->GetAutoLayout() ) {
324 // now deal with the selection
325 // ---------------------------
327 // if the inserted page is before the selected one, we must update the
328 // index of the selected page
330 if ( int(nPage
) <= m_nSelection
)
333 // while this still is the same page showing, we need to update the tabs
334 SetControl32BitValue( *m_peer
, m_nSelection
+ 1 ) ;
337 // some page should be selected: either this one or the first one if there
338 // is still no selection
342 else if ( m_nSelection
== -1 )
346 SetSelection(selNew
);
351 /* Added by Mark Newsam
352 * When a page is added or deleted to the notebook this function updates
353 * information held in the control so that it matches the order
354 * the user would expect.
356 void wxNotebook::MacSetupTabs()
358 SetControl32BitMaximum( *m_peer
, GetPageCount() ) ;
360 wxNotebookPage
*page
;
361 ControlTabInfoRec info
;
363 const size_t countPages
= GetPageCount();
364 for(size_t ii
= 0; ii
< countPages
; ii
++)
368 info
.iconSuiteID
= 0;
369 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
371 SetControlData( *m_peer
, ii
+1, kControlTabInfoTag
,
372 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
373 SetTabEnabled( *m_peer
, ii
+1 , true ) ;
375 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
377 // tab controls only support very specific types of images, therefore we are doing an odyssee
378 // accross the icon worlds (even Apple DTS did not find a shorter path)
379 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
380 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
381 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
383 const wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
386 wxBitmap scaledBitmap
;
387 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
389 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
390 bmap
= &scaledBitmap
;
392 ControlButtonContentInfo info
;
393 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
394 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
395 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
396 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
398 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1, iconFamily
, &iconRef
) ;
399 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
400 info
.contentType
= kControlContentIconRef
;
401 info
.u
.iconRef
= iconRef
;
402 SetControlData( *m_peer
, ii
+1,kControlTabImageContentTag
,
403 sizeof( info
), (Ptr
)&info
);
404 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
405 if ( UMAGetSystemVersion() < 0x1030 )
407 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
410 ReleaseIconRef( iconRef
) ;
411 DisposeHandle( (Handle
) iconFamily
) ;
417 UMAGetControlBoundsInWindowCoords(*m_peer
, &bounds
);
418 InvalWindowRect((WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
421 wxRect
wxNotebook::GetPageRect() const
423 wxSize size
= GetClientSize() ;
424 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
426 // ----------------------------------------------------------------------------
427 // wxNotebook callbacks
428 // ----------------------------------------------------------------------------
430 // @@@ OnSize() is used for setting the font when it's called for the first
431 // time because doing it in ::Create() doesn't work (for unknown reasons)
432 void wxNotebook::OnSize(wxSizeEvent
& event
)
435 unsigned int nCount
= m_pages
.Count();
436 wxRect rect
= GetPageRect() ;
437 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
438 wxNotebookPage
*pPage
= m_pages
[nPage
];
439 pPage
->SetSize(rect
);
440 if ( pPage
->GetAutoLayout() ) {
445 // Processing continues to next OnSize
449 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
451 // is it our tab control?
452 if ( event
.GetEventObject() == this )
453 ChangePage(event
.GetOldSelection(), event
.GetSelection());
455 // we want to give others a chance to process this message as well
459 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
461 // set focus to the currently selected page if any
462 if ( m_nSelection
!= -1 )
463 m_pages
[m_nSelection
]->SetFocus();
468 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
470 if ( event
.IsWindowChange() ) {
472 AdvanceSelection(event
.GetDirection());
475 // we get this event in 2 cases
477 // a) one of our pages might have generated it because the user TABbed
478 // out from it in which case we should propagate the event upwards and
479 // our parent will take care of setting the focus to prev/next sibling
483 // b) the parent panel wants to give the focus to us so that we
484 // forward it to our selected page. We can't deal with this in
485 // OnSetFocus() because we don't know which direction the focus came
486 // from in this case and so can't choose between setting the focus to
487 // first or last panel child
488 wxWindow
*parent
= GetParent();
489 // the cast is here to fic a GCC ICE
490 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
492 // no, it doesn't come from child, case (b): forward to a page
493 if ( m_nSelection
!= -1 )
495 // so that the page knows that the event comes from it's parent
496 // and is being propagated downwards
497 event
.SetEventObject(this);
499 wxWindow
*page
= m_pages
[m_nSelection
];
500 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
504 //else: page manages focus inside it itself
508 // we have no pages - still have to give focus to _something_
514 // it comes from our child, case (a), pass to the parent
516 event
.SetCurrentFocus(this);
517 parent
->GetEventHandler()->ProcessEvent(event
);
523 // ----------------------------------------------------------------------------
524 // wxNotebook base class virtuals
525 // ----------------------------------------------------------------------------
527 #if wxUSE_CONSTRAINTS
529 // override these 2 functions to do nothing: everything is done in OnSize
531 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
533 // don't set the sizes of the pages - their correct size is not yet known
534 wxControl::SetConstraintSizes(FALSE
);
537 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
542 #endif // wxUSE_CONSTRAINTS
544 void wxNotebook::Command(wxCommandEvent
& event
)
546 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
549 // ----------------------------------------------------------------------------
550 // wxNotebook helper functions
551 // ----------------------------------------------------------------------------
553 // hide the currently active panel and show the new one
554 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
558 m_pages
[nOldSel
]->Show(FALSE
);
563 wxNotebookPage
*pPage
= m_pages
[nSel
];
569 SetControl32BitValue( *m_peer
, m_nSelection
+ 1 ) ;
572 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
574 OSStatus status
= eventNotHandledErr
;
576 SInt32 newSel
= GetControl32BitValue( *m_peer
) - 1 ;
577 if ( newSel
!= m_nSelection
)
579 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
580 newSel
, m_nSelection
);
581 changing
.SetEventObject(this);
582 GetEventHandler()->ProcessEvent(changing
);
584 if(changing
.IsAllowed())
586 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
587 newSel
, m_nSelection
);
588 event
.SetEventObject(this);
590 GetEventHandler()->ProcessEvent(event
);
594 SetControl32BitValue( *m_peer
, m_nSelection
+ 1 ) ;