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