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 if( !IS_VALID_PAGE(nPage
) )
153 return m_nSelection
;
155 ChangePage(m_nSelection
, nPage
);
156 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
161 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
163 wxASSERT( IS_VALID_PAGE(nPage
) );
165 wxNotebookPage
*page
= m_pages
[nPage
];
166 page
->SetLabel(strText
);
172 wxString
wxNotebook::GetPageText(int nPage
) const
174 wxASSERT( IS_VALID_PAGE(nPage
) );
176 wxNotebookPage
*page
= m_pages
[nPage
];
177 return page
->GetLabel();
180 int wxNotebook::GetPageImage(int nPage
) const
182 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
187 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
189 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
191 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
192 _T("invalid image index in SetPageImage()") );
197 // ----------------------------------------------------------------------------
198 // wxNotebook operations
199 // ----------------------------------------------------------------------------
201 // remove one page from the notebook, without deleting the window
202 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
204 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
205 wxNotebookPage
* page
= m_pages
[nPage
] ;
206 m_pages
.RemoveAt(nPage
);
210 if(m_nSelection
>= GetPageCount()) {
211 m_nSelection
= GetPageCount() - 1;
213 if(m_nSelection
>= 0) {
214 m_pages
[m_nSelection
]->Show(true);
220 bool wxNotebook::DeleteAllPages()
222 // TODO: delete native widget pages
224 WX_CLEAR_ARRAY(m_pages
) ;
231 // same as AddPage() but does it at given position
232 bool wxNotebook::InsertPage(int nPage
,
233 wxNotebookPage
*pPage
,
234 const wxString
& strText
,
238 wxASSERT( pPage
!= NULL
);
239 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
241 pPage
->SetLabel(strText
);
243 // save the pointer to the page
244 m_pages
.Insert(pPage
, nPage
);
249 m_nSelection
= nPage
;
251 else if ( m_nSelection
== -1 ) {
254 else if (m_nSelection
>= nPage
) {
257 // don't show pages by default (we'll need to adjust their size first)
258 pPage
->Show( false ) ;
262 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
263 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
264 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
265 if ( pPage
->GetAutoLayout() ) {
272 /* Added by Mark Newsam
273 * When a page is added or deleted to the notebook this function updates
274 * information held in the m_macControl so that it matches the order
275 * the user would expect.
277 void wxNotebook::MacSetupTabs()
279 SetControlMaximum( m_macControl
, GetPageCount() ) ;
281 wxNotebookPage
*page
;
282 ControlTabInfoRec info
;
283 Boolean enabled
= true;
284 for(int ii
= 0; ii
< GetPageCount(); ii
++)
288 info
.iconSuiteID
= 0;
290 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
292 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
293 c2pstr( (char *) info
.name
) ;
295 SetControlData( m_macControl
, ii
+1, kControlTabInfoTag
,
296 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
297 SetControlData( m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
298 sizeof( Boolean
), (Ptr
)&enabled
);
301 GetControlBounds(m_macControl
, &bounds
);
302 InvalWindowRect(GetMacRootWindow(), &bounds
);
305 // ----------------------------------------------------------------------------
306 // wxNotebook callbacks
307 // ----------------------------------------------------------------------------
309 // @@@ OnSize() is used for setting the font when it's called for the first
310 // time because doing it in ::Create() doesn't work (for unknown reasons)
311 void wxNotebook::OnSize(wxSizeEvent
& event
)
313 // emulate page change (it's esp. important to do it first time because
314 // otherwise our page would stay invisible)
315 int nSel
= m_nSelection
;
319 // fit the notebook page to the tab control's display area
323 unsigned int nCount
= m_pages
.Count();
324 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
325 wxNotebookPage
*pPage
= m_pages
[nPage
];
326 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
327 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
328 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
329 if ( pPage
->GetAutoLayout() ) {
334 // Processing continues to next OnSize
338 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
340 // is it our tab control?
341 if ( event
.GetEventObject() == this )
342 ChangePage(event
.GetOldSelection(), event
.GetSelection());
344 // we want to give others a chance to process this message as well
348 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
350 // set focus to the currently selected page if any
351 if ( m_nSelection
!= -1 )
352 m_pages
[m_nSelection
]->SetFocus();
357 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
359 if ( event
.IsWindowChange() ) {
361 AdvanceSelection(event
.GetDirection());
364 // pass to the parent
366 event
.SetCurrentFocus(this);
367 GetParent()->ProcessEvent(event
);
372 // ----------------------------------------------------------------------------
373 // wxNotebook base class virtuals
374 // ----------------------------------------------------------------------------
376 // override these 2 functions to do nothing: everything is done in OnSize
378 void wxNotebook::SetConstraintSizes(bool /* recurse */)
380 // don't set the sizes of the pages - their correct size is not yet known
381 wxControl::SetConstraintSizes(FALSE
);
384 bool wxNotebook::DoPhase(int /* nPhase */)
389 void wxNotebook::Command(wxCommandEvent
& event
)
391 wxFAIL_MSG("wxNotebook::Command not implemented");
394 // ----------------------------------------------------------------------------
395 // wxNotebook helper functions
396 // ----------------------------------------------------------------------------
398 // hide the currently active panel and show the new one
399 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
401 // it's not an error (the message may be generated by the tab control itself)
402 // and it may happen - just do nothing
403 if ( nSel
== nOldSel
)
405 wxNotebookPage
*pPage
= m_pages
[nSel
];
412 // Hide previous page
413 if ( nOldSel
!= -1 ) {
414 m_pages
[nOldSel
]->Show(FALSE
);
417 wxNotebookPage
*pPage
= m_pages
[nSel
];
424 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
426 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
427 event
.SetEventObject(this);