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