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