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 const short kwxMacTabLeftMargin
= 16 ;
36 const short kwxMacTabTopMargin
= 30 ;
37 const short kwxMacTabRightMargin
= 16 ;
38 const short kwxMacTabBottomMargin
= 16 ;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
45 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
47 EVT_SIZE(wxNotebook::OnSize
)
48 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
49 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
52 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxNotebook construction
61 // ----------------------------------------------------------------------------
63 // common part of all ctors
64 void wxNotebook::Init()
70 // default for dynamic class
71 wxNotebook::wxNotebook()
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow
*parent
,
86 Create(parent
, id
, pos
, size
, style
, name
);
90 bool wxNotebook::Create(wxWindow
*parent
,
100 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
102 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
103 kControlTabSmallProc
, (long) this ) ;
105 MacPostControlCreate() ;
110 wxNotebook::~wxNotebook()
114 // ----------------------------------------------------------------------------
115 // wxNotebook accessors
116 // ----------------------------------------------------------------------------
117 int wxNotebook::GetPageCount() const
119 return m_aPages
.Count();
122 int wxNotebook::GetRowCount() const
127 int wxNotebook::SetSelection(int nPage
)
129 wxASSERT( IS_VALID_PAGE(nPage
) );
131 ChangePage(m_nSelection
, nPage
);
132 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
133 // Boolean enabled = true ;
135 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
139 void wxNotebook::AdvanceSelection(bool bForward
)
141 int nSel
= GetSelection();
142 int nMax
= GetPageCount() - 1;
144 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
146 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
149 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
151 wxASSERT( IS_VALID_PAGE(nPage
) );
157 wxString
wxNotebook::GetPageText(int nPage
) const
159 wxASSERT( IS_VALID_PAGE(nPage
) );
165 int wxNotebook::GetPageImage(int nPage
) const
167 wxASSERT( IS_VALID_PAGE(nPage
) );
173 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
175 wxASSERT( IS_VALID_PAGE(nPage
) );
181 void wxNotebook::SetImageList(wxImageList
* imageList
)
183 m_pImageList
= imageList
;
187 // ----------------------------------------------------------------------------
188 // wxNotebook operations
189 // ----------------------------------------------------------------------------
191 // remove one page from the notebook
192 bool wxNotebook::DeletePage(int nPage
)
194 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
196 // TODO: delete native widget page
198 delete m_aPages
[nPage
];
199 m_aPages
.Remove(nPage
);
204 // remove one page from the notebook, without deleting the window
205 bool wxNotebook::RemovePage(int nPage
)
207 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
209 m_aPages
.Remove(nPage
);
215 bool wxNotebook::DeleteAllPages()
217 // TODO: delete native widget pages
219 int nPageCount
= GetPageCount();
221 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
222 delete m_aPages
[nPage
];
229 // add a page to the notebook
230 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
231 const wxString
& strText
,
235 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
238 // same as AddPage() but does it at given position
239 bool wxNotebook::InsertPage(int nPage
,
240 wxNotebookPage
*pPage
,
241 const wxString
& strText
,
245 wxASSERT( pPage
!= NULL
);
246 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
248 ControlTabInfoRec tie
;
249 Boolean enabled
= true ;
250 if ( nPage
+ 1 > GetControlMaximum( m_macControl
) )
252 SetControlMaximum( m_macControl
, nPage
+ 1 ) ;
256 tie
.iconSuiteID
= 0 ;
257 strcpy( (char*) tie
.name
, strText
) ;
258 c2pstr( (char*) tie
.name
) ;
259 SetControlData( m_macControl
, nPage
+ 1, kControlTabInfoTag
, sizeof( ControlTabInfoRec
) , (char*) &tie
) ;
260 SetControlData( m_macControl
, m_nSelection
+ 1, kControlTabEnabledFlagTag
, sizeof( Boolean
), (Ptr
)&enabled
);
262 // save the pointer to the page
263 m_aPages
.Insert(pPage
, nPage
);
265 // some page must be selected: either this one or the first one if there is
266 // still no selection
268 m_nSelection
= nPage
;
269 else if ( m_nSelection
== -1 )
272 // don't show pages by default (we'll need to adjust their size first)
273 pPage
->Show( FALSE
) ;
278 // ----------------------------------------------------------------------------
279 // wxNotebook callbacks
280 // ----------------------------------------------------------------------------
282 // @@@ OnSize() is used for setting the font when it's called for the first
283 // time because doing it in ::Create() doesn't work (for unknown reasons)
284 void wxNotebook::OnSize(wxSizeEvent
& event
)
286 static bool s_bFirstTime
= TRUE
;
287 if ( s_bFirstTime
) {
288 // TODO: any first-time-size processing.
289 s_bFirstTime
= FALSE
;
292 // TODO: all this may or may not be necessary for your platform
294 // emulate page change (it's esp. important to do it first time because
295 // otherwise our page would stay invisible)
296 int nSel
= m_nSelection
;
300 // fit the notebook page to the tab control's display area
304 unsigned int nCount
= m_aPages
.Count();
305 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
306 wxNotebookPage
*pPage
= m_aPages
[nPage
];
307 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
, w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
308 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
309 // pPage->SetSize(0, 0, w, h);
310 if ( pPage
->GetAutoLayout() )
314 // Processing continues to next OnSize
318 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
320 // is it our tab control?
321 if ( event
.GetEventObject() == this )
322 ChangePage(event
.GetOldSelection(), event
.GetSelection());
324 // we want to give others a chance to process this message as well
328 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
330 // set focus to the currently selected page if any
331 if ( m_nSelection
!= -1 )
332 m_aPages
[m_nSelection
]->SetFocus();
337 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
339 if ( event
.IsWindowChange() ) {
341 AdvanceSelection(event
.GetDirection());
344 // pass to the parent
346 event
.SetCurrentFocus(this);
347 GetParent()->ProcessEvent(event
);
352 // ----------------------------------------------------------------------------
353 // wxNotebook base class virtuals
354 // ----------------------------------------------------------------------------
356 // override these 2 functions to do nothing: everything is done in OnSize
358 void wxNotebook::SetConstraintSizes(bool /* recurse */)
360 // don't set the sizes of the pages - their correct size is not yet known
361 wxControl::SetConstraintSizes(FALSE
);
364 bool wxNotebook::DoPhase(int /* nPhase */)
369 void wxNotebook::Command(wxCommandEvent
& event
)
371 wxFAIL_MSG("wxNotebook::Command not implemented");
374 // ----------------------------------------------------------------------------
375 // wxNotebook helper functions
376 // ----------------------------------------------------------------------------
378 // hide the currently active panel and show the new one
379 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
381 // it's not an error (the message may be generated by the tab control itself)
382 // and it may happen - just do nothing
383 if ( nSel
== nOldSel
)
385 wxNotebookPage
*pPage
= m_aPages
[nSel
];
392 if ( nOldSel
!= -1 ) {
393 m_aPages
[nOldSel
]->Show(FALSE
);
396 wxNotebookPage
*pPage
= m_aPages
[nSel
];
403 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
405 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
406 event
.SetEventObject(this);