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