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