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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
48 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
49 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
51 EVT_SIZE(wxNotebook::OnSize
)
52 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
53 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
56 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
57 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxNotebook construction
66 // ----------------------------------------------------------------------------
68 // common part of all ctors
69 void wxNotebook::Init()
75 // default for dynamic class
76 wxNotebook::wxNotebook()
81 // the same arguments as for wxControl
82 wxNotebook::wxNotebook(wxWindow
*parent
,
91 Create(parent
, id
, pos
, size
, style
, name
);
95 bool wxNotebook::Create(wxWindow
*parent
,
100 const wxString
& name
)
105 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
107 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
108 kControlTabSmallProc
, (long) this ) ;
110 MacPostControlCreate() ;
115 wxNotebook::~wxNotebook()
117 m_macControl
= NULL
;
120 // ----------------------------------------------------------------------------
121 // wxNotebook accessors
122 // ----------------------------------------------------------------------------
123 int wxNotebook::GetPageCount() const
125 return m_aPages
.Count();
128 int wxNotebook::GetRowCount() const
133 int wxNotebook::SetSelection(int nPage
)
135 wxASSERT( IS_VALID_PAGE(nPage
) );
137 ChangePage(m_nSelection
, nPage
);
138 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
139 // Boolean enabled = true ;
141 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
145 void wxNotebook::AdvanceSelection(bool bForward
)
147 int nSel
= GetSelection();
148 int nMax
= GetPageCount() - 1;
150 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
152 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
155 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
157 wxASSERT( IS_VALID_PAGE(nPage
) );
163 wxString
wxNotebook::GetPageText(int nPage
) const
165 wxASSERT( IS_VALID_PAGE(nPage
) );
171 int wxNotebook::GetPageImage(int nPage
) const
173 wxASSERT( IS_VALID_PAGE(nPage
) );
179 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
181 wxASSERT( IS_VALID_PAGE(nPage
) );
187 void wxNotebook::SetImageList(wxImageList
* imageList
)
189 m_pImageList
= imageList
;
193 // ----------------------------------------------------------------------------
194 // wxNotebook operations
195 // ----------------------------------------------------------------------------
197 // remove one page from the notebook
198 bool wxNotebook::DeletePage(int nPage
)
200 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
202 // TODO: delete native widget page
204 delete m_aPages
[nPage
];
205 m_aPages
.Remove(nPage
);
210 // remove one page from the notebook, without deleting the window
211 bool wxNotebook::RemovePage(int nPage
)
213 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
215 m_aPages
.Remove(nPage
);
221 bool wxNotebook::DeleteAllPages()
223 // TODO: delete native widget pages
225 int nPageCount
= GetPageCount();
227 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
228 delete m_aPages
[nPage
];
235 // add a page to the notebook
236 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
237 const wxString
& strText
,
241 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
244 // same as AddPage() but does it at given position
245 bool wxNotebook::InsertPage(int nPage
,
246 wxNotebookPage
*pPage
,
247 const wxString
& strText
,
251 wxASSERT( pPage
!= NULL
);
252 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
254 ControlTabInfoRec tie
;
255 Boolean enabled
= true ;
256 if ( nPage
+ 1 > GetControlMaximum( m_macControl
) )
258 SetControlMaximum( m_macControl
, nPage
+ 1 ) ;
262 tie
.iconSuiteID
= 0 ;
264 c2pstrcpy( (StringPtr
) tie
.name
, strText
) ;
266 strcpy( (char *) tie
.name
, strText
) ;
267 c2pstr( (char *) tie
.name
) ;
269 SetControlData( m_macControl
, nPage
+ 1, kControlTabInfoTag
, sizeof( ControlTabInfoRec
) , (char*) &tie
) ;
270 SetControlData( m_macControl
, m_nSelection
+ 1, kControlTabEnabledFlagTag
, sizeof( Boolean
), (Ptr
)&enabled
);
272 // save the pointer to the page
273 m_aPages
.Insert(pPage
, nPage
);
275 // some page must be selected: either this one or the first one if there is
276 // still no selection
278 m_nSelection
= nPage
;
279 else if ( m_nSelection
== -1 )
282 // don't show pages by default (we'll need to adjust their size first)
283 pPage
->Show( FALSE
) ;
288 // ----------------------------------------------------------------------------
289 // wxNotebook callbacks
290 // ----------------------------------------------------------------------------
292 // @@@ OnSize() is used for setting the font when it's called for the first
293 // time because doing it in ::Create() doesn't work (for unknown reasons)
294 void wxNotebook::OnSize(wxSizeEvent
& event
)
296 static bool s_bFirstTime
= TRUE
;
297 if ( s_bFirstTime
) {
298 // TODO: any first-time-size processing.
299 s_bFirstTime
= FALSE
;
302 // TODO: all this may or may not be necessary for your platform
304 // emulate page change (it's esp. important to do it first time because
305 // otherwise our page would stay invisible)
306 int nSel
= m_nSelection
;
310 // fit the notebook page to the tab control's display area
314 unsigned int nCount
= m_aPages
.Count();
315 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
316 wxNotebookPage
*pPage
= m_aPages
[nPage
];
317 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
, w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
318 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
319 // pPage->SetSize(0, 0, w, h);
320 if ( pPage
->GetAutoLayout() )
324 // Processing continues to next OnSize
328 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
330 // is it our tab control?
331 if ( event
.GetEventObject() == this )
332 ChangePage(event
.GetOldSelection(), event
.GetSelection());
334 // we want to give others a chance to process this message as well
338 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
340 // set focus to the currently selected page if any
341 if ( m_nSelection
!= -1 )
342 m_aPages
[m_nSelection
]->SetFocus();
347 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
349 if ( event
.IsWindowChange() ) {
351 AdvanceSelection(event
.GetDirection());
354 // pass to the parent
356 event
.SetCurrentFocus(this);
357 GetParent()->ProcessEvent(event
);
362 // ----------------------------------------------------------------------------
363 // wxNotebook base class virtuals
364 // ----------------------------------------------------------------------------
366 // override these 2 functions to do nothing: everything is done in OnSize
368 void wxNotebook::SetConstraintSizes(bool /* recurse */)
370 // don't set the sizes of the pages - their correct size is not yet known
371 wxControl::SetConstraintSizes(FALSE
);
374 bool wxNotebook::DoPhase(int /* nPhase */)
379 void wxNotebook::Command(wxCommandEvent
& event
)
381 wxFAIL_MSG("wxNotebook::Command not implemented");
384 // ----------------------------------------------------------------------------
385 // wxNotebook helper functions
386 // ----------------------------------------------------------------------------
388 // hide the currently active panel and show the new one
389 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
391 // it's not an error (the message may be generated by the tab control itself)
392 // and it may happen - just do nothing
393 if ( nSel
== nOldSel
)
395 wxNotebookPage
*pPage
= m_aPages
[nSel
];
402 if ( nOldSel
!= -1 ) {
403 m_aPages
[nOldSel
]->Show(FALSE
);
406 wxNotebookPage
*pPage
= m_aPages
[nSel
];
413 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
415 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
416 event
.SetEventObject(this);