1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/notebmac.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"
14 // ============================================================================
16 // ============================================================================
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
22 #include "wx/notebook.h"
25 #include "wx/string.h"
30 #include "wx/imaglist.h"
32 #include "wx/mac/uma.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // check that the page index is valid
38 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
48 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
49 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
50 EVT_MOUSE_EVENTS(wxNotebook::OnMouse
)
52 EVT_SIZE(wxNotebook::OnSize
)
53 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
54 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
57 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
58 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
60 // ============================================================================
62 // ============================================================================
64 // The Appearance Manager docs show using tab controls in either edge to edge
65 // mode, or inset. I think edge to edge conforms better to the other ports,
66 // and inset mode is better accomplished with space around the wxNotebook rather
67 // than within it. --Robin
69 // CS : had to switch off tight spacing due to 10.3 problems
70 #define wxMAC_EDGE_TO_EDGE 0
72 static inline int wxMacTabMargin(long nbStyle
, long side
)
74 static int tabMargin
= -1;
75 static int otherMargin
= -1;
79 if ( UMAHasAquaLayout() )
81 tabMargin
= 26; // From Appearance Manager docs for small tab control dimensions
82 #if wxMAC_EDGE_TO_EDGE
86 // JACS - this seems fine on 10.3; 20 is way too much
93 #if wxMAC_EDGE_TO_EDGE
101 // If the style matches the side asked for then return the tab margin,
102 // but we have to special case wxBK_TOP since it is zero...
103 if ( side
== wxBK_TOP
)
105 if ( nbStyle
!= 0 && nbStyle
& (wxBK_LEFT
|wxBK_RIGHT
|wxBK_BOTTOM
))
114 else if ( nbStyle
& side
)
120 static inline int wxMacTabLeftMargin(long style
)
122 return wxMacTabMargin(style
, wxBK_LEFT
);
125 static inline int wxMacTabTopMargin(long style
)
127 return wxMacTabMargin(style
, wxBK_TOP
);
130 static inline int wxMacTabRightMargin(long style
)
132 return wxMacTabMargin(style
, wxBK_RIGHT
);
135 static inline int wxMacTabBottomMargin(long style
)
137 return wxMacTabMargin(style
, wxBK_BOTTOM
);
140 // ----------------------------------------------------------------------------
141 // wxNotebook construction
142 // ----------------------------------------------------------------------------
144 // common part of all ctors
145 void wxNotebook::Init()
147 if ( UMAHasAquaLayout() )
149 // Should these depend on wxMAC_EDGE_TO_EDGE too?
150 m_macHorizontalBorder
= 7;
151 m_macVerticalBorder
= 8;
157 // default for dynamic class
158 wxNotebook::wxNotebook()
163 // the same arguments as for wxControl
164 wxNotebook::wxNotebook(wxWindow
*parent
,
169 const wxString
& name
)
173 Create(parent
, id
, pos
, size
, style
, name
);
177 bool wxNotebook::Create(wxWindow
*parent
,
182 const wxString
& name
)
184 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
190 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
192 int tabstyle
= kControlTabSmallNorthProc
;
193 if ( HasFlag(wxBK_LEFT
) )
194 tabstyle
= kControlTabSmallWestProc
;
195 else if ( HasFlag( wxBK_RIGHT
) )
196 tabstyle
= kControlTabSmallEastProc
;
197 else if ( HasFlag( wxBK_BOTTOM
) )
198 tabstyle
= kControlTabSmallSouthProc
;
201 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
202 tabstyle
, (long) this ) ;
204 MacPostControlCreate() ;
209 wxNotebook::~wxNotebook()
213 // ----------------------------------------------------------------------------
214 // wxNotebook accessors
215 // ----------------------------------------------------------------------------
217 void wxNotebook::SetPadding(const wxSize
& padding
)
222 void wxNotebook::SetTabSize(const wxSize
& sz
)
227 void wxNotebook::SetPageSize(const wxSize
& size
)
229 SetSize( CalcSizeFromPage( size
) );
232 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
234 wxSize sizeTotal
= sizePage
;
235 sizeTotal
.x
+= 2 * m_macHorizontalBorder
+ wxMacTabLeftMargin(GetWindowStyle()) +
236 wxMacTabRightMargin(GetWindowStyle()) ;
237 sizeTotal
.y
+= 2 * m_macVerticalBorder
+ wxMacTabTopMargin(GetWindowStyle()) +
238 wxMacTabBottomMargin(GetWindowStyle()) ;
243 wxSize
wxNotebook::DoGetBestSize() const
245 // calculate the max page size
248 size_t count
= GetPageCount();
251 for ( size_t n
= 0; n
< count
; n
++ )
253 wxSize sizePage
= m_pages
[n
]->GetSize();
255 if ( size
.x
< sizePage
.x
)
257 if ( size
.y
< sizePage
.y
)
263 // use some arbitrary default size
268 return CalcSizeFromPage(size
);
271 int wxNotebook::SetSelection(size_t nPage
)
273 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
275 if ( int(nPage
) != m_nSelection
)
277 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
278 event
.SetSelection(nPage
);
279 event
.SetOldSelection(m_nSelection
);
280 event
.SetEventObject(this);
281 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
283 // program allows the page change
284 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
285 (void)GetEventHandler()->ProcessEvent(event
);
287 ChangePage(m_nSelection
, nPage
);
294 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
296 wxASSERT( IS_VALID_PAGE(nPage
) );
298 wxNotebookPage
*page
= m_pages
[nPage
];
299 page
->SetLabel(strText
);
305 wxString
wxNotebook::GetPageText(size_t nPage
) const
307 wxASSERT( IS_VALID_PAGE(nPage
) );
309 wxNotebookPage
*page
= m_pages
[nPage
];
310 return page
->GetLabel();
313 int wxNotebook::GetPageImage(size_t nPage
) const
315 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
317 return m_images
[nPage
];
320 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
322 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
324 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
325 _T("invalid image index in SetPageImage()") );
327 if ( nImage
!= m_images
[nPage
] )
329 // if the item didn't have an icon before or, on the contrary, did have
330 // it but has lost it now, its size will change - but if the icon just
332 m_images
[nPage
] = nImage
;
340 // ----------------------------------------------------------------------------
341 // wxNotebook operations
342 // ----------------------------------------------------------------------------
344 // remove one page from the notebook, without deleting the window
345 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
347 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
348 wxNotebookPage
* page
= m_pages
[nPage
] ;
349 m_pages
.RemoveAt(nPage
);
353 if(m_nSelection
>= (int)GetPageCount()) {
354 m_nSelection
= GetPageCount() - 1;
356 if(m_nSelection
>= 0) {
357 m_pages
[m_nSelection
]->Show(true);
359 InvalidateBestSize();
364 bool wxNotebook::DeleteAllPages()
366 WX_CLEAR_ARRAY(m_pages
) ;
369 InvalidateBestSize();
374 // same as AddPage() but does it at given position
375 bool wxNotebook::InsertPage(size_t nPage
,
376 wxNotebookPage
*pPage
,
377 const wxString
& strText
,
381 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
384 wxASSERT_MSG( pPage
->GetParent() == this,
385 _T("notebook pages must have notebook as parent") );
387 // don't show pages by default (we'll need to adjust their size first)
388 pPage
->Show( false ) ;
390 pPage
->SetLabel(strText
);
392 m_images
.Insert(imageId
, nPage
);
396 wxRect rect
= GetPageRect() ;
397 pPage
->SetSize(rect
);
398 if ( pPage
->GetAutoLayout() ) {
403 // now deal with the selection
404 // ---------------------------
406 // if the inserted page is before the selected one, we must update the
407 // index of the selected page
409 if ( int(nPage
) <= m_nSelection
)
412 // while this still is the same page showing, we need to update the tabs
413 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
416 // some page should be selected: either this one or the first one if there
417 // is still no selection
421 else if ( m_nSelection
== -1 )
425 SetSelection(selNew
);
427 InvalidateBestSize();
431 /* Added by Mark Newsam
432 * When a page is added or deleted to the notebook this function updates
433 * information held in the m_macControl so that it matches the order
434 * the user would expect.
436 void wxNotebook::MacSetupTabs()
438 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
440 wxNotebookPage
*page
;
441 ControlTabInfoRec info
;
443 const size_t countPages
= GetPageCount();
444 for(size_t ii
= 0; ii
< countPages
; ii
++)
448 info
.iconSuiteID
= 0;
449 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
451 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
452 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
453 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
455 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
457 // tab controls only support very specific types of images, therefore we are doing an odyssee
458 // accross the icon worlds (even Apple DTS did not find a shorter path)
459 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
460 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
461 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
463 const wxBitmap
* bmap
= GetImageList()->GetBitmapPtr( GetPageImage(ii
) ) ;
466 wxBitmap scaledBitmap
;
467 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
469 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
470 bmap
= &scaledBitmap
;
472 ControlButtonContentInfo info
;
473 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
474 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
475 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
476 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
478 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1, iconFamily
, &iconRef
) ;
479 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
480 info
.contentType
= kControlContentIconRef
;
481 info
.u
.iconRef
= iconRef
;
482 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
483 sizeof( info
), (Ptr
)&info
);
484 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
485 if ( UMAGetSystemVersion() < 0x1030 )
487 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
490 ReleaseIconRef( iconRef
) ;
491 DisposeHandle( (Handle
) iconFamily
) ;
497 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
498 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
501 wxRect
wxNotebook::GetPageRect() const
503 // fit the notebook page to the tab control's display area
508 wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
509 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
510 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
511 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
513 // ----------------------------------------------------------------------------
514 // wxNotebook callbacks
515 // ----------------------------------------------------------------------------
517 // @@@ OnSize() is used for setting the font when it's called for the first
518 // time because doing it in ::Create() doesn't work (for unknown reasons)
519 void wxNotebook::OnSize(wxSizeEvent
& event
)
522 unsigned int nCount
= m_pages
.Count();
523 wxRect rect
= GetPageRect() ;
524 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
525 wxNotebookPage
*pPage
= m_pages
[nPage
];
526 pPage
->SetSize(rect
);
527 if ( pPage
->GetAutoLayout() ) {
532 // Processing continues to next OnSize
536 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
538 // is it our tab control?
539 if ( event
.GetEventObject() == this )
540 ChangePage(event
.GetOldSelection(), event
.GetSelection());
542 // we want to give others a chance to process this message as well
546 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
548 // set focus to the currently selected page if any
549 if ( m_nSelection
!= -1 )
550 m_pages
[m_nSelection
]->SetFocus();
555 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
557 if ( event
.IsWindowChange() ) {
559 AdvanceSelection(event
.GetDirection());
562 // we get this event in 2 cases
564 // a) one of our pages might have generated it because the user TABbed
565 // out from it in which case we should propagate the event upwards and
566 // our parent will take care of setting the focus to prev/next sibling
570 // b) the parent panel wants to give the focus to us so that we
571 // forward it to our selected page. We can't deal with this in
572 // OnSetFocus() because we don't know which direction the focus came
573 // from in this case and so can't choose between setting the focus to
574 // first or last panel child
575 wxWindow
*parent
= GetParent();
576 // the cast is here to fic a GCC ICE
577 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
579 // no, it doesn't come from child, case (b): forward to a page
580 if ( m_nSelection
!= -1 )
582 // so that the page knows that the event comes from it's parent
583 // and is being propagated downwards
584 event
.SetEventObject(this);
586 wxWindow
*page
= m_pages
[m_nSelection
];
587 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
591 //else: page manages focus inside it itself
595 // we have no pages - still have to give focus to _something_
601 // it comes from our child, case (a), pass to the parent
603 event
.SetCurrentFocus(this);
604 parent
->GetEventHandler()->ProcessEvent(event
);
610 // ----------------------------------------------------------------------------
611 // wxNotebook base class virtuals
612 // ----------------------------------------------------------------------------
614 #if wxUSE_CONSTRAINTS
616 // override these 2 functions to do nothing: everything is done in OnSize
618 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
620 // don't set the sizes of the pages - their correct size is not yet known
621 wxControl::SetConstraintSizes(false);
624 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
629 #endif // wxUSE_CONSTRAINTS
631 void wxNotebook::Command(wxCommandEvent
& event
)
633 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
636 // ----------------------------------------------------------------------------
637 // wxNotebook helper functions
638 // ----------------------------------------------------------------------------
640 // hide the currently active panel and show the new one
641 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
645 m_pages
[nOldSel
]->Show(false);
650 wxNotebookPage
*pPage
= m_pages
[nSel
];
656 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
660 void wxNotebook::OnMouse( wxMouseEvent
&event
)
662 if ( (ControlHandle
) m_macControl
== NULL
)
668 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
673 MacClientToRootWindow( &x
, &y
) ;
675 ControlHandle control
;
684 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
685 modifiers
|= btnState
;
687 if ( event
.m_shiftDown
)
688 modifiers
|= shiftKey
;
690 if ( event
.m_controlDown
)
691 modifiers
|= controlKey
;
693 if ( event
.m_altDown
)
694 modifiers
|= optionKey
;
696 if ( event
.m_metaDown
)
697 modifiers
|= cmdKey
;
699 control
= (ControlHandle
) m_macControl
;
700 if ( control
&& ::IsControlActive( control
) )
703 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
704 ::GetControl32BitValue(control
) - 1, m_nSelection
);
705 changing
.SetEventObject(this);
706 GetEventHandler()->ProcessEvent(changing
);
708 if(changing
.IsAllowed())
710 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
711 (ControlActionUPP
) -1);
712 wxTheApp
->s_lastMouseDown
= 0 ;
714 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
715 ::GetControl32BitValue(control
) - 1, m_nSelection
);
716 event
.SetEventObject(this);
718 GetEventHandler()->ProcessEvent(event
);
726 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
729 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
730 event
.SetEventObject(this);