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
)
47 EVT_MOUSE_EVENTS(wxNotebook::OnMouse
)
49 EVT_SIZE(wxNotebook::OnSize
)
50 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
51 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
57 // ============================================================================
59 // ============================================================================
61 // The Appearance Manager docs show using tab controls in either edge to edge
62 // mode, or inset. I think edge to edge conforms better to the other ports,
63 // and inset mode is better accomplished with space around the wxNotebook rather
64 // than within it. --Robin
66 // CS : had to switch off tight spacing due to 10.3 problems
67 #define wxMAC_EDGE_TO_EDGE 0
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
83 // JACS - this seems fine on 10.3; 20 is way too much
90 #if wxMAC_EDGE_TO_EDGE
98 // If the style matches the side asked for then return the tab margin,
99 // but we have to special case wxNB_TOP since it is zero...
100 if ( side
== wxNB_TOP
)
102 if ( nbStyle
!= 0 && nbStyle
& (wxNB_LEFT
|wxNB_RIGHT
|wxNB_BOTTOM
))
111 else if ( nbStyle
& side
)
117 static inline int wxMacTabLeftMargin(long style
)
119 return wxMacTabMargin(style
, wxNB_LEFT
);
122 static inline int wxMacTabTopMargin(long style
)
124 return wxMacTabMargin(style
, wxNB_TOP
);
127 static inline int wxMacTabRightMargin(long style
)
129 return wxMacTabMargin(style
, wxNB_RIGHT
);
132 static inline int wxMacTabBottomMargin(long style
)
134 return wxMacTabMargin(style
, wxNB_BOTTOM
);
137 // ----------------------------------------------------------------------------
138 // wxNotebook construction
139 // ----------------------------------------------------------------------------
141 // common part of all ctors
142 void wxNotebook::Init()
144 if ( UMAHasAquaLayout() )
146 // Should these depend on wxMAC_EDGE_TO_EDGE too?
147 m_macHorizontalBorder
= 7;
148 m_macVerticalBorder
= 8;
154 // default for dynamic class
155 wxNotebook::wxNotebook()
160 // the same arguments as for wxControl
161 wxNotebook::wxNotebook(wxWindow
*parent
,
166 const wxString
& name
)
170 Create(parent
, id
, pos
, size
, style
, name
);
174 bool wxNotebook::Create(wxWindow
*parent
,
179 const wxString
& name
)
181 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
187 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
189 int tabstyle
= kControlTabSmallNorthProc
;
190 if ( HasFlag(wxNB_LEFT
) )
191 tabstyle
= kControlTabSmallWestProc
;
192 else if ( HasFlag( wxNB_RIGHT
) )
193 tabstyle
= kControlTabSmallEastProc
;
194 else if ( HasFlag( wxNB_BOTTOM
) )
195 tabstyle
= kControlTabSmallSouthProc
;
198 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
199 tabstyle
, (long) this ) ;
201 MacPostControlCreate() ;
206 wxNotebook::~wxNotebook()
210 // ----------------------------------------------------------------------------
211 // wxNotebook accessors
212 // ----------------------------------------------------------------------------
214 void wxNotebook::SetPadding(const wxSize
& padding
)
219 void wxNotebook::SetTabSize(const wxSize
& sz
)
224 void wxNotebook::SetPageSize(const wxSize
& size
)
226 SetSize( CalcSizeFromPage( size
) );
229 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
231 wxSize sizeTotal
= sizePage
;
232 sizeTotal
.x
+= 2 * m_macHorizontalBorder
+ wxMacTabLeftMargin(GetWindowStyle()) +
233 wxMacTabRightMargin(GetWindowStyle()) ;
234 sizeTotal
.y
+= 2 * m_macVerticalBorder
+ wxMacTabTopMargin(GetWindowStyle()) +
235 wxMacTabBottomMargin(GetWindowStyle()) ;
240 wxSize
wxNotebook::DoGetBestSize() const
242 // calculate the max page size
245 size_t count
= GetPageCount();
248 for ( size_t n
= 0; n
< count
; n
++ )
250 wxSize sizePage
= m_pages
[n
]->GetSize();
252 if ( size
.x
< sizePage
.x
)
254 if ( size
.y
< sizePage
.y
)
260 // use some arbitrary default size
265 return CalcSizeFromPage(size
);
268 int wxNotebook::SetSelection(size_t nPage
)
270 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
272 if ( int(nPage
) != m_nSelection
)
274 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
275 event
.SetSelection(nPage
);
276 event
.SetOldSelection(m_nSelection
);
277 event
.SetEventObject(this);
278 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
280 // program allows the page change
281 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
282 (void)GetEventHandler()->ProcessEvent(event
);
284 ChangePage(m_nSelection
, nPage
);
291 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
293 wxASSERT( IS_VALID_PAGE(nPage
) );
295 wxNotebookPage
*page
= m_pages
[nPage
];
296 page
->SetLabel(strText
);
302 wxString
wxNotebook::GetPageText(size_t nPage
) const
304 wxASSERT( IS_VALID_PAGE(nPage
) );
306 wxNotebookPage
*page
= m_pages
[nPage
];
307 return page
->GetLabel();
310 int wxNotebook::GetPageImage(size_t nPage
) const
312 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
314 return m_images
[nPage
];
317 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
319 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
321 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
322 _T("invalid image index in SetPageImage()") );
324 if ( nImage
!= m_images
[nPage
] )
326 // if the item didn't have an icon before or, on the contrary, did have
327 // it but has lost it now, its size will change - but if the icon just
329 m_images
[nPage
] = nImage
;
337 // ----------------------------------------------------------------------------
338 // wxNotebook operations
339 // ----------------------------------------------------------------------------
341 // remove one page from the notebook, without deleting the window
342 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
344 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
345 wxNotebookPage
* page
= m_pages
[nPage
] ;
346 m_pages
.RemoveAt(nPage
);
350 if(m_nSelection
>= (int)GetPageCount()) {
351 m_nSelection
= GetPageCount() - 1;
353 if(m_nSelection
>= 0) {
354 m_pages
[m_nSelection
]->Show(true);
360 bool wxNotebook::DeleteAllPages()
362 WX_CLEAR_ARRAY(m_pages
) ;
369 // same as AddPage() but does it at given position
370 bool wxNotebook::InsertPage(size_t nPage
,
371 wxNotebookPage
*pPage
,
372 const wxString
& strText
,
376 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
379 wxASSERT_MSG( pPage
->GetParent() == this,
380 _T("notebook pages must have notebook as parent") );
382 // don't show pages by default (we'll need to adjust their size first)
383 pPage
->Show( false ) ;
385 pPage
->SetLabel(strText
);
387 m_images
.Insert(imageId
, nPage
);
391 wxRect rect
= GetPageRect() ;
392 pPage
->SetSize(rect
);
393 if ( pPage
->GetAutoLayout() ) {
398 // now deal with the selection
399 // ---------------------------
401 // if the inserted page is before the selected one, we must update the
402 // index of the selected page
404 if ( int(nPage
) <= m_nSelection
)
407 // while this still is the same page showing, we need to update the tabs
408 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
411 // some page should be selected: either this one or the first one if there
412 // is still no selection
416 else if ( m_nSelection
== -1 )
420 SetSelection(selNew
);
425 /* Added by Mark Newsam
426 * When a page is added or deleted to the notebook this function updates
427 * information held in the m_macControl so that it matches the order
428 * the user would expect.
430 void wxNotebook::MacSetupTabs()
432 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
434 wxNotebookPage
*page
;
435 ControlTabInfoRec info
;
437 const size_t countPages
= GetPageCount();
438 for(size_t ii
= 0; ii
< countPages
; ii
++)
442 info
.iconSuiteID
= 0;
443 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
445 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
446 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
447 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
449 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
451 // tab controls only support very specific types of images, therefore we are doing an odyssee
452 // accross the icon worlds (even Apple DTS did not find a shorter path)
453 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
454 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
455 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
457 const wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
460 wxBitmap scaledBitmap
;
461 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
463 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
464 bmap
= &scaledBitmap
;
466 ControlButtonContentInfo info
;
467 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
468 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
469 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
470 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
472 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1, iconFamily
, &iconRef
) ;
473 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
474 info
.contentType
= kControlContentIconRef
;
475 info
.u
.iconRef
= iconRef
;
476 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
477 sizeof( info
), (Ptr
)&info
);
478 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
479 if ( UMAGetSystemVersion() < 0x1030 )
481 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
484 ReleaseIconRef( iconRef
) ;
485 DisposeHandle( (Handle
) iconFamily
) ;
491 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
492 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
495 wxRect
wxNotebook::GetPageRect() const
497 // fit the notebook page to the tab control's display area
502 wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
503 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
504 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
505 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
507 // ----------------------------------------------------------------------------
508 // wxNotebook callbacks
509 // ----------------------------------------------------------------------------
511 // @@@ OnSize() is used for setting the font when it's called for the first
512 // time because doing it in ::Create() doesn't work (for unknown reasons)
513 void wxNotebook::OnSize(wxSizeEvent
& event
)
516 unsigned int nCount
= m_pages
.Count();
517 wxRect rect
= GetPageRect() ;
518 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
519 wxNotebookPage
*pPage
= m_pages
[nPage
];
520 pPage
->SetSize(rect
);
521 if ( pPage
->GetAutoLayout() ) {
526 // Processing continues to next OnSize
530 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
532 // is it our tab control?
533 if ( event
.GetEventObject() == this )
534 ChangePage(event
.GetOldSelection(), event
.GetSelection());
536 // we want to give others a chance to process this message as well
540 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
542 // set focus to the currently selected page if any
543 if ( m_nSelection
!= -1 )
544 m_pages
[m_nSelection
]->SetFocus();
549 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
551 if ( event
.IsWindowChange() ) {
553 AdvanceSelection(event
.GetDirection());
556 // we get this event in 2 cases
558 // a) one of our pages might have generated it because the user TABbed
559 // out from it in which case we should propagate the event upwards and
560 // our parent will take care of setting the focus to prev/next sibling
564 // b) the parent panel wants to give the focus to us so that we
565 // forward it to our selected page. We can't deal with this in
566 // OnSetFocus() because we don't know which direction the focus came
567 // from in this case and so can't choose between setting the focus to
568 // first or last panel child
569 wxWindow
*parent
= GetParent();
570 // the cast is here to fic a GCC ICE
571 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
573 // no, it doesn't come from child, case (b): forward to a page
574 if ( m_nSelection
!= -1 )
576 // so that the page knows that the event comes from it's parent
577 // and is being propagated downwards
578 event
.SetEventObject(this);
580 wxWindow
*page
= m_pages
[m_nSelection
];
581 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
585 //else: page manages focus inside it itself
589 // we have no pages - still have to give focus to _something_
595 // it comes from our child, case (a), pass to the parent
597 event
.SetCurrentFocus(this);
598 parent
->GetEventHandler()->ProcessEvent(event
);
604 // ----------------------------------------------------------------------------
605 // wxNotebook base class virtuals
606 // ----------------------------------------------------------------------------
608 #if wxUSE_CONSTRAINTS
610 // override these 2 functions to do nothing: everything is done in OnSize
612 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
614 // don't set the sizes of the pages - their correct size is not yet known
615 wxControl::SetConstraintSizes(FALSE
);
618 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
623 #endif // wxUSE_CONSTRAINTS
625 void wxNotebook::Command(wxCommandEvent
& event
)
627 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
630 // ----------------------------------------------------------------------------
631 // wxNotebook helper functions
632 // ----------------------------------------------------------------------------
634 // hide the currently active panel and show the new one
635 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
639 m_pages
[nOldSel
]->Show(FALSE
);
644 wxNotebookPage
*pPage
= m_pages
[nSel
];
650 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
654 void wxNotebook::OnMouse( wxMouseEvent
&event
)
656 if ( (ControlHandle
) m_macControl
== NULL
)
662 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
667 MacClientToRootWindow( &x
, &y
) ;
669 ControlHandle control
;
678 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
679 modifiers
|= btnState
;
681 if ( event
.m_shiftDown
)
682 modifiers
|= shiftKey
;
684 if ( event
.m_controlDown
)
685 modifiers
|= controlKey
;
687 if ( event
.m_altDown
)
688 modifiers
|= optionKey
;
690 if ( event
.m_metaDown
)
691 modifiers
|= cmdKey
;
693 control
= (ControlHandle
) m_macControl
;
694 if ( control
&& ::IsControlActive( control
) )
697 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
698 ::GetControl32BitValue(control
) - 1, m_nSelection
);
699 changing
.SetEventObject(this);
700 GetEventHandler()->ProcessEvent(changing
);
702 if(changing
.IsAllowed())
704 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
705 (ControlActionUPP
) -1);
706 wxTheApp
->s_lastMouseDown
= 0 ;
708 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
709 ::GetControl32BitValue(control
) - 1, m_nSelection
);
710 event
.SetEventObject(this);
712 GetEventHandler()->ProcessEvent(event
);
720 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
723 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
724 event
.SetEventObject(this);