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()))
36 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
37 const short kwxMacTabLeftMargin
= 20 ;
38 const short kwxMacTabTopMargin
= 38 ;
39 const short kwxMacTabRightMargin
= 20 ;
40 const short kwxMacTabBottomMargin
= 12 ;
42 const short kwxMacTabLeftMargin
= 16 ;
43 const short kwxMacTabTopMargin
= 30 ;
44 const short kwxMacTabRightMargin
= 16 ;
45 const short kwxMacTabBottomMargin
= 16 ;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #if !USE_SHARED_LIBRARIES
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
56 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
57 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
59 EVT_SIZE(wxNotebook::OnSize
)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
65 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxNotebook construction
74 // ----------------------------------------------------------------------------
76 // common part of all ctors
77 void wxNotebook::Init()
80 m_macHorizontalBorder
= 7;
81 m_macVerticalBorder
= 8;
87 // default for dynamic class
88 wxNotebook::wxNotebook()
93 // the same arguments as for wxControl
94 wxNotebook::wxNotebook(wxWindow
*parent
,
103 Create(parent
, id
, pos
, size
, style
, name
);
107 bool wxNotebook::Create(wxWindow
*parent
,
112 const wxString
& name
)
117 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
119 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
120 kControlTabSmallProc
, (long) this ) ;
122 MacPostControlCreate() ;
127 wxNotebook::~wxNotebook()
129 m_macControl
= NULL
;
132 // ----------------------------------------------------------------------------
133 // wxNotebook accessors
134 // ----------------------------------------------------------------------------
135 int wxNotebook::GetPageCount() const
137 return m_aPages
.Count();
140 int wxNotebook::GetRowCount() const
145 int wxNotebook::SetSelection(int nPage
)
147 wxASSERT( IS_VALID_PAGE(nPage
) );
149 ChangePage(m_nSelection
, nPage
);
150 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
155 void wxNotebook::AdvanceSelection(bool bForward
)
157 if (GetPageCount() == 0) {
160 int nSel
= GetSelection();
161 int nMax
= GetPageCount() - 1;
163 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
165 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
168 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
170 wxASSERT( IS_VALID_PAGE(nPage
) );
172 wxNotebookPage
*page
= m_aPages
[nPage
];
173 page
->SetLabel(strText
);
179 wxString
wxNotebook::GetPageText(int nPage
) const
181 wxASSERT( IS_VALID_PAGE(nPage
) );
183 wxNotebookPage
*page
= m_aPages
[nPage
];
184 return page
->GetLabel();
187 int wxNotebook::GetPageImage(int nPage
) const
189 wxASSERT( IS_VALID_PAGE(nPage
) );
195 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
197 wxASSERT( IS_VALID_PAGE(nPage
) );
203 void wxNotebook::SetImageList(wxImageList
* imageList
)
205 m_pImageList
= imageList
;
209 // ----------------------------------------------------------------------------
210 // wxNotebook operations
211 // ----------------------------------------------------------------------------
213 // remove one page from the notebook
214 bool wxNotebook::DeletePage(int nPage
)
216 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
218 delete m_aPages
[nPage
];
219 m_aPages
.Remove(nPage
);
223 if(m_nSelection
>= GetPageCount()) {
224 m_nSelection
= GetPageCount() - 1;
226 if(m_nSelection
>= 0) {
227 m_aPages
[m_nSelection
]->Show(true);
233 // remove one page from the notebook, without deleting the window
234 bool wxNotebook::RemovePage(int nPage
)
236 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
238 m_aPages
.Remove(nPage
);
244 bool wxNotebook::DeleteAllPages()
246 // TODO: delete native widget pages
248 int nPageCount
= GetPageCount();
250 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
251 delete m_aPages
[nPage
];
260 // add a page to the notebook
261 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
262 const wxString
& strText
,
266 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
269 // same as AddPage() but does it at given position
270 bool wxNotebook::InsertPage(int nPage
,
271 wxNotebookPage
*pPage
,
272 const wxString
& strText
,
276 wxASSERT( pPage
!= NULL
);
277 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
279 pPage
->SetLabel(strText
);
281 // save the pointer to the page
282 m_aPages
.Insert(pPage
, nPage
);
287 m_nSelection
= nPage
;
289 else if ( m_nSelection
== -1 ) {
292 else if (m_nSelection
>= nPage
) {
295 // don't show pages by default (we'll need to adjust their size first)
296 pPage
->Show( false ) ;
300 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
301 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
302 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
303 if ( pPage
->GetAutoLayout() ) {
310 /* Added by Mark Newsam
311 * When a page is added or deleted to the notebook this function updates
312 * information held in the m_macControl so that it matches the order
313 * the user would expect.
315 void wxNotebook::MacSetupTabs()
317 SetControlMaximum( m_macControl
, GetPageCount() ) ;
319 wxNotebookPage
*page
;
320 ControlTabInfoRec info
;
321 Boolean enabled
= true;
322 for(int ii
= 0; ii
< GetPageCount(); ii
++)
326 info
.iconSuiteID
= 0;
328 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
330 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
331 c2pstr( (char *) info
.name
) ;
333 SetControlData( m_macControl
, ii
+1, kControlTabInfoTag
,
334 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
335 SetControlData( m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
336 sizeof( Boolean
), (Ptr
)&enabled
);
339 GetControlBounds(m_macControl
, &bounds
);
340 InvalWindowRect(GetMacRootWindow(), &bounds
);
343 // ----------------------------------------------------------------------------
344 // wxNotebook callbacks
345 // ----------------------------------------------------------------------------
347 // @@@ OnSize() is used for setting the font when it's called for the first
348 // time because doing it in ::Create() doesn't work (for unknown reasons)
349 void wxNotebook::OnSize(wxSizeEvent
& event
)
351 static bool s_bFirstTime
= TRUE
;
352 if ( s_bFirstTime
) {
353 // TODO: any first-time-size processing.
354 s_bFirstTime
= FALSE
;
357 // TODO: all this may or may not be necessary for your platform
359 // emulate page change (it's esp. important to do it first time because
360 // otherwise our page would stay invisible)
361 int nSel
= m_nSelection
;
365 // fit the notebook page to the tab control's display area
369 unsigned int nCount
= m_aPages
.Count();
370 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
371 wxNotebookPage
*pPage
= m_aPages
[nPage
];
372 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
373 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
374 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
375 if ( pPage
->GetAutoLayout() ) {
380 // Processing continues to next OnSize
384 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
386 // is it our tab control?
387 if ( event
.GetEventObject() == this )
388 ChangePage(event
.GetOldSelection(), event
.GetSelection());
390 // we want to give others a chance to process this message as well
394 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
396 // set focus to the currently selected page if any
397 if ( m_nSelection
!= -1 )
398 m_aPages
[m_nSelection
]->SetFocus();
403 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
405 if ( event
.IsWindowChange() ) {
407 AdvanceSelection(event
.GetDirection());
410 // pass to the parent
412 event
.SetCurrentFocus(this);
413 GetParent()->ProcessEvent(event
);
418 // ----------------------------------------------------------------------------
419 // wxNotebook base class virtuals
420 // ----------------------------------------------------------------------------
422 // override these 2 functions to do nothing: everything is done in OnSize
424 void wxNotebook::SetConstraintSizes(bool /* recurse */)
426 // don't set the sizes of the pages - their correct size is not yet known
427 wxControl::SetConstraintSizes(FALSE
);
430 bool wxNotebook::DoPhase(int /* nPhase */)
435 void wxNotebook::Command(wxCommandEvent
& event
)
437 wxFAIL_MSG("wxNotebook::Command not implemented");
440 // ----------------------------------------------------------------------------
441 // wxNotebook helper functions
442 // ----------------------------------------------------------------------------
444 // hide the currently active panel and show the new one
445 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
447 // it's not an error (the message may be generated by the tab control itself)
448 // and it may happen - just do nothing
449 if ( nSel
== nOldSel
)
451 wxNotebookPage
*pPage
= m_aPages
[nSel
];
458 // Hide previous page
459 if ( nOldSel
!= -1 ) {
460 m_aPages
[nOldSel
]->Show(FALSE
);
463 wxNotebookPage
*pPage
= m_aPages
[nSel
];
470 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
472 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
473 event
.SetEventObject(this);