1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
23 #include "wx/string.h"
25 #include "wx/imaglist.h"
26 #include "wx/notebook.h"
27 #include "wx/mac/uma.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 // check that the page index is valid
33 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
35 static bool constantsSet
= false ;
37 short kwxMacTabLeftMargin
= 0 ;
38 short kwxMacTabTopMargin
= 0 ;
39 short kwxMacTabRightMargin
= 0 ;
40 short kwxMacTabBottomMargin
= 0 ;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 #if !USE_SHARED_LIBRARIES
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
50 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
51 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
53 EVT_SIZE(wxNotebook::OnSize
)
54 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
55 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
58 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxNotebook construction
68 // ----------------------------------------------------------------------------
70 // common part of all ctors
71 void wxNotebook::Init()
75 if ( UMAHasAquaLayout() )
77 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
78 kwxMacTabLeftMargin
= 20 ;
79 kwxMacTabTopMargin
= 38 ;
80 kwxMacTabRightMargin
= 20 ;
81 kwxMacTabBottomMargin
= 12 ;
85 kwxMacTabLeftMargin
= 16 ;
86 kwxMacTabTopMargin
= 30 ;
87 kwxMacTabRightMargin
= 16 ;
88 kwxMacTabBottomMargin
= 16 ;
92 if ( UMAHasAquaLayout() )
94 m_macHorizontalBorder
= 7;
95 m_macVerticalBorder
= 8;
101 // default for dynamic class
102 wxNotebook::wxNotebook()
107 // the same arguments as for wxControl
108 wxNotebook::wxNotebook(wxWindow
*parent
,
113 const wxString
& name
)
117 Create(parent
, id
, pos
, size
, style
, name
);
121 bool wxNotebook::Create(wxWindow
*parent
,
126 const wxString
& name
)
131 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
133 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
134 kControlTabSmallProc
, (long) this ) ;
136 MacPostControlCreate() ;
141 wxNotebook::~wxNotebook()
143 m_macControl
= NULL
;
146 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
)
148 wxSize sizeTotal
= sizePage
;
151 wxGetOsVersion( &major
, &minor
);
153 // Mac has large notebook borders. Aqua even more so.
155 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
181 // ----------------------------------------------------------------------------
182 // wxNotebook accessors
183 // ----------------------------------------------------------------------------
185 void wxNotebook::SetPadding(const wxSize
& padding
)
187 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
190 void wxNotebook::SetTabSize(const wxSize
& sz
)
192 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
195 void wxNotebook::SetPageSize(const wxSize
& size
)
197 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
200 int wxNotebook::SetSelection(int nPage
)
202 if( !IS_VALID_PAGE(nPage
) )
203 return m_nSelection
;
205 ChangePage(m_nSelection
, nPage
);
206 SetControlValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
211 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
213 wxASSERT( IS_VALID_PAGE(nPage
) );
215 wxNotebookPage
*page
= m_pages
[nPage
];
216 page
->SetLabel(strText
);
222 wxString
wxNotebook::GetPageText(int nPage
) const
224 wxASSERT( IS_VALID_PAGE(nPage
) );
226 wxNotebookPage
*page
= m_pages
[nPage
];
227 return page
->GetLabel();
230 int wxNotebook::GetPageImage(int nPage
) const
232 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
237 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
239 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
241 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
242 _T("invalid image index in SetPageImage()") );
247 // ----------------------------------------------------------------------------
248 // wxNotebook operations
249 // ----------------------------------------------------------------------------
251 // remove one page from the notebook, without deleting the window
252 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
254 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
255 wxNotebookPage
* page
= m_pages
[nPage
] ;
256 m_pages
.RemoveAt(nPage
);
260 if(m_nSelection
>= GetPageCount()) {
261 m_nSelection
= GetPageCount() - 1;
263 if(m_nSelection
>= 0) {
264 m_pages
[m_nSelection
]->Show(true);
270 bool wxNotebook::DeleteAllPages()
272 // TODO: delete native widget pages
274 WX_CLEAR_ARRAY(m_pages
) ;
281 // same as AddPage() but does it at given position
282 bool wxNotebook::InsertPage(int nPage
,
283 wxNotebookPage
*pPage
,
284 const wxString
& strText
,
288 wxASSERT( pPage
!= NULL
);
289 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
291 pPage
->SetLabel(strText
);
293 // save the pointer to the page
294 m_pages
.Insert(pPage
, nPage
);
299 m_nSelection
= nPage
;
301 else if ( m_nSelection
== -1 ) {
304 else if (m_nSelection
>= nPage
) {
307 // don't show pages by default (we'll need to adjust their size first)
308 pPage
->Show( false ) ;
312 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
313 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
314 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
315 if ( pPage
->GetAutoLayout() ) {
322 /* Added by Mark Newsam
323 * When a page is added or deleted to the notebook this function updates
324 * information held in the m_macControl so that it matches the order
325 * the user would expect.
327 void wxNotebook::MacSetupTabs()
329 SetControlMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
331 wxNotebookPage
*page
;
332 ControlTabInfoRec info
;
333 Boolean enabled
= true;
334 for(int ii
= 0; ii
< GetPageCount(); ii
++)
338 info
.iconSuiteID
= 0;
340 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
342 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
343 c2pstr( (char *) info
.name
) ;
345 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
346 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
347 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
348 sizeof( Boolean
), (Ptr
)&enabled
);
351 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
352 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
355 // ----------------------------------------------------------------------------
356 // wxNotebook callbacks
357 // ----------------------------------------------------------------------------
359 // @@@ OnSize() is used for setting the font when it's called for the first
360 // time because doing it in ::Create() doesn't work (for unknown reasons)
361 void wxNotebook::OnSize(wxSizeEvent
& event
)
363 // emulate page change (it's esp. important to do it first time because
364 // otherwise our page would stay invisible)
365 int nSel
= m_nSelection
;
369 // fit the notebook page to the tab control's display area
373 unsigned int nCount
= m_pages
.Count();
374 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
375 wxNotebookPage
*pPage
= m_pages
[nPage
];
376 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
377 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
378 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
379 if ( pPage
->GetAutoLayout() ) {
384 // Processing continues to next OnSize
388 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
390 // is it our tab control?
391 if ( event
.GetEventObject() == this )
392 ChangePage(event
.GetOldSelection(), event
.GetSelection());
394 // we want to give others a chance to process this message as well
398 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
400 // set focus to the currently selected page if any
401 if ( m_nSelection
!= -1 )
402 m_pages
[m_nSelection
]->SetFocus();
407 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
409 if ( event
.IsWindowChange() ) {
411 AdvanceSelection(event
.GetDirection());
414 // pass to the parent
416 event
.SetCurrentFocus(this);
417 GetParent()->ProcessEvent(event
);
422 // ----------------------------------------------------------------------------
423 // wxNotebook base class virtuals
424 // ----------------------------------------------------------------------------
426 // override these 2 functions to do nothing: everything is done in OnSize
428 void wxNotebook::SetConstraintSizes(bool /* recurse */)
430 // don't set the sizes of the pages - their correct size is not yet known
431 wxControl::SetConstraintSizes(FALSE
);
434 bool wxNotebook::DoPhase(int /* nPhase */)
439 void wxNotebook::Command(wxCommandEvent
& event
)
441 wxFAIL_MSG("wxNotebook::Command not implemented");
444 // ----------------------------------------------------------------------------
445 // wxNotebook helper functions
446 // ----------------------------------------------------------------------------
448 // hide the currently active panel and show the new one
449 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
451 // it's not an error (the message may be generated by the tab control itself)
452 // and it may happen - just do nothing
453 if ( nSel
== nOldSel
)
455 wxNotebookPage
*pPage
= m_pages
[nSel
];
462 // Hide previous page
463 if ( nOldSel
!= -1 ) {
464 m_pages
[nOldSel
]->Show(FALSE
);
467 wxNotebookPage
*pPage
= m_pages
[nSel
];
474 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
476 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
477 event
.SetEventObject(this);