]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
fixed crash introduced in v1.41
[wxWidgets.git] / src / mac / carbon / notebmac.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/notebmac.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
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/app.h"
17 #include "wx/string.h"
18 #include "wx/log.h"
19 #include "wx/imaglist.h"
20 #include "wx/image.h"
21 #include "wx/notebook.h"
22 #include "wx/mac/uma.h"
23
24
25 // check that the page index is valid
26 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
27
28
29 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
30 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
31
32 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
33 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
34
35 EVT_SIZE(wxNotebook::OnSize)
36 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
37 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
38 END_EVENT_TABLE()
39
40 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
41 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
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 ( !wxNotebookBase::Create( parent, id, pos, size, style, name ) )
79 return false;
80
81 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
82
83 if ( bounds.right <= bounds.left )
84 bounds.right = bounds.left + 100;
85 if ( bounds.bottom <= bounds.top )
86 bounds.bottom = bounds.top + 100;
87
88 UInt16 tabstyle = kControlTabDirectionNorth;
89 if ( HasFlag(wxBK_LEFT) )
90 tabstyle = kControlTabDirectionWest;
91 else if ( HasFlag( wxBK_RIGHT ) )
92 tabstyle = kControlTabDirectionEast;
93 else if ( HasFlag( wxBK_BOTTOM ) )
94 tabstyle = kControlTabDirectionSouth;
95
96 ControlTabSize tabsize;
97 switch (GetWindowVariant())
98 {
99 case wxWINDOW_VARIANT_MINI:
100 if ( UMAGetSystemVersion() >= 0x1030 )
101 tabsize = 3 ;
102 else
103 tabsize = kControlSizeSmall;
104 break;
105
106 case wxWINDOW_VARIANT_SMALL:
107 tabsize = kControlTabSizeSmall;
108 break;
109
110 default:
111 tabsize = kControlTabSizeLarge;
112 break;
113 }
114
115 m_peer = new wxMacControl( this );
116 OSStatus err = CreateTabsControl(
117 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
118 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
119 verify_noerr( err );
120
121 MacPostControlCreate( pos, size );
122
123 return true ;
124 }
125
126 // dtor
127 wxNotebook::~wxNotebook()
128 {
129 }
130
131 // ----------------------------------------------------------------------------
132 // wxNotebook accessors
133 // ----------------------------------------------------------------------------
134
135 void wxNotebook::SetPadding(const wxSize& padding)
136 {
137 // unsupported by OS
138 }
139
140 void wxNotebook::SetTabSize(const wxSize& sz)
141 {
142 // unsupported by OS
143 }
144
145 void wxNotebook::SetPageSize(const wxSize& size)
146 {
147 SetSize( CalcSizeFromPage( size ) );
148 }
149
150 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
151 {
152 return DoGetSizeFromClientSize( sizePage );
153 }
154
155 int wxNotebook::SetSelection(size_t nPage)
156 {
157 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("SetSelection: invalid notebook page") );
158
159 if ( int(nPage) != m_nSelection )
160 {
161 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
162 event.SetSelection(nPage);
163 event.SetOldSelection(m_nSelection);
164 event.SetEventObject(this);
165 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
166 {
167 // program allows the page change
168 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
169 (void)GetEventHandler()->ProcessEvent(event);
170
171 ChangePage(m_nSelection, nPage);
172 }
173 }
174
175 return m_nSelection;
176 }
177
178 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
179 {
180 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") );
181
182 wxNotebookPage *page = m_pages[nPage];
183 page->SetLabel(strText);
184 MacSetupTabs();
185
186 return true;
187 }
188
189 wxString wxNotebook::GetPageText(size_t nPage) const
190 {
191 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") );
192
193 wxNotebookPage *page = m_pages[nPage];
194
195 return page->GetLabel();
196 }
197
198 int wxNotebook::GetPageImage(size_t nPage) const
199 {
200 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") );
201
202 return m_images[nPage];
203 }
204
205 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
206 {
207 wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
208 wxT("SetPageImage: invalid notebook page") );
209 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
210 wxT("SetPageImage: invalid image index") );
211
212 if ( nImage != m_images[nPage] )
213 {
214 // if the item didn't have an icon before or, on the contrary, did have
215 // it but has lost it now, its size will change - but if the icon just
216 // changes, it won't
217 m_images[nPage] = nImage;
218
219 MacSetupTabs() ;
220 }
221
222 return true;
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxNotebook operations
227 // ----------------------------------------------------------------------------
228
229 // remove one page from the notebook, without deleting the window
230 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
231 {
232 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL,
233 wxT("DoRemovePage: invalid notebook page") );
234
235 wxNotebookPage* page = m_pages[nPage] ;
236 m_pages.RemoveAt(nPage);
237
238 MacSetupTabs();
239
240 if (m_nSelection >= (int)GetPageCount())
241 m_nSelection = GetPageCount() - 1;
242
243 if (m_nSelection >= 0)
244 m_pages[m_nSelection]->Show(true);
245
246 InvalidateBestSize();
247
248 return page;
249 }
250
251 // remove all pages
252 bool wxNotebook::DeleteAllPages()
253 {
254 WX_CLEAR_ARRAY(m_pages) ;
255 MacSetupTabs();
256 m_nSelection = -1 ;
257 InvalidateBestSize();
258
259 return true;
260 }
261
262 // same as AddPage() but does it at given position
263 bool wxNotebook::InsertPage(size_t nPage,
264 wxNotebookPage *pPage,
265 const wxString& strText,
266 bool bSelect,
267 int imageId )
268 {
269 if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) )
270 return false;
271
272 wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") );
273
274 // don't show pages by default (we'll need to adjust their size first)
275 pPage->Show( false ) ;
276
277 pPage->SetLabel( strText );
278
279 m_images.Insert( imageId, nPage );
280
281 MacSetupTabs();
282
283 wxRect rect = GetPageRect() ;
284 pPage->SetSize( rect );
285 if ( pPage->GetAutoLayout() )
286 pPage->Layout();
287
288 // now deal with the selection
289 // ---------------------------
290
291 // if the inserted page is before the selected one, we must update the
292 // index of the selected page
293
294 if ( int(nPage) <= m_nSelection )
295 {
296 m_nSelection++;
297
298 // while this still is the same page showing, we need to update the tabs
299 m_peer->SetValue( m_nSelection + 1 ) ;
300 }
301
302 // some page should be selected: either this one or the first one if there
303 // is still no selection
304 int selNew = -1;
305 if ( bSelect )
306 selNew = nPage;
307 else if ( m_nSelection == -1 )
308 selNew = 0;
309
310 if ( selNew != -1 )
311 SetSelection( selNew );
312
313 InvalidateBestSize();
314
315 return true;
316 }
317
318 int wxNotebook::HitTest(const wxPoint& pt, long * flags) const
319 {
320 int resultV = wxNOT_FOUND;
321
322 #if TARGET_API_MAC_OSX
323 const int countPages = GetPageCount();
324
325 HIPoint hipoint = { pt.x , pt.y } ;
326 HIViewPartCode outPart = 0 ;
327 OSStatus err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
328
329 int max = m_peer->GetMaximum() ;
330 if ( outPart == 0 && max > 0 )
331 {
332 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
333 // so we have to go some extra miles to make sure we select something different
334 // and try again ..
335 int val = m_peer->GetValue() ;
336 int maxval = max ;
337 if ( max == 1 )
338 {
339 m_peer->SetMaximum( 2 ) ;
340 maxval = 2 ;
341 }
342
343 if ( val == 1 )
344 m_peer->SetValue( maxval ) ;
345 else
346 m_peer->SetValue( 1 ) ;
347
348 err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
349
350 m_peer->SetValue( val ) ;
351 if ( max == 1 )
352 m_peer->SetMaximum( 1 ) ;
353 }
354
355 if ( outPart >= 1 && outPart <= countPages )
356 resultV = outPart ;
357 #endif // TARGET_API_MAC_OSX
358
359 if (flags != NULL)
360 {
361 *flags = 0;
362
363 // we cannot differentiate better
364 if (resultV >= 1)
365 *flags |= wxNB_HITTEST_ONLABEL;
366 else
367 *flags |= wxNB_HITTEST_NOWHERE;
368 }
369
370 return resultV;
371 }
372
373 // Added by Mark Newsam
374 // When a page is added or deleted to the notebook this function updates
375 // information held in the control so that it matches the order
376 // the user would expect.
377 //
378 void wxNotebook::MacSetupTabs()
379 {
380 m_peer->SetMaximum( GetPageCount() ) ;
381
382 wxNotebookPage *page;
383 ControlTabInfoRecV1 info;
384
385 const size_t countPages = GetPageCount();
386 for (size_t ii = 0; ii < countPages; ii++)
387 {
388 page = m_pages[ii];
389 info.version = kControlTabInfoVersionOne;
390 info.iconSuiteID = 0;
391 wxMacCFStringHolder cflabel( page->GetLabel(), m_font.GetEncoding() ) ;
392 info.name = cflabel ;
393 m_peer->SetData<ControlTabInfoRecV1>( ii + 1, kControlTabInfoTag, &info ) ;
394
395 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
396 {
397 const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage( ii ) ) ;
398 if ( bmap.Ok() )
399 {
400 ControlButtonContentInfo info ;
401
402 wxMacCreateBitmapButton( &info, bmap ) ;
403
404 OSStatus err = m_peer->SetData<ControlButtonContentInfo>( ii + 1, kControlTabImageContentTag, &info );
405 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
406
407 wxMacReleaseBitmapButton( &info ) ;
408 }
409 }
410
411 m_peer->SetTabEnabled( ii + 1, true ) ;
412 }
413
414 Rect bounds;
415 m_peer->GetRectInWindowCoords( &bounds ) ;
416 InvalWindowRect( (WindowRef)MacGetTopLevelWindowRef(), &bounds );
417 }
418
419 wxRect wxNotebook::GetPageRect() const
420 {
421 wxSize size = GetClientSize() ;
422
423 return wxRect( 0 , 0 , size.x , size.y ) ;
424 }
425
426 // ----------------------------------------------------------------------------
427 // wxNotebook callbacks
428 // ----------------------------------------------------------------------------
429
430 // @@@ OnSize() is used for setting the font when it's called for the first
431 // time because doing it in ::Create() doesn't work (for unknown reasons)
432 void wxNotebook::OnSize(wxSizeEvent& event)
433 {
434 unsigned int nCount = m_pages.Count();
435 wxRect rect = GetPageRect() ;
436
437 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
438 {
439 wxNotebookPage *pPage = m_pages[nPage];
440 pPage->SetSize(rect);
441 if ( pPage->GetAutoLayout() )
442 pPage->Layout();
443 }
444
445 // Processing continues to next OnSize
446 event.Skip();
447 }
448
449 void wxNotebook::OnSelChange(wxNotebookEvent& event)
450 {
451 // is it our tab control?
452 if ( event.GetEventObject() == this )
453 ChangePage(event.GetOldSelection(), event.GetSelection());
454
455 // we want to give others a chance to process this message as well
456 event.Skip();
457 }
458
459 void wxNotebook::OnSetFocus(wxFocusEvent& event)
460 {
461 // set focus to the currently selected page if any
462 if ( m_nSelection != -1 )
463 m_pages[m_nSelection]->SetFocus();
464
465 event.Skip();
466 }
467
468 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
469 {
470 if ( event.IsWindowChange() )
471 {
472 // change pages
473 AdvanceSelection( event.GetDirection() );
474 }
475 else
476 {
477 // we get this event in 2 cases
478 //
479 // a) one of our pages might have generated it because the user TABbed
480 // out from it in which case we should propagate the event upwards and
481 // our parent will take care of setting the focus to prev/next sibling
482 //
483 // or
484 //
485 // b) the parent panel wants to give the focus to us so that we
486 // forward it to our selected page. We can't deal with this in
487 // OnSetFocus() because we don't know which direction the focus came
488 // from in this case and so can't choose between setting the focus to
489 // first or last panel child
490 wxWindow *parent = GetParent();
491
492 // the cast is here to fix a GCC ICE
493 if ( ((wxWindow*)event.GetEventObject()) == parent )
494 {
495 // no, it doesn't come from child, case (b): forward to a page
496 if ( m_nSelection != -1 )
497 {
498 // so that the page knows that the event comes from it's parent
499 // and is being propagated downwards
500 event.SetEventObject( this );
501
502 wxWindow *page = m_pages[m_nSelection];
503 if ( !page->GetEventHandler()->ProcessEvent( event ) )
504 {
505 page->SetFocus();
506 }
507 //else: page manages focus inside it itself
508 }
509 else
510 {
511 // we have no pages - still have to give focus to _something_
512 SetFocus();
513 }
514 }
515 else
516 {
517 // it comes from our child, case (a), pass to the parent
518 if ( parent )
519 {
520 event.SetCurrentFocus( this );
521 parent->GetEventHandler()->ProcessEvent( event );
522 }
523 }
524 }
525 }
526
527 // ----------------------------------------------------------------------------
528 // wxNotebook base class virtuals
529 // ----------------------------------------------------------------------------
530
531 #if wxUSE_CONSTRAINTS
532
533 // override these 2 functions to do nothing: everything is done in OnSize
534
535 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
536 {
537 // don't set the sizes of the pages - their correct size is not yet known
538 wxControl::SetConstraintSizes( false );
539 }
540
541 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
542 {
543 return true;
544 }
545
546 #endif // wxUSE_CONSTRAINTS
547
548 void wxNotebook::Command(wxCommandEvent& event)
549 {
550 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
551 }
552
553 // ----------------------------------------------------------------------------
554 // wxNotebook helper functions
555 // ----------------------------------------------------------------------------
556
557 // hide the currently active panel and show the new one
558 void wxNotebook::ChangePage(int nOldSel, int nSel)
559 {
560 if (nOldSel == nSel)
561 return;
562
563 if ( nOldSel != -1 )
564 m_pages[nOldSel]->Show( false );
565
566 if ( nSel != -1 )
567 {
568 wxNotebookPage *pPage = m_pages[nSel];
569 pPage->Show( true );
570 pPage->SetFocus();
571 }
572
573 m_nSelection = nSel;
574 m_peer->SetValue( m_nSelection + 1 ) ;
575 }
576
577 wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
578 {
579 OSStatus status = eventNotHandledErr ;
580
581 SInt32 newSel = m_peer->GetValue() - 1 ;
582 if ( newSel != m_nSelection )
583 {
584 wxNotebookEvent changing(
585 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
586 newSel , m_nSelection );
587 changing.SetEventObject( this );
588 GetEventHandler()->ProcessEvent( changing );
589
590 if ( changing.IsAllowed() )
591 {
592 wxNotebookEvent event(
593 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
594 newSel, m_nSelection );
595 event.SetEventObject( this );
596 GetEventHandler()->ProcessEvent( event );
597 }
598 else
599 {
600 m_peer->SetValue( m_nSelection + 1 ) ;
601 }
602
603 status = noErr ;
604 }
605
606 return (wxInt32)status ;
607 }
608
609 #endif