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()))
36 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
37 const short kwxMacTabLeftMargin
= 20 ;
38 const short kwxMacTabTopMargin
= 38 ;
39 const short kwxMacTabRightMargin
= 20 ;
40 const short kwxMacTabBottomMargin
= 12 ;
42 const short kwxMacTabLeftMargin
= 16 ;
43 const short kwxMacTabTopMargin
= 30 ;
44 const short kwxMacTabRightMargin
= 16 ;
45 const short kwxMacTabBottomMargin
= 16 ;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #if !USE_SHARED_LIBRARIES
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
56 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
57 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
59 EVT_SIZE(wxNotebook::OnSize
)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxNotebook construction
74 // ----------------------------------------------------------------------------
76 // common part of all ctors
77 void wxNotebook::Init()
80 m_macHorizontalBorder
= 7;
81 m_macVerticalBorder
= 8;
86 // default for dynamic class
87 wxNotebook::wxNotebook()
92 // the same arguments as for wxControl
93 wxNotebook::wxNotebook(wxWindow
*parent
,
102 Create(parent
, id
, pos
, size
, style
, name
);
106 bool wxNotebook::Create(wxWindow
*parent
,
111 const wxString
& name
)
116 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
118 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
119 kControlTabSmallProc
, (long) this ) ;
121 MacPostControlCreate() ;
126 wxNotebook::~wxNotebook()
128 m_macControl
= NULL
;
131 // ----------------------------------------------------------------------------
132 // wxNotebook accessors
133 // ----------------------------------------------------------------------------
135 void wxNotebook::SetPadding(const wxSize
& padding
)
137 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
140 void wxNotebook::SetTabSize(const wxSize
& sz
)
142 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
145 void wxNotebook::SetPageSize(const wxSize
& size
)
147 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
150 int wxNotebook::SetSelection(int nPage
)
152 wxASSERT( IS_VALID_PAGE(nPage
) );
154 ChangePage(m_nSelection
, nPage
);
155 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
160 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
162 wxASSERT( IS_VALID_PAGE(nPage
) );
164 wxNotebookPage
*page
= m_pages
[nPage
];
165 page
->SetLabel(strText
);
171 wxString
wxNotebook::GetPageText(int nPage
) const
173 wxASSERT( IS_VALID_PAGE(nPage
) );
175 wxNotebookPage
*page
= m_pages
[nPage
];
176 return page
->GetLabel();
179 int wxNotebook::GetPageImage(int nPage
) const
181 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
186 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
188 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
190 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
191 _T("invalid image index in SetPageImage()") );
196 // ----------------------------------------------------------------------------
197 // wxNotebook operations
198 // ----------------------------------------------------------------------------
200 // remove one page from the notebook, without deleting the window
201 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
203 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
204 wxNotebookPage
* page
= m_pages
[nPage
] ;
205 m_pages
.RemoveAt(nPage
);
209 if(m_nSelection
>= GetPageCount()) {
210 m_nSelection
= GetPageCount() - 1;
212 if(m_nSelection
>= 0) {
213 m_pages
[m_nSelection
]->Show(true);
219 bool wxNotebook::DeleteAllPages()
221 // TODO: delete native widget pages
223 WX_CLEAR_ARRAY(m_pages
) ;
230 // same as AddPage() but does it at given position
231 bool wxNotebook::InsertPage(int nPage
,
232 wxNotebookPage
*pPage
,
233 const wxString
& strText
,
237 wxASSERT( pPage
!= NULL
);
238 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
240 pPage
->SetLabel(strText
);
242 // save the pointer to the page
243 m_pages
.Insert(pPage
, nPage
);
248 m_nSelection
= nPage
;
250 else if ( m_nSelection
== -1 ) {
253 else if (m_nSelection
>= nPage
) {
256 // don't show pages by default (we'll need to adjust their size first)
257 pPage
->Show( false ) ;
261 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
262 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
263 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
264 if ( pPage
->GetAutoLayout() ) {
271 /* Added by Mark Newsam
272 * When a page is added or deleted to the notebook this function updates
273 * information held in the m_macControl so that it matches the order
274 * the user would expect.
276 void wxNotebook::MacSetupTabs()
278 SetControlMaximum( m_macControl
, GetPageCount() ) ;
280 wxNotebookPage
*page
;
281 ControlTabInfoRec info
;
282 Boolean enabled
= true;
283 for(int ii
= 0; ii
< GetPageCount(); ii
++)
287 info
.iconSuiteID
= 0;
289 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
291 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
292 c2pstr( (char *) info
.name
) ;
294 SetControlData( m_macControl
, ii
+1, kControlTabInfoTag
,
295 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
296 SetControlData( m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
297 sizeof( Boolean
), (Ptr
)&enabled
);
300 GetControlBounds(m_macControl
, &bounds
);
301 InvalWindowRect(GetMacRootWindow(), &bounds
);
304 // ----------------------------------------------------------------------------
305 // wxNotebook callbacks
306 // ----------------------------------------------------------------------------
308 // @@@ OnSize() is used for setting the font when it's called for the first
309 // time because doing it in ::Create() doesn't work (for unknown reasons)
310 void wxNotebook::OnSize(wxSizeEvent
& event
)
312 // emulate page change (it's esp. important to do it first time because
313 // otherwise our page would stay invisible)
314 int nSel
= m_nSelection
;
318 // fit the notebook page to the tab control's display area
322 unsigned int nCount
= m_pages
.Count();
323 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
324 wxNotebookPage
*pPage
= m_pages
[nPage
];
325 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
326 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
327 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
328 if ( pPage
->GetAutoLayout() ) {
333 // Processing continues to next OnSize
337 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
339 // is it our tab control?
340 if ( event
.GetEventObject() == this )
341 ChangePage(event
.GetOldSelection(), event
.GetSelection());
343 // we want to give others a chance to process this message as well
347 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
349 // set focus to the currently selected page if any
350 if ( m_nSelection
!= -1 )
351 m_pages
[m_nSelection
]->SetFocus();
356 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
358 if ( event
.IsWindowChange() ) {
360 AdvanceSelection(event
.GetDirection());
363 // pass to the parent
365 event
.SetCurrentFocus(this);
366 GetParent()->ProcessEvent(event
);
371 // ----------------------------------------------------------------------------
372 // wxNotebook base class virtuals
373 // ----------------------------------------------------------------------------
375 // override these 2 functions to do nothing: everything is done in OnSize
377 void wxNotebook::SetConstraintSizes(bool /* recurse */)
379 // don't set the sizes of the pages - their correct size is not yet known
380 wxControl::SetConstraintSizes(FALSE
);
383 bool wxNotebook::DoPhase(int /* nPhase */)
388 void wxNotebook::Command(wxCommandEvent
& event
)
390 wxFAIL_MSG("wxNotebook::Command not implemented");
393 // ----------------------------------------------------------------------------
394 // wxNotebook helper functions
395 // ----------------------------------------------------------------------------
397 // hide the currently active panel and show the new one
398 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
400 // it's not an error (the message may be generated by the tab control itself)
401 // and it may happen - just do nothing
402 if ( nSel
== nOldSel
)
404 wxNotebookPage
*pPage
= m_pages
[nSel
];
411 // Hide previous page
412 if ( nOldSel
!= -1 ) {
413 m_pages
[nOldSel
]->Show(FALSE
);
416 wxNotebookPage
*pPage
= m_pages
[nSel
];
423 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
425 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
426 event
.SetEventObject(this);