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;
86 // default for dynamic class
87 wxNotebook::wxNotebook()
92 // the same arguments as for wxControl
93 wxNotebook::wxNotebook(wxWindow
*parent
,
102 Create(parent
, id
, pos
, size
, style
, name
);
106 bool wxNotebook::Create(wxWindow
*parent
,
111 const wxString
& name
)
116 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
118 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
119 kControlTabSmallProc
, (long) this ) ;
121 MacPostControlCreate() ;
126 wxNotebook::~wxNotebook()
128 m_macControl
= NULL
;
131 // ----------------------------------------------------------------------------
132 // wxNotebook accessors
133 // ----------------------------------------------------------------------------
135 void wxNotebook::SetPadding(const wxSize
& padding
)
139 void wxNotebook::SetTabSize(const wxSize
& sz
)
143 void wxNotebook::SetPageSize(const wxSize
& size
)
147 int wxNotebook::SetSelection(int nPage
)
149 wxASSERT( IS_VALID_PAGE(nPage
) );
151 ChangePage(m_nSelection
, nPage
);
152 SetControlValue( m_macControl
, m_nSelection
+ 1 ) ;
157 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
159 wxASSERT( IS_VALID_PAGE(nPage
) );
161 wxNotebookPage
*page
= m_pages
[nPage
];
162 page
->SetLabel(strText
);
168 wxString
wxNotebook::GetPageText(int nPage
) const
170 wxASSERT( IS_VALID_PAGE(nPage
) );
172 wxNotebookPage
*page
= m_pages
[nPage
];
173 return page
->GetLabel();
176 int wxNotebook::GetPageImage(int nPage
) const
178 wxASSERT( IS_VALID_PAGE(nPage
) );
184 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
186 wxASSERT( IS_VALID_PAGE(nPage
) );
192 // ----------------------------------------------------------------------------
193 // wxNotebook operations
194 // ----------------------------------------------------------------------------
196 // remove one page from the notebook, without deleting the window
197 wxNotebookPage
* wxNotebook::DoRemovePage(int nPage
)
199 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
200 wxNotebookPage
* page
= m_pages
[nPage
] ;
201 m_pages
.Remove(nPage
);
205 if(m_nSelection
>= GetPageCount()) {
206 m_nSelection
= GetPageCount() - 1;
208 if(m_nSelection
>= 0) {
209 m_pages
[m_nSelection
]->Show(true);
215 bool wxNotebook::DeleteAllPages()
217 // TODO: delete native widget pages
219 WX_CLEAR_ARRAY(m_pages
) ;
226 // same as AddPage() but does it at given position
227 bool wxNotebook::InsertPage(int nPage
,
228 wxNotebookPage
*pPage
,
229 const wxString
& strText
,
233 wxASSERT( pPage
!= NULL
);
234 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
236 pPage
->SetLabel(strText
);
238 // save the pointer to the page
239 m_pages
.Insert(pPage
, nPage
);
244 m_nSelection
= nPage
;
246 else if ( m_nSelection
== -1 ) {
249 else if (m_nSelection
>= nPage
) {
252 // don't show pages by default (we'll need to adjust their size first)
253 pPage
->Show( false ) ;
257 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
258 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
259 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
260 if ( pPage
->GetAutoLayout() ) {
267 /* Added by Mark Newsam
268 * When a page is added or deleted to the notebook this function updates
269 * information held in the m_macControl so that it matches the order
270 * the user would expect.
272 void wxNotebook::MacSetupTabs()
274 SetControlMaximum( m_macControl
, GetPageCount() ) ;
276 wxNotebookPage
*page
;
277 ControlTabInfoRec info
;
278 Boolean enabled
= true;
279 for(int ii
= 0; ii
< GetPageCount(); ii
++)
283 info
.iconSuiteID
= 0;
285 c2pstrcpy( (StringPtr
) info
.name
, page
->GetLabel() ) ;
287 strcpy( (char *) info
.name
, page
->GetLabel() ) ;
288 c2pstr( (char *) info
.name
) ;
290 SetControlData( m_macControl
, ii
+1, kControlTabInfoTag
,
291 sizeof( ControlTabInfoRec
) , (char*) &info
) ;
292 SetControlData( m_macControl
, ii
+1, kControlTabEnabledFlagTag
,
293 sizeof( Boolean
), (Ptr
)&enabled
);
296 GetControlBounds(m_macControl
, &bounds
);
297 InvalWindowRect(GetMacRootWindow(), &bounds
);
300 // ----------------------------------------------------------------------------
301 // wxNotebook callbacks
302 // ----------------------------------------------------------------------------
304 // @@@ OnSize() is used for setting the font when it's called for the first
305 // time because doing it in ::Create() doesn't work (for unknown reasons)
306 void wxNotebook::OnSize(wxSizeEvent
& event
)
308 static bool s_bFirstTime
= TRUE
;
309 if ( s_bFirstTime
) {
310 // TODO: any first-time-size processing.
311 s_bFirstTime
= FALSE
;
314 // TODO: all this may or may not be necessary for your platform
316 // emulate page change (it's esp. important to do it first time because
317 // otherwise our page would stay invisible)
318 int nSel
= m_nSelection
;
322 // fit the notebook page to the tab control's display area
326 unsigned int nCount
= m_pages
.Count();
327 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
328 wxNotebookPage
*pPage
= m_pages
[nPage
];
329 pPage
->SetSize(kwxMacTabLeftMargin
, kwxMacTabTopMargin
,
330 w
- kwxMacTabLeftMargin
- kwxMacTabRightMargin
,
331 h
- kwxMacTabTopMargin
- kwxMacTabBottomMargin
);
332 if ( pPage
->GetAutoLayout() ) {
337 // Processing continues to next OnSize
341 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
343 // is it our tab control?
344 if ( event
.GetEventObject() == this )
345 ChangePage(event
.GetOldSelection(), event
.GetSelection());
347 // we want to give others a chance to process this message as well
351 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
353 // set focus to the currently selected page if any
354 if ( m_nSelection
!= -1 )
355 m_pages
[m_nSelection
]->SetFocus();
360 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
362 if ( event
.IsWindowChange() ) {
364 AdvanceSelection(event
.GetDirection());
367 // pass to the parent
369 event
.SetCurrentFocus(this);
370 GetParent()->ProcessEvent(event
);
375 // ----------------------------------------------------------------------------
376 // wxNotebook base class virtuals
377 // ----------------------------------------------------------------------------
379 // override these 2 functions to do nothing: everything is done in OnSize
381 void wxNotebook::SetConstraintSizes(bool /* recurse */)
383 // don't set the sizes of the pages - their correct size is not yet known
384 wxControl::SetConstraintSizes(FALSE
);
387 bool wxNotebook::DoPhase(int /* nPhase */)
392 void wxNotebook::Command(wxCommandEvent
& event
)
394 wxFAIL_MSG("wxNotebook::Command not implemented");
397 // ----------------------------------------------------------------------------
398 // wxNotebook helper functions
399 // ----------------------------------------------------------------------------
401 // hide the currently active panel and show the new one
402 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
404 // it's not an error (the message may be generated by the tab control itself)
405 // and it may happen - just do nothing
406 if ( nSel
== nOldSel
)
408 wxNotebookPage
*pPage
= m_pages
[nSel
];
415 // Hide previous page
416 if ( nOldSel
!= -1 ) {
417 m_pages
[nOldSel
]->Show(FALSE
);
420 wxNotebookPage
*pPage
= m_pages
[nSel
];
427 void wxNotebook::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
429 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_windowId
, ::GetControlValue(m_macControl
) - 1, m_nSelection
);
430 event
.SetEventObject(this);