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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
47 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
48 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
50 EVT_SIZE(wxNotebook::OnSize
)
51 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
52 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
56 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxNotebook construction
64 // ----------------------------------------------------------------------------
66 // common part of all ctors
67 void wxNotebook::Init()
73 // default for dynamic class
74 wxNotebook::wxNotebook()
79 // the same arguments as for wxControl
80 wxNotebook::wxNotebook(wxWindow
*parent
,
89 Create(parent
, id
, pos
, size
, style
, name
);
93 bool wxNotebook::Create(wxWindow
*parent
,
103 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
105 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
106 kControlTabSmallProc
, (long) this ) ;
108 MacPostControlCreate() ;
113 wxNotebook::~wxNotebook()
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 c2pstrcpy( (StringPtr
) tie
.name
, strText
) ;
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);