]>
Commit | Line | Data |
---|---|---|
e53b3d16 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/notebook_osx.cpp | |
3 | // Purpose: implementation of wxNotebook | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id: notebmac.cpp 55079 2008-08-13 14:56:42Z PC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_NOTEBOOK | |
15 | ||
16 | #include "wx/notebook.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/string.h" | |
20 | #include "wx/log.h" | |
21 | #include "wx/app.h" | |
22 | #include "wx/image.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/string.h" | |
26 | #include "wx/imaglist.h" | |
27 | #include "wx/osx/private.h" | |
28 | ||
29 | ||
30 | // check that the page index is valid | |
31 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
32 | ||
33 | BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase) | |
34 | EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange) | |
35 | ||
36 | EVT_SIZE(wxNotebook::OnSize) | |
37 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
38 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
39 | END_EVENT_TABLE() | |
40 | ||
41 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase) | |
42 | ||
43 | ||
44 | // common part of all ctors | |
45 | void wxNotebook::Init() | |
46 | { | |
47 | m_nSelection = -1; | |
48 | } | |
49 | ||
50 | // default for dynamic class | |
51 | wxNotebook::wxNotebook() | |
52 | { | |
53 | Init(); | |
54 | } | |
55 | ||
56 | // the same arguments as for wxControl | |
57 | wxNotebook::wxNotebook( wxWindow *parent, | |
58 | wxWindowID id, | |
59 | const wxPoint& pos, | |
60 | const wxSize& size, | |
61 | long style, | |
62 | const wxString& name ) | |
63 | { | |
64 | Init(); | |
65 | ||
66 | Create( parent, id, pos, size, style, name ); | |
67 | } | |
68 | ||
69 | bool wxNotebook::Create( wxWindow *parent, | |
70 | wxWindowID id, | |
71 | const wxPoint& pos, | |
72 | const wxSize& size, | |
73 | long style, | |
74 | const wxString& name ) | |
75 | { | |
76 | m_macIsUserPane = false ; | |
77 | ||
78 | if (! (style & wxBK_ALIGN_MASK)) | |
79 | style |= wxBK_TOP; | |
80 | ||
81 | if ( !wxNotebookBase::Create( parent, id, pos, size, style, name ) ) | |
82 | return false; | |
83 | ||
84 | m_peer = wxWidgetImpl::CreateTabView(this,parent, id, pos, size, style, GetExtraStyle() ); | |
85 | ||
86 | MacPostControlCreate( pos, size ); | |
87 | ||
88 | return true ; | |
89 | } | |
90 | ||
91 | // dtor | |
92 | wxNotebook::~wxNotebook() | |
93 | { | |
94 | } | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // wxNotebook accessors | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding)) | |
101 | { | |
102 | // unsupported by OS | |
103 | } | |
104 | ||
105 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) | |
106 | { | |
107 | // unsupported by OS | |
108 | } | |
109 | ||
110 | void wxNotebook::SetPageSize(const wxSize& size) | |
111 | { | |
112 | SetSize( CalcSizeFromPage( size ) ); | |
113 | } | |
114 | ||
115 | wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const | |
116 | { | |
117 | return DoGetSizeFromClientSize( sizePage ); | |
118 | } | |
119 | ||
120 | int wxNotebook::DoSetSelection(size_t nPage, int flags) | |
121 | { | |
122 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("DoSetSelection: invalid notebook page") ); | |
123 | ||
124 | if ( m_nSelection == wxNOT_FOUND || nPage != (size_t)m_nSelection ) | |
125 | { | |
126 | if ( flags & SetSelection_SendEvent ) | |
127 | { | |
128 | if ( !SendPageChangingEvent(nPage) ) | |
129 | { | |
130 | // vetoed by program | |
131 | return m_nSelection; | |
132 | } | |
133 | //else: program allows the page change | |
134 | ||
135 | SendPageChangedEvent(m_nSelection, nPage); | |
136 | } | |
137 | ||
138 | ChangePage(m_nSelection, nPage); | |
139 | } | |
140 | //else: no change | |
141 | ||
142 | return m_nSelection; | |
143 | } | |
144 | ||
145 | bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) | |
146 | { | |
147 | wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") ); | |
148 | ||
149 | wxNotebookPage *page = m_pages[nPage]; | |
150 | page->SetLabel(wxStripMenuCodes(strText)); | |
151 | MacSetupTabs(); | |
152 | ||
153 | return true; | |
154 | } | |
155 | ||
156 | wxString wxNotebook::GetPageText(size_t nPage) const | |
157 | { | |
158 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") ); | |
159 | ||
160 | wxNotebookPage *page = m_pages[nPage]; | |
161 | ||
162 | return page->GetLabel(); | |
163 | } | |
164 | ||
165 | int wxNotebook::GetPageImage(size_t nPage) const | |
166 | { | |
167 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") ); | |
168 | ||
169 | return m_images[nPage]; | |
170 | } | |
171 | ||
172 | bool wxNotebook::SetPageImage(size_t nPage, int nImage) | |
173 | { | |
174 | wxCHECK_MSG( IS_VALID_PAGE(nPage), false, | |
175 | wxT("SetPageImage: invalid notebook page") ); | |
176 | wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false, | |
177 | wxT("SetPageImage: invalid image index") ); | |
178 | ||
179 | if ( nImage != m_images[nPage] ) | |
180 | { | |
181 | // if the item didn't have an icon before or, on the contrary, did have | |
182 | // it but has lost it now, its size will change - but if the icon just | |
183 | // changes, it won't | |
184 | m_images[nPage] = nImage; | |
185 | ||
186 | MacSetupTabs() ; | |
187 | } | |
188 | ||
189 | return true; | |
190 | } | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // wxNotebook operations | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | // remove one page from the notebook, without deleting the window | |
197 | wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage) | |
198 | { | |
199 | wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL, | |
200 | wxT("DoRemovePage: invalid notebook page") ); | |
201 | ||
202 | wxNotebookPage* page = m_pages[nPage] ; | |
203 | m_pages.RemoveAt(nPage); | |
204 | ||
205 | MacSetupTabs(); | |
206 | ||
207 | if (m_nSelection >= (int)GetPageCount()) | |
208 | m_nSelection = GetPageCount() - 1; | |
209 | ||
210 | if (m_nSelection >= 0) | |
211 | m_pages[m_nSelection]->Show(true); | |
212 | ||
213 | InvalidateBestSize(); | |
214 | ||
215 | return page; | |
216 | } | |
217 | ||
218 | // remove all pages | |
219 | bool wxNotebook::DeleteAllPages() | |
220 | { | |
221 | WX_CLEAR_ARRAY(m_pages) ; | |
222 | MacSetupTabs(); | |
223 | m_nSelection = -1 ; | |
224 | InvalidateBestSize(); | |
225 | ||
226 | return true; | |
227 | } | |
228 | ||
229 | // same as AddPage() but does it at given position | |
230 | bool wxNotebook::InsertPage(size_t nPage, | |
231 | wxNotebookPage *pPage, | |
232 | const wxString& strText, | |
233 | bool bSelect, | |
234 | int imageId ) | |
235 | { | |
236 | if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) ) | |
237 | return false; | |
238 | ||
239 | wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") ); | |
240 | ||
241 | // don't show pages by default (we'll need to adjust their size first) | |
242 | pPage->Show( false ) ; | |
243 | ||
244 | pPage->SetLabel( wxStripMenuCodes(strText) ); | |
245 | ||
246 | m_images.Insert( imageId, nPage ); | |
247 | ||
248 | MacSetupTabs(); | |
249 | ||
250 | wxRect rect = GetPageRect() ; | |
251 | pPage->SetSize( rect ); | |
252 | if ( pPage->GetAutoLayout() ) | |
253 | pPage->Layout(); | |
254 | ||
255 | // now deal with the selection | |
256 | // --------------------------- | |
257 | ||
258 | // if the inserted page is before the selected one, we must update the | |
259 | // index of the selected page | |
260 | ||
261 | if ( int(nPage) <= m_nSelection ) | |
262 | { | |
263 | m_nSelection++; | |
264 | ||
265 | // while this still is the same page showing, we need to update the tabs | |
266 | m_peer->SetValue( m_nSelection + 1 ) ; | |
267 | } | |
268 | ||
269 | // some page should be selected: either this one or the first one if there | |
270 | // is still no selection | |
271 | int selNew = -1; | |
272 | if ( bSelect ) | |
273 | selNew = nPage; | |
274 | else if ( m_nSelection == -1 ) | |
275 | selNew = 0; | |
276 | ||
277 | if ( selNew != -1 ) | |
278 | SetSelection( selNew ); | |
279 | ||
280 | InvalidateBestSize(); | |
281 | ||
282 | return true; | |
283 | } | |
284 | ||
285 | int wxNotebook::HitTest(const wxPoint& pt, long * flags) const | |
286 | { | |
287 | int resultV = wxNOT_FOUND; | |
288 | #if 0 | |
289 | const int countPages = GetPageCount(); | |
290 | ||
291 | // we have to convert from Client to Window relative coordinates | |
292 | wxPoint adjustedPt = pt + GetClientAreaOrigin(); | |
293 | // and now to HIView native ones | |
294 | adjustedPt.x -= MacGetLeftBorderSize() ; | |
295 | adjustedPt.y -= MacGetTopBorderSize() ; | |
296 | ||
297 | HIPoint hipoint= { adjustedPt.x , adjustedPt.y } ; | |
298 | HIViewPartCode outPart = 0 ; | |
299 | OSStatus err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart ); | |
300 | ||
301 | int max = m_peer->GetMaximum() ; | |
302 | if ( outPart == 0 && max > 0 ) | |
303 | { | |
304 | // this is a hack, as unfortunately a hit on an already selected tab returns 0, | |
305 | // so we have to go some extra miles to make sure we select something different | |
306 | // and try again .. | |
307 | int val = m_peer->GetValue() ; | |
308 | int maxval = max ; | |
309 | if ( max == 1 ) | |
310 | { | |
311 | m_peer->SetMaximum( 2 ) ; | |
312 | maxval = 2 ; | |
313 | } | |
314 | ||
315 | if ( val == 1 ) | |
316 | m_peer->SetValue( maxval ) ; | |
317 | else | |
318 | m_peer->SetValue( 1 ) ; | |
319 | ||
320 | err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart ); | |
321 | ||
322 | m_peer->SetValue( val ) ; | |
323 | if ( max == 1 ) | |
324 | m_peer->SetMaximum( 1 ) ; | |
325 | } | |
326 | ||
327 | if ( outPart >= 1 && outPart <= countPages ) | |
328 | resultV = outPart - 1 ; | |
329 | ||
330 | if (flags != NULL) | |
331 | { | |
332 | *flags = 0; | |
333 | ||
334 | // we cannot differentiate better | |
335 | if (resultV >= 0) | |
336 | *flags |= wxBK_HITTEST_ONLABEL; | |
337 | else | |
338 | *flags |= wxBK_HITTEST_NOWHERE; | |
339 | } | |
340 | #endif | |
341 | return resultV; | |
342 | } | |
343 | ||
344 | // Added by Mark Newsam | |
345 | // When a page is added or deleted to the notebook this function updates | |
346 | // information held in the control so that it matches the order | |
347 | // the user would expect. | |
348 | // | |
349 | void wxNotebook::MacSetupTabs() | |
350 | { | |
351 | m_peer->SetupTabs(*this); | |
352 | Refresh(); | |
353 | } | |
354 | ||
355 | wxRect wxNotebook::GetPageRect() const | |
356 | { | |
357 | wxSize size = GetClientSize() ; | |
358 | ||
359 | return wxRect( 0 , 0 , size.x , size.y ) ; | |
360 | } | |
361 | ||
362 | // ---------------------------------------------------------------------------- | |
363 | // wxNotebook callbacks | |
364 | // ---------------------------------------------------------------------------- | |
365 | ||
366 | // @@@ OnSize() is used for setting the font when it's called for the first | |
367 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
368 | void wxNotebook::OnSize(wxSizeEvent& event) | |
369 | { | |
370 | unsigned int nCount = m_pages.Count(); | |
371 | wxRect rect = GetPageRect() ; | |
372 | ||
373 | for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) | |
374 | { | |
375 | wxNotebookPage *pPage = m_pages[nPage]; | |
376 | pPage->SetSize(rect); | |
377 | if ( pPage->GetAutoLayout() ) | |
378 | pPage->Layout(); | |
379 | } | |
380 | ||
381 | // Processing continues to next OnSize | |
382 | event.Skip(); | |
383 | } | |
384 | ||
385 | void wxNotebook::OnSelChange(wxBookCtrlEvent& event) | |
386 | { | |
387 | // is it our tab control? | |
388 | if ( event.GetEventObject() == this ) | |
389 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
390 | ||
391 | // we want to give others a chance to process this message as well | |
392 | event.Skip(); | |
393 | } | |
394 | ||
395 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
396 | { | |
397 | // set focus to the currently selected page if any | |
398 | if ( m_nSelection != -1 ) | |
399 | m_pages[m_nSelection]->SetFocus(); | |
400 | ||
401 | event.Skip(); | |
402 | } | |
403 | ||
404 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
405 | { | |
406 | if ( event.IsWindowChange() ) | |
407 | { | |
408 | // change pages | |
409 | AdvanceSelection( event.GetDirection() ); | |
410 | } | |
411 | else | |
412 | { | |
413 | // we get this event in 2 cases | |
414 | // | |
415 | // a) one of our pages might have generated it because the user TABbed | |
416 | // out from it in which case we should propagate the event upwards and | |
417 | // our parent will take care of setting the focus to prev/next sibling | |
418 | // | |
419 | // or | |
420 | // | |
421 | // b) the parent panel wants to give the focus to us so that we | |
422 | // forward it to our selected page. We can't deal with this in | |
423 | // OnSetFocus() because we don't know which direction the focus came | |
424 | // from in this case and so can't choose between setting the focus to | |
425 | // first or last panel child | |
426 | wxWindow *parent = GetParent(); | |
427 | ||
428 | // the cast is here to fix a GCC ICE | |
429 | if ( ((wxWindow*)event.GetEventObject()) == parent ) | |
430 | { | |
431 | // no, it doesn't come from child, case (b): forward to a page | |
432 | if ( m_nSelection != -1 ) | |
433 | { | |
434 | // so that the page knows that the event comes from it's parent | |
435 | // and is being propagated downwards | |
436 | event.SetEventObject( this ); | |
437 | ||
438 | wxWindow *page = m_pages[m_nSelection]; | |
439 | if ( !page->HandleWindowEvent( event ) ) | |
440 | { | |
441 | page->SetFocus(); | |
442 | } | |
443 | //else: page manages focus inside it itself | |
444 | } | |
445 | else | |
446 | { | |
447 | // we have no pages - still have to give focus to _something_ | |
448 | SetFocus(); | |
449 | } | |
450 | } | |
451 | else | |
452 | { | |
453 | // it comes from our child, case (a), pass to the parent | |
454 | if ( parent ) | |
455 | { | |
456 | event.SetCurrentFocus( this ); | |
457 | parent->HandleWindowEvent( event ); | |
458 | } | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | // ---------------------------------------------------------------------------- | |
464 | // wxNotebook base class virtuals | |
465 | // ---------------------------------------------------------------------------- | |
466 | ||
467 | #if wxUSE_CONSTRAINTS | |
468 | ||
469 | // override these 2 functions to do nothing: everything is done in OnSize | |
470 | ||
471 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) | |
472 | { | |
473 | // don't set the sizes of the pages - their correct size is not yet known | |
474 | wxControl::SetConstraintSizes( false ); | |
475 | } | |
476 | ||
477 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) | |
478 | { | |
479 | return true; | |
480 | } | |
481 | ||
482 | #endif // wxUSE_CONSTRAINTS | |
483 | ||
484 | void wxNotebook::Command(wxCommandEvent& WXUNUSED(event)) | |
485 | { | |
486 | wxFAIL_MSG(wxT("wxNotebook::Command not implemented")); | |
487 | } | |
488 | ||
489 | // ---------------------------------------------------------------------------- | |
490 | // wxNotebook helper functions | |
491 | // ---------------------------------------------------------------------------- | |
492 | ||
493 | // hide the currently active panel and show the new one | |
494 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
495 | { | |
496 | if (nOldSel == nSel) | |
497 | return; | |
498 | ||
499 | if ( nOldSel != -1 ) | |
500 | m_pages[nOldSel]->Show( false ); | |
501 | ||
502 | if ( nSel != -1 ) | |
503 | { | |
504 | wxNotebookPage *pPage = m_pages[nSel]; | |
505 | pPage->Show( true ); | |
506 | pPage->SetFocus(); | |
507 | } | |
508 | ||
509 | m_nSelection = nSel; | |
510 | m_peer->SetValue( m_nSelection + 1 ) ; | |
511 | } | |
512 | ||
4eb5a0ec | 513 | bool wxNotebook::OSXHandleClicked( double timestampsec ) |
e53b3d16 SC |
514 | { |
515 | bool status = false ; | |
516 | ||
517 | SInt32 newSel = m_peer->GetValue() - 1 ; | |
518 | if ( newSel != m_nSelection ) | |
519 | { | |
520 | wxBookCtrlEvent changing( | |
521 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId, | |
522 | newSel , m_nSelection ); | |
523 | changing.SetEventObject( this ); | |
524 | HandleWindowEvent( changing ); | |
525 | ||
526 | if ( changing.IsAllowed() ) | |
527 | { | |
528 | wxBookCtrlEvent event( | |
529 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId, | |
530 | newSel, m_nSelection ); | |
531 | event.SetEventObject( this ); | |
532 | HandleWindowEvent( event ); | |
533 | } | |
534 | else | |
535 | { | |
536 | m_peer->SetValue( m_nSelection + 1 ) ; | |
537 | } | |
538 | ||
539 | status = true ; | |
540 | } | |
541 | ||
542 | return status ; | |
543 | } | |
544 | ||
545 | #endif |