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
)
181 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
183 int tabstyle
= kControlTabSmallNorthProc
;
184 if ( HasFlag(wxNB_LEFT
) )
185 tabstyle
= kControlTabSmallWestProc
;
186 else if ( HasFlag( wxNB_RIGHT
) )
187 tabstyle
= kControlTabSmallEastProc
;
188 else if ( HasFlag( wxNB_BOTTOM
) )
189 tabstyle
= kControlTabSmallSouthProc
;
192 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
193 tabstyle
, (long) this ) ;
195 MacPostControlCreate() ;
200 wxNotebook::~wxNotebook()
202 m_macControl
= NULL
;
205 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
207 wxSize sizeTotal
= sizePage
;
210 wxGetOsVersion( &major
, &minor
);
212 // Mac has large notebook borders. Aqua even more so.
214 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
240 // ----------------------------------------------------------------------------
241 // wxNotebook accessors
242 // ----------------------------------------------------------------------------
244 void wxNotebook::SetPadding(const wxSize
& padding
)
246 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
249 void wxNotebook::SetTabSize(const wxSize
& sz
)
251 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
254 void wxNotebook::SetPageSize(const wxSize
& size
)
256 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
259 int wxNotebook::SetSelection(int nPage
)
261 if( !IS_VALID_PAGE(nPage
) )
262 return m_nSelection
;
264 ChangePage(m_nSelection
, nPage
);
265 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
271 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
273 wxASSERT( IS_VALID_PAGE(nPage
) );
275 wxNotebookPage
*page
= m_pages
[nPage
];
276 page
->SetLabel(strText
);
282 wxString
wxNotebook::GetPageText(int nPage
) const
284 wxASSERT( IS_VALID_PAGE(nPage
) );
286 wxNotebookPage
*page
= m_pages
[nPage
];
287 return page
->GetLabel();
290 int wxNotebook::GetPageImage(int nPage
) const
292 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
294 return m_images
[nPage
];
297 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
299 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
301 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
302 _T("invalid image index in SetPageImage()") );
304 if ( nImage
!= m_images
[nPage
] )
306 // if the item didn't have an icon before or, on the contrary, did have
307 // it but has lost it now, its size will change - but if the icon just
309 m_images
[nPage
] = nImage
;
317 // ----------------------------------------------------------------------------
318 // wxNotebook operations
319 // ----------------------------------------------------------------------------
321 // remove one page from the notebook, without deleting the window
322 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
324 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
325 wxNotebookPage
* page
= m_pages
[nPage
] ;
326 m_pages
.RemoveAt(nPage
);
330 if(m_nSelection
>= GetPageCount()) {
331 m_nSelection
= GetPageCount() - 1;
333 if(m_nSelection
>= 0) {
334 m_pages
[m_nSelection
]->Show(true);
340 bool wxNotebook::DeleteAllPages()
342 // TODO: delete native widget pages
344 WX_CLEAR_ARRAY(m_pages
) ;
351 // same as AddPage() but does it at given position
352 bool wxNotebook::InsertPage(int nPage
,
353 wxNotebookPage
*pPage
,
354 const wxString
& strText
,
358 wxASSERT( pPage
!= NULL
);
359 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
361 pPage
->SetLabel(strText
);
363 // save the pointer to the page
364 m_pages
.Insert(pPage
, nPage
);
366 m_images
.Insert(imageId
, nPage
);
371 m_nSelection
= nPage
;
373 else if ( m_nSelection
== -1 ) {
376 else if (m_nSelection
>= nPage
) {
379 // don't show pages by default (we'll need to adjust their size first)
380 pPage
->Show( false ) ;
384 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
385 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
386 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
387 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
388 if ( pPage
->GetAutoLayout() ) {
395 /* Added by Mark Newsam
396 * When a page is added or deleted to the notebook this function updates
397 * information held in the m_macControl so that it matches the order
398 * the user would expect.
400 void wxNotebook::MacSetupTabs()
402 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
404 wxNotebookPage
*page
;
405 ControlTabInfoRec info
;
407 for(int ii
= 0; ii
< GetPageCount(); ii
++)
411 info
.iconSuiteID
= 0;
412 wxMacStringToPascal( page
->GetLabel() , info
.name
) ;
414 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
415 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
416 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
417 #if 0 // TARGET_CARBON
418 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
420 // tab controls only support very specific types of images, therefore we are doing an odyssee
421 // accross the icon worlds (even Apple DTS did not find a shorter path)
422 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
423 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
424 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
426 ControlButtonContentInfo info
;
427 wxMacCreateBitmapButton( &info
, *GetImageList()->GetBitmap( GetPageImage(ii
) ) , kControlContentPictHandle
) ;
428 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
429 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
430 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
432 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1 , iconFamily
, &iconRef
) ;
433 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
434 info
.contentType
= kControlContentIconRef
;
435 info
.u
.iconRef
= iconRef
;
436 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
437 sizeof( info
), (Ptr
)&info
);
438 wxASSERT_MSG( err
== noErr
, "Error when setting icon on tab" ) ;
439 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
440 ReleaseIconRef( iconRef
) ;
441 DisposeHandle( (Handle
) iconFamily
) ;
446 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
447 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
450 // ----------------------------------------------------------------------------
451 // wxNotebook callbacks
452 // ----------------------------------------------------------------------------
454 // @@@ OnSize() is used for setting the font when it's called for the first
455 // time because doing it in ::Create() doesn't work (for unknown reasons)
456 void wxNotebook::OnSize(wxSizeEvent
& event
)
458 // emulate page change (it's esp. important to do it first time because
459 // otherwise our page would stay invisible)
460 int nSel
= m_nSelection
;
464 // fit the notebook page to the tab control's display area
468 unsigned int nCount
= m_pages
.Count();
469 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
470 wxNotebookPage
*pPage
= m_pages
[nPage
];
471 pPage
->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder
,
472 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder
,
473 w
- wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder
,
474 h
- wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder
);
475 if ( pPage
->GetAutoLayout() ) {
480 // Processing continues to next OnSize
484 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
486 // is it our tab control?
487 if ( event
.GetEventObject() == this )
488 ChangePage(event
.GetOldSelection(), event
.GetSelection());
490 // we want to give others a chance to process this message as well
494 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
496 // set focus to the currently selected page if any
497 if ( m_nSelection
!= -1 )
498 m_pages
[m_nSelection
]->SetFocus();
503 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
505 if ( event
.IsWindowChange() ) {
507 AdvanceSelection(event
.GetDirection());
510 // pass to the parent
512 event
.SetCurrentFocus(this);
513 GetParent()->ProcessEvent(event
);
518 // ----------------------------------------------------------------------------
519 // wxNotebook base class virtuals
520 // ----------------------------------------------------------------------------
522 // override these 2 functions to do nothing: everything is done in OnSize
524 void wxNotebook::SetConstraintSizes(bool /* recurse */)
526 // don't set the sizes of the pages - their correct size is not yet known
527 wxControl::SetConstraintSizes(FALSE
);
530 bool wxNotebook::DoPhase(int /* nPhase */)
535 void wxNotebook::Command(wxCommandEvent
& event
)
537 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
540 // ----------------------------------------------------------------------------
541 // wxNotebook helper functions
542 // ----------------------------------------------------------------------------
544 // hide the currently active panel and show the new one
545 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
547 // it's not an error (the message may be generated by the tab control itself)
548 // and it may happen - just do nothing
549 if ( nSel
== nOldSel
)
551 wxNotebookPage
*pPage
= m_pages
[nSel
];
558 // Hide previous page
559 if ( nOldSel
!= -1 ) {
560 m_pages
[nOldSel
]->Show(FALSE
);
563 wxNotebookPage
*pPage
= m_pages
[nSel
];
571 void wxNotebook::OnMouse( wxMouseEvent
&event
)
573 if ( (ControlHandle
) m_macControl
== NULL
)
579 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
584 MacClientToRootWindow( &x
, &y
) ;
586 ControlHandle control
;
595 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
596 modifiers
|= btnState
;
598 if ( event
.m_shiftDown
)
599 modifiers
|= shiftKey
;
601 if ( event
.m_controlDown
)
602 modifiers
|= controlKey
;
604 if ( event
.m_altDown
)
605 modifiers
|= optionKey
;
607 if ( event
.m_metaDown
)
608 modifiers
|= cmdKey
;
610 control
= (ControlHandle
) m_macControl
;
611 if ( control
&& ::IsControlActive( control
) )
614 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
615 ::GetControl32BitValue(control
) - 1, m_nSelection
);
616 changing
.SetEventObject(this);
617 GetEventHandler()->ProcessEvent(changing
);
619 if(changing
.IsAllowed())
621 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
622 (ControlActionUPP
) -1);
623 wxTheApp
->s_lastMouseDown
= 0 ;
625 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
626 ::GetControl32BitValue(control
) - 1, m_nSelection
);
627 event
.SetEventObject(this);
629 GetEventHandler()->ProcessEvent(event
);
637 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
640 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
641 event
.SetEventObject(this);