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()))
36 static bool constantsSet
= false ;
38 short kwxMacTabLeftMargin
= 0 ;
39 short kwxMacTabTopMargin
= 0 ;
40 short kwxMacTabRightMargin
= 0 ;
41 short kwxMacTabBottomMargin
= 0 ;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 #if !USE_SHARED_LIBRARIES
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
51 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
52 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
53 EVT_MOUSE_EVENTS(wxNotebook::OnMouse
)
55 EVT_SIZE(wxNotebook::OnSize
)
56 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
57 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
60 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxNotebook construction
70 // ----------------------------------------------------------------------------
72 // common part of all ctors
73 void wxNotebook::Init()
77 if ( UMAHasAquaLayout() )
79 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
80 kwxMacTabLeftMargin
= 20 ;
81 kwxMacTabTopMargin
= 38 ;
82 kwxMacTabRightMargin
= 20 ;
83 kwxMacTabBottomMargin
= 12 ;
87 kwxMacTabLeftMargin
= 16 ;
88 kwxMacTabTopMargin
= 30 ;
89 kwxMacTabRightMargin
= 16 ;
90 kwxMacTabBottomMargin
= 16 ;
94 if ( UMAHasAquaLayout() )
96 m_macHorizontalBorder
= 7;
97 m_macVerticalBorder
= 8;
103 // default for dynamic class
104 wxNotebook::wxNotebook()
109 // the same arguments as for wxControl
110 wxNotebook::wxNotebook(wxWindow
*parent
,
115 const wxString
& name
)
119 Create(parent
, id
, pos
, size
, style
, name
);
123 bool wxNotebook::Create(wxWindow
*parent
,
128 const wxString
& name
)
133 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
135 int tabstyle
= kControlTabSmallNorthProc
;
136 if ( HasFlag(wxNB_LEFT
) )
137 tabstyle
= kControlTabSmallWestProc
;
138 else if ( HasFlag( wxNB_RIGHT
) )
139 tabstyle
= kControlTabSmallEastProc
;
140 else if ( HasFlag( wxNB_BOTTOM
) )
141 tabstyle
= kControlTabSmallSouthProc
;
144 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
145 tabstyle
, (long) this ) ;
147 MacPostControlCreate() ;
152 wxNotebook::~wxNotebook()
154 m_macControl
= NULL
;
157 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
)
159 wxSize sizeTotal
= sizePage
;
162 wxGetOsVersion( &major
, &minor
);
164 // Mac has large notebook borders. Aqua even more so.
166 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
192 // ----------------------------------------------------------------------------
193 // wxNotebook accessors
194 // ----------------------------------------------------------------------------
196 void wxNotebook::SetPadding(const wxSize
& padding
)
198 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
201 void wxNotebook::SetTabSize(const wxSize
& sz
)
203 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
206 void wxNotebook::SetPageSize(const wxSize
& size
)
208 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
211 int wxNotebook::SetSelection(int nPage
)
213 if( !IS_VALID_PAGE(nPage
) )
214 return m_nSelection
;
216 ChangePage(m_nSelection
, nPage
);
217 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
223 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
225 wxASSERT( IS_VALID_PAGE(nPage
) );
227 wxNotebookPage
*page
= m_pages
[nPage
];
228 page
->SetLabel(strText
);
234 wxString
wxNotebook::GetPageText(int nPage
) const
236 wxASSERT( IS_VALID_PAGE(nPage
) );
238 wxNotebookPage
*page
= m_pages
[nPage
];
239 return page
->GetLabel();
242 int wxNotebook::GetPageImage(int nPage
) const
244 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
246 return m_images
[nPage
];
249 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
253 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
254 _T("invalid image index in SetPageImage()") );
256 if ( nImage
!= m_images
[nPage
] )
258 // if the item didn't have an icon before or, on the contrary, did have
259 // it but has lost it now, its size will change - but if the icon just
261 m_images
[nPage
] = nImage
;
269 // ----------------------------------------------------------------------------
270 // wxNotebook operations
271 // ----------------------------------------------------------------------------
273 // remove one page from the notebook, without deleting the window
274 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
276 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
277 wxNotebookPage
* page
= m_pages
[nPage
] ;
278 m_pages
.RemoveAt(nPage
);
282 if(m_nSelection
>= GetPageCount()) {
283 m_nSelection
= GetPageCount() - 1;
285 if(m_nSelection
>= 0) {
286 m_pages
[m_nSelection
]->Show(true);
292 bool wxNotebook::DeleteAllPages()
294 // TODO: delete native widget pages
296 WX_CLEAR_ARRAY(m_pages
) ;
303 // same as AddPage() but does it at given position
304 bool wxNotebook::InsertPage(int nPage
,
305 wxNotebookPage
*pPage
,
306 const wxString
& strText
,
310 wxASSERT( pPage
!= NULL
);
311 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
313 pPage
->SetLabel(strText
);
315 // save the pointer to the page
316 m_pages
.Insert(pPage
, nPage
);
318 m_images
.Insert(imageId
, nPage
);
323 m_nSelection
= nPage
;
325 else if ( m_nSelection
== -1 ) {
328 else if (m_nSelection
>= nPage
) {
331 // don't show pages by default (we'll need to adjust their size first)
332 pPage
->Show( false ) ;
336 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
337 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
338 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
339 if ( pPage
->GetAutoLayout() ) {
346 /* Added by Mark Newsam
347 * When a page is added or deleted to the notebook this function updates
348 * information held in the m_macControl so that it matches the order
349 * the user would expect.
351 void wxNotebook::MacSetupTabs()
353 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
355 wxNotebookPage
*page
;
356 ControlTabInfoRec info
;
358 for(int ii
= 0; ii
< GetPageCount(); ii
++)
362 info
.iconSuiteID
= 0;
364 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
366 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
367 c2pstr( (char *) info
.name
) ;
369 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
370 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
371 SetTabEnabled( (ControlHandle
) m_macControl
, ii
+1 , true ) ;
374 if ( GetImageList() && GetPageImage(ii
) >= 0 && UMAGetSystemVersion() >= 0x1020 )
376 // tab controls only support very specific types of images, therefore we are doing an odyssee
377 // accross the icon worlds (even Apple DTS did not find a shorter path)
378 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
379 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
380 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
382 ControlButtonContentInfo info
;
383 wxMacCreateBitmapButton( &info
, *GetImageList()->GetBitmap( GetPageImage(ii
) ) , kControlContentPictHandle
) ;
384 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
385 OSErr err
= SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) info
.u
.picture
) ;
386 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
388 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) 1 , iconFamily
, &iconRef
) ;
389 wxASSERT_MSG( err
== noErr
, "Error when adding bitmap" ) ;
390 info
.contentType
= kControlContentIconRef
;
391 info
.u
.iconRef
= iconRef
;
392 SetControlData( (ControlHandle
) m_macControl
, ii
+1,kControlTabImageContentTag
,
393 sizeof( info
), (Ptr
)&info
);
394 wxASSERT_MSG( err
== noErr
, "Error when setting icon on tab" ) ;
395 UnregisterIconRef( 'WXNG' , (OSType
) 1 ) ;
396 ReleaseIconRef( iconRef
) ;
397 DisposeHandle( (Handle
) iconFamily
) ;
402 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
403 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
406 // ----------------------------------------------------------------------------
407 // wxNotebook callbacks
408 // ----------------------------------------------------------------------------
410 // @@@ OnSize() is used for setting the font when it's called for the first
411 // time because doing it in ::Create() doesn't work (for unknown reasons)
412 void wxNotebook::OnSize(wxSizeEvent
& event
)
414 // emulate page change (it's esp. important to do it first time because
415 // otherwise our page would stay invisible)
416 int nSel
= m_nSelection
;
420 // fit the notebook page to the tab control's display area
424 unsigned int nCount
= m_pages
.Count();
425 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
426 wxNotebookPage
*pPage
= m_pages
[nPage
];
427 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
428 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
429 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
430 if ( pPage
->GetAutoLayout() ) {
435 // Processing continues to next OnSize
439 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
441 // is it our tab control?
442 if ( event
.GetEventObject() == this )
443 ChangePage(event
.GetOldSelection(), event
.GetSelection());
445 // we want to give others a chance to process this message as well
449 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
451 // set focus to the currently selected page if any
452 if ( m_nSelection
!= -1 )
453 m_pages
[m_nSelection
]->SetFocus();
458 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
460 if ( event
.IsWindowChange() ) {
462 AdvanceSelection(event
.GetDirection());
465 // pass to the parent
467 event
.SetCurrentFocus(this);
468 GetParent()->ProcessEvent(event
);
473 // ----------------------------------------------------------------------------
474 // wxNotebook base class virtuals
475 // ----------------------------------------------------------------------------
477 // override these 2 functions to do nothing: everything is done in OnSize
479 void wxNotebook::SetConstraintSizes(bool /* recurse */)
481 // don't set the sizes of the pages - their correct size is not yet known
482 wxControl::SetConstraintSizes(FALSE
);
485 bool wxNotebook::DoPhase(int /* nPhase */)
490 void wxNotebook::Command(wxCommandEvent
& event
)
492 wxFAIL_MSG("wxNotebook::Command not implemented");
495 // ----------------------------------------------------------------------------
496 // wxNotebook helper functions
497 // ----------------------------------------------------------------------------
499 // hide the currently active panel and show the new one
500 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
502 // it's not an error (the message may be generated by the tab control itself)
503 // and it may happen - just do nothing
504 if ( nSel
== nOldSel
)
506 wxNotebookPage
*pPage
= m_pages
[nSel
];
513 // Hide previous page
514 if ( nOldSel
!= -1 ) {
515 m_pages
[nOldSel
]->Show(FALSE
);
518 wxNotebookPage
*pPage
= m_pages
[nSel
];
526 void wxNotebook::OnMouse( wxMouseEvent
&event
)
528 if ( (ControlHandle
) m_macControl
== NULL
)
534 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
539 MacClientToRootWindow( &x
, &y
) ;
541 ControlHandle control
;
550 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
551 modifiers
|= btnState
;
553 if ( event
.m_shiftDown
)
554 modifiers
|= shiftKey
;
556 if ( event
.m_controlDown
)
557 modifiers
|= controlKey
;
559 if ( event
.m_altDown
)
560 modifiers
|= optionKey
;
562 if ( event
.m_metaDown
)
563 modifiers
|= cmdKey
;
565 control
= (ControlHandle
) m_macControl
;
566 if ( control
&& ::IsControlActive( control
) )
569 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
570 ::GetControl32BitValue(control
) - 1, m_nSelection
);
571 changing
.SetEventObject(this);
572 ProcessEvent(changing
);
574 if(changing
.IsAllowed())
576 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
577 (ControlActionUPP
) -1);
578 wxTheApp
->s_lastMouseDown
= 0 ;
580 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
581 ::GetControl32BitValue(control
) - 1, m_nSelection
);
582 event
.SetEventObject(this);
592 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
595 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
596 event
.SetEventObject(this);