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 #if !USE_SHARED_LIBRARIES
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
46 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
47 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
48 EVT_MOUSE_EVENTS(wxNotebook::OnMouse
)
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
)
59 // ============================================================================
61 // ============================================================================
63 // The Appearance Manager docs show using tab controls in either edge to edge
64 // mode, or inset. I think edge to edge conforms better to the other ports,
65 // and inset mode is better accomplished with space around the wxNotebook rather
66 // than within it. --Robin
67 #define wxMAC_EDGE_TO_EDGE 1
69 static inline int wxMacTabMargin(long nbStyle
, long side
)
71 static int tabMargin
= -1;
72 static int otherMargin
= -1;
76 if ( UMAHasAquaLayout() )
78 tabMargin
= 26; // From Appearance Manager docs for small tab control dimensions
79 #if wxMAC_EDGE_TO_EDGE
88 #if wxMAC_EDGE_TO_EDGE
96 // If the style matches the side asked for then return the tab margin,
97 // but we have to special case wxNB_TOP since it is zero...
98 if ( side
== wxNB_TOP
)
100 if ( nbStyle
!= 0 && nbStyle
& (wxNB_LEFT
|wxNB_RIGHT
|wxNB_BOTTOM
))
109 else if ( nbStyle
& side
)
115 static inline int wxMacTabLeftMargin(long style
)
117 return wxMacTabMargin(style
, wxNB_LEFT
);
120 static inline int wxMacTabTopMargin(long style
)
122 return wxMacTabMargin(style
, wxNB_TOP
);
125 static inline int wxMacTabRightMargin(long style
)
127 return wxMacTabMargin(style
, wxNB_RIGHT
);
130 static inline int wxMacTabBottomMargin(long style
)
132 return wxMacTabMargin(style
, wxNB_BOTTOM
);
135 // ----------------------------------------------------------------------------
136 // wxNotebook construction
137 // ----------------------------------------------------------------------------
139 // common part of all ctors
140 void wxNotebook::Init()
142 if ( UMAHasAquaLayout() )
144 // Should these depend on wxMAC_EDGE_TO_EDGE too?
145 m_macHorizontalBorder
= 7;
146 m_macVerticalBorder
= 8;
152 // default for dynamic class
153 wxNotebook::wxNotebook()
158 // the same arguments as for wxControl
159 wxNotebook::wxNotebook(wxWindow
*parent
,
164 const wxString
& name
)
168 Create(parent
, id
, pos
, size
, style
, name
);
172 bool wxNotebook::Create(wxWindow
*parent
,
177 const wxString
& name
)
179 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
185 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
187 int tabstyle
= kControlTabSmallNorthProc
;
188 if ( HasFlag(wxNB_LEFT
) )
189 tabstyle
= kControlTabSmallWestProc
;
190 else if ( HasFlag( wxNB_RIGHT
) )
191 tabstyle
= kControlTabSmallEastProc
;
192 else if ( HasFlag( wxNB_BOTTOM
) )
193 tabstyle
= kControlTabSmallSouthProc
;
196 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
197 tabstyle
, (long) this ) ;
199 MacPostControlCreate() ;
204 wxNotebook::~wxNotebook()
208 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
210 wxSize sizeTotal
= sizePage
;
213 wxGetOsVersion( &major
, &minor
);
215 // Mac has large notebook borders. Aqua even more so.
217 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
243 // ----------------------------------------------------------------------------
244 // wxNotebook accessors
245 // ----------------------------------------------------------------------------
247 void wxNotebook::SetPadding(const wxSize
& padding
)
249 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
252 void wxNotebook::SetTabSize(const wxSize
& sz
)
254 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
257 void wxNotebook::SetPageSize(const wxSize
& size
)
259 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
262 int wxNotebook::SetSelection(size_t nPage
)
264 if( !IS_VALID_PAGE(nPage
) )
265 return m_nSelection
;
267 ChangePage(m_nSelection
, nPage
);
268 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
274 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
276 wxASSERT( IS_VALID_PAGE(nPage
) );
278 wxNotebookPage
*page
= m_pages
[nPage
];
279 page
->SetLabel(strText
);
285 wxString
wxNotebook::GetPageText(size_t nPage
) const
287 wxASSERT( IS_VALID_PAGE(nPage
) );
289 wxNotebookPage
*page
= m_pages
[nPage
];
290 return page
->GetLabel();
293 int wxNotebook::GetPageImage(size_t nPage
) const
295 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
297 return m_images
[nPage
];
300 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
302 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
304 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
305 _T("invalid image index in SetPageImage()") );
307 if ( nImage
!= m_images
[nPage
] )
309 // if the item didn't have an icon before or, on the contrary, did have
310 // it but has lost it now, its size will change - but if the icon just
312 m_images
[nPage
] = nImage
;
320 // ----------------------------------------------------------------------------
321 // wxNotebook operations
322 // ----------------------------------------------------------------------------
324 // remove one page from the notebook, without deleting the window
325 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
327 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
328 wxNotebookPage
* page
= m_pages
[nPage
] ;
329 m_pages
.RemoveAt(nPage
);
333 if(m_nSelection
>= (int)GetPageCount()) {
334 m_nSelection
= GetPageCount() - 1;
336 if(m_nSelection
>= 0) {
337 m_pages
[m_nSelection
]->Show(true);
343 bool wxNotebook::DeleteAllPages()
345 WX_CLEAR_ARRAY(m_pages
) ;
352 // same as AddPage() but does it at given position
353 bool wxNotebook::InsertPage(size_t nPage
,
354 wxNotebookPage
*pPage
,
355 const wxString
& strText
,
359 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
362 pPage
->SetLabel(strText
);
364 m_images
.Insert(imageId
, nPage
);
369 m_nSelection
= nPage
;
371 else if ( m_nSelection
== -1 ) {
374 else if ((size_t)m_nSelection
>= nPage
) {
377 // don't show pages by default (we'll need to adjust their size first)
378 pPage
->Show( false ) ;
382 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
383 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
384 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
385 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
386 if ( pPage
->GetAutoLayout() ) {
393 /* Added by Mark Newsam
394 * When a page is added or deleted to the notebook this function updates
395 * information held in the m_macControl so that it matches the order
396 * the user would expect.
398 void wxNotebook::MacSetupTabs()
400 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
402 wxNotebookPage
*page
;
403 ControlTabInfoRec info
;
405 const size_t countPages
= GetPageCount();
406 for(size_t ii
= 0; ii
< countPages
; ii
++)
410 info
.iconSuiteID
= 0;
411 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
413 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
414 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
415 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
417 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
419 // tab controls only support very specific types of images, therefore we are doing an odyssee
420 // accross the icon worlds (even Apple DTS did not find a shorter path)
421 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
422 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
423 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
425 wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
428 wxBitmap scaledBitmap
;
429 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
431 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
432 bmap
= &scaledBitmap
;
434 ControlButtonContentInfo info
;
435 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
436 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
437 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
438 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
440 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1 , iconFamily
, &iconRef
) ;
441 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
442 info
.contentType
= kControlContentIconRef
;
443 info
.u
.iconRef
= iconRef
;
444 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
445 sizeof( info
), (Ptr
)&info
);
446 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
447 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
448 ReleaseIconRef( iconRef
) ;
449 DisposeHandle( (Handle
) iconFamily
) ;
455 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
456 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
459 // ----------------------------------------------------------------------------
460 // wxNotebook callbacks
461 // ----------------------------------------------------------------------------
463 // @@@ OnSize() is used for setting the font when it's called for the first
464 // time because doing it in ::Create() doesn't work (for unknown reasons)
465 void wxNotebook::OnSize(wxSizeEvent
& event
)
467 // emulate page change (it's esp. important to do it first time because
468 // otherwise our page would stay invisible)
469 int nSel
= m_nSelection
;
473 // fit the notebook page to the tab control's display area
477 unsigned int nCount
= m_pages
.Count();
478 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
479 wxNotebookPage
*pPage
= m_pages
[nPage
];
480 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
481 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
482 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
483 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
484 if ( pPage
->GetAutoLayout() ) {
489 // Processing continues to next OnSize
493 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
495 // is it our tab control?
496 if ( event
.GetEventObject() == this )
497 ChangePage(event
.GetOldSelection(), event
.GetSelection());
499 // we want to give others a chance to process this message as well
503 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
505 // set focus to the currently selected page if any
506 if ( m_nSelection
!= -1 )
507 m_pages
[m_nSelection
]->SetFocus();
512 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
514 if ( event
.IsWindowChange() ) {
516 AdvanceSelection(event
.GetDirection());
519 // pass to the parent
521 event
.SetCurrentFocus(this);
522 GetParent()->ProcessEvent(event
);
527 // ----------------------------------------------------------------------------
528 // wxNotebook base class virtuals
529 // ----------------------------------------------------------------------------
531 // override these 2 functions to do nothing: everything is done in OnSize
533 void wxNotebook::SetConstraintSizes(bool /* recurse */)
535 // don't set the sizes of the pages - their correct size is not yet known
536 wxControl::SetConstraintSizes(FALSE
);
539 bool wxNotebook::DoPhase(int /* nPhase */)
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
)
556 // it's not an error (the message may be generated by the tab control itself)
557 // and it may happen - just do nothing
558 if ( nSel
== nOldSel
)
560 wxNotebookPage
*pPage
= m_pages
[nSel
];
567 // Hide previous page
568 if ( nOldSel
!= -1 ) {
569 m_pages
[nOldSel
]->Show(FALSE
);
572 wxNotebookPage
*pPage
= m_pages
[nSel
];
580 void wxNotebook::OnMouse( wxMouseEvent
&event
)
582 if ( (ControlHandle
) m_macControl
== NULL
)
588 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
593 MacClientToRootWindow( &x
, &y
) ;
595 ControlHandle control
;
604 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
605 modifiers
|= btnState
;
607 if ( event
.m_shiftDown
)
608 modifiers
|= shiftKey
;
610 if ( event
.m_controlDown
)
611 modifiers
|= controlKey
;
613 if ( event
.m_altDown
)
614 modifiers
|= optionKey
;
616 if ( event
.m_metaDown
)
617 modifiers
|= cmdKey
;
619 control
= (ControlHandle
) m_macControl
;
620 if ( control
&& ::IsControlActive( control
) )
623 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
624 ::GetControl32BitValue(control
) - 1, m_nSelection
);
625 changing
.SetEventObject(this);
626 GetEventHandler()->ProcessEvent(changing
);
628 if(changing
.IsAllowed())
630 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
631 (ControlActionUPP
) -1);
632 wxTheApp
->s_lastMouseDown
= 0 ;
634 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
635 ::GetControl32BitValue(control
) - 1, m_nSelection
);
636 event
.SetEventObject(this);
638 GetEventHandler()->ProcessEvent(event
);
646 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
649 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
650 event
.SetEventObject(this);