]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
making getter const
[wxWidgets.git] / src / mac / carbon / notebmac.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.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 #ifdef __GNUG__
13 #pragma implementation "notebook.h"
14 #endif
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23 #include "wx/app.h"
24 #include "wx/string.h"
25 #include "wx/log.h"
26 #include "wx/imaglist.h"
27 #include "wx/image.h"
28 #include "wx/notebook.h"
29 #include "wx/mac/uma.h"
30 // ----------------------------------------------------------------------------
31 // macros
32 // ----------------------------------------------------------------------------
33
34 // check that the page index is valid
35 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
36
37
38 // ----------------------------------------------------------------------------
39 // event table
40 // ----------------------------------------------------------------------------
41
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
44
45 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
46 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
47
48 EVT_SIZE(wxNotebook::OnSize)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
51 END_EVENT_TABLE()
52
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxNotebook construction
62 // ----------------------------------------------------------------------------
63
64 // common part of all ctors
65 void wxNotebook::Init()
66 {
67 m_nSelection = -1;
68 }
69
70 // default for dynamic class
71 wxNotebook::wxNotebook()
72 {
73 Init();
74 }
75
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow *parent,
78 wxWindowID id,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxString& name)
83 {
84 Init();
85
86 Create(parent, id, pos, size, style, name);
87 }
88
89 // Create() function
90 bool wxNotebook::Create(wxWindow *parent,
91 wxWindowID id,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 m_macIsUserPane = FALSE ;
98
99 if ( !wxNotebookBase::Create(parent, id, pos, size, style, name) )
100 return false;
101
102 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
103
104 if( bounds.right <= bounds.left )
105 bounds.right = bounds.left + 100 ;
106 if ( bounds.bottom <= bounds.top )
107 bounds.bottom = bounds.top + 100 ;
108
109 UInt16 tabstyle = kControlTabDirectionNorth ;
110 if ( HasFlag(wxNB_LEFT) )
111 tabstyle = kControlTabDirectionWest ;
112 else if ( HasFlag( wxNB_RIGHT ) )
113 tabstyle = kControlTabDirectionEast ;
114 else if ( HasFlag( wxNB_BOTTOM ) )
115 tabstyle = kControlTabDirectionSouth ;
116
117 ControlTabSize tabsize = kControlTabSizeLarge ;
118 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
119 tabsize = kControlTabSizeSmall ;
120 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
121 {
122 if (UMAGetSystemVersion() >= 0x1030 )
123 tabsize = 3 ;
124 else
125 tabsize = kControlSizeSmall;
126 }
127
128 m_peer = new wxMacControl() ;
129 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
130 tabsize , tabstyle, 0, NULL, m_peer->GetControlRefAddr() ) );
131
132
133 MacPostControlCreate(pos,size) ;
134 return TRUE ;
135 }
136
137 // dtor
138 wxNotebook::~wxNotebook()
139 {
140 }
141
142 // ----------------------------------------------------------------------------
143 // wxNotebook accessors
144 // ----------------------------------------------------------------------------
145
146 void wxNotebook::SetPadding(const wxSize& padding)
147 {
148 // unsupported by OS
149 }
150
151 void wxNotebook::SetTabSize(const wxSize& sz)
152 {
153 // unsupported by OS
154 }
155
156 void wxNotebook::SetPageSize(const wxSize& size)
157 {
158 SetSize( CalcSizeFromPage( size ) );
159 }
160
161 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
162 {
163 return DoGetSizeFromClientSize( sizePage ) ;
164 }
165
166 wxSize wxNotebook::DoGetBestSize() const
167 {
168 // calculate the max page size
169 wxSize size(0, 0);
170
171 size_t count = GetPageCount();
172 if ( count )
173 {
174 for ( size_t n = 0; n < count; n++ )
175 {
176 wxSize sizePage = m_pages[n]->GetSize();
177
178 if ( size.x < sizePage.x )
179 size.x = sizePage.x;
180 if ( size.y < sizePage.y )
181 size.y = sizePage.y;
182 }
183 }
184 else // no pages
185 {
186 // use some arbitrary default size
187 size.x =
188 size.y = 100;
189 }
190
191 return CalcSizeFromPage(size);
192 }
193
194 int wxNotebook::SetSelection(size_t nPage)
195 {
196 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
197
198 if ( int(nPage) != m_nSelection )
199 {
200 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
201 event.SetSelection(nPage);
202 event.SetOldSelection(m_nSelection);
203 event.SetEventObject(this);
204 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
205 {
206 // program allows the page change
207 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
208 (void)GetEventHandler()->ProcessEvent(event);
209
210 ChangePage(m_nSelection, nPage);
211 }
212 }
213
214 return m_nSelection;
215 }
216
217 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
218 {
219 wxASSERT( IS_VALID_PAGE(nPage) );
220
221 wxNotebookPage *page = m_pages[nPage];
222 page->SetLabel(strText);
223 MacSetupTabs();
224
225 return true;
226 }
227
228 wxString wxNotebook::GetPageText(size_t nPage) const
229 {
230 wxASSERT( IS_VALID_PAGE(nPage) );
231
232 wxNotebookPage *page = m_pages[nPage];
233 return page->GetLabel();
234 }
235
236 int wxNotebook::GetPageImage(size_t nPage) const
237 {
238 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
239
240 return m_images[nPage];
241 }
242
243 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
244 {
245 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
246
247 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
248 _T("invalid image index in SetPageImage()") );
249
250 if ( nImage != m_images[nPage] )
251 {
252 // if the item didn't have an icon before or, on the contrary, did have
253 // it but has lost it now, its size will change - but if the icon just
254 // changes, it won't
255 m_images[nPage] = nImage;
256
257 MacSetupTabs() ;
258 }
259
260 return TRUE;
261 }
262
263 // ----------------------------------------------------------------------------
264 // wxNotebook operations
265 // ----------------------------------------------------------------------------
266
267 // remove one page from the notebook, without deleting the window
268 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
269 {
270 wxCHECK( IS_VALID_PAGE(nPage), NULL );
271 wxNotebookPage* page = m_pages[nPage] ;
272 m_pages.RemoveAt(nPage);
273
274 MacSetupTabs();
275
276 if(m_nSelection >= (int)GetPageCount()) {
277 m_nSelection = GetPageCount() - 1;
278 }
279 if(m_nSelection >= 0) {
280 m_pages[m_nSelection]->Show(true);
281 }
282 InvalidateBestSize();
283 return page;
284 }
285
286 // remove all pages
287 bool wxNotebook::DeleteAllPages()
288 {
289 WX_CLEAR_ARRAY(m_pages) ;
290 MacSetupTabs();
291 m_nSelection = -1 ;
292 InvalidateBestSize();
293 return TRUE;
294 }
295
296
297 // same as AddPage() but does it at given position
298 bool wxNotebook::InsertPage(size_t nPage,
299 wxNotebookPage *pPage,
300 const wxString& strText,
301 bool bSelect,
302 int imageId)
303 {
304 if ( !wxNotebookBase::InsertPage(nPage, pPage, strText, bSelect, imageId) )
305 return false;
306
307 wxASSERT_MSG( pPage->GetParent() == this,
308 _T("notebook pages must have notebook as parent") );
309
310 // don't show pages by default (we'll need to adjust their size first)
311 pPage->Show( false ) ;
312
313 pPage->SetLabel(strText);
314
315 m_images.Insert(imageId, nPage);
316
317 MacSetupTabs();
318
319 wxRect rect = GetPageRect() ;
320 pPage->SetSize(rect);
321 if ( pPage->GetAutoLayout() ) {
322 pPage->Layout();
323 }
324
325
326 // now deal with the selection
327 // ---------------------------
328
329 // if the inserted page is before the selected one, we must update the
330 // index of the selected page
331
332 if ( int(nPage) <= m_nSelection )
333 {
334 m_nSelection++;
335 // while this still is the same page showing, we need to update the tabs
336 m_peer->SetValue( m_nSelection + 1 ) ;
337 }
338
339 // some page should be selected: either this one or the first one if there
340 // is still no selection
341 int selNew = -1;
342 if ( bSelect )
343 selNew = nPage;
344 else if ( m_nSelection == -1 )
345 selNew = 0;
346
347 if ( selNew != -1 )
348 SetSelection(selNew);
349
350 InvalidateBestSize();
351 return true;
352 }
353
354 /* Added by Mark Newsam
355 * When a page is added or deleted to the notebook this function updates
356 * information held in the control so that it matches the order
357 * the user would expect.
358 */
359 void wxNotebook::MacSetupTabs()
360 {
361 m_peer->SetMaximum( GetPageCount() ) ;
362
363 wxNotebookPage *page;
364 ControlTabInfoRec info;
365
366 const size_t countPages = GetPageCount();
367 for(size_t ii = 0; ii < countPages; ii++)
368 {
369 page = m_pages[ii];
370 info.version = 0;
371 info.iconSuiteID = 0;
372 wxMacStringToPascal( page->GetLabel() , info.name ) ;
373 m_peer->SetData<ControlTabInfoRec>( ii+1, kControlTabInfoTag, &info ) ;
374 m_peer->SetTabEnabled( ii + 1 , true ) ;
375 #if TARGET_CARBON
376 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
377 {
378 // tab controls only support very specific types of images, therefore we are doing an odyssee
379 // accross the icon worlds (even Apple DTS did not find a shorter path)
380 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
381 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
382 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
383 // of static counter
384 const wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ;
385 if ( bmap )
386 {
387 wxBitmap scaledBitmap ;
388 if ( bmap->GetWidth() != 16 || bmap->GetHeight() != 16 )
389 {
390 scaledBitmap = wxBitmap( bmap->ConvertToImage().Scale(16,16) ) ;
391 bmap = &scaledBitmap ;
392 }
393 ControlButtonContentInfo info ;
394 wxMacCreateBitmapButton( &info , *bmap , kControlContentPictHandle) ;
395 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
396 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
397 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
398 IconRef iconRef ;
399 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1, iconFamily, &iconRef ) ;
400 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
401 info.contentType = kControlContentIconRef ;
402 info.u.iconRef = iconRef ;
403 m_peer->SetData<ControlButtonContentInfo>( ii+1,kControlTabImageContentTag, &info );
404 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
405 if ( UMAGetSystemVersion() < 0x1030 )
406 {
407 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
408 }
409
410 ReleaseIconRef( iconRef ) ;
411 DisposeHandle( (Handle) iconFamily ) ;
412 }
413 }
414 #endif
415 }
416 Rect bounds;
417 m_peer->GetRectInWindowCoords( &bounds ) ;
418 InvalWindowRect((WindowRef)MacGetTopLevelWindowRef(), &bounds);
419 }
420
421 wxRect wxNotebook::GetPageRect() const
422 {
423 wxSize size = GetClientSize() ;
424 return wxRect( 0 , 0 , size.x , size.y ) ;
425 }
426 // ----------------------------------------------------------------------------
427 // wxNotebook callbacks
428 // ----------------------------------------------------------------------------
429
430 // @@@ OnSize() is used for setting the font when it's called for the first
431 // time because doing it in ::Create() doesn't work (for unknown reasons)
432 void wxNotebook::OnSize(wxSizeEvent& event)
433 {
434
435 unsigned int nCount = m_pages.Count();
436 wxRect rect = GetPageRect() ;
437 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
438 wxNotebookPage *pPage = m_pages[nPage];
439 pPage->SetSize(rect);
440 if ( pPage->GetAutoLayout() ) {
441 pPage->Layout();
442 }
443 }
444
445 // Processing continues to next OnSize
446 event.Skip();
447 }
448
449 void wxNotebook::OnSelChange(wxNotebookEvent& event)
450 {
451 // is it our tab control?
452 if ( event.GetEventObject() == this )
453 ChangePage(event.GetOldSelection(), event.GetSelection());
454
455 // we want to give others a chance to process this message as well
456 event.Skip();
457 }
458
459 void wxNotebook::OnSetFocus(wxFocusEvent& event)
460 {
461 // set focus to the currently selected page if any
462 if ( m_nSelection != -1 )
463 m_pages[m_nSelection]->SetFocus();
464
465 event.Skip();
466 }
467
468 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
469 {
470 if ( event.IsWindowChange() ) {
471 // change pages
472 AdvanceSelection(event.GetDirection());
473 }
474 else {
475 // we get this event in 2 cases
476 //
477 // a) one of our pages might have generated it because the user TABbed
478 // out from it in which case we should propagate the event upwards and
479 // our parent will take care of setting the focus to prev/next sibling
480 //
481 // or
482 //
483 // b) the parent panel wants to give the focus to us so that we
484 // forward it to our selected page. We can't deal with this in
485 // OnSetFocus() because we don't know which direction the focus came
486 // from in this case and so can't choose between setting the focus to
487 // first or last panel child
488 wxWindow *parent = GetParent();
489 // the cast is here to fic a GCC ICE
490 if ( ((wxWindow*)event.GetEventObject()) == parent )
491 {
492 // no, it doesn't come from child, case (b): forward to a page
493 if ( m_nSelection != -1 )
494 {
495 // so that the page knows that the event comes from it's parent
496 // and is being propagated downwards
497 event.SetEventObject(this);
498
499 wxWindow *page = m_pages[m_nSelection];
500 if ( !page->GetEventHandler()->ProcessEvent(event) )
501 {
502 page->SetFocus();
503 }
504 //else: page manages focus inside it itself
505 }
506 else
507 {
508 // we have no pages - still have to give focus to _something_
509 SetFocus();
510 }
511 }
512 else
513 {
514 // it comes from our child, case (a), pass to the parent
515 if ( parent ) {
516 event.SetCurrentFocus(this);
517 parent->GetEventHandler()->ProcessEvent(event);
518 }
519 }
520 }
521 }
522
523 // ----------------------------------------------------------------------------
524 // wxNotebook base class virtuals
525 // ----------------------------------------------------------------------------
526
527 #if wxUSE_CONSTRAINTS
528
529 // override these 2 functions to do nothing: everything is done in OnSize
530
531 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
532 {
533 // don't set the sizes of the pages - their correct size is not yet known
534 wxControl::SetConstraintSizes(FALSE);
535 }
536
537 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
538 {
539 return TRUE;
540 }
541
542 #endif // wxUSE_CONSTRAINTS
543
544 void wxNotebook::Command(wxCommandEvent& event)
545 {
546 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
547 }
548
549 // ----------------------------------------------------------------------------
550 // wxNotebook helper functions
551 // ----------------------------------------------------------------------------
552
553 // hide the currently active panel and show the new one
554 void wxNotebook::ChangePage(int nOldSel, int nSel)
555 {
556 if ( nOldSel != -1 )
557 {
558 m_pages[nOldSel]->Show(FALSE);
559 }
560
561 if ( nSel != -1 )
562 {
563 wxNotebookPage *pPage = m_pages[nSel];
564 pPage->Show(TRUE);
565 pPage->SetFocus();
566 }
567
568 m_nSelection = nSel;
569 m_peer->SetValue( m_nSelection + 1 ) ;
570 }
571
572 wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
573 {
574 OSStatus status = eventNotHandledErr ;
575
576 SInt32 newSel = m_peer->GetValue() - 1 ;
577 if ( newSel != m_nSelection )
578 {
579 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
580 newSel , m_nSelection);
581 changing.SetEventObject(this);
582 GetEventHandler()->ProcessEvent(changing);
583
584 if(changing.IsAllowed())
585 {
586 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
587 newSel, m_nSelection);
588 event.SetEventObject(this);
589
590 GetEventHandler()->ProcessEvent(event);
591 }
592 else
593 {
594 m_peer->SetValue( m_nSelection + 1 ) ;
595 }
596 status = noErr ;
597 }
598 return status ;
599 }
600