]>
Commit | Line | Data |
---|---|---|
ee6b1d97 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: notebook.cpp | |
3 | // Purpose: implementation of wxNotebook | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #ifdef __GNUG__ | |
3ef585df | 20 | #pragma implementation "notebook.h" |
ee6b1d97 SC |
21 | #endif |
22 | ||
23 | #include <wx/string.h> | |
24 | #include <wx/log.h> | |
25 | #include <wx/imaglist.h> | |
26 | #include <wx/notebook.h> | |
27 | #include <wx/mac/uma.h> | |
28 | // ---------------------------------------------------------------------------- | |
29 | // macros | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | // check that the page index is valid | |
33 | #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) | |
34 | ||
c854c7d9 GD |
35 | #ifdef __WXMAC_X__ |
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 ; | |
41 | #else | |
ee6b1d97 SC |
42 | const short kwxMacTabLeftMargin = 16 ; |
43 | const short kwxMacTabTopMargin = 30 ; | |
44 | const short kwxMacTabRightMargin = 16 ; | |
45 | const short kwxMacTabBottomMargin = 16 ; | |
c854c7d9 | 46 | #endif |
ee6b1d97 SC |
47 | |
48 | // ---------------------------------------------------------------------------- | |
49 | // event table | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | #if !USE_SHARED_LIBRARIES | |
5b781a67 SC |
53 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) |
54 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
55 | ||
ee6b1d97 SC |
56 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
57 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) | |
58 | ||
59 | EVT_SIZE(wxNotebook::OnSize) | |
60 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
61 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
62 | END_EVENT_TABLE() | |
63 | ||
64 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
65 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
66 | #endif | |
67 | ||
68 | // ============================================================================ | |
69 | // implementation | |
70 | // ============================================================================ | |
71 | ||
72 | // ---------------------------------------------------------------------------- | |
73 | // wxNotebook construction | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
76 | // common part of all ctors | |
77 | void wxNotebook::Init() | |
78 | { | |
c854c7d9 GD |
79 | #ifdef __WXMAC_X__ |
80 | m_macHorizontalBorder = 7; | |
81 | m_macVerticalBorder = 8; | |
82 | #endif | |
ee6b1d97 SC |
83 | m_nSelection = -1; |
84 | } | |
85 | ||
86 | // default for dynamic class | |
87 | wxNotebook::wxNotebook() | |
88 | { | |
89 | Init(); | |
90 | } | |
91 | ||
92 | // the same arguments as for wxControl | |
93 | wxNotebook::wxNotebook(wxWindow *parent, | |
94 | wxWindowID id, | |
95 | const wxPoint& pos, | |
96 | const wxSize& size, | |
97 | long style, | |
98 | const wxString& name) | |
99 | { | |
100 | Init(); | |
101 | ||
102 | Create(parent, id, pos, size, style, name); | |
103 | } | |
104 | ||
105 | // Create() function | |
106 | bool wxNotebook::Create(wxWindow *parent, | |
107 | wxWindowID id, | |
108 | const wxPoint& pos, | |
109 | const wxSize& size, | |
110 | long style, | |
111 | const wxString& name) | |
112 | { | |
113 | Rect bounds ; | |
114 | Str255 title ; | |
115 | ||
116 | MacPreControlCreate( parent , id , "" , pos , size ,style, wxDefaultValidator , name , &bounds , title ) ; | |
117 | ||
fdaf613a | 118 | m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , false , 0 , 0 , 1, |
ee6b1d97 SC |
119 | kControlTabSmallProc , (long) this ) ; |
120 | ||
121 | MacPostControlCreate() ; | |
122 | return TRUE ; | |
123 | } | |
124 | ||
125 | // dtor | |
126 | wxNotebook::~wxNotebook() | |
127 | { | |
128 | m_macControl = NULL ; | |
129 | } | |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // wxNotebook accessors | |
133 | // ---------------------------------------------------------------------------- | |
90b959ae SC |
134 | |
135 | void wxNotebook::SetPadding(const wxSize& padding) | |
136 | { | |
fc0daf84 | 137 | wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") ); |
90b959ae SC |
138 | } |
139 | ||
140 | void wxNotebook::SetTabSize(const wxSize& sz) | |
ee6b1d97 | 141 | { |
fc0daf84 | 142 | wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") ); |
ee6b1d97 SC |
143 | } |
144 | ||
90b959ae | 145 | void wxNotebook::SetPageSize(const wxSize& size) |
ee6b1d97 | 146 | { |
fc0daf84 | 147 | wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") ); |
ee6b1d97 SC |
148 | } |
149 | ||
150 | int wxNotebook::SetSelection(int nPage) | |
151 | { | |
152 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
153 | ||
154 | ChangePage(m_nSelection, nPage); | |
c854c7d9 GD |
155 | SetControlValue( m_macControl , m_nSelection + 1 ) ; |
156 | ||
ee6b1d97 SC |
157 | return m_nSelection; |
158 | } | |
159 | ||
ee6b1d97 SC |
160 | bool wxNotebook::SetPageText(int nPage, const wxString& strText) |
161 | { | |
162 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
163 | ||
90b959ae | 164 | wxNotebookPage *page = m_pages[nPage]; |
c854c7d9 GD |
165 | page->SetLabel(strText); |
166 | MacSetupTabs(); | |
167 | ||
168 | return true; | |
ee6b1d97 SC |
169 | } |
170 | ||
171 | wxString wxNotebook::GetPageText(int nPage) const | |
172 | { | |
173 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
174 | ||
90b959ae | 175 | wxNotebookPage *page = m_pages[nPage]; |
c854c7d9 | 176 | return page->GetLabel(); |
ee6b1d97 SC |
177 | } |
178 | ||
179 | int wxNotebook::GetPageImage(int nPage) const | |
180 | { | |
fc0daf84 | 181 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") ); |
ee6b1d97 | 182 | |
fc0daf84 | 183 | return 0 ; |
ee6b1d97 SC |
184 | } |
185 | ||
186 | bool wxNotebook::SetPageImage(int nPage, int nImage) | |
187 | { | |
fc0daf84 SC |
188 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") ); |
189 | ||
190 | wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE, | |
191 | _T("invalid image index in SetPageImage()") ); | |
ee6b1d97 | 192 | |
ee6b1d97 SC |
193 | return FALSE; |
194 | } | |
195 | ||
ee6b1d97 SC |
196 | // ---------------------------------------------------------------------------- |
197 | // wxNotebook operations | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
90b959ae SC |
200 | // remove one page from the notebook, without deleting the window |
201 | wxNotebookPage* wxNotebook::DoRemovePage(int nPage) | |
ee6b1d97 | 202 | { |
90b959ae SC |
203 | wxCHECK( IS_VALID_PAGE(nPage), NULL ); |
204 | wxNotebookPage* page = m_pages[nPage] ; | |
3ef585df | 205 | m_pages.RemoveAt(nPage); |
ee6b1d97 | 206 | |
c854c7d9 GD |
207 | MacSetupTabs(); |
208 | ||
209 | if(m_nSelection >= GetPageCount()) { | |
210 | m_nSelection = GetPageCount() - 1; | |
211 | } | |
212 | if(m_nSelection >= 0) { | |
90b959ae | 213 | m_pages[m_nSelection]->Show(true); |
c854c7d9 | 214 | } |
90b959ae | 215 | return page; |
ee6b1d97 SC |
216 | } |
217 | ||
218 | // remove all pages | |
219 | bool wxNotebook::DeleteAllPages() | |
220 | { | |
221 | // TODO: delete native widget pages | |
222 | ||
90b959ae | 223 | WX_CLEAR_ARRAY(m_pages) ; |
c854c7d9 GD |
224 | MacSetupTabs(); |
225 | ||
ee6b1d97 SC |
226 | return TRUE; |
227 | } | |
228 | ||
ee6b1d97 SC |
229 | |
230 | // same as AddPage() but does it at given position | |
231 | bool wxNotebook::InsertPage(int nPage, | |
232 | wxNotebookPage *pPage, | |
233 | const wxString& strText, | |
234 | bool bSelect, | |
235 | int imageId) | |
236 | { | |
237 | wxASSERT( pPage != NULL ); | |
238 | wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE ); | |
239 | ||
c854c7d9 | 240 | pPage->SetLabel(strText); |
ee6b1d97 SC |
241 | |
242 | // save the pointer to the page | |
90b959ae | 243 | m_pages.Insert(pPage, nPage); |
ee6b1d97 | 244 | |
c854c7d9 GD |
245 | MacSetupTabs(); |
246 | ||
247 | if ( bSelect ) { | |
ee6b1d97 | 248 | m_nSelection = nPage; |
c854c7d9 GD |
249 | } |
250 | else if ( m_nSelection == -1 ) { | |
ee6b1d97 | 251 | m_nSelection = 0; |
c854c7d9 GD |
252 | } |
253 | else if (m_nSelection >= nPage) { | |
254 | m_nSelection++; | |
255 | } | |
256 | // don't show pages by default (we'll need to adjust their size first) | |
257 | pPage->Show( false ) ; | |
ee6b1d97 | 258 | |
c854c7d9 GD |
259 | int h, w; |
260 | GetSize(&w, &h); | |
261 | pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin, | |
262 | w - kwxMacTabLeftMargin - kwxMacTabRightMargin, | |
263 | h - kwxMacTabTopMargin - kwxMacTabBottomMargin ); | |
264 | if ( pPage->GetAutoLayout() ) { | |
265 | pPage->Layout(); | |
266 | } | |
ee6b1d97 | 267 | |
c854c7d9 GD |
268 | return true; |
269 | } | |
270 | ||
271 | /* Added by Mark Newsam | |
272 | * When a page is added or deleted to the notebook this function updates | |
273 | * information held in the m_macControl so that it matches the order | |
274 | * the user would expect. | |
275 | */ | |
276 | void wxNotebook::MacSetupTabs() | |
277 | { | |
278 | SetControlMaximum( m_macControl , GetPageCount() ) ; | |
279 | ||
280 | wxNotebookPage *page; | |
281 | ControlTabInfoRec info; | |
282 | Boolean enabled = true; | |
283 | for(int ii = 0; ii < GetPageCount(); ii++) | |
284 | { | |
90b959ae | 285 | page = m_pages[ii]; |
c854c7d9 GD |
286 | info.version = 0; |
287 | info.iconSuiteID = 0; | |
288 | #if TARGET_CARBON | |
289 | c2pstrcpy( (StringPtr) info.name , page->GetLabel() ) ; | |
290 | #else | |
291 | strcpy( (char *) info.name , page->GetLabel() ) ; | |
292 | c2pstr( (char *) info.name ) ; | |
293 | #endif | |
294 | SetControlData( m_macControl, ii+1, kControlTabInfoTag, | |
295 | sizeof( ControlTabInfoRec) , (char*) &info ) ; | |
296 | SetControlData( m_macControl, ii+1, kControlTabEnabledFlagTag, | |
297 | sizeof( Boolean ), (Ptr)&enabled ); | |
298 | } | |
299 | Rect bounds; | |
300 | GetControlBounds(m_macControl, &bounds); | |
301 | InvalWindowRect(GetMacRootWindow(), &bounds); | |
ee6b1d97 SC |
302 | } |
303 | ||
304 | // ---------------------------------------------------------------------------- | |
305 | // wxNotebook callbacks | |
306 | // ---------------------------------------------------------------------------- | |
307 | ||
308 | // @@@ OnSize() is used for setting the font when it's called for the first | |
309 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
310 | void wxNotebook::OnSize(wxSizeEvent& event) | |
311 | { | |
ee6b1d97 SC |
312 | // emulate page change (it's esp. important to do it first time because |
313 | // otherwise our page would stay invisible) | |
314 | int nSel = m_nSelection; | |
315 | m_nSelection = -1; | |
316 | SetSelection(nSel); | |
317 | ||
318 | // fit the notebook page to the tab control's display area | |
319 | int w, h; | |
320 | GetSize(&w, &h); | |
321 | ||
90b959ae | 322 | unsigned int nCount = m_pages.Count(); |
ee6b1d97 | 323 | for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) { |
90b959ae | 324 | wxNotebookPage *pPage = m_pages[nPage]; |
c854c7d9 GD |
325 | pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin, |
326 | w - kwxMacTabLeftMargin - kwxMacTabRightMargin, | |
327 | h - kwxMacTabTopMargin - kwxMacTabBottomMargin ); | |
328 | if ( pPage->GetAutoLayout() ) { | |
ee6b1d97 | 329 | pPage->Layout(); |
c854c7d9 | 330 | } |
ee6b1d97 SC |
331 | } |
332 | ||
333 | // Processing continues to next OnSize | |
334 | event.Skip(); | |
335 | } | |
336 | ||
337 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
338 | { | |
339 | // is it our tab control? | |
340 | if ( event.GetEventObject() == this ) | |
341 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
342 | ||
343 | // we want to give others a chance to process this message as well | |
344 | event.Skip(); | |
345 | } | |
346 | ||
347 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
348 | { | |
349 | // set focus to the currently selected page if any | |
350 | if ( m_nSelection != -1 ) | |
90b959ae | 351 | m_pages[m_nSelection]->SetFocus(); |
ee6b1d97 SC |
352 | |
353 | event.Skip(); | |
354 | } | |
355 | ||
356 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
357 | { | |
358 | if ( event.IsWindowChange() ) { | |
359 | // change pages | |
360 | AdvanceSelection(event.GetDirection()); | |
361 | } | |
362 | else { | |
363 | // pass to the parent | |
364 | if ( GetParent() ) { | |
365 | event.SetCurrentFocus(this); | |
366 | GetParent()->ProcessEvent(event); | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
371 | // ---------------------------------------------------------------------------- | |
372 | // wxNotebook base class virtuals | |
373 | // ---------------------------------------------------------------------------- | |
374 | ||
375 | // override these 2 functions to do nothing: everything is done in OnSize | |
376 | ||
377 | void wxNotebook::SetConstraintSizes(bool /* recurse */) | |
378 | { | |
379 | // don't set the sizes of the pages - their correct size is not yet known | |
380 | wxControl::SetConstraintSizes(FALSE); | |
381 | } | |
382 | ||
383 | bool wxNotebook::DoPhase(int /* nPhase */) | |
384 | { | |
385 | return TRUE; | |
386 | } | |
387 | ||
388 | void wxNotebook::Command(wxCommandEvent& event) | |
389 | { | |
390 | wxFAIL_MSG("wxNotebook::Command not implemented"); | |
391 | } | |
392 | ||
393 | // ---------------------------------------------------------------------------- | |
394 | // wxNotebook helper functions | |
395 | // ---------------------------------------------------------------------------- | |
396 | ||
397 | // hide the currently active panel and show the new one | |
398 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
399 | { | |
c854c7d9 GD |
400 | // it's not an error (the message may be generated by the tab control itself) |
401 | // and it may happen - just do nothing | |
402 | if ( nSel == nOldSel ) | |
403 | { | |
90b959ae | 404 | wxNotebookPage *pPage = m_pages[nSel]; |
c854c7d9 GD |
405 | pPage->Show(FALSE); |
406 | pPage->Show(TRUE); | |
407 | pPage->SetFocus(); | |
408 | return; | |
409 | } | |
ee6b1d97 | 410 | |
c854c7d9 | 411 | // Hide previous page |
ee6b1d97 | 412 | if ( nOldSel != -1 ) { |
90b959ae | 413 | m_pages[nOldSel]->Show(FALSE); |
ee6b1d97 SC |
414 | } |
415 | ||
90b959ae | 416 | wxNotebookPage *pPage = m_pages[nSel]; |
ee6b1d97 SC |
417 | pPage->Show(TRUE); |
418 | pPage->SetFocus(); | |
419 | ||
420 | m_nSelection = nSel; | |
421 | } | |
422 | ||
423 | void wxNotebook::MacHandleControlClick( ControlHandle control , SInt16 controlpart ) | |
424 | { | |
425 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControlValue(m_macControl) - 1, m_nSelection); | |
426 | event.SetEventObject(this); | |
427 | ||
428 | ProcessEvent(event); | |
429 | } | |
430 |