]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
compilation fix after last commit (apparently patch was misapplied)
[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 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
172 event.SetSelection(nPage);
173 event.SetOldSelection(m_nSelection);
174 event.SetEventObject(this);
175 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
176 {
177 // vetoed by program
178 return m_nSelection;
179 }
180 //else: program allows the page change
181
182 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
183 (void)GetEventHandler()->ProcessEvent(event);
184 }
185
186 ChangePage(m_nSelection, nPage);
187 }
188
189 return m_nSelection;
190 }
191
192 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
193 {
194 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") );
195
196 wxNotebookPage *page = m_pages[nPage];
197 page->SetLabel(strText);
198 MacSetupTabs();
199
200 return true;
201 }
202
203 wxString wxNotebook::GetPageText(size_t nPage) const
204 {
205 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") );
206
207 wxNotebookPage *page = m_pages[nPage];
208
209 return page->GetLabel();
210 }
211
212 int wxNotebook::GetPageImage(size_t nPage) const
213 {
214 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") );
215
216 return m_images[nPage];
217 }
218
219 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
220 {
221 wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
222 wxT("SetPageImage: invalid notebook page") );
223 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
224 wxT("SetPageImage: invalid image index") );
225
226 if ( nImage != m_images[nPage] )
227 {
228 // if the item didn't have an icon before or, on the contrary, did have
229 // it but has lost it now, its size will change - but if the icon just
230 // changes, it won't
231 m_images[nPage] = nImage;
232
233 MacSetupTabs() ;
234 }
235
236 return true;
237 }
238
239 // ----------------------------------------------------------------------------
240 // wxNotebook operations
241 // ----------------------------------------------------------------------------
242
243 // remove one page from the notebook, without deleting the window
244 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
245 {
246 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL,
247 wxT("DoRemovePage: invalid notebook page") );
248
249 wxNotebookPage* page = m_pages[nPage] ;
250 m_pages.RemoveAt(nPage);
251
252 MacSetupTabs();
253
254 if (m_nSelection >= (int)GetPageCount())
255 m_nSelection = GetPageCount() - 1;
256
257 if (m_nSelection >= 0)
258 m_pages[m_nSelection]->Show(true);
259
260 InvalidateBestSize();
261
262 return page;
263 }
264
265 // remove all pages
266 bool wxNotebook::DeleteAllPages()
267 {
268 WX_CLEAR_ARRAY(m_pages) ;
269 MacSetupTabs();
270 m_nSelection = -1 ;
271 InvalidateBestSize();
272
273 return true;
274 }
275
276 // same as AddPage() but does it at given position
277 bool wxNotebook::InsertPage(size_t nPage,
278 wxNotebookPage *pPage,
279 const wxString& strText,
280 bool bSelect,
281 int imageId )
282 {
283 if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) )
284 return false;
285
286 wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") );
287
288 // don't show pages by default (we'll need to adjust their size first)
289 pPage->Show( false ) ;
290
291 pPage->SetLabel( strText );
292
293 m_images.Insert( imageId, nPage );
294
295 MacSetupTabs();
296
297 wxRect rect = GetPageRect() ;
298 pPage->SetSize( rect );
299 if ( pPage->GetAutoLayout() )
300 pPage->Layout();
301
302 // now deal with the selection
303 // ---------------------------
304
305 // if the inserted page is before the selected one, we must update the
306 // index of the selected page
307
308 if ( int(nPage) <= m_nSelection )
309 {
310 m_nSelection++;
311
312 // while this still is the same page showing, we need to update the tabs
313 m_peer->SetValue( m_nSelection + 1 ) ;
314 }
315
316 // some page should be selected: either this one or the first one if there
317 // is still no selection
318 int selNew = -1;
319 if ( bSelect )
320 selNew = nPage;
321 else if ( m_nSelection == -1 )
322 selNew = 0;
323
324 if ( selNew != -1 )
325 SetSelection( selNew );
326
327 InvalidateBestSize();
328
329 return true;
330 }
331
332 int wxNotebook::HitTest(const wxPoint& pt, long * flags) const
333 {
334 int resultV = wxNOT_FOUND;
335
336 #if TARGET_API_MAC_OSX
337 const int countPages = GetPageCount();
338
339 // we have to convert from Client to Window relative coordinates
340 wxPoint adjustedPt = pt + GetClientAreaOrigin();
341 // and now to HIView native ones
342 adjustedPt.x -= MacGetLeftBorderSize() ;
343 adjustedPt.y -= MacGetTopBorderSize() ;
344
345 HIPoint hipoint= { adjustedPt.x , adjustedPt.y } ;
346 HIViewPartCode outPart = 0 ;
347 OSStatus err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
348
349 int max = m_peer->GetMaximum() ;
350 if ( outPart == 0 && max > 0 )
351 {
352 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
353 // so we have to go some extra miles to make sure we select something different
354 // and try again ..
355 int val = m_peer->GetValue() ;
356 int maxval = max ;
357 if ( max == 1 )
358 {
359 m_peer->SetMaximum( 2 ) ;
360 maxval = 2 ;
361 }
362
363 if ( val == 1 )
364 m_peer->SetValue( maxval ) ;
365 else
366 m_peer->SetValue( 1 ) ;
367
368 err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
369
370 m_peer->SetValue( val ) ;
371 if ( max == 1 )
372 m_peer->SetMaximum( 1 ) ;
373 }
374
375 if ( outPart >= 1 && outPart <= countPages )
376 resultV = outPart - 1 ;
377 #endif // TARGET_API_MAC_OSX
378
379 if (flags != NULL)
380 {
381 *flags = 0;
382
383 // we cannot differentiate better
384 if (resultV >= 0)
385 *flags |= wxBK_HITTEST_ONLABEL;
386 else
387 *flags |= wxBK_HITTEST_NOWHERE;
388 }
389
390 return resultV;
391 }
392
393 // Added by Mark Newsam
394 // When a page is added or deleted to the notebook this function updates
395 // information held in the control so that it matches the order
396 // the user would expect.
397 //
398 void wxNotebook::MacSetupTabs()
399 {
400 m_peer->SetMaximum( GetPageCount() ) ;
401
402 wxNotebookPage *page;
403 ControlTabInfoRecV1 info;
404
405 const size_t countPages = GetPageCount();
406 for (size_t ii = 0; ii < countPages; ii++)
407 {
408 page = m_pages[ii];
409 info.version = kControlTabInfoVersionOne;
410 info.iconSuiteID = 0;
411 wxMacCFStringHolder cflabel( page->GetLabel(), m_font.GetEncoding() ) ;
412 info.name = cflabel ;
413 m_peer->SetData<ControlTabInfoRecV1>( ii + 1, kControlTabInfoTag, &info ) ;
414
415 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
416 {
417 const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage( ii ) ) ;
418 if ( bmap.Ok() )
419 {
420 ControlButtonContentInfo info ;
421
422 wxMacCreateBitmapButton( &info, bmap ) ;
423
424 OSStatus err = m_peer->SetData<ControlButtonContentInfo>( ii + 1, kControlTabImageContentTag, &info );
425 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
426
427 wxMacReleaseBitmapButton( &info ) ;
428 }
429 }
430
431 m_peer->SetTabEnabled( ii + 1, true ) ;
432 }
433
434 Rect bounds;
435 m_peer->GetRectInWindowCoords( &bounds ) ;
436 InvalWindowRect( (WindowRef)MacGetTopLevelWindowRef(), &bounds );
437 }
438
439 wxRect wxNotebook::GetPageRect() const
440 {
441 wxSize size = GetClientSize() ;
442
443 return wxRect( 0 , 0 , size.x , size.y ) ;
444 }
445
446 // ----------------------------------------------------------------------------
447 // wxNotebook callbacks
448 // ----------------------------------------------------------------------------
449
450 // @@@ OnSize() is used for setting the font when it's called for the first
451 // time because doing it in ::Create() doesn't work (for unknown reasons)
452 void wxNotebook::OnSize(wxSizeEvent& event)
453 {
454 unsigned int nCount = m_pages.Count();
455 wxRect rect = GetPageRect() ;
456
457 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
458 {
459 wxNotebookPage *pPage = m_pages[nPage];
460 pPage->SetSize(rect);
461 if ( pPage->GetAutoLayout() )
462 pPage->Layout();
463 }
464
465 // Processing continues to next OnSize
466 event.Skip();
467 }
468
469 void wxNotebook::OnSelChange(wxNotebookEvent& event)
470 {
471 // is it our tab control?
472 if ( event.GetEventObject() == this )
473 ChangePage(event.GetOldSelection(), event.GetSelection());
474
475 // we want to give others a chance to process this message as well
476 event.Skip();
477 }
478
479 void wxNotebook::OnSetFocus(wxFocusEvent& event)
480 {
481 // set focus to the currently selected page if any
482 if ( m_nSelection != -1 )
483 m_pages[m_nSelection]->SetFocus();
484
485 event.Skip();
486 }
487
488 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
489 {
490 if ( event.IsWindowChange() )
491 {
492 // change pages
493 AdvanceSelection( event.GetDirection() );
494 }
495 else
496 {
497 // we get this event in 2 cases
498 //
499 // a) one of our pages might have generated it because the user TABbed
500 // out from it in which case we should propagate the event upwards and
501 // our parent will take care of setting the focus to prev/next sibling
502 //
503 // or
504 //
505 // b) the parent panel wants to give the focus to us so that we
506 // forward it to our selected page. We can't deal with this in
507 // OnSetFocus() because we don't know which direction the focus came
508 // from in this case and so can't choose between setting the focus to
509 // first or last panel child
510 wxWindow *parent = GetParent();
511
512 // the cast is here to fix a GCC ICE
513 if ( ((wxWindow*)event.GetEventObject()) == parent )
514 {
515 // no, it doesn't come from child, case (b): forward to a page
516 if ( m_nSelection != -1 )
517 {
518 // so that the page knows that the event comes from it's parent
519 // and is being propagated downwards
520 event.SetEventObject( this );
521
522 wxWindow *page = m_pages[m_nSelection];
523 if ( !page->GetEventHandler()->ProcessEvent( event ) )
524 {
525 page->SetFocus();
526 }
527 //else: page manages focus inside it itself
528 }
529 else
530 {
531 // we have no pages - still have to give focus to _something_
532 SetFocus();
533 }
534 }
535 else
536 {
537 // it comes from our child, case (a), pass to the parent
538 if ( parent )
539 {
540 event.SetCurrentFocus( this );
541 parent->GetEventHandler()->ProcessEvent( event );
542 }
543 }
544 }
545 }
546
547 // ----------------------------------------------------------------------------
548 // wxNotebook base class virtuals
549 // ----------------------------------------------------------------------------
550
551 #if wxUSE_CONSTRAINTS
552
553 // override these 2 functions to do nothing: everything is done in OnSize
554
555 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
556 {
557 // don't set the sizes of the pages - their correct size is not yet known
558 wxControl::SetConstraintSizes( false );
559 }
560
561 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
562 {
563 return true;
564 }
565
566 #endif // wxUSE_CONSTRAINTS
567
568 void wxNotebook::Command(wxCommandEvent& event)
569 {
570 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
571 }
572
573 // ----------------------------------------------------------------------------
574 // wxNotebook helper functions
575 // ----------------------------------------------------------------------------
576
577 // hide the currently active panel and show the new one
578 void wxNotebook::ChangePage(int nOldSel, int nSel)
579 {
580 if (nOldSel == nSel)
581 return;
582
583 if ( nOldSel != -1 )
584 m_pages[nOldSel]->Show( false );
585
586 if ( nSel != -1 )
587 {
588 wxNotebookPage *pPage = m_pages[nSel];
589 pPage->Show( true );
590 pPage->SetFocus();
591 }
592
593 m_nSelection = nSel;
594 m_peer->SetValue( m_nSelection + 1 ) ;
595 }
596
597 wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
598 {
599 OSStatus status = eventNotHandledErr ;
600
601 SInt32 newSel = m_peer->GetValue() - 1 ;
602 if ( newSel != m_nSelection )
603 {
604 wxNotebookEvent changing(
605 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
606 newSel , m_nSelection );
607 changing.SetEventObject( this );
608 GetEventHandler()->ProcessEvent( changing );
609
610 if ( changing.IsAllowed() )
611 {
612 wxNotebookEvent event(
613 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
614 newSel, m_nSelection );
615 event.SetEventObject( this );
616 GetEventHandler()->ProcessEvent( event );
617 }
618 else
619 {
620 m_peer->SetValue( m_nSelection + 1 ) ;
621 }
622
623 status = noErr ;
624 }
625
626 return (wxInt32)status ;
627 }
628
629 #endif