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"
27 #include "wx/notebook.h"
28 #include "wx/mac/uma.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // check that the page index is valid
34 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if !USE_SHARED_LIBRARIES
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
)
58 // ============================================================================
60 // ============================================================================
62 // The Appearance Manager docs show using tab controls in either edge to edge
63 // mode, or inset. I think edge to edge conforms better to the other ports,
64 // and inset mode is better accomplished with space around the wxNotebook rather
65 // than within it. --Robin
66 #define wxMAC_EDGE_TO_EDGE 1
68 static inline int wxMacTabMargin(long nbStyle
, long side
)
70 static int tabMargin
= -1;
71 static int otherMargin
= -1;
75 if ( UMAHasAquaLayout() )
77 tabMargin
= 26; // From Appearance Manager docs for small tab control dimensions
78 #if wxMAC_EDGE_TO_EDGE
87 #if wxMAC_EDGE_TO_EDGE
95 // If the style matches the side asked for then return the tab margin,
96 // but we have to special case wxNB_TOP since it is zero...
97 if ( side
== wxNB_TOP
)
99 if ( nbStyle
!= 0 && nbStyle
& (wxNB_LEFT
|wxNB_RIGHT
|wxNB_BOTTOM
))
108 else if ( nbStyle
& side
)
114 static inline int wxMacTabLeftMargin(long style
)
116 return wxMacTabMargin(style
, wxNB_LEFT
);
119 static inline int wxMacTabTopMargin(long style
)
121 return wxMacTabMargin(style
, wxNB_TOP
);
124 static inline int wxMacTabRightMargin(long style
)
126 return wxMacTabMargin(style
, wxNB_RIGHT
);
129 static inline int wxMacTabBottomMargin(long style
)
131 return wxMacTabMargin(style
, wxNB_BOTTOM
);
134 // ----------------------------------------------------------------------------
135 // wxNotebook construction
136 // ----------------------------------------------------------------------------
138 // common part of all ctors
139 void wxNotebook::Init()
141 if ( UMAHasAquaLayout() )
143 // Should these depend on wxMAC_EDGE_TO_EDGE too?
144 m_macHorizontalBorder
= 7;
145 m_macVerticalBorder
= 8;
151 // default for dynamic class
152 wxNotebook::wxNotebook()
157 // the same arguments as for wxControl
158 wxNotebook::wxNotebook(wxWindow
*parent
,
163 const wxString
& name
)
167 Create(parent
, id
, pos
, size
, style
, name
);
171 bool wxNotebook::Create(wxWindow
*parent
,
176 const wxString
& name
)
178 if ( !wxNotebookBase::Create(parent
, id
, pos
, size
, style
, name
) )
184 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
186 int tabstyle
= kControlTabSmallNorthProc
;
187 if ( HasFlag(wxNB_LEFT
) )
188 tabstyle
= kControlTabSmallWestProc
;
189 else if ( HasFlag( wxNB_RIGHT
) )
190 tabstyle
= kControlTabSmallEastProc
;
191 else if ( HasFlag( wxNB_BOTTOM
) )
192 tabstyle
= kControlTabSmallSouthProc
;
195 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
196 tabstyle
, (long) this ) ;
198 MacPostControlCreate() ;
203 wxNotebook::~wxNotebook()
205 m_macControl
= NULL
;
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(int 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(int nPage
, const wxString
& strText
)
276 wxASSERT( IS_VALID_PAGE(nPage
) );
278 wxNotebookPage
*page
= m_pages
[nPage
];
279 page
->SetLabel(strText
);
285 wxString
wxNotebook::GetPageText(int nPage
) const
287 wxASSERT( IS_VALID_PAGE(nPage
) );
289 wxNotebookPage
*page
= m_pages
[nPage
];
290 return page
->GetLabel();
293 int wxNotebook::GetPageImage(int nPage
) const
295 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
297 return m_images
[nPage
];
300 bool wxNotebook::SetPageImage(int 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(int nPage
)
327 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
328 wxNotebookPage
* page
= m_pages
[nPage
] ;
329 m_pages
.RemoveAt(nPage
);
333 if(m_nSelection
>= GetPageCount()) {
334 m_nSelection
= GetPageCount() - 1;
336 if(m_nSelection
>= 0) {
337 m_pages
[m_nSelection
]->Show(true);
343 bool wxNotebook::DeleteAllPages()
345 // TODO: delete native widget pages
347 WX_CLEAR_ARRAY(m_pages
) ;
354 // same as AddPage() but does it at given position
355 bool wxNotebook::InsertPage(int nPage
,
356 wxNotebookPage
*pPage
,
357 const wxString
& strText
,
361 wxASSERT( pPage
!= NULL
);
362 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
364 pPage
->SetLabel(strText
);
366 // save the pointer to the page
367 m_pages
.Insert(pPage
, nPage
);
369 m_images
.Insert(imageId
, nPage
);
374 m_nSelection
= nPage
;
376 else if ( m_nSelection
== -1 ) {
379 else if (m_nSelection
>= nPage
) {
382 // don't show pages by default (we'll need to adjust their size first)
383 pPage
->Show( false ) ;
387 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
388 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
389 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
390 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
391 if ( pPage
->GetAutoLayout() ) {
398 /* Added by Mark Newsam
399 * When a page is added or deleted to the notebook this function updates
400 * information held in the m_macControl so that it matches the order
401 * the user would expect.
403 void wxNotebook::MacSetupTabs()
405 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
407 wxNotebookPage
*page
;
408 ControlTabInfoRec info
;
410 for(int ii
= 0; ii
< GetPageCount(); ii
++)
414 info
.iconSuiteID
= 0;
415 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
417 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
418 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
419 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
420 #if 0 // TARGET_CARBON
421 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
423 // tab controls only support very specific types of images, therefore we are doing an odyssee
424 // accross the icon worlds (even Apple DTS did not find a shorter path)
425 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
426 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
427 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
429 ControlButtonContentInfo info
;
430 wxMacCreateBitmapButton( &info
, *GetImageList()->GetBitmap( GetPageImage(ii
) ) , kControlContentPictHandle
) ;
431 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
432 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
433 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
435 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1 , iconFamily
, &iconRef
) ;
436 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
437 info
.contentType
= kControlContentIconRef
;
438 info
.u
.iconRef
= iconRef
;
439 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
440 sizeof( info
), (Ptr
)&info
);
441 wxASSERT_MSG( err
== noErr
, "Error when setting icon on tab" ) ;
442 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
443 ReleaseIconRef( iconRef
) ;
444 DisposeHandle( (Handle
) iconFamily
) ;
449 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
450 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
453 // ----------------------------------------------------------------------------
454 // wxNotebook callbacks
455 // ----------------------------------------------------------------------------
457 // @@@ OnSize() is used for setting the font when it's called for the first
458 // time because doing it in ::Create() doesn't work (for unknown reasons)
459 void wxNotebook::OnSize(wxSizeEvent
& event
)
461 // emulate page change (it's esp. important to do it first time because
462 // otherwise our page would stay invisible)
463 int nSel
= m_nSelection
;
467 // fit the notebook page to the tab control's display area
471 unsigned int nCount
= m_pages
.Count();
472 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
473 wxNotebookPage
*pPage
= m_pages
[nPage
];
474 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
475 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
476 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
477 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
478 if ( pPage
->GetAutoLayout() ) {
483 // Processing continues to next OnSize
487 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
489 // is it our tab control?
490 if ( event
.GetEventObject() == this )
491 ChangePage(event
.GetOldSelection(), event
.GetSelection());
493 // we want to give others a chance to process this message as well
497 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
499 // set focus to the currently selected page if any
500 if ( m_nSelection
!= -1 )
501 m_pages
[m_nSelection
]->SetFocus();
506 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
508 if ( event
.IsWindowChange() ) {
510 AdvanceSelection(event
.GetDirection());
513 // pass to the parent
515 event
.SetCurrentFocus(this);
516 GetParent()->ProcessEvent(event
);
521 // ----------------------------------------------------------------------------
522 // wxNotebook base class virtuals
523 // ----------------------------------------------------------------------------
525 // override these 2 functions to do nothing: everything is done in OnSize
527 void wxNotebook::SetConstraintSizes(bool /* recurse */)
529 // don't set the sizes of the pages - their correct size is not yet known
530 wxControl::SetConstraintSizes(FALSE
);
533 bool wxNotebook::DoPhase(int /* nPhase */)
538 void wxNotebook::Command(wxCommandEvent
& event
)
540 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
543 // ----------------------------------------------------------------------------
544 // wxNotebook helper functions
545 // ----------------------------------------------------------------------------
547 // hide the currently active panel and show the new one
548 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
550 // it's not an error (the message may be generated by the tab control itself)
551 // and it may happen - just do nothing
552 if ( nSel
== nOldSel
)
554 wxNotebookPage
*pPage
= m_pages
[nSel
];
561 // Hide previous page
562 if ( nOldSel
!= -1 ) {
563 m_pages
[nOldSel
]->Show(FALSE
);
566 wxNotebookPage
*pPage
= m_pages
[nSel
];
574 void wxNotebook::OnMouse( wxMouseEvent
&event
)
576 if ( (ControlHandle
) m_macControl
== NULL
)
582 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
587 MacClientToRootWindow( &x
, &y
) ;
589 ControlHandle control
;
598 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
599 modifiers
|= btnState
;
601 if ( event
.m_shiftDown
)
602 modifiers
|= shiftKey
;
604 if ( event
.m_controlDown
)
605 modifiers
|= controlKey
;
607 if ( event
.m_altDown
)
608 modifiers
|= optionKey
;
610 if ( event
.m_metaDown
)
611 modifiers
|= cmdKey
;
613 control
= (ControlHandle
) m_macControl
;
614 if ( control
&& ::IsControlActive( control
) )
617 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
618 ::GetControl32BitValue(control
) - 1, m_nSelection
);
619 changing
.SetEventObject(this);
620 GetEventHandler()->ProcessEvent(changing
);
622 if(changing
.IsAllowed())
624 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
625 (ControlActionUPP
) -1);
626 wxTheApp
->s_lastMouseDown
= 0 ;
628 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
629 ::GetControl32BitValue(control
) - 1, m_nSelection
);
630 event
.SetEventObject(this);
632 GetEventHandler()->ProcessEvent(event
);
640 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
643 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
644 event
.SetEventObject(this);