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