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