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 ;
263 strcpy( (char*) tie
.name
, strText
) ;
264 c2pstr( (char*) tie
.name
) ;
265 SetControlData( m_macControl
, nPage
+ 1, kControlTabInfoTag
, sizeof( ControlTabInfoRec
) , (char*) &tie
) ;
266 SetControlData( m_macControl
, m_nSelection
+ 1, kControlTabEnabledFlagTag
, sizeof( Boolean
), (Ptr
)&enabled
);
268 // save the pointer to the page
269 m_aPages
.Insert(pPage
, nPage
);
271 // some page must be selected: either this one or the first one if there is
272 // still no selection
274 m_nSelection
= nPage
;
275 else if ( m_nSelection
== -1 )
278 // don't show pages by default (we'll need to adjust their size first)
279 pPage
->Show( FALSE
) ;
284 // ----------------------------------------------------------------------------
285 // wxNotebook callbacks
286 // ----------------------------------------------------------------------------
288 // @@@ OnSize() is used for setting the font when it's called for the first
289 // time because doing it in ::Create() doesn't work (for unknown reasons)
290 void wxNotebook::OnSize(wxSizeEvent
& event
)
292 static bool s_bFirstTime
= TRUE
;
293 if ( s_bFirstTime
) {
294 // TODO: any first-time-size processing.
295 s_bFirstTime
= FALSE
;
298 // TODO: all this may or may not be necessary for your platform
300 // emulate page change (it's esp. important to do it first time because
301 // otherwise our page would stay invisible)
302 int nSel
= m_nSelection
;
306 // fit the notebook page to the tab control's display area
310 unsigned int nCount
= m_aPages
.Count();
311 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
312 wxNotebookPage
*pPage
= m_aPages
[nPage
];
313 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
, w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
314 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
315 // pPage->SetSize(0, 0, w, h);
316 if ( pPage
->GetAutoLayout() )
320 // Processing continues to next OnSize
324 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
326 // is it our tab control?
327 if ( event
.GetEventObject() == this )
328 ChangePage(event
.GetOldSelection(), event
.GetSelection());
330 // we want to give others a chance to process this message as well
334 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
336 // set focus to the currently selected page if any
337 if ( m_nSelection
!= -1 )
338 m_aPages
[m_nSelection
]->SetFocus();
343 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
345 if ( event
.IsWindowChange() ) {
347 AdvanceSelection(event
.GetDirection());
350 // pass to the parent
352 event
.SetCurrentFocus(this);
353 GetParent()->ProcessEvent(event
);
358 // ----------------------------------------------------------------------------
359 // wxNotebook base class virtuals
360 // ----------------------------------------------------------------------------
362 // override these 2 functions to do nothing: everything is done in OnSize
364 void wxNotebook::SetConstraintSizes(bool /* recurse */)
366 // don't set the sizes of the pages - their correct size is not yet known
367 wxControl::SetConstraintSizes(FALSE
);
370 bool wxNotebook::DoPhase(int /* nPhase */)
375 void wxNotebook::Command(wxCommandEvent
& event
)
377 wxFAIL_MSG("wxNotebook::Command not implemented");
380 // ----------------------------------------------------------------------------
381 // wxNotebook helper functions
382 // ----------------------------------------------------------------------------
384 // hide the currently active panel and show the new one
385 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
387 // it's not an error (the message may be generated by the tab control itself)
388 // and it may happen - just do nothing
389 if ( nSel
== nOldSel
)
391 wxNotebookPage
*pPage
= m_aPages
[nSel
];
398 if ( nOldSel
!= -1 ) {
399 m_aPages
[nOldSel
]->Show(FALSE
);
402 wxNotebookPage
*pPage
= m_aPages
[nSel
];
409 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
411 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
412 event
.SetEventObject(this);