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 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
129 tabsize
, tabstyle
, 0, NULL
, (ControlRef
*) &m_macControl
) );
131 MacPostControlCreate(pos
,size
) ;
136 wxNotebook::~wxNotebook()
140 // ----------------------------------------------------------------------------
141 // wxNotebook accessors
142 // ----------------------------------------------------------------------------
144 void wxNotebook::SetPadding(const wxSize
& padding
)
149 void wxNotebook::SetTabSize(const wxSize
& sz
)
154 void wxNotebook::SetPageSize(const wxSize
& size
)
156 SetSize( CalcSizeFromPage( size
) );
159 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
161 return DoGetSizeFromClientSize( sizePage
) ;
164 wxSize
wxNotebook::DoGetBestSize() const
166 // calculate the max page size
169 size_t count
= GetPageCount();
172 for ( size_t n
= 0; n
< count
; n
++ )
174 wxSize sizePage
= m_pages
[n
]->GetSize();
176 if ( size
.x
< sizePage
.x
)
178 if ( size
.y
< sizePage
.y
)
184 // use some arbitrary default size
189 return CalcSizeFromPage(size
);
192 int wxNotebook::SetSelection(size_t nPage
)
194 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
196 if ( int(nPage
) != m_nSelection
)
198 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
199 event
.SetSelection(nPage
);
200 event
.SetOldSelection(m_nSelection
);
201 event
.SetEventObject(this);
202 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
204 // program allows the page change
205 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
206 (void)GetEventHandler()->ProcessEvent(event
);
208 ChangePage(m_nSelection
, nPage
);
215 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
217 wxASSERT( IS_VALID_PAGE(nPage
) );
219 wxNotebookPage
*page
= m_pages
[nPage
];
220 page
->SetLabel(strText
);
226 wxString
wxNotebook::GetPageText(size_t nPage
) const
228 wxASSERT( IS_VALID_PAGE(nPage
) );
230 wxNotebookPage
*page
= m_pages
[nPage
];
231 return page
->GetLabel();
234 int wxNotebook::GetPageImage(size_t nPage
) const
236 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
238 return m_images
[nPage
];
241 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
243 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
245 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
246 _T("invalid image index in SetPageImage()") );
248 if ( nImage
!= m_images
[nPage
] )
250 // if the item didn't have an icon before or, on the contrary, did have
251 // it but has lost it now, its size will change - but if the icon just
253 m_images
[nPage
] = nImage
;
261 // ----------------------------------------------------------------------------
262 // wxNotebook operations
263 // ----------------------------------------------------------------------------
265 // remove one page from the notebook, without deleting the window
266 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
268 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
269 wxNotebookPage
* page
= m_pages
[nPage
] ;
270 m_pages
.RemoveAt(nPage
);
274 if(m_nSelection
>= (int)GetPageCount()) {
275 m_nSelection
= GetPageCount() - 1;
277 if(m_nSelection
>= 0) {
278 m_pages
[m_nSelection
]->Show(true);
284 bool wxNotebook::DeleteAllPages()
286 WX_CLEAR_ARRAY(m_pages
) ;
293 // same as AddPage() but does it at given position
294 bool wxNotebook::InsertPage(size_t nPage
,
295 wxNotebookPage
*pPage
,
296 const wxString
& strText
,
300 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
303 wxASSERT_MSG( pPage
->GetParent() == this,
304 _T("notebook pages must have notebook as parent") );
306 // don't show pages by default (we'll need to adjust their size first)
307 pPage
->Show( false ) ;
309 pPage
->SetLabel(strText
);
311 m_images
.Insert(imageId
, nPage
);
315 wxRect rect
= GetPageRect() ;
316 pPage
->SetSize(rect
);
317 if ( pPage
->GetAutoLayout() ) {
322 // now deal with the selection
323 // ---------------------------
325 // if the inserted page is before the selected one, we must update the
326 // index of the selected page
328 if ( int(nPage
) <= m_nSelection
)
331 // while this still is the same page showing, we need to update the tabs
332 SetControl32BitValue( (ControlRef
) m_macControl
, m_nSelection
+ 1 ) ;
335 // some page should be selected: either this one or the first one if there
336 // is still no selection
340 else if ( m_nSelection
== -1 )
344 SetSelection(selNew
);
349 /* Added by Mark Newsam
350 * When a page is added or deleted to the notebook this function updates
351 * information held in the m_macControl so that it matches the order
352 * the user would expect.
354 void wxNotebook::MacSetupTabs()
356 SetControl32BitMaximum( (ControlRef
) m_macControl
, GetPageCount() ) ;
358 wxNotebookPage
*page
;
359 ControlTabInfoRec info
;
361 const size_t countPages
= GetPageCount();
362 for(size_t ii
= 0; ii
< countPages
; ii
++)
366 info
.iconSuiteID
= 0;
367 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
369 SetControlData( (ControlRef
) m_macControl
, ii
+1, kControlTabInfoTag
,
370 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
371 SetTabEnabled( (ControlRef
) m_macControl
, ii
+1 , true ) ;
373 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
375 // tab controls only support very specific types of images, therefore we are doing an odyssee
376 // accross the icon worlds (even Apple DTS did not find a shorter path)
377 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
378 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
379 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
381 const wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
384 wxBitmap scaledBitmap
;
385 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
387 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
388 bmap
= &scaledBitmap
;
390 ControlButtonContentInfo info
;
391 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
392 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
393 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
394 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
396 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1, iconFamily
, &iconRef
) ;
397 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
398 info
.contentType
= kControlContentIconRef
;
399 info
.u
.iconRef
= iconRef
;
400 SetControlData( (ControlRef
) m_macControl
, ii
+1,kControlTabImageContentTag
,
401 sizeof( info
), (Ptr
)&info
);
402 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
403 if ( UMAGetSystemVersion() < 0x1030 )
405 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
408 ReleaseIconRef( iconRef
) ;
409 DisposeHandle( (Handle
) iconFamily
) ;
415 UMAGetControlBoundsInWindowCoords((ControlRef
)m_macControl
, &bounds
);
416 InvalWindowRect((WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
419 wxRect
wxNotebook::GetPageRect() const
421 wxSize size
= GetClientSize() ;
422 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
424 // ----------------------------------------------------------------------------
425 // wxNotebook callbacks
426 // ----------------------------------------------------------------------------
428 // @@@ OnSize() is used for setting the font when it's called for the first
429 // time because doing it in ::Create() doesn't work (for unknown reasons)
430 void wxNotebook::OnSize(wxSizeEvent
& event
)
433 unsigned int nCount
= m_pages
.Count();
434 wxRect rect
= GetPageRect() ;
435 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
436 wxNotebookPage
*pPage
= m_pages
[nPage
];
437 pPage
->SetSize(rect
);
438 if ( pPage
->GetAutoLayout() ) {
443 // Processing continues to next OnSize
447 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
449 // is it our tab control?
450 if ( event
.GetEventObject() == this )
451 ChangePage(event
.GetOldSelection(), event
.GetSelection());
453 // we want to give others a chance to process this message as well
457 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
459 // set focus to the currently selected page if any
460 if ( m_nSelection
!= -1 )
461 m_pages
[m_nSelection
]->SetFocus();
466 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
468 if ( event
.IsWindowChange() ) {
470 AdvanceSelection(event
.GetDirection());
473 // we get this event in 2 cases
475 // a) one of our pages might have generated it because the user TABbed
476 // out from it in which case we should propagate the event upwards and
477 // our parent will take care of setting the focus to prev/next sibling
481 // b) the parent panel wants to give the focus to us so that we
482 // forward it to our selected page. We can't deal with this in
483 // OnSetFocus() because we don't know which direction the focus came
484 // from in this case and so can't choose between setting the focus to
485 // first or last panel child
486 wxWindow
*parent
= GetParent();
487 // the cast is here to fic a GCC ICE
488 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
490 // no, it doesn't come from child, case (b): forward to a page
491 if ( m_nSelection
!= -1 )
493 // so that the page knows that the event comes from it's parent
494 // and is being propagated downwards
495 event
.SetEventObject(this);
497 wxWindow
*page
= m_pages
[m_nSelection
];
498 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
502 //else: page manages focus inside it itself
506 // we have no pages - still have to give focus to _something_
512 // it comes from our child, case (a), pass to the parent
514 event
.SetCurrentFocus(this);
515 parent
->GetEventHandler()->ProcessEvent(event
);
521 // ----------------------------------------------------------------------------
522 // wxNotebook base class virtuals
523 // ----------------------------------------------------------------------------
525 #if wxUSE_CONSTRAINTS
527 // override these 2 functions to do nothing: everything is done in OnSize
529 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
531 // don't set the sizes of the pages - their correct size is not yet known
532 wxControl::SetConstraintSizes(FALSE
);
535 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
540 #endif // wxUSE_CONSTRAINTS
542 void wxNotebook::Command(wxCommandEvent
& event
)
544 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
547 // ----------------------------------------------------------------------------
548 // wxNotebook helper functions
549 // ----------------------------------------------------------------------------
551 // hide the currently active panel and show the new one
552 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
556 m_pages
[nOldSel
]->Show(FALSE
);
561 wxNotebookPage
*pPage
= m_pages
[nSel
];
567 SetControl32BitValue( (ControlRef
) m_macControl
, m_nSelection
+ 1 ) ;
570 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
572 OSStatus status
= eventNotHandledErr
;
574 SInt32 newSel
= GetControl32BitValue( (ControlRef
) m_macControl
) - 1 ;
575 if ( newSel
!= m_nSelection
)
577 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
578 newSel
, m_nSelection
);
579 changing
.SetEventObject(this);
580 GetEventHandler()->ProcessEvent(changing
);
582 if(changing
.IsAllowed())
584 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
585 newSel
, m_nSelection
);
586 event
.SetEventObject(this);
588 GetEventHandler()->ProcessEvent(event
);
592 SetControl32BitValue( (ControlRef
) m_macControl
, m_nSelection
+ 1 ) ;