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
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
104 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
105 kControlTabSmallProc
, (long) this ) ;
107 MacPostControlCreate() ;
112 wxNotebook::~wxNotebook()
116 // ----------------------------------------------------------------------------
117 // wxNotebook accessors
118 // ----------------------------------------------------------------------------
119 int wxNotebook::GetPageCount() const
121 return m_aPages
.Count();
124 int wxNotebook::GetRowCount() const
129 int wxNotebook::SetSelection(int nPage
)
131 wxASSERT( IS_VALID_PAGE(nPage
) );
133 ChangePage(m_nSelection
, nPage
);
134 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
135 // Boolean enabled = true ;
137 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
141 void wxNotebook::AdvanceSelection(bool bForward
)
143 int nSel
= GetSelection();
144 int nMax
= GetPageCount() - 1;
146 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
148 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
151 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
153 wxASSERT( IS_VALID_PAGE(nPage
) );
159 wxString
wxNotebook::GetPageText(int nPage
) const
161 wxASSERT( IS_VALID_PAGE(nPage
) );
167 int wxNotebook::GetPageImage(int nPage
) const
169 wxASSERT( IS_VALID_PAGE(nPage
) );
175 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
177 wxASSERT( IS_VALID_PAGE(nPage
) );
183 void wxNotebook::SetImageList(wxImageList
* imageList
)
185 m_pImageList
= imageList
;
189 // ----------------------------------------------------------------------------
190 // wxNotebook operations
191 // ----------------------------------------------------------------------------
193 // remove one page from the notebook
194 bool wxNotebook::DeletePage(int nPage
)
196 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
198 // TODO: delete native widget page
200 delete m_aPages
[nPage
];
201 m_aPages
.Remove(nPage
);
206 // remove one page from the notebook, without deleting the window
207 bool wxNotebook::RemovePage(int nPage
)
209 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
211 m_aPages
.Remove(nPage
);
217 bool wxNotebook::DeleteAllPages()
219 // TODO: delete native widget pages
221 int nPageCount
= GetPageCount();
223 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
224 delete m_aPages
[nPage
];
231 // add a page to the notebook
232 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
233 const wxString
& strText
,
237 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
240 // same as AddPage() but does it at given position
241 bool wxNotebook::InsertPage(int nPage
,
242 wxNotebookPage
*pPage
,
243 const wxString
& strText
,
247 wxASSERT( pPage
!= NULL
);
248 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
250 ControlTabInfoRec tie
;
251 Boolean enabled
= true ;
252 if ( nPage
+ 1 > GetControlMaximum( m_macControl
) )
254 SetControlMaximum( m_macControl
, nPage
+ 1 ) ;
258 tie
.iconSuiteID
= 0 ;
259 strcpy( (char*) tie
.name
, strText
) ;
260 c2pstr( (char*) tie
.name
) ;
261 SetControlData( m_macControl
, nPage
+ 1, kControlTabInfoTag
, sizeof( ControlTabInfoRec
) , (char*) &tie
) ;
262 SetControlData( m_macControl
, m_nSelection
+ 1, kControlTabEnabledFlagTag
, sizeof( Boolean
), (Ptr
)&enabled
);
264 // save the pointer to the page
265 m_aPages
.Insert(pPage
, nPage
);
267 // some page must be selected: either this one or the first one if there is
268 // still no selection
270 m_nSelection
= nPage
;
271 else if ( m_nSelection
== -1 )
274 // don't show pages by default (we'll need to adjust their size first)
275 pPage
->Show( FALSE
) ;
280 // ----------------------------------------------------------------------------
281 // wxNotebook callbacks
282 // ----------------------------------------------------------------------------
284 // @@@ OnSize() is used for setting the font when it's called for the first
285 // time because doing it in ::Create() doesn't work (for unknown reasons)
286 void wxNotebook::OnSize(wxSizeEvent
& event
)
288 static bool s_bFirstTime
= TRUE
;
289 if ( s_bFirstTime
) {
290 // TODO: any first-time-size processing.
291 s_bFirstTime
= FALSE
;
294 // TODO: all this may or may not be necessary for your platform
296 // emulate page change (it's esp. important to do it first time because
297 // otherwise our page would stay invisible)
298 int nSel
= m_nSelection
;
302 // fit the notebook page to the tab control's display area
306 unsigned int nCount
= m_aPages
.Count();
307 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
308 wxNotebookPage
*pPage
= m_aPages
[nPage
];
309 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
, w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
310 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
311 // pPage->SetSize(0, 0, w, h);
312 if ( pPage
->GetAutoLayout() )
316 // Processing continues to next OnSize
320 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
322 // is it our tab control?
323 if ( event
.GetEventObject() == this )
324 ChangePage(event
.GetOldSelection(), event
.GetSelection());
326 // we want to give others a chance to process this message as well
330 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
332 // set focus to the currently selected page if any
333 if ( m_nSelection
!= -1 )
334 m_aPages
[m_nSelection
]->SetFocus();
339 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
341 if ( event
.IsWindowChange() ) {
343 AdvanceSelection(event
.GetDirection());
346 // pass to the parent
348 event
.SetCurrentFocus(this);
349 GetParent()->ProcessEvent(event
);
354 // ----------------------------------------------------------------------------
355 // wxNotebook base class virtuals
356 // ----------------------------------------------------------------------------
358 // override these 2 functions to do nothing: everything is done in OnSize
360 void wxNotebook::SetConstraintSizes(bool /* recurse */)
362 // don't set the sizes of the pages - their correct size is not yet known
363 wxControl::SetConstraintSizes(FALSE
);
366 bool wxNotebook::DoPhase(int /* nPhase */)
371 void wxNotebook::Command(wxCommandEvent
& event
)
373 wxFAIL_MSG("wxNotebook::Command not implemented");
376 // ----------------------------------------------------------------------------
377 // wxNotebook helper functions
378 // ----------------------------------------------------------------------------
380 // hide the currently active panel and show the new one
381 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
383 // it's not an error (the message may be generated by the tab control itself)
384 // and it may happen - just do nothing
385 if ( nSel
== nOldSel
)
387 wxNotebookPage
*pPage
= m_aPages
[nSel
];
394 if ( nOldSel
!= -1 ) {
395 m_aPages
[nOldSel
]->Show(FALSE
);
398 wxNotebookPage
*pPage
= m_aPages
[nSel
];
405 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
407 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
408 event
.SetEventObject(this);