]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/notebmac.cpp
correcting DropData behaviour so that preferred format is handled correctly
[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 339 wxNotebookPage *page;
5324268e 340 ControlTabInfoRecV1 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];
5324268e 346 info.version = kControlTabInfoVersionOne;
c854c7d9 347 info.iconSuiteID = 0;
5324268e
SC
348 wxMacCFStringHolder cflabel( page->GetLabel() , m_font.GetEncoding() ) ;
349 info.name = cflabel ;
350 m_peer->SetData<ControlTabInfoRecV1>( ii+1, kControlTabInfoTag, &info ) ;
20b69855 351
2b5f62a0
VZ
352 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
353 {
b337acd7
SC
354 const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ;
355 if ( bmap.Ok() )
061174e3 356 {
061174e3 357 ControlButtonContentInfo info ;
20b69855 358
b337acd7 359 wxMacCreateBitmapButton( &info , bmap ) ;
81e2cbc6
VZ
360#ifdef __WXDEBUG__
361 OSStatus err =
362#endif // __WXDEBUG__
363 m_peer->SetData<ControlButtonContentInfo>( ii+1,kControlTabImageContentTag, &info );
061174e3 364 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
20b69855 365 wxMacReleaseBitmapButton( &info ) ;
061174e3 366 }
2b5f62a0 367 }
5324268e 368 m_peer->SetTabEnabled( ii + 1 , true ) ;
c854c7d9
GD
369 }
370 Rect bounds;
5ca0d812 371 m_peer->GetRectInWindowCoords( &bounds ) ;
facd6764 372 InvalWindowRect((WindowRef)MacGetTopLevelWindowRef(), &bounds);
ee6b1d97
SC
373}
374
63ff6f53
SC
375wxRect wxNotebook::GetPageRect() const
376{
facd6764
SC
377 wxSize size = GetClientSize() ;
378 return wxRect( 0 , 0 , size.x , size.y ) ;
63ff6f53 379}
ee6b1d97
SC
380// ----------------------------------------------------------------------------
381// wxNotebook callbacks
382// ----------------------------------------------------------------------------
383
384// @@@ OnSize() is used for setting the font when it's called for the first
385// time because doing it in ::Create() doesn't work (for unknown reasons)
386void wxNotebook::OnSize(wxSizeEvent& event)
387{
5aed9d50 388
90b959ae 389 unsigned int nCount = m_pages.Count();
63ff6f53 390 wxRect rect = GetPageRect() ;
ee6b1d97 391 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
90b959ae 392 wxNotebookPage *pPage = m_pages[nPage];
63ff6f53 393 pPage->SetSize(rect);
c854c7d9 394 if ( pPage->GetAutoLayout() ) {
ee6b1d97 395 pPage->Layout();
c854c7d9 396 }
ee6b1d97 397 }
5aed9d50 398
ee6b1d97
SC
399 // Processing continues to next OnSize
400 event.Skip();
401}
402
403void wxNotebook::OnSelChange(wxNotebookEvent& event)
404{
405 // is it our tab control?
406 if ( event.GetEventObject() == this )
407 ChangePage(event.GetOldSelection(), event.GetSelection());
5aed9d50 408
ee6b1d97
SC
409 // we want to give others a chance to process this message as well
410 event.Skip();
411}
412
413void wxNotebook::OnSetFocus(wxFocusEvent& event)
414{
415 // set focus to the currently selected page if any
416 if ( m_nSelection != -1 )
90b959ae 417 m_pages[m_nSelection]->SetFocus();
5aed9d50 418
ee6b1d97
SC
419 event.Skip();
420}
421
422void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
423{
424 if ( event.IsWindowChange() ) {
425 // change pages
426 AdvanceSelection(event.GetDirection());
427 }
428 else {
461fe6e2
SC
429 // we get this event in 2 cases
430 //
431 // a) one of our pages might have generated it because the user TABbed
432 // out from it in which case we should propagate the event upwards and
433 // our parent will take care of setting the focus to prev/next sibling
434 //
435 // or
436 //
437 // b) the parent panel wants to give the focus to us so that we
438 // forward it to our selected page. We can't deal with this in
439 // OnSetFocus() because we don't know which direction the focus came
440 // from in this case and so can't choose between setting the focus to
441 // first or last panel child
442 wxWindow *parent = GetParent();
443 // the cast is here to fic a GCC ICE
444 if ( ((wxWindow*)event.GetEventObject()) == parent )
445 {
446 // no, it doesn't come from child, case (b): forward to a page
447 if ( m_nSelection != -1 )
448 {
449 // so that the page knows that the event comes from it's parent
450 // and is being propagated downwards
451 event.SetEventObject(this);
452
453 wxWindow *page = m_pages[m_nSelection];
454 if ( !page->GetEventHandler()->ProcessEvent(event) )
455 {
456 page->SetFocus();
457 }
458 //else: page manages focus inside it itself
459 }
460 else
461 {
462 // we have no pages - still have to give focus to _something_
463 SetFocus();
464 }
465 }
466 else
467 {
468 // it comes from our child, case (a), pass to the parent
469 if ( parent ) {
470 event.SetCurrentFocus(this);
471 parent->GetEventHandler()->ProcessEvent(event);
472 }
ee6b1d97
SC
473 }
474 }
475}
476
477// ----------------------------------------------------------------------------
478// wxNotebook base class virtuals
479// ----------------------------------------------------------------------------
480
461fe6e2
SC
481#if wxUSE_CONSTRAINTS
482
ee6b1d97
SC
483// override these 2 functions to do nothing: everything is done in OnSize
484
461fe6e2 485void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
ee6b1d97 486{
461fe6e2
SC
487 // don't set the sizes of the pages - their correct size is not yet known
488 wxControl::SetConstraintSizes(FALSE);
ee6b1d97
SC
489}
490
461fe6e2 491bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
ee6b1d97 492{
461fe6e2 493 return TRUE;
ee6b1d97
SC
494}
495
461fe6e2
SC
496#endif // wxUSE_CONSTRAINTS
497
ee6b1d97
SC
498void wxNotebook::Command(wxCommandEvent& event)
499{
427ff662 500 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
ee6b1d97
SC
501}
502
503// ----------------------------------------------------------------------------
504// wxNotebook helper functions
505// ----------------------------------------------------------------------------
506
507// hide the currently active panel and show the new one
508void wxNotebook::ChangePage(int nOldSel, int nSel)
509{
461fe6e2
SC
510 if ( nOldSel != -1 )
511 {
512 m_pages[nOldSel]->Show(FALSE);
513 }
514
515 if ( nSel != -1 )
c854c7d9 516 {
90b959ae 517 wxNotebookPage *pPage = m_pages[nSel];
c854c7d9
GD
518 pPage->Show(TRUE);
519 pPage->SetFocus();
c854c7d9 520 }
461fe6e2 521
ee6b1d97 522 m_nSelection = nSel;
5ca0d812 523 m_peer->SetValue( m_nSelection + 1 ) ;
ee6b1d97
SC
524}
525
4c37f124 526wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
799690a0 527{
4c37f124
SC
528 OSStatus status = eventNotHandledErr ;
529
5ca0d812 530 SInt32 newSel = m_peer->GetValue() - 1 ;
4c37f124 531 if ( newSel != m_nSelection )
e40298d5 532 {
4c37f124
SC
533 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
534 newSel , m_nSelection);
535 changing.SetEventObject(this);
536 GetEventHandler()->ProcessEvent(changing);
5aed9d50 537
4c37f124 538 if(changing.IsAllowed())
e40298d5 539 {
4c37f124
SC
540 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
541 newSel, m_nSelection);
542 event.SetEventObject(this);
5aed9d50 543
4c37f124
SC
544 GetEventHandler()->ProcessEvent(event);
545 }
546 else
547 {
5ca0d812 548 m_peer->SetValue( m_nSelection + 1 ) ;
e40298d5 549 }
4c37f124 550 status = noErr ;
e40298d5 551 }
4c37f124 552 return status ;
ee6b1d97
SC
553}
554
179e085f
RN
555#endif
556