1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
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 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
136 kControlTabSmallProc
, (long) this ) ;
138 MacPostControlCreate() ;
143 wxNotebook::~wxNotebook()
145 m_macControl
= NULL
;
148 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
)
150 wxSize sizeTotal
= sizePage
;
153 wxGetOsVersion( &major
, &minor
);
155 // Mac has large notebook borders. Aqua even more so.
157 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
183 // ----------------------------------------------------------------------------
184 // wxNotebook accessors
185 // ----------------------------------------------------------------------------
187 void wxNotebook::SetPadding(const wxSize
& padding
)
189 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
192 void wxNotebook::SetTabSize(const wxSize
& sz
)
194 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
197 void wxNotebook::SetPageSize(const wxSize
& size
)
199 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
202 int wxNotebook::SetSelection(int nPage
)
204 if( !IS_VALID_PAGE(nPage
) )
205 return m_nSelection
;
207 ChangePage(m_nSelection
, nPage
);
208 SetControl32BitValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
213 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
215 wxASSERT( IS_VALID_PAGE(nPage
) );
217 wxNotebookPage
*page
= m_pages
[nPage
];
218 page
->SetLabel(strText
);
224 wxString
wxNotebook::GetPageText(int nPage
) const
226 wxASSERT( IS_VALID_PAGE(nPage
) );
228 wxNotebookPage
*page
= m_pages
[nPage
];
229 return page
->GetLabel();
232 int wxNotebook::GetPageImage(int nPage
) const
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
239 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
241 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
243 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
244 _T("invalid image index in SetPageImage()") );
249 // ----------------------------------------------------------------------------
250 // wxNotebook operations
251 // ----------------------------------------------------------------------------
253 // remove one page from the notebook, without deleting the window
254 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
256 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
257 wxNotebookPage
* page
= m_pages
[nPage
] ;
258 m_pages
.RemoveAt(nPage
);
262 if(m_nSelection
>= GetPageCount()) {
263 m_nSelection
= GetPageCount() - 1;
265 if(m_nSelection
>= 0) {
266 m_pages
[m_nSelection
]->Show(true);
272 bool wxNotebook::DeleteAllPages()
274 // TODO: delete native widget pages
276 WX_CLEAR_ARRAY(m_pages
) ;
283 // same as AddPage() but does it at given position
284 bool wxNotebook::InsertPage(int nPage
,
285 wxNotebookPage
*pPage
,
286 const wxString
& strText
,
290 wxASSERT( pPage
!= NULL
);
291 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
293 pPage
->SetLabel(strText
);
295 // save the pointer to the page
296 m_pages
.Insert(pPage
, nPage
);
301 m_nSelection
= nPage
;
303 else if ( m_nSelection
== -1 ) {
306 else if (m_nSelection
>= nPage
) {
309 // don't show pages by default (we'll need to adjust their size first)
310 pPage
->Show( false ) ;
314 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
315 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
316 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
317 if ( pPage
->GetAutoLayout() ) {
324 /* Added by Mark Newsam
325 * When a page is added or deleted to the notebook this function updates
326 * information held in the m_macControl so that it matches the order
327 * the user would expect.
329 void wxNotebook::MacSetupTabs()
331 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
333 wxNotebookPage
*page
;
334 ControlTabInfoRec info
;
335 Boolean enabled
= true;
336 for(int ii
= 0; ii
< GetPageCount(); ii
++)
340 info
.iconSuiteID
= 0;
342 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
344 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
345 c2pstr( (char *) info
.name
) ;
347 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
348 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
349 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
350 sizeof( Boolean
), (Ptr
)&enabled
);
353 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
354 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
357 // ----------------------------------------------------------------------------
358 // wxNotebook callbacks
359 // ----------------------------------------------------------------------------
361 // @@@ OnSize() is used for setting the font when it's called for the first
362 // time because doing it in ::Create() doesn't work (for unknown reasons)
363 void wxNotebook::OnSize(wxSizeEvent
& event
)
365 // emulate page change (it's esp. important to do it first time because
366 // otherwise our page would stay invisible)
367 int nSel
= m_nSelection
;
371 // fit the notebook page to the tab control's display area
375 unsigned int nCount
= m_pages
.Count();
376 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
377 wxNotebookPage
*pPage
= m_pages
[nPage
];
378 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
379 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
380 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
381 if ( pPage
->GetAutoLayout() ) {
386 // Processing continues to next OnSize
390 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
392 // is it our tab control?
393 if ( event
.GetEventObject() == this )
394 ChangePage(event
.GetOldSelection(), event
.GetSelection());
396 // we want to give others a chance to process this message as well
400 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
402 // set focus to the currently selected page if any
403 if ( m_nSelection
!= -1 )
404 m_pages
[m_nSelection
]->SetFocus();
409 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
411 if ( event
.IsWindowChange() ) {
413 AdvanceSelection(event
.GetDirection());
416 // pass to the parent
418 event
.SetCurrentFocus(this);
419 GetParent()->ProcessEvent(event
);
424 // ----------------------------------------------------------------------------
425 // wxNotebook base class virtuals
426 // ----------------------------------------------------------------------------
428 // override these 2 functions to do nothing: everything is done in OnSize
430 void wxNotebook::SetConstraintSizes(bool /* recurse */)
432 // don't set the sizes of the pages - their correct size is not yet known
433 wxControl::SetConstraintSizes(FALSE
);
436 bool wxNotebook::DoPhase(int /* nPhase */)
441 void wxNotebook::Command(wxCommandEvent
& event
)
443 wxFAIL_MSG("wxNotebook::Command not implemented");
446 // ----------------------------------------------------------------------------
447 // wxNotebook helper functions
448 // ----------------------------------------------------------------------------
450 // hide the currently active panel and show the new one
451 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
453 // it's not an error (the message may be generated by the tab control itself)
454 // and it may happen - just do nothing
455 if ( nSel
== nOldSel
)
457 wxNotebookPage
*pPage
= m_pages
[nSel
];
464 // Hide previous page
465 if ( nOldSel
!= -1 ) {
466 m_pages
[nOldSel
]->Show(FALSE
);
469 wxNotebookPage
*pPage
= m_pages
[nSel
];
477 void wxNotebook::OnMouse( wxMouseEvent
&event
)
479 if ( (ControlHandle
) m_macControl
== NULL
)
485 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
490 MacClientToRootWindow( &x
, &y
) ;
492 ControlHandle control
;
501 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
502 modifiers
|= btnState
;
504 if ( event
.m_shiftDown
)
505 modifiers
|= shiftKey
;
507 if ( event
.m_controlDown
)
508 modifiers
|= controlKey
;
510 if ( event
.m_altDown
)
511 modifiers
|= optionKey
;
513 if ( event
.m_metaDown
)
514 modifiers
|= cmdKey
;
516 control
= (ControlHandle
) m_macControl
;
517 if ( control
&& ::IsControlActive( control
) )
520 wxNotebookEvent
changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
,
521 ::GetControl32BitValue(control
) - 1, m_nSelection
);
522 changing
.SetEventObject(this);
523 ProcessEvent(changing
);
525 if(changing
.IsAllowed())
527 controlpart
= ::HandleControlClick(control
, localwhere
, modifiers
,
528 (ControlActionUPP
) -1);
529 wxTheApp
->s_lastMouseDown
= 0 ;
531 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
,
532 ::GetControl32BitValue(control
) - 1, m_nSelection
);
533 event
.SetEventObject(this);
543 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
546 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControl32BitValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
547 event
.SetEventObject(this);