1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "notebook.h"
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
23 #include "wx/wxprec.h"
26 #include "wx/string.h"
28 #include "wx/imaglist.h"
30 #include "wx/notebook.h"
31 #include "wx/mac/uma.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // check that the page index is valid
37 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
47 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
48 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
50 EVT_SIZE(wxNotebook::OnSize
)
51 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
52 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
56 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxNotebook construction
64 // ----------------------------------------------------------------------------
66 // common part of all ctors
67 void wxNotebook::Init()
72 // default for dynamic class
73 wxNotebook::wxNotebook()
78 // the same arguments as for wxControl
79 wxNotebook::wxNotebook(wxWindow
*parent
,
88 Create(parent
, id
, pos
, size
, style
, name
);
92 bool wxNotebook::Create(wxWindow
*parent
,
99 m_macIsUserPane
= FALSE
;
101 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
104 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
106 if( bounds
.right
<= bounds
.left
)
107 bounds
.right
= bounds
.left
+ 100 ;
108 if ( bounds
.bottom
<= bounds
.top
)
109 bounds
.bottom
= bounds
.top
+ 100 ;
111 UInt16 tabstyle
= kControlTabDirectionNorth
;
112 if ( HasFlag(wxNB_LEFT
) )
113 tabstyle
= kControlTabDirectionWest
;
114 else if ( HasFlag( wxNB_RIGHT
) )
115 tabstyle
= kControlTabDirectionEast
;
116 else if ( HasFlag( wxNB_BOTTOM
) )
117 tabstyle
= kControlTabDirectionSouth
;
119 ControlTabSize tabsize
= kControlTabSizeLarge
;
120 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL
)
121 tabsize
= kControlTabSizeSmall
;
122 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
124 if (UMAGetSystemVersion() >= 0x1030 )
127 tabsize
= kControlSizeSmall
;
130 m_peer
= new wxMacControl() ;
131 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
132 tabsize
, tabstyle
, 0, NULL
, m_peer
->GetControlRefAddr() ) );
135 MacPostControlCreate(pos
,size
) ;
140 wxNotebook::~wxNotebook()
144 // ----------------------------------------------------------------------------
145 // wxNotebook accessors
146 // ----------------------------------------------------------------------------
148 void wxNotebook::SetPadding(const wxSize
& padding
)
153 void wxNotebook::SetTabSize(const wxSize
& sz
)
158 void wxNotebook::SetPageSize(const wxSize
& size
)
160 SetSize( CalcSizeFromPage( size
) );
163 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
165 return DoGetSizeFromClientSize( sizePage
) ;
168 int wxNotebook::SetSelection(size_t nPage
)
170 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
172 if ( int(nPage
) != m_nSelection
)
174 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
175 event
.SetSelection(nPage
);
176 event
.SetOldSelection(m_nSelection
);
177 event
.SetEventObject(this);
178 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
180 // program allows the page change
181 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
182 (void)GetEventHandler()->ProcessEvent(event
);
184 ChangePage(m_nSelection
, nPage
);
191 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
193 wxASSERT( IS_VALID_PAGE(nPage
) );
195 wxNotebookPage
*page
= m_pages
[nPage
];
196 page
->SetLabel(strText
);
202 wxString
wxNotebook::GetPageText(size_t nPage
) const
204 wxASSERT( IS_VALID_PAGE(nPage
) );
206 wxNotebookPage
*page
= m_pages
[nPage
];
207 return page
->GetLabel();
210 int wxNotebook::GetPageImage(size_t nPage
) const
212 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
214 return m_images
[nPage
];
217 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
219 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
221 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
222 _T("invalid image index in SetPageImage()") );
224 if ( nImage
!= m_images
[nPage
] )
226 // if the item didn't have an icon before or, on the contrary, did have
227 // it but has lost it now, its size will change - but if the icon just
229 m_images
[nPage
] = nImage
;
237 // ----------------------------------------------------------------------------
238 // wxNotebook operations
239 // ----------------------------------------------------------------------------
241 // remove one page from the notebook, without deleting the window
242 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
244 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
245 wxNotebookPage
* page
= m_pages
[nPage
] ;
246 m_pages
.RemoveAt(nPage
);
250 if(m_nSelection
>= (int)GetPageCount()) {
251 m_nSelection
= GetPageCount() - 1;
253 if(m_nSelection
>= 0) {
254 m_pages
[m_nSelection
]->Show(true);
256 InvalidateBestSize();
261 bool wxNotebook::DeleteAllPages()
263 WX_CLEAR_ARRAY(m_pages
) ;
266 InvalidateBestSize();
271 // same as AddPage() but does it at given position
272 bool wxNotebook::InsertPage(size_t nPage
,
273 wxNotebookPage
*pPage
,
274 const wxString
& strText
,
278 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
281 wxASSERT_MSG( pPage
->GetParent() == this,
282 _T("notebook pages must have notebook as parent") );
284 // don't show pages by default (we'll need to adjust their size first)
285 pPage
->Show( false ) ;
287 pPage
->SetLabel(strText
);
289 m_images
.Insert(imageId
, nPage
);
293 wxRect rect
= GetPageRect() ;
294 pPage
->SetSize(rect
);
295 if ( pPage
->GetAutoLayout() ) {
300 // now deal with the selection
301 // ---------------------------
303 // if the inserted page is before the selected one, we must update the
304 // index of the selected page
306 if ( int(nPage
) <= m_nSelection
)
309 // while this still is the same page showing, we need to update the tabs
310 m_peer
->SetValue( m_nSelection
+ 1 ) ;
313 // some page should be selected: either this one or the first one if there
314 // is still no selection
318 else if ( m_nSelection
== -1 )
322 SetSelection(selNew
);
324 InvalidateBestSize();
328 /* Added by Mark Newsam
329 * When a page is added or deleted to the notebook this function updates
330 * information held in the control so that it matches the order
331 * the user would expect.
333 void wxNotebook::MacSetupTabs()
335 m_peer
->SetMaximum( GetPageCount() ) ;
337 wxNotebookPage
*page
;
338 ControlTabInfoRec info
;
340 const size_t countPages
= GetPageCount();
341 for(size_t ii
= 0; ii
< countPages
; ii
++)
345 info
.iconSuiteID
= 0;
346 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
347 m_peer
->SetData
<ControlTabInfoRec
>( ii
+1, kControlTabInfoTag
, &info
) ;
348 m_peer
->SetTabEnabled( ii
+ 1 , true ) ;
350 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
352 // tab controls only support very specific types of images, therefore we are doing an odyssee
353 // accross the icon worlds (even Apple DTS did not find a shorter path)
354 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
355 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
356 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
358 const wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
361 wxBitmap scaledBitmap
;
362 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
364 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
365 bmap
= &scaledBitmap
;
367 ControlButtonContentInfo info
;
368 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
369 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
370 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
371 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
373 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1, iconFamily
, &iconRef
) ;
374 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
375 info
.contentType
= kControlContentIconRef
;
376 info
.u
.iconRef
= iconRef
;
377 m_peer
->SetData
<ControlButtonContentInfo
>( ii
+1,kControlTabImageContentTag
, &info
);
378 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
379 if ( UMAGetSystemVersion() < 0x1030 )
381 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
384 ReleaseIconRef( iconRef
) ;
385 DisposeHandle( (Handle
) iconFamily
) ;
391 m_peer
->GetRectInWindowCoords( &bounds
) ;
392 InvalWindowRect((WindowRef
)MacGetTopLevelWindowRef(), &bounds
);
395 wxRect
wxNotebook::GetPageRect() const
397 wxSize size
= GetClientSize() ;
398 return wxRect( 0 , 0 , size
.x
, size
.y
) ;
400 // ----------------------------------------------------------------------------
401 // wxNotebook callbacks
402 // ----------------------------------------------------------------------------
404 // @@@ OnSize() is used for setting the font when it's called for the first
405 // time because doing it in ::Create() doesn't work (for unknown reasons)
406 void wxNotebook::OnSize(wxSizeEvent
& event
)
409 unsigned int nCount
= m_pages
.Count();
410 wxRect rect
= GetPageRect() ;
411 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
412 wxNotebookPage
*pPage
= m_pages
[nPage
];
413 pPage
->SetSize(rect
);
414 if ( pPage
->GetAutoLayout() ) {
419 // Processing continues to next OnSize
423 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
425 // is it our tab control?
426 if ( event
.GetEventObject() == this )
427 ChangePage(event
.GetOldSelection(), event
.GetSelection());
429 // we want to give others a chance to process this message as well
433 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
435 // set focus to the currently selected page if any
436 if ( m_nSelection
!= -1 )
437 m_pages
[m_nSelection
]->SetFocus();
442 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
444 if ( event
.IsWindowChange() ) {
446 AdvanceSelection(event
.GetDirection());
449 // we get this event in 2 cases
451 // a) one of our pages might have generated it because the user TABbed
452 // out from it in which case we should propagate the event upwards and
453 // our parent will take care of setting the focus to prev/next sibling
457 // b) the parent panel wants to give the focus to us so that we
458 // forward it to our selected page. We can't deal with this in
459 // OnSetFocus() because we don't know which direction the focus came
460 // from in this case and so can't choose between setting the focus to
461 // first or last panel child
462 wxWindow
*parent
= GetParent();
463 // the cast is here to fic a GCC ICE
464 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
466 // no, it doesn't come from child, case (b): forward to a page
467 if ( m_nSelection
!= -1 )
469 // so that the page knows that the event comes from it's parent
470 // and is being propagated downwards
471 event
.SetEventObject(this);
473 wxWindow
*page
= m_pages
[m_nSelection
];
474 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
478 //else: page manages focus inside it itself
482 // we have no pages - still have to give focus to _something_
488 // it comes from our child, case (a), pass to the parent
490 event
.SetCurrentFocus(this);
491 parent
->GetEventHandler()->ProcessEvent(event
);
497 // ----------------------------------------------------------------------------
498 // wxNotebook base class virtuals
499 // ----------------------------------------------------------------------------
501 #if wxUSE_CONSTRAINTS
503 // override these 2 functions to do nothing: everything is done in OnSize
505 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
507 // don't set the sizes of the pages - their correct size is not yet known
508 wxControl::SetConstraintSizes(FALSE
);
511 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
516 #endif // wxUSE_CONSTRAINTS
518 void wxNotebook::Command(wxCommandEvent
& event
)
520 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
523 // ----------------------------------------------------------------------------
524 // wxNotebook helper functions
525 // ----------------------------------------------------------------------------
527 // hide the currently active panel and show the new one
528 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
532 m_pages
[nOldSel
]->Show(FALSE
);
537 wxNotebookPage
*pPage
= m_pages
[nSel
];
543 m_peer
->SetValue( m_nSelection
+ 1 ) ;
546 wxInt32
wxNotebook::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
548 OSStatus status
= eventNotHandledErr
;
550 SInt32 newSel
= m_peer
->GetValue() - 1 ;
551 if ( newSel
!= m_nSelection
)
553 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
554 newSel
, m_nSelection
);
555 changing
.SetEventObject(this);
556 GetEventHandler()->ProcessEvent(changing
);
558 if(changing
.IsAllowed())
560 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
561 newSel
, m_nSelection
);
562 event
.SetEventObject(this);
564 GetEventHandler()->ProcessEvent(event
);
568 m_peer
->SetValue( m_nSelection
+ 1 ) ;