]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/notebmac.cpp
Some compile fixes.
[wxWidgets.git] / src / mac / carbon / notebmac.cpp
... / ...
CommitLineData
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
34DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
35DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
36
37BEGIN_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)
43END_EVENT_TABLE()
44
45IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
46IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
47
48
49// common part of all ctors
50void wxNotebook::Init()
51{
52 m_nSelection = -1;
53}
54
55// default for dynamic class
56wxNotebook::wxNotebook()
57{
58 Init();
59}
60
61// the same arguments as for wxControl
62wxNotebook::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
74bool 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
135wxNotebook::~wxNotebook()
136{
137}
138
139// ----------------------------------------------------------------------------
140// wxNotebook accessors
141// ----------------------------------------------------------------------------
142
143void wxNotebook::SetPadding(const wxSize& padding)
144{
145 // unsupported by OS
146}
147
148void wxNotebook::SetTabSize(const wxSize& sz)
149{
150 // unsupported by OS
151}
152
153void wxNotebook::SetPageSize(const wxSize& size)
154{
155 SetSize( CalcSizeFromPage( size ) );
156}
157
158wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
159{
160 return DoGetSizeFromClientSize( sizePage );
161}
162
163int 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
188bool 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
199wxString 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
208int 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
215bool 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
240wxNotebookPage* 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
262bool 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
273bool 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
328int 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//
394void 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#if wxMAC_USE_CORE_GRAPHICS
431 Refresh();
432#else
433 Rect bounds;
434 m_peer->GetRectInWindowCoords( &bounds ) ;
435 InvalWindowRect( (WindowRef)MacGetTopLevelWindowRef(), &bounds );
436#endif
437}
438
439wxRect 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)
452void 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
469void 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
479void 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
488void 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
555void 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
561bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
562{
563 return true;
564}
565
566#endif // wxUSE_CONSTRAINTS
567
568void 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
578void 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
597wxInt32 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