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
65 #define wxMAC_EDGE_TO_EDGE 1
67 static inline int wxMacTabMargin(long nbStyle
, long side
)
69 static int tabMargin
= -1;
70 static int otherMargin
= -1;
74 if ( UMAHasAquaLayout() )
76 tabMargin
= 26; // From Appearance Manager docs for small tab control dimensions
77 #if wxMAC_EDGE_TO_EDGE
86 #if wxMAC_EDGE_TO_EDGE
94 // If the style matches the side asked for then return the tab margin,
95 // but we have to special case wxNB_TOP since it is zero...
96 if ( side
== wxNB_TOP
)
98 if ( nbStyle
!= 0 && nbStyle
& (wxNB_LEFT
|wxNB_RIGHT
|wxNB_BOTTOM
))
107 else if ( nbStyle
& side
)
113 static inline int wxMacTabLeftMargin(long style
)
115 return wxMacTabMargin(style
, wxNB_LEFT
);
118 static inline int wxMacTabTopMargin(long style
)
120 return wxMacTabMargin(style
, wxNB_TOP
);
123 static inline int wxMacTabRightMargin(long style
)
125 return wxMacTabMargin(style
, wxNB_RIGHT
);
128 static inline int wxMacTabBottomMargin(long style
)
130 return wxMacTabMargin(style
, wxNB_BOTTOM
);
133 // ----------------------------------------------------------------------------
134 // wxNotebook construction
135 // ----------------------------------------------------------------------------
137 // common part of all ctors
138 void wxNotebook::Init()
140 if ( UMAHasAquaLayout() )
142 // Should these depend on wxMAC_EDGE_TO_EDGE too?
143 m_macHorizontalBorder
= 7;
144 m_macVerticalBorder
= 8;
150 // default for dynamic class
151 wxNotebook::wxNotebook()
156 // the same arguments as for wxControl
157 wxNotebook::wxNotebook(wxWindow
*parent
,
162 const wxString
& name
)
166 Create(parent
, id
, pos
, size
, style
, name
);
170 bool wxNotebook::Create(wxWindow
*parent
,
175 const wxString
& name
)
177 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
183 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
185 int tabstyle
= kControlTabSmallNorthProc
;
186 if ( HasFlag(wxNB_LEFT
) )
187 tabstyle
= kControlTabSmallWestProc
;
188 else if ( HasFlag( wxNB_RIGHT
) )
189 tabstyle
= kControlTabSmallEastProc
;
190 else if ( HasFlag( wxNB_BOTTOM
) )
191 tabstyle
= kControlTabSmallSouthProc
;
194 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
195 tabstyle
, (long) this ) ;
197 MacPostControlCreate() ;
202 wxNotebook::~wxNotebook()
206 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
208 wxSize sizeTotal
= sizePage
;
211 wxGetOsVersion( &major
, &minor
);
213 // Mac has large notebook borders. Aqua even more so.
215 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
241 // ----------------------------------------------------------------------------
242 // wxNotebook accessors
243 // ----------------------------------------------------------------------------
245 void wxNotebook::SetPadding(const wxSize
& padding
)
247 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
250 void wxNotebook::SetTabSize(const wxSize
& sz
)
252 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
255 void wxNotebook::SetPageSize(const wxSize
& size
)
257 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
260 int wxNotebook::SetSelection(size_t nPage
)
262 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
264 if ( int(nPage
) != m_nSelection
)
266 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
267 event
.SetSelection(nPage
);
268 event
.SetOldSelection(m_nSelection
);
269 event
.SetEventObject(this);
270 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
272 // program allows the page change
273 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
274 (void)GetEventHandler()->ProcessEvent(event
);
276 ChangePage(m_nSelection
, nPage
);
283 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
285 wxASSERT( IS_VALID_PAGE(nPage
) );
287 wxNotebookPage
*page
= m_pages
[nPage
];
288 page
->SetLabel(strText
);
294 wxString
wxNotebook::GetPageText(size_t nPage
) const
296 wxASSERT( IS_VALID_PAGE(nPage
) );
298 wxNotebookPage
*page
= m_pages
[nPage
];
299 return page
->GetLabel();
302 int wxNotebook::GetPageImage(size_t nPage
) const
304 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
306 return m_images
[nPage
];
309 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
311 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
313 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
314 _T("invalid image index in SetPageImage()") );
316 if ( nImage
!= m_images
[nPage
] )
318 // if the item didn't have an icon before or, on the contrary, did have
319 // it but has lost it now, its size will change - but if the icon just
321 m_images
[nPage
] = nImage
;
329 // ----------------------------------------------------------------------------
330 // wxNotebook operations
331 // ----------------------------------------------------------------------------
333 // remove one page from the notebook, without deleting the window
334 wxNotebookPage
* wxNotebook::DoRemovePage(size_t nPage
)
336 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
337 wxNotebookPage
* page
= m_pages
[nPage
] ;
338 m_pages
.RemoveAt(nPage
);
342 if(m_nSelection
>= (int)GetPageCount()) {
343 m_nSelection
= GetPageCount() - 1;
345 if(m_nSelection
>= 0) {
346 m_pages
[m_nSelection
]->Show(true);
352 bool wxNotebook::DeleteAllPages()
354 WX_CLEAR_ARRAY(m_pages
) ;
361 // same as AddPage() but does it at given position
362 bool wxNotebook::InsertPage(size_t nPage
,
363 wxNotebookPage
*pPage
,
364 const wxString
& strText
,
368 if ( !wxNotebookBase::InsertPage(nPage
, pPage
, strText
, bSelect
, imageId
) )
371 wxASSERT_MSG( pPage
->GetParent() == this,
372 _T("notebook pages must have notebook as parent") );
374 // don't show pages by default (we'll need to adjust their size first)
375 pPage
->Show( false ) ;
377 pPage
->SetLabel(strText
);
379 m_images
.Insert(imageId
, nPage
);
385 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
386 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
387 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
388 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
389 if ( pPage
->GetAutoLayout() ) {
394 // now deal with the selection
395 // ---------------------------
397 // if the inserted page is before the selected one, we must update the
398 // index of the selected page
400 if ( int(nPage
) <= m_nSelection
)
403 // while this still is the same page showing, we need to update the tabs
404 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
407 // some page should be selected: either this one or the first one if there
408 // is still no selection
412 else if ( m_nSelection
== -1 )
416 SetSelection(selNew
);
421 /* Added by Mark Newsam
422 * When a page is added or deleted to the notebook this function updates
423 * information held in the m_macControl so that it matches the order
424 * the user would expect.
426 void wxNotebook::MacSetupTabs()
428 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
430 wxNotebookPage
*page
;
431 ControlTabInfoRec info
;
433 const size_t countPages
= GetPageCount();
434 for(size_t ii
= 0; ii
< countPages
; ii
++)
438 info
.iconSuiteID
= 0;
439 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
441 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
442 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
443 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
445 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
447 // tab controls only support very specific types of images, therefore we are doing an odyssee
448 // accross the icon worlds (even Apple DTS did not find a shorter path)
449 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
450 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
451 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
453 wxBitmap
* bmap
= GetImageList()->GetBitmap( GetPageImage(ii
) ) ;
456 wxBitmap scaledBitmap
;
457 if ( bmap
->GetWidth() != 16 || bmap
->GetHeight() != 16 )
459 scaledBitmap
= wxBitmap( bmap
->ConvertToImage().Scale(16,16) ) ;
460 bmap
= &scaledBitmap
;
462 ControlButtonContentInfo info
;
463 wxMacCreateBitmapButton( &info
, *bmap
, kControlContentPictHandle
) ;
464 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
465 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
466 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
468 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1 , iconFamily
, &iconRef
) ;
469 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
470 info
.contentType
= kControlContentIconRef
;
471 info
.u
.iconRef
= iconRef
;
472 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
473 sizeof( info
), (Ptr
)&info
);
474 wxASSERT_MSG( err
== noErr
, wxT("Error when setting icon on tab") ) ;
475 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
476 ReleaseIconRef( iconRef
) ;
477 DisposeHandle( (Handle
) iconFamily
) ;
483 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
484 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
487 // ----------------------------------------------------------------------------
488 // wxNotebook callbacks
489 // ----------------------------------------------------------------------------
491 // @@@ OnSize() is used for setting the font when it's called for the first
492 // time because doing it in ::Create() doesn't work (for unknown reasons)
493 void wxNotebook::OnSize(wxSizeEvent
& event
)
495 // emulate page change (it's esp. important to do it first time because
496 // otherwise our page would stay invisible)
498 int nSel = m_nSelection;
503 // fit the notebook page to the tab control's display area
507 unsigned int nCount
= m_pages
.Count();
508 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
509 wxNotebookPage
*pPage
= m_pages
[nPage
];
510 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
511 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
512 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
513 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
514 if ( pPage
->GetAutoLayout() ) {
519 // Processing continues to next OnSize
523 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
525 // is it our tab control?
526 if ( event
.GetEventObject() == this )
527 ChangePage(event
.GetOldSelection(), event
.GetSelection());
529 // we want to give others a chance to process this message as well
533 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
535 // set focus to the currently selected page if any
536 if ( m_nSelection
!= -1 )
537 m_pages
[m_nSelection
]->SetFocus();
542 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
544 if ( event
.IsWindowChange() ) {
546 AdvanceSelection(event
.GetDirection());
549 // we get this event in 2 cases
551 // a) one of our pages might have generated it because the user TABbed
552 // out from it in which case we should propagate the event upwards and
553 // our parent will take care of setting the focus to prev/next sibling
557 // b) the parent panel wants to give the focus to us so that we
558 // forward it to our selected page. We can't deal with this in
559 // OnSetFocus() because we don't know which direction the focus came
560 // from in this case and so can't choose between setting the focus to
561 // first or last panel child
562 wxWindow
*parent
= GetParent();
563 // the cast is here to fic a GCC ICE
564 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
566 // no, it doesn't come from child, case (b): forward to a page
567 if ( m_nSelection
!= -1 )
569 // so that the page knows that the event comes from it's parent
570 // and is being propagated downwards
571 event
.SetEventObject(this);
573 wxWindow
*page
= m_pages
[m_nSelection
];
574 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
578 //else: page manages focus inside it itself
582 // we have no pages - still have to give focus to _something_
588 // it comes from our child, case (a), pass to the parent
590 event
.SetCurrentFocus(this);
591 parent
->GetEventHandler()->ProcessEvent(event
);
597 // ----------------------------------------------------------------------------
598 // wxNotebook base class virtuals
599 // ----------------------------------------------------------------------------
601 #if wxUSE_CONSTRAINTS
603 // override these 2 functions to do nothing: everything is done in OnSize
605 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
607 // don't set the sizes of the pages - their correct size is not yet known
608 wxControl::SetConstraintSizes(FALSE
);
611 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
616 #endif // wxUSE_CONSTRAINTS
618 void wxNotebook::Command(wxCommandEvent
& event
)
620 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
623 // ----------------------------------------------------------------------------
624 // wxNotebook helper functions
625 // ----------------------------------------------------------------------------
627 // hide the currently active panel and show the new one
628 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
632 m_pages
[nOldSel
]->Show(FALSE
);
637 wxNotebookPage
*pPage
= m_pages
[nSel
];
643 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
647 void wxNotebook::OnMouse( wxMouseEvent
&event
)
649 if ( (ControlHandle
) m_macControl
== NULL
)
655 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
660 MacClientToRootWindow( &x
, &y
) ;
662 ControlHandle control
;
671 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
672 modifiers
|= btnState
;
674 if ( event
.m_shiftDown
)
675 modifiers
|= shiftKey
;
677 if ( event
.m_controlDown
)
678 modifiers
|= controlKey
;
680 if ( event
.m_altDown
)
681 modifiers
|= optionKey
;
683 if ( event
.m_metaDown
)
684 modifiers
|= cmdKey
;
686 control
= (ControlHandle
) m_macControl
;
687 if ( control
&& ::IsControlActive( control
) )
690 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
691 ::GetControl32BitValue(control
) - 1, m_nSelection
);
692 changing
.SetEventObject(this);
693 GetEventHandler()->ProcessEvent(changing
);
695 if(changing
.IsAllowed())
697 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
698 (ControlActionUPP
) -1);
699 wxTheApp
->s_lastMouseDown
= 0 ;
701 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
702 ::GetControl32BitValue(control
) - 1, m_nSelection
);
703 event
.SetEventObject(this);
705 GetEventHandler()->ProcessEvent(event
);
713 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
716 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
717 event
.SetEventObject(this);