]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/notebmac.cpp
fix compilation - Stefan see my last commit bundle :)
[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
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
6bc3b8e9 198 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
2646f485
SC
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 }
37144cf0 356 InvalidateBestSize();
2646f485
SC
357 return page;
358}
359
360// remove all pages
361bool wxNotebook::DeleteAllPages()
362{
363 WX_CLEAR_ARRAY(m_pages) ;
364 MacSetupTabs();
365 m_nSelection = -1 ;
37144cf0 366 InvalidateBestSize();
2646f485
SC
367 return TRUE;
368}
369
370
371// same as AddPage() but does it at given position
372bool wxNotebook::InsertPage(size_t nPage,
373 wxNotebookPage *pPage,
374 const wxString& strText,
375 bool bSelect,
376 int imageId)
377{
378 if ( !wxNotebookBase::InsertPage(nPage, pPage, strText, bSelect, imageId) )
379 return false;
380
381 wxASSERT_MSG( pPage->GetParent() == this,
382 _T("notebook pages must have notebook as parent") );
383
384 // don't show pages by default (we'll need to adjust their size first)
385 pPage->Show( false ) ;
386
387 pPage->SetLabel(strText);
388
389 m_images.Insert(imageId, nPage);
390
391 MacSetupTabs();
392
393 wxRect rect = GetPageRect() ;
394 pPage->SetSize(rect);
395 if ( pPage->GetAutoLayout() ) {
396 pPage->Layout();
397 }
398
399
400 // now deal with the selection
401 // ---------------------------
402
403 // if the inserted page is before the selected one, we must update the
404 // index of the selected page
405
406 if ( int(nPage) <= m_nSelection )
407 {
408 m_nSelection++;
409 // while this still is the same page showing, we need to update the tabs
410 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
411 }
412
413 // some page should be selected: either this one or the first one if there
414 // is still no selection
415 int selNew = -1;
416 if ( bSelect )
417 selNew = nPage;
418 else if ( m_nSelection == -1 )
419 selNew = 0;
420
421 if ( selNew != -1 )
422 SetSelection(selNew);
423
37144cf0 424 InvalidateBestSize();
2646f485
SC
425 return true;
426}
427
428/* Added by Mark Newsam
429* When a page is added or deleted to the notebook this function updates
430* information held in the m_macControl so that it matches the order
431* the user would expect.
432*/
433void wxNotebook::MacSetupTabs()
434{
435 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
436
437 wxNotebookPage *page;
438 ControlTabInfoRec info;
439
440 const size_t countPages = GetPageCount();
441 for(size_t ii = 0; ii < countPages; ii++)
442 {
443 page = m_pages[ii];
444 info.version = 0;
445 info.iconSuiteID = 0;
446 wxMacStringToPascal( page->GetLabel() , info.name ) ;
447
448 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
449 sizeof( ControlTabInfoRec) , (char*) &info ) ;
450 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
451#if TARGET_CARBON
452 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
453 {
454 // tab controls only support very specific types of images, therefore we are doing an odyssee
455 // accross the icon worlds (even Apple DTS did not find a shorter path)
456 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
457 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
458 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
459 // of static counter
460 const wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ;
461 if ( bmap )
462 {
463 wxBitmap scaledBitmap ;
464 if ( bmap->GetWidth() != 16 || bmap->GetHeight() != 16 )
465 {
466 scaledBitmap = wxBitmap( bmap->ConvertToImage().Scale(16,16) ) ;
467 bmap = &scaledBitmap ;
468 }
469 ControlButtonContentInfo info ;
470 wxMacCreateBitmapButton( &info , *bmap , kControlContentPictHandle) ;
471 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
472 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
473 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
474 IconRef iconRef ;
475 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1, iconFamily, &iconRef ) ;
476 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
477 info.contentType = kControlContentIconRef ;
478 info.u.iconRef = iconRef ;
479 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
480 sizeof( info ), (Ptr)&info );
481 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
482 if ( UMAGetSystemVersion() < 0x1030 )
483 {
484 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
485 }
486
487 ReleaseIconRef( iconRef ) ;
488 DisposeHandle( (Handle) iconFamily ) ;
489 }
490 }
491#endif
492 }
493 Rect bounds;
494 GetControlBounds((ControlHandle)m_macControl, &bounds);
495 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
496}
497
498wxRect wxNotebook::GetPageRect() const
499{
500 // fit the notebook page to the tab control's display area
501 int w, h;
502 GetSize(&w, &h);
503
504 return wxRect(
505 wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder,
506 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder,
507 w - wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder,
508 h - wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder);
509}
510// ----------------------------------------------------------------------------
511// wxNotebook callbacks
512// ----------------------------------------------------------------------------
513
514// @@@ OnSize() is used for setting the font when it's called for the first
515// time because doing it in ::Create() doesn't work (for unknown reasons)
516void wxNotebook::OnSize(wxSizeEvent& event)
517{
518
519 unsigned int nCount = m_pages.Count();
520 wxRect rect = GetPageRect() ;
521 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
522 wxNotebookPage *pPage = m_pages[nPage];
523 pPage->SetSize(rect);
524 if ( pPage->GetAutoLayout() ) {
525 pPage->Layout();
526 }
527 }
528
529 // Processing continues to next OnSize
530 event.Skip();
531}
532
533void wxNotebook::OnSelChange(wxNotebookEvent& event)
534{
535 // is it our tab control?
536 if ( event.GetEventObject() == this )
537 ChangePage(event.GetOldSelection(), event.GetSelection());
538
539 // we want to give others a chance to process this message as well
540 event.Skip();
541}
542
543void wxNotebook::OnSetFocus(wxFocusEvent& event)
544{
545 // set focus to the currently selected page if any
546 if ( m_nSelection != -1 )
547 m_pages[m_nSelection]->SetFocus();
548
549 event.Skip();
550}
551
552void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
553{
554 if ( event.IsWindowChange() ) {
555 // change pages
556 AdvanceSelection(event.GetDirection());
557 }
558 else {
559 // we get this event in 2 cases
560 //
561 // a) one of our pages might have generated it because the user TABbed
562 // out from it in which case we should propagate the event upwards and
563 // our parent will take care of setting the focus to prev/next sibling
564 //
565 // or
566 //
567 // b) the parent panel wants to give the focus to us so that we
568 // forward it to our selected page. We can't deal with this in
569 // OnSetFocus() because we don't know which direction the focus came
570 // from in this case and so can't choose between setting the focus to
571 // first or last panel child
572 wxWindow *parent = GetParent();
573 // the cast is here to fic a GCC ICE
574 if ( ((wxWindow*)event.GetEventObject()) == parent )
575 {
576 // no, it doesn't come from child, case (b): forward to a page
577 if ( m_nSelection != -1 )
578 {
579 // so that the page knows that the event comes from it's parent
580 // and is being propagated downwards
581 event.SetEventObject(this);
582
583 wxWindow *page = m_pages[m_nSelection];
584 if ( !page->GetEventHandler()->ProcessEvent(event) )
585 {
586 page->SetFocus();
587 }
588 //else: page manages focus inside it itself
589 }
590 else
591 {
592 // we have no pages - still have to give focus to _something_
593 SetFocus();
594 }
595 }
596 else
597 {
598 // it comes from our child, case (a), pass to the parent
599 if ( parent ) {
600 event.SetCurrentFocus(this);
601 parent->GetEventHandler()->ProcessEvent(event);
602 }
603 }
604 }
605}
606
607// ----------------------------------------------------------------------------
608// wxNotebook base class virtuals
609// ----------------------------------------------------------------------------
610
611#if wxUSE_CONSTRAINTS
612
613// override these 2 functions to do nothing: everything is done in OnSize
614
615void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
616{
617 // don't set the sizes of the pages - their correct size is not yet known
618 wxControl::SetConstraintSizes(FALSE);
619}
620
621bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
622{
623 return TRUE;
624}
625
626#endif // wxUSE_CONSTRAINTS
627
628void wxNotebook::Command(wxCommandEvent& event)
629{
630 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
631}
632
633// ----------------------------------------------------------------------------
634// wxNotebook helper functions
635// ----------------------------------------------------------------------------
636
637// hide the currently active panel and show the new one
638void wxNotebook::ChangePage(int nOldSel, int nSel)
639{
640 if ( nOldSel != -1 )
641 {
642 m_pages[nOldSel]->Show(FALSE);
643 }
644
645 if ( nSel != -1 )
646 {
647 wxNotebookPage *pPage = m_pages[nSel];
648 pPage->Show(TRUE);
649 pPage->SetFocus();
650 }
651
652 m_nSelection = nSel;
653 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
654}
655
656
657void wxNotebook::OnMouse( wxMouseEvent &event )
658{
659 if ( (ControlHandle) m_macControl == NULL )
660 {
661 event.Skip() ;
662 return ;
663 }
664
665 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
666 {
667 int x = event.m_x ;
668 int y = event.m_y ;
669
670 MacClientToRootWindow( &x , &y ) ;
671
672 ControlHandle control ;
673 Point localwhere ;
674 SInt16 controlpart ;
675
676 localwhere.h = x ;
677 localwhere.v = y ;
678
679 short modifiers = 0;
680
681 if ( !event.m_leftDown && !event.m_rightDown )
682 modifiers |= btnState ;
683
684 if ( event.m_shiftDown )
685 modifiers |= shiftKey ;
686
687 if ( event.m_controlDown )
688 modifiers |= controlKey ;
689
690 if ( event.m_altDown )
691 modifiers |= optionKey ;
692
693 if ( event.m_metaDown )
694 modifiers |= cmdKey ;
695
696 control = (ControlHandle) m_macControl ;
697 if ( control && ::IsControlActive( control ) )
698 {
699 {
700 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
701 ::GetControl32BitValue(control) - 1, m_nSelection);
702 changing.SetEventObject(this);
703 GetEventHandler()->ProcessEvent(changing);
704
705 if(changing.IsAllowed())
706 {
707 controlpart = ::HandleControlClick(control, localwhere, modifiers,
708 (ControlActionUPP) -1);
709 wxTheApp->s_lastMouseDown = 0 ;
710
711 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
712 ::GetControl32BitValue(control) - 1, m_nSelection);
713 event.SetEventObject(this);
714
715 GetEventHandler()->ProcessEvent(event);
716 }
717 }
718 }
719 }
720}
721
722
723void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
724{
725#if 0
726 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
727 event.SetEventObject(this);
728
729 ProcessEvent(event);
730#endif
731}
732