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 // ----------------------------------------------------------------------------
147 // wxNotebook accessors
148 // ----------------------------------------------------------------------------
150 void wxNotebook::SetPadding(const wxSize
& padding
)
152 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
155 void wxNotebook::SetTabSize(const wxSize
& sz
)
157 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
160 void wxNotebook::SetPageSize(const wxSize
& size
)
162 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
165 int wxNotebook::SetSelection(int nPage
)
167 if( !IS_VALID_PAGE(nPage
) )
168 return m_nSelection
;
170 ChangePage(m_nSelection
, nPage
);
171 SetControlValue( (ControlHandle
) m_macControl
, m_nSelection
+ 1 ) ;
176 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
178 wxASSERT( IS_VALID_PAGE(nPage
) );
180 wxNotebookPage
*page
= m_pages
[nPage
];
181 page
->SetLabel(strText
);
187 wxString
wxNotebook::GetPageText(int nPage
) const
189 wxASSERT( IS_VALID_PAGE(nPage
) );
191 wxNotebookPage
*page
= m_pages
[nPage
];
192 return page
->GetLabel();
195 int wxNotebook::GetPageImage(int nPage
) const
197 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
202 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
204 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
206 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
207 _T("invalid image index in SetPageImage()") );
212 // ----------------------------------------------------------------------------
213 // wxNotebook operations
214 // ----------------------------------------------------------------------------
216 // remove one page from the notebook, without deleting the window
217 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
219 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
220 wxNotebookPage
* page
= m_pages
[nPage
] ;
221 m_pages
.RemoveAt(nPage
);
225 if(m_nSelection
>= GetPageCount()) {
226 m_nSelection
= GetPageCount() - 1;
228 if(m_nSelection
>= 0) {
229 m_pages
[m_nSelection
]->Show(true);
235 bool wxNotebook::DeleteAllPages()
237 // TODO: delete native widget pages
239 WX_CLEAR_ARRAY(m_pages
) ;
246 // same as AddPage() but does it at given position
247 bool wxNotebook::InsertPage(int nPage
,
248 wxNotebookPage
*pPage
,
249 const wxString
& strText
,
253 wxASSERT( pPage
!= NULL
);
254 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
256 pPage
->SetLabel(strText
);
258 // save the pointer to the page
259 m_pages
.Insert(pPage
, nPage
);
264 m_nSelection
= nPage
;
266 else if ( m_nSelection
== -1 ) {
269 else if (m_nSelection
>= nPage
) {
272 // don't show pages by default (we'll need to adjust their size first)
273 pPage
->Show( false ) ;
277 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
278 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
279 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
280 if ( pPage
->GetAutoLayout() ) {
287 /* Added by Mark Newsam
288 * When a page is added or deleted to the notebook this function updates
289 * information held in the m_macControl so that it matches the order
290 * the user would expect.
292 void wxNotebook::MacSetupTabs()
294 SetControlMaximum( (ControlHandle
) m_macControl
, GetPageCount() ) ;
296 wxNotebookPage
*page
;
297 ControlTabInfoRec info
;
298 Boolean enabled
= true;
299 for(int ii
= 0; ii
< GetPageCount(); ii
++)
303 info
.iconSuiteID
= 0;
305 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
307 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
308 c2pstr( (char *) info
.name
) ;
310 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabInfoTag
,
311 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
312 SetControlData( (ControlHandle
) m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
313 sizeof( Boolean
), (Ptr
)&enabled
);
316 GetControlBounds((ControlHandle
)m_macControl
, &bounds
);
317 InvalWindowRect((WindowRef
)MacGetRootWindow(), &bounds
);
320 // ----------------------------------------------------------------------------
321 // wxNotebook callbacks
322 // ----------------------------------------------------------------------------
324 // @@@ OnSize() is used for setting the font when it's called for the first
325 // time because doing it in ::Create() doesn't work (for unknown reasons)
326 void wxNotebook::OnSize(wxSizeEvent
& event
)
328 // emulate page change (it's esp. important to do it first time because
329 // otherwise our page would stay invisible)
330 int nSel
= m_nSelection
;
334 // fit the notebook page to the tab control's display area
338 unsigned int nCount
= m_pages
.Count();
339 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
340 wxNotebookPage
*pPage
= m_pages
[nPage
];
341 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
342 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
343 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
344 if ( pPage
->GetAutoLayout() ) {
349 // Processing continues to next OnSize
353 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
355 // is it our tab control?
356 if ( event
.GetEventObject() == this )
357 ChangePage(event
.GetOldSelection(), event
.GetSelection());
359 // we want to give others a chance to process this message as well
363 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
365 // set focus to the currently selected page if any
366 if ( m_nSelection
!= -1 )
367 m_pages
[m_nSelection
]->SetFocus();
372 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
374 if ( event
.IsWindowChange() ) {
376 AdvanceSelection(event
.GetDirection());
379 // pass to the parent
381 event
.SetCurrentFocus(this);
382 GetParent()->ProcessEvent(event
);
387 // ----------------------------------------------------------------------------
388 // wxNotebook base class virtuals
389 // ----------------------------------------------------------------------------
391 // override these 2 functions to do nothing: everything is done in OnSize
393 void wxNotebook::SetConstraintSizes(bool /* recurse */)
395 // don't set the sizes of the pages - their correct size is not yet known
396 wxControl::SetConstraintSizes(FALSE
);
399 bool wxNotebook::DoPhase(int /* nPhase */)
404 void wxNotebook::Command(wxCommandEvent
& event
)
406 wxFAIL_MSG("wxNotebook::Command not implemented");
409 // ----------------------------------------------------------------------------
410 // wxNotebook helper functions
411 // ----------------------------------------------------------------------------
413 // hide the currently active panel and show the new one
414 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
416 // it's not an error (the message may be generated by the tab control itself)
417 // and it may happen - just do nothing
418 if ( nSel
== nOldSel
)
420 wxNotebookPage
*pPage
= m_pages
[nSel
];
427 // Hide previous page
428 if ( nOldSel
!= -1 ) {
429 m_pages
[nOldSel
]->Show(FALSE
);
432 wxNotebookPage
*pPage
= m_pages
[nSel
];
439 void wxNotebook::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
441 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue((ControlHandle
)m_macControl
) - 1, m_nSelection
);
442 event
.SetEventObject(this);