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