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