]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/notebmac.cpp
added test for Replace(whatever, "")
[wxWidgets.git] / src / osx / carbon / notebmac.cpp
CommitLineData
489468fe
SC
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"
1f0c8f31 27#include "wx/osx/uma.h"
489468fe
SC
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, wxBookCtrlBase)
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, wxBookCtrlBase)
489468fe
SC
46
47
48// common part of all ctors
49void wxNotebook::Init()
50{
51 m_nSelection = -1;
52}
53
54// default for dynamic class
55wxNotebook::wxNotebook()
56{
57 Init();
58}
59
60// the same arguments as for wxControl
61wxNotebook::wxNotebook( wxWindow *parent,
62 wxWindowID id,
63 const wxPoint& pos,
64 const wxSize& size,
65 long style,
66 const wxString& name )
67{
68 Init();
69
70 Create( parent, id, pos, size, style, name );
71}
72
73bool wxNotebook::Create( wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name )
79{
80 m_macIsUserPane = false ;
81
82 if (! (style & wxBK_ALIGN_MASK))
83 style |= wxBK_TOP;
84
85 if ( !wxNotebookBase::Create( parent, id, pos, size, style, name ) )
86 return false;
87
88 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
89
90 if ( bounds.right <= bounds.left )
91 bounds.right = bounds.left + 100;
92 if ( bounds.bottom <= bounds.top )
93 bounds.bottom = bounds.top + 100;
94
95 UInt16 tabstyle = kControlTabDirectionNorth;
96 if ( HasFlag(wxBK_LEFT) )
97 tabstyle = kControlTabDirectionWest;
98 else if ( HasFlag( wxBK_RIGHT ) )
99 tabstyle = kControlTabDirectionEast;
100 else if ( HasFlag( wxBK_BOTTOM ) )
101 tabstyle = kControlTabDirectionSouth;
102
103 ControlTabSize tabsize;
104 switch (GetWindowVariant())
105 {
106 case wxWINDOW_VARIANT_MINI:
107 tabsize = 3 ;
108 break;
109
110 case wxWINDOW_VARIANT_SMALL:
111 tabsize = kControlTabSizeSmall;
112 break;
113
114 default:
115 tabsize = kControlTabSizeLarge;
116 break;
117 }
118
119 m_peer = new wxMacControl( this );
120 OSStatus err = CreateTabsControl(
121 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
122 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
123 verify_noerr( err );
124
125 MacPostControlCreate( pos, size );
126
127 return true ;
128}
129
130// dtor
131wxNotebook::~wxNotebook()
132{
133}
134
135// ----------------------------------------------------------------------------
136// wxNotebook accessors
137// ----------------------------------------------------------------------------
138
139void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding))
140{
141 // unsupported by OS
142}
143
144void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
145{
146 // unsupported by OS
147}
148
149void wxNotebook::SetPageSize(const wxSize& size)
150{
151 SetSize( CalcSizeFromPage( size ) );
152}
153
154wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
155{
156 return DoGetSizeFromClientSize( sizePage );
157}
158
159int wxNotebook::DoSetSelection(size_t nPage, int flags)
160{
161 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("DoSetSelection: invalid notebook page") );
162
163 if ( m_nSelection == wxNOT_FOUND || nPage != (size_t)m_nSelection )
164 {
165 if ( flags & SetSelection_SendEvent )
166 {
167 if ( !SendPageChangingEvent(nPage) )
168 {
169 // vetoed by program
170 return m_nSelection;
171 }
172 //else: program allows the page change
173
174 SendPageChangedEvent(m_nSelection, nPage);
175 }
176
177 ChangePage(m_nSelection, nPage);
178 }
179 //else: no change
180
181 return m_nSelection;
182}
183
184bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
185{
186 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") );
187
188 wxNotebookPage *page = m_pages[nPage];
189 page->SetLabel(wxStripMenuCodes(strText));
190 MacSetupTabs();
191
192 return true;
193}
194
195wxString wxNotebook::GetPageText(size_t nPage) const
196{
197 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") );
198
199 wxNotebookPage *page = m_pages[nPage];
200
201 return page->GetLabel();
202}
203
204int wxNotebook::GetPageImage(size_t nPage) const
205{
206 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") );
207
208 return m_images[nPage];
209}
210
211bool wxNotebook::SetPageImage(size_t nPage, int nImage)
212{
213 wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
214 wxT("SetPageImage: invalid notebook page") );
215 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
216 wxT("SetPageImage: invalid image index") );
217
218 if ( nImage != m_images[nPage] )
219 {
220 // if the item didn't have an icon before or, on the contrary, did have
221 // it but has lost it now, its size will change - but if the icon just
222 // changes, it won't
223 m_images[nPage] = nImage;
224
225 MacSetupTabs() ;
226 }
227
228 return true;
229}
230
231// ----------------------------------------------------------------------------
232// wxNotebook operations
233// ----------------------------------------------------------------------------
234
235// remove one page from the notebook, without deleting the window
236wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
237{
238 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL,
239 wxT("DoRemovePage: invalid notebook page") );
240
241 wxNotebookPage* page = m_pages[nPage] ;
242 m_pages.RemoveAt(nPage);
243
244 MacSetupTabs();
245
246 if (m_nSelection >= (int)GetPageCount())
247 m_nSelection = GetPageCount() - 1;
248
249 if (m_nSelection >= 0)
250 m_pages[m_nSelection]->Show(true);
251
252 InvalidateBestSize();
253
254 return page;
255}
256
257// remove all pages
258bool wxNotebook::DeleteAllPages()
259{
260 WX_CLEAR_ARRAY(m_pages) ;
261 MacSetupTabs();
262 m_nSelection = -1 ;
263 InvalidateBestSize();
264
265 return true;
266}
267
268// same as AddPage() but does it at given position
269bool wxNotebook::InsertPage(size_t nPage,
270 wxNotebookPage *pPage,
271 const wxString& strText,
272 bool bSelect,
273 int imageId )
274{
275 if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) )
276 return false;
277
278 wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") );
279
280 // don't show pages by default (we'll need to adjust their size first)
281 pPage->Show( false ) ;
282
283 pPage->SetLabel( wxStripMenuCodes(strText) );
284
285 m_images.Insert( imageId, nPage );
286
287 MacSetupTabs();
288
289 wxRect rect = GetPageRect() ;
290 pPage->SetSize( rect );
291 if ( pPage->GetAutoLayout() )
292 pPage->Layout();
293
294 // now deal with the selection
295 // ---------------------------
296
297 // if the inserted page is before the selected one, we must update the
298 // index of the selected page
299
300 if ( int(nPage) <= m_nSelection )
301 {
302 m_nSelection++;
303
304 // while this still is the same page showing, we need to update the tabs
305 m_peer->SetValue( m_nSelection + 1 ) ;
306 }
307
308 // some page should be selected: either this one or the first one if there
309 // is still no selection
310 int selNew = -1;
311 if ( bSelect )
312 selNew = nPage;
313 else if ( m_nSelection == -1 )
314 selNew = 0;
315
316 if ( selNew != -1 )
317 SetSelection( selNew );
318
319 InvalidateBestSize();
320
321 return true;
322}
323
324int wxNotebook::HitTest(const wxPoint& pt, long * flags) const
325{
326 int resultV = wxNOT_FOUND;
327
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
369 if (flags != NULL)
370 {
371 *flags = 0;
372
373 // we cannot differentiate better
374 if (resultV >= 0)
375 *flags |= wxBK_HITTEST_ONLABEL;
376 else
377 *flags |= wxBK_HITTEST_NOWHERE;
378 }
379
380 return resultV;
381}
382
383// Added by Mark Newsam
384// When a page is added or deleted to the notebook this function updates
385// information held in the control so that it matches the order
386// the user would expect.
387//
388void wxNotebook::MacSetupTabs()
389{
390 m_peer->SetMaximum( GetPageCount() ) ;
391
392 wxNotebookPage *page;
393 ControlTabInfoRecV1 info;
394
395 const size_t countPages = GetPageCount();
396 for (size_t ii = 0; ii < countPages; ii++)
397 {
398 page = m_pages[ii];
399 info.version = kControlTabInfoVersionOne;
400 info.iconSuiteID = 0;
401 wxCFStringRef cflabel( page->GetLabel(), GetFont().GetEncoding() ) ;
402 info.name = cflabel ;
403 m_peer->SetData<ControlTabInfoRecV1>( ii + 1, kControlTabInfoTag, &info ) ;
404
405 if ( GetImageList() && GetPageImage(ii) >= 0 )
406 {
407 const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage( ii ) ) ;
408 if ( bmap.Ok() )
409 {
410 ControlButtonContentInfo info ;
411
412 wxMacCreateBitmapButton( &info, bmap ) ;
413
414 OSStatus err = m_peer->SetData<ControlButtonContentInfo>( ii + 1, kControlTabImageContentTag, &info );
415 if ( err != noErr )
416 {
417 wxFAIL_MSG("Error when setting icon on tab");
418 }
419
420 wxMacReleaseBitmapButton( &info ) ;
421 }
422 }
423
424 m_peer->SetTabEnabled( ii + 1, true ) ;
425 }
426
427 Refresh();
428}
429
430wxRect 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)
443void 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
3e97a905 460void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
489468fe
SC
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
470void 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
479void 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->HandleWindowEvent( 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->HandleWindowEvent( 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
546void 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
552bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
553{
554 return true;
555}
556
557#endif // wxUSE_CONSTRAINTS
558
559void wxNotebook::Command(wxCommandEvent& WXUNUSED(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
569void 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
588wxInt32 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 {
3e97a905 595 wxBookCtrlEvent changing(
489468fe
SC
596 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
597 newSel , m_nSelection );
598 changing.SetEventObject( this );
599 HandleWindowEvent( changing );
600
601 if ( changing.IsAllowed() )
602 {
3e97a905 603 wxBookCtrlEvent event(
489468fe
SC
604 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
605 newSel, m_nSelection );
606 event.SetEventObject( this );
607 HandleWindowEvent( 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