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 #if !USE_SHARED_LIBRARIES
45 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
46 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
48 EVT_SIZE(wxNotebook::OnSize
)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxNotebook construction
63 // ----------------------------------------------------------------------------
65 // common part of all ctors
66 void wxNotebook::Init()
72 // default for dynamic class
73 wxNotebook::wxNotebook()
78 // the same arguments as for wxControl
79 wxNotebook::wxNotebook(wxWindow
*parent
,
88 Create(parent
, id
, pos
, size
, style
, name
);
92 bool wxNotebook::Create(wxWindow
*parent
,
102 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
104 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
105 kControlTabSmallProc
, (long) this ) ;
107 MacPostControlCreate() ;
112 wxNotebook::~wxNotebook()
114 m_macControl
= NULL
;
117 // ----------------------------------------------------------------------------
118 // wxNotebook accessors
119 // ----------------------------------------------------------------------------
120 int wxNotebook::GetPageCount() const
122 return m_aPages
.Count();
125 int wxNotebook::GetRowCount() const
130 int wxNotebook::SetSelection(int nPage
)
132 wxASSERT( IS_VALID_PAGE(nPage
) );
134 ChangePage(m_nSelection
, nPage
);
135 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
136 // Boolean enabled = true ;
138 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
142 void wxNotebook::AdvanceSelection(bool bForward
)
144 int nSel
= GetSelection();
145 int nMax
= GetPageCount() - 1;
147 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
149 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
152 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
154 wxASSERT( IS_VALID_PAGE(nPage
) );
160 wxString
wxNotebook::GetPageText(int nPage
) const
162 wxASSERT( IS_VALID_PAGE(nPage
) );
168 int wxNotebook::GetPageImage(int nPage
) const
170 wxASSERT( IS_VALID_PAGE(nPage
) );
176 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
178 wxASSERT( IS_VALID_PAGE(nPage
) );
184 void wxNotebook::SetImageList(wxImageList
* imageList
)
186 m_pImageList
= imageList
;
190 // ----------------------------------------------------------------------------
191 // wxNotebook operations
192 // ----------------------------------------------------------------------------
194 // remove one page from the notebook
195 bool wxNotebook::DeletePage(int nPage
)
197 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
199 // TODO: delete native widget page
201 delete m_aPages
[nPage
];
202 m_aPages
.Remove(nPage
);
207 // remove one page from the notebook, without deleting the window
208 bool wxNotebook::RemovePage(int nPage
)
210 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
212 m_aPages
.Remove(nPage
);
218 bool wxNotebook::DeleteAllPages()
220 // TODO: delete native widget pages
222 int nPageCount
= GetPageCount();
224 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
225 delete m_aPages
[nPage
];
232 // add a page to the notebook
233 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
234 const wxString
& strText
,
238 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
241 // same as AddPage() but does it at given position
242 bool wxNotebook::InsertPage(int nPage
,
243 wxNotebookPage
*pPage
,
244 const wxString
& strText
,
248 wxASSERT( pPage
!= NULL
);
249 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
251 ControlTabInfoRec tie
;
252 Boolean enabled
= true ;
253 if ( nPage
+ 1 > GetControlMaximum( m_macControl
) )
255 SetControlMaximum( m_macControl
, nPage
+ 1 ) ;
259 tie
.iconSuiteID
= 0 ;
260 strcpy( (char*) tie
.name
, strText
) ;
261 c2pstr( (char*) tie
.name
) ;
262 SetControlData( m_macControl
, nPage
+ 1, kControlTabInfoTag
, sizeof( ControlTabInfoRec
) , (char*) &tie
) ;
263 SetControlData( m_macControl
, m_nSelection
+ 1, kControlTabEnabledFlagTag
, sizeof( Boolean
), (Ptr
)&enabled
);
265 // save the pointer to the page
266 m_aPages
.Insert(pPage
, nPage
);
268 // some page must be selected: either this one or the first one if there is
269 // still no selection
271 m_nSelection
= nPage
;
272 else if ( m_nSelection
== -1 )
275 // don't show pages by default (we'll need to adjust their size first)
276 pPage
->Show( FALSE
) ;
281 // ----------------------------------------------------------------------------
282 // wxNotebook callbacks
283 // ----------------------------------------------------------------------------
285 // @@@ OnSize() is used for setting the font when it's called for the first
286 // time because doing it in ::Create() doesn't work (for unknown reasons)
287 void wxNotebook::OnSize(wxSizeEvent
& event
)
289 static bool s_bFirstTime
= TRUE
;
290 if ( s_bFirstTime
) {
291 // TODO: any first-time-size processing.
292 s_bFirstTime
= FALSE
;
295 // TODO: all this may or may not be necessary for your platform
297 // emulate page change (it's esp. important to do it first time because
298 // otherwise our page would stay invisible)
299 int nSel
= m_nSelection
;
303 // fit the notebook page to the tab control's display area
307 unsigned int nCount
= m_aPages
.Count();
308 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
309 wxNotebookPage
*pPage
= m_aPages
[nPage
];
310 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
, w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
311 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
312 // pPage->SetSize(0, 0, w, h);
313 if ( pPage
->GetAutoLayout() )
317 // Processing continues to next OnSize
321 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
323 // is it our tab control?
324 if ( event
.GetEventObject() == this )
325 ChangePage(event
.GetOldSelection(), event
.GetSelection());
327 // we want to give others a chance to process this message as well
331 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
333 // set focus to the currently selected page if any
334 if ( m_nSelection
!= -1 )
335 m_aPages
[m_nSelection
]->SetFocus();
340 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
342 if ( event
.IsWindowChange() ) {
344 AdvanceSelection(event
.GetDirection());
347 // pass to the parent
349 event
.SetCurrentFocus(this);
350 GetParent()->ProcessEvent(event
);
355 // ----------------------------------------------------------------------------
356 // wxNotebook base class virtuals
357 // ----------------------------------------------------------------------------
359 // override these 2 functions to do nothing: everything is done in OnSize
361 void wxNotebook::SetConstraintSizes(bool /* recurse */)
363 // don't set the sizes of the pages - their correct size is not yet known
364 wxControl::SetConstraintSizes(FALSE
);
367 bool wxNotebook::DoPhase(int /* nPhase */)
372 void wxNotebook::Command(wxCommandEvent
& event
)
374 wxFAIL_MSG("wxNotebook::Command not implemented");
377 // ----------------------------------------------------------------------------
378 // wxNotebook helper functions
379 // ----------------------------------------------------------------------------
381 // hide the currently active panel and show the new one
382 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
384 // it's not an error (the message may be generated by the tab control itself)
385 // and it may happen - just do nothing
386 if ( nSel
== nOldSel
)
388 wxNotebookPage
*pPage
= m_aPages
[nSel
];
395 if ( nOldSel
!= -1 ) {
396 m_aPages
[nOldSel
]->Show(FALSE
);
399 wxNotebookPage
*pPage
= m_aPages
[nSel
];
406 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
408 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
409 event
.SetEventObject(this);