]> git.saurik.com Git - wxWidgets.git/blame - src/mac/notebmac.cpp
wxMac Unicode support
[wxWidgets.git] / src / mac / notebmac.cpp
CommitLineData
ee6b1d97
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: notebook.cpp
3// Purpose: implementation of wxNotebook
a31a5f85 4// Author: Stefan Csomor
ee6b1d97 5// Modified by:
a31a5f85 6// Created: 1998-01-01
ee6b1d97 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
ee6b1d97
SC
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
eb22f2a6 12#ifdef __GNUG__
e40298d5 13#pragma implementation "notebook.h"
eb22f2a6
GD
14#endif
15
ee6b1d97
SC
16// ============================================================================
17// declarations
18// ============================================================================
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
799690a0 23#include "wx/app.h"
d497dca4
GD
24#include "wx/string.h"
25#include "wx/log.h"
26#include "wx/imaglist.h"
27#include "wx/notebook.h"
28#include "wx/mac/uma.h"
ee6b1d97
SC
29// ----------------------------------------------------------------------------
30// macros
31// ----------------------------------------------------------------------------
32
33// check that the page index is valid
34#define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
35
dd47b3d3
SC
36static bool constantsSet = false ;
37
e40298d5
JS
38short kwxMacTabLeftMargin = 0 ;
39short kwxMacTabTopMargin = 0 ;
40short kwxMacTabRightMargin = 0 ;
41short kwxMacTabBottomMargin = 0 ;
ee6b1d97
SC
42
43// ----------------------------------------------------------------------------
44// event table
45// ----------------------------------------------------------------------------
46
47#if !USE_SHARED_LIBRARIES
5b781a67
SC
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
49DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
50
ee6b1d97 51BEGIN_EVENT_TABLE(wxNotebook, wxControl)
e40298d5
JS
52EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
53EVT_MOUSE_EVENTS(wxNotebook::OnMouse)
54
55EVT_SIZE(wxNotebook::OnSize)
56EVT_SET_FOCUS(wxNotebook::OnSetFocus)
57EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
ee6b1d97
SC
58END_EVENT_TABLE()
59
60IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
61IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
62#endif
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxNotebook construction
70// ----------------------------------------------------------------------------
71
72// common part of all ctors
73void wxNotebook::Init()
74{
dd47b3d3
SC
75 if ( !constantsSet )
76 {
77 if ( UMAHasAquaLayout() )
78 {
e40298d5 79 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
dd47b3d3
SC
80 kwxMacTabLeftMargin = 20 ;
81 kwxMacTabTopMargin = 38 ;
82 kwxMacTabRightMargin = 20 ;
83 kwxMacTabBottomMargin = 12 ;
84 }
85 else
86 {
87 kwxMacTabLeftMargin = 16 ;
88 kwxMacTabTopMargin = 30 ;
89 kwxMacTabRightMargin = 16 ;
90 kwxMacTabBottomMargin = 16 ;
91 }
92 constantsSet = true ;
93 }
94 if ( UMAHasAquaLayout() )
95 {
96 m_macHorizontalBorder = 7;
97 m_macVerticalBorder = 8;
98 }
e40298d5 99
ee6b1d97
SC
100 m_nSelection = -1;
101}
102
103// default for dynamic class
104wxNotebook::wxNotebook()
105{
106 Init();
107}
108
109// the same arguments as for wxControl
110wxNotebook::wxNotebook(wxWindow *parent,
111 wxWindowID id,
112 const wxPoint& pos,
113 const wxSize& size,
114 long style,
115 const wxString& name)
116{
117 Init();
e40298d5 118
ee6b1d97
SC
119 Create(parent, id, pos, size, style, name);
120}
121
122// Create() function
123bool wxNotebook::Create(wxWindow *parent,
124 wxWindowID id,
125 const wxPoint& pos,
126 const wxSize& size,
127 long style,
128 const wxString& name)
129{
e40298d5
JS
130 Rect bounds ;
131 Str255 title ;
132
133 MacPreControlCreate( parent , id , "" , pos , size ,style, wxDefaultValidator , name , &bounds , title ) ;
134
135 int tabstyle = kControlTabSmallNorthProc ;
136 if ( HasFlag(wxNB_LEFT) )
137 tabstyle = kControlTabSmallWestProc ;
138 else if ( HasFlag( wxNB_RIGHT ) )
139 tabstyle = kControlTabSmallEastProc ;
140 else if ( HasFlag( wxNB_BOTTOM ) )
141 tabstyle = kControlTabSmallSouthProc ;
142
143
144 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
145 tabstyle , (long) this ) ;
146
147 MacPostControlCreate() ;
148 return TRUE ;
ee6b1d97
SC
149}
150
151// dtor
152wxNotebook::~wxNotebook()
153{
e40298d5 154 m_macControl = NULL ;
ee6b1d97
SC
155}
156
60ec3e58
RR
157wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage)
158{
159 wxSize sizeTotal = sizePage;
160
161 int major,minor;
162 wxGetOsVersion( &major, &minor );
163
164 // Mac has large notebook borders. Aqua even more so.
e40298d5 165
60ec3e58
RR
166 if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) )
167 {
168 sizeTotal.x += 90;
169
170 if (major >= 10)
171 sizeTotal.y += 28;
172 else
173 sizeTotal.y += 20;
174 }
175 else
176 {
177 if (major >= 10)
178 {
179 sizeTotal.x += 34;
180 sizeTotal.y += 46;
181 }
182 else
183 {
184 sizeTotal.x += 22;
185 sizeTotal.y += 44;
186 }
187 }
e40298d5 188
60ec3e58
RR
189 return sizeTotal;
190}
191
ee6b1d97
SC
192// ----------------------------------------------------------------------------
193// wxNotebook accessors
194// ----------------------------------------------------------------------------
90b959ae
SC
195
196void wxNotebook::SetPadding(const wxSize& padding)
197{
e40298d5 198 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
90b959ae
SC
199}
200
201void wxNotebook::SetTabSize(const wxSize& sz)
ee6b1d97 202{
fc0daf84 203 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
ee6b1d97
SC
204}
205
90b959ae 206void wxNotebook::SetPageSize(const wxSize& size)
ee6b1d97 207{
fc0daf84 208 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
ee6b1d97
SC
209}
210
211int wxNotebook::SetSelection(int nPage)
212{
e40298d5
JS
213 if( !IS_VALID_PAGE(nPage) )
214 return m_nSelection ;
215
ee6b1d97 216 ChangePage(m_nSelection, nPage);
e40298d5
JS
217 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
218
2b5f62a0 219 Refresh();
ee6b1d97
SC
220 return m_nSelection;
221}
222
ee6b1d97
SC
223bool wxNotebook::SetPageText(int nPage, const wxString& strText)
224{
225 wxASSERT( IS_VALID_PAGE(nPage) );
e40298d5 226
90b959ae 227 wxNotebookPage *page = m_pages[nPage];
c854c7d9
GD
228 page->SetLabel(strText);
229 MacSetupTabs();
e40298d5 230
c854c7d9 231 return true;
ee6b1d97
SC
232}
233
234wxString wxNotebook::GetPageText(int nPage) const
235{
236 wxASSERT( IS_VALID_PAGE(nPage) );
e40298d5 237
90b959ae 238 wxNotebookPage *page = m_pages[nPage];
c854c7d9 239 return page->GetLabel();
ee6b1d97
SC
240}
241
242int wxNotebook::GetPageImage(int nPage) const
243{
fc0daf84 244 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
e40298d5 245
2b5f62a0 246 return m_images[nPage];
ee6b1d97
SC
247}
248
249bool wxNotebook::SetPageImage(int nPage, int nImage)
250{
fc0daf84 251 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
e40298d5 252
fc0daf84 253 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
e40298d5
JS
254 _T("invalid image index in SetPageImage()") );
255
2b5f62a0
VZ
256 if ( nImage != m_images[nPage] )
257 {
258 // if the item didn't have an icon before or, on the contrary, did have
259 // it but has lost it now, its size will change - but if the icon just
260 // changes, it won't
261 m_images[nPage] = nImage;
e40298d5
JS
262
263 MacSetupTabs() ;
2b5f62a0 264 }
e40298d5 265
2b5f62a0 266 return TRUE;
ee6b1d97
SC
267}
268
ee6b1d97
SC
269// ----------------------------------------------------------------------------
270// wxNotebook operations
271// ----------------------------------------------------------------------------
272
90b959ae
SC
273// remove one page from the notebook, without deleting the window
274wxNotebookPage* wxNotebook::DoRemovePage(int nPage)
ee6b1d97 275{
90b959ae
SC
276 wxCHECK( IS_VALID_PAGE(nPage), NULL );
277 wxNotebookPage* page = m_pages[nPage] ;
3ef585df 278 m_pages.RemoveAt(nPage);
e40298d5 279
c854c7d9 280 MacSetupTabs();
e40298d5 281
c854c7d9
GD
282 if(m_nSelection >= GetPageCount()) {
283 m_nSelection = GetPageCount() - 1;
284 }
285 if(m_nSelection >= 0) {
90b959ae 286 m_pages[m_nSelection]->Show(true);
c854c7d9 287 }
90b959ae 288 return page;
ee6b1d97
SC
289}
290
291// remove all pages
292bool wxNotebook::DeleteAllPages()
293{
294 // TODO: delete native widget pages
e40298d5 295
90b959ae 296 WX_CLEAR_ARRAY(m_pages) ;
c854c7d9 297 MacSetupTabs();
e40298d5 298
ee6b1d97
SC
299 return TRUE;
300}
301
ee6b1d97
SC
302
303// same as AddPage() but does it at given position
304bool wxNotebook::InsertPage(int nPage,
305 wxNotebookPage *pPage,
306 const wxString& strText,
307 bool bSelect,
308 int imageId)
309{
310 wxASSERT( pPage != NULL );
311 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
e40298d5 312
c854c7d9 313 pPage->SetLabel(strText);
e40298d5 314
ee6b1d97 315 // save the pointer to the page
90b959ae 316 m_pages.Insert(pPage, nPage);
e40298d5 317
2b5f62a0 318 m_images.Insert(imageId, nPage);
e40298d5 319
c854c7d9 320 MacSetupTabs();
e40298d5 321
c854c7d9 322 if ( bSelect ) {
ee6b1d97 323 m_nSelection = nPage;
c854c7d9
GD
324 }
325 else if ( m_nSelection == -1 ) {
ee6b1d97 326 m_nSelection = 0;
c854c7d9
GD
327 }
328 else if (m_nSelection >= nPage) {
329 m_nSelection++;
330 }
331 // don't show pages by default (we'll need to adjust their size first)
332 pPage->Show( false ) ;
e40298d5 333
c854c7d9
GD
334 int h, w;
335 GetSize(&w, &h);
336 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin,
e40298d5
JS
337 w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
338 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
c854c7d9
GD
339 if ( pPage->GetAutoLayout() ) {
340 pPage->Layout();
341 }
e40298d5 342
c854c7d9
GD
343 return true;
344}
345
346/* Added by Mark Newsam
e40298d5
JS
347* When a page is added or deleted to the notebook this function updates
348* information held in the m_macControl so that it matches the order
349* the user would expect.
350*/
c854c7d9
GD
351void wxNotebook::MacSetupTabs()
352{
467e3168 353 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
e40298d5 354
c854c7d9
GD
355 wxNotebookPage *page;
356 ControlTabInfoRec info;
e40298d5 357
c854c7d9
GD
358 for(int ii = 0; ii < GetPageCount(); ii++)
359 {
90b959ae 360 page = m_pages[ii];
c854c7d9
GD
361 info.version = 0;
362 info.iconSuiteID = 0;
363#if TARGET_CARBON
e40298d5 364 c2pstrcpy( (StringPtr) info.name , page->GetLabel() ) ;
c854c7d9 365#else
e40298d5
JS
366 strcpy( (char *) info.name , page->GetLabel() ) ;
367 c2pstr( (char *) info.name ) ;
c854c7d9 368#endif
76a5e5d2 369 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
e40298d5 370 sizeof( ControlTabInfoRec) , (char*) &info ) ;
2b5f62a0 371 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
3f0fac91 372#if 0 // TARGET_CARBON
2b5f62a0
VZ
373 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
374 {
e40298d5
JS
375 // tab controls only support very specific types of images, therefore we are doing an odyssee
376 // accross the icon worlds (even Apple DTS did not find a shorter path)
377 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
378 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
379 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
380 // of static counter
381 ControlButtonContentInfo info ;
382 wxMacCreateBitmapButton( &info , *GetImageList()->GetBitmap( GetPageImage(ii ) ) , kControlContentPictHandle) ;
383 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
384 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
2b5f62a0 385 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
e40298d5
JS
386 IconRef iconRef ;
387 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1 , iconFamily, &iconRef ) ;
2b5f62a0 388 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
e40298d5
JS
389 info.contentType = kControlContentIconRef ;
390 info.u.iconRef = iconRef ;
391 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
392 sizeof( info ), (Ptr)&info );
2b5f62a0 393 wxASSERT_MSG( err == noErr , "Error when setting icon on tab" ) ;
e40298d5 394 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
2b5f62a0
VZ
395 ReleaseIconRef( iconRef ) ;
396 DisposeHandle( (Handle) iconFamily ) ;
397 }
398#endif
c854c7d9
GD
399 }
400 Rect bounds;
76a5e5d2
SC
401 GetControlBounds((ControlHandle)m_macControl, &bounds);
402 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
ee6b1d97
SC
403}
404
405// ----------------------------------------------------------------------------
406// wxNotebook callbacks
407// ----------------------------------------------------------------------------
408
409// @@@ OnSize() is used for setting the font when it's called for the first
410// time because doing it in ::Create() doesn't work (for unknown reasons)
411void wxNotebook::OnSize(wxSizeEvent& event)
412{
ee6b1d97
SC
413 // emulate page change (it's esp. important to do it first time because
414 // otherwise our page would stay invisible)
415 int nSel = m_nSelection;
416 m_nSelection = -1;
417 SetSelection(nSel);
e40298d5 418
ee6b1d97
SC
419 // fit the notebook page to the tab control's display area
420 int w, h;
421 GetSize(&w, &h);
e40298d5 422
90b959ae 423 unsigned int nCount = m_pages.Count();
ee6b1d97 424 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
90b959ae 425 wxNotebookPage *pPage = m_pages[nPage];
c854c7d9 426 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin,
e40298d5
JS
427 w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
428 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
c854c7d9 429 if ( pPage->GetAutoLayout() ) {
ee6b1d97 430 pPage->Layout();
c854c7d9 431 }
ee6b1d97 432 }
e40298d5 433
ee6b1d97
SC
434 // Processing continues to next OnSize
435 event.Skip();
436}
437
438void wxNotebook::OnSelChange(wxNotebookEvent& event)
439{
440 // is it our tab control?
441 if ( event.GetEventObject() == this )
442 ChangePage(event.GetOldSelection(), event.GetSelection());
e40298d5 443
ee6b1d97
SC
444 // we want to give others a chance to process this message as well
445 event.Skip();
446}
447
448void wxNotebook::OnSetFocus(wxFocusEvent& event)
449{
450 // set focus to the currently selected page if any
451 if ( m_nSelection != -1 )
90b959ae 452 m_pages[m_nSelection]->SetFocus();
e40298d5 453
ee6b1d97
SC
454 event.Skip();
455}
456
457void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
458{
459 if ( event.IsWindowChange() ) {
460 // change pages
461 AdvanceSelection(event.GetDirection());
462 }
463 else {
464 // pass to the parent
465 if ( GetParent() ) {
466 event.SetCurrentFocus(this);
467 GetParent()->ProcessEvent(event);
468 }
469 }
470}
471
472// ----------------------------------------------------------------------------
473// wxNotebook base class virtuals
474// ----------------------------------------------------------------------------
475
476// override these 2 functions to do nothing: everything is done in OnSize
477
478void wxNotebook::SetConstraintSizes(bool /* recurse */)
479{
480 // don't set the sizes of the pages - their correct size is not yet known
481 wxControl::SetConstraintSizes(FALSE);
482}
483
484bool wxNotebook::DoPhase(int /* nPhase */)
485{
486 return TRUE;
487}
488
489void wxNotebook::Command(wxCommandEvent& event)
490{
491 wxFAIL_MSG("wxNotebook::Command not implemented");
492}
493
494// ----------------------------------------------------------------------------
495// wxNotebook helper functions
496// ----------------------------------------------------------------------------
497
498// hide the currently active panel and show the new one
499void wxNotebook::ChangePage(int nOldSel, int nSel)
500{
c854c7d9
GD
501 // it's not an error (the message may be generated by the tab control itself)
502 // and it may happen - just do nothing
503 if ( nSel == nOldSel )
504 {
90b959ae 505 wxNotebookPage *pPage = m_pages[nSel];
c854c7d9
GD
506 pPage->Show(FALSE);
507 pPage->Show(TRUE);
508 pPage->SetFocus();
509 return;
510 }
e40298d5 511
c854c7d9 512 // Hide previous page
ee6b1d97 513 if ( nOldSel != -1 ) {
90b959ae 514 m_pages[nOldSel]->Show(FALSE);
ee6b1d97 515 }
e40298d5 516
90b959ae 517 wxNotebookPage *pPage = m_pages[nSel];
ee6b1d97
SC
518 pPage->Show(TRUE);
519 pPage->SetFocus();
e40298d5 520
ee6b1d97
SC
521 m_nSelection = nSel;
522}
523
799690a0
SC
524
525void wxNotebook::OnMouse( wxMouseEvent &event )
526{
e40298d5
JS
527 if ( (ControlHandle) m_macControl == NULL )
528 {
529 event.Skip() ;
530 return ;
531 }
532
533 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
534 {
535 int x = event.m_x ;
536 int y = event.m_y ;
537
538 MacClientToRootWindow( &x , &y ) ;
539
540 ControlHandle control ;
541 Point localwhere ;
542 SInt16 controlpart ;
543
544 localwhere.h = x ;
545 localwhere.v = y ;
546
547 short modifiers = 0;
548
549 if ( !event.m_leftDown && !event.m_rightDown )
550 modifiers |= btnState ;
551
552 if ( event.m_shiftDown )
553 modifiers |= shiftKey ;
554
555 if ( event.m_controlDown )
556 modifiers |= controlKey ;
557
558 if ( event.m_altDown )
559 modifiers |= optionKey ;
560
561 if ( event.m_metaDown )
562 modifiers |= cmdKey ;
563
564 control = (ControlHandle) m_macControl ;
565 if ( control && ::IsControlActive( control ) )
566 {
567 {
568 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
569 ::GetControl32BitValue(control) - 1, m_nSelection);
570 changing.SetEventObject(this);
571 ProcessEvent(changing);
572
573 if(changing.IsAllowed())
574 {
575 controlpart = ::HandleControlClick(control, localwhere, modifiers,
576 (ControlActionUPP) -1);
577 wxTheApp->s_lastMouseDown = 0 ;
578
579 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
580 ::GetControl32BitValue(control) - 1, m_nSelection);
581 event.SetEventObject(this);
582
583 ProcessEvent(event);
584 }
585 }
586 }
587 }
799690a0 588}
e40298d5 589
799690a0 590
76a5e5d2 591void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
ee6b1d97 592{
799690a0 593#if 0
e40298d5
JS
594 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
595 event.SetEventObject(this);
596
597 ProcessEvent(event);
799690a0 598#endif
ee6b1d97
SC
599}
600