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