]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/notebmac.cpp
don't translate strings when it doesn't make any sense
[wxWidgets.git] / src / mac / carbon / notebmac.cpp
CommitLineData
ee6b1d97
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: notebook.cpp
3// Purpose: implementation of wxNotebook
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#ifdef __GNUG__
3ef585df 20 #pragma implementation "notebook.h"
ee6b1d97
SC
21#endif
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
38 short kwxMacTabLeftMargin = 0 ;
39 short kwxMacTabTopMargin = 0 ;
40 short kwxMacTabRightMargin = 0 ;
41 short 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
SC
51BEGIN_EVENT_TABLE(wxNotebook, wxControl)
52 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
799690a0
SC
53 EVT_MOUSE_EVENTS(wxNotebook::OnMouse)
54
ee6b1d97
SC
55 EVT_SIZE(wxNotebook::OnSize)
56 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
57 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
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 {
79 // I got these values for Mac OS X from the Appearance mgr docs. (Mark Newsam)
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 }
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();
118
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{
130 Rect bounds ;
131 Str255 title ;
132
133 MacPreControlCreate( parent , id , "" , pos , size ,style, wxDefaultValidator , name , &bounds , title ) ;
134
2b5f62a0
VZ
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
76a5e5d2 144 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
2b5f62a0 145 tabstyle , (long) this ) ;
ee6b1d97
SC
146
147 MacPostControlCreate() ;
148 return TRUE ;
149}
150
151// dtor
152wxNotebook::~wxNotebook()
153{
154 m_macControl = NULL ;
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.
165
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 }
188
189 return sizeTotal;
190}
191
ee6b1d97
SC
192// ----------------------------------------------------------------------------
193// wxNotebook accessors
194// ----------------------------------------------------------------------------
90b959ae
SC
195
196void wxNotebook::SetPadding(const wxSize& padding)
197{
fc0daf84 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{
9453cf2b
SC
213 if( !IS_VALID_PAGE(nPage) )
214 return m_nSelection ;
ee6b1d97
SC
215
216 ChangePage(m_nSelection, nPage);
467e3168 217 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
c854c7d9 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) );
226
90b959ae 227 wxNotebookPage *page = m_pages[nPage];
c854c7d9
GD
228 page->SetLabel(strText);
229 MacSetupTabs();
230
231 return true;
ee6b1d97
SC
232}
233
234wxString wxNotebook::GetPageText(int nPage) const
235{
236 wxASSERT( IS_VALID_PAGE(nPage) );
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") );
ee6b1d97 245
2b5f62a0 246 return m_images[nPage];
ee6b1d97
SC
247}
248
249bool wxNotebook::SetPageImage(int nPage, int nImage)
250{
fc0daf84
SC
251 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
252
253 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
254 _T("invalid image index in SetPageImage()") );
ee6b1d97 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;
262
263 MacSetupTabs() ;
264 }
265
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);
ee6b1d97 279
c854c7d9
GD
280 MacSetupTabs();
281
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
295
90b959ae 296 WX_CLEAR_ARRAY(m_pages) ;
c854c7d9
GD
297 MacSetupTabs();
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 );
312
c854c7d9 313 pPage->SetLabel(strText);
ee6b1d97
SC
314
315 // save the pointer to the page
90b959ae 316 m_pages.Insert(pPage, nPage);
ee6b1d97 317
2b5f62a0
VZ
318 m_images.Insert(imageId, nPage);
319
c854c7d9
GD
320 MacSetupTabs();
321
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 ) ;
ee6b1d97 333
c854c7d9
GD
334 int h, w;
335 GetSize(&w, &h);
336 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin,
337 w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
338 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
339 if ( pPage->GetAutoLayout() ) {
340 pPage->Layout();
341 }
ee6b1d97 342
c854c7d9
GD
343 return true;
344}
345
346/* Added by Mark Newsam
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 */
351void wxNotebook::MacSetupTabs()
352{
467e3168 353 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
c854c7d9
GD
354
355 wxNotebookPage *page;
356 ControlTabInfoRec info;
2b5f62a0
VZ
357
358 OSStatus err = noErr ;
c854c7d9
GD
359 for(int ii = 0; ii < GetPageCount(); ii++)
360 {
90b959ae 361 page = m_pages[ii];
c854c7d9
GD
362 info.version = 0;
363 info.iconSuiteID = 0;
364#if TARGET_CARBON
365 c2pstrcpy( (StringPtr) info.name , page->GetLabel() ) ;
366#else
367 strcpy( (char *) info.name , page->GetLabel() ) ;
368 c2pstr( (char *) info.name ) ;
369#endif
76a5e5d2 370 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
c854c7d9 371 sizeof( ControlTabInfoRec) , (char*) &info ) ;
2b5f62a0
VZ
372 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
373
374#if TARGET_CARBON
375 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
376 {
377 // tab controls only support very specific types of images, therefore we are doing an odyssee
378 // accross the icon worlds (even Apple DTS did not find a shorter path)
379 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
380 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
381 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
382 // of static counter
383 ControlButtonContentInfo info ;
384 wxMacCreateBitmapButton( &info , *GetImageList()->GetBitmap( GetPageImage(ii ) ) , kControlContentPictHandle) ;
385 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
386 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
387 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
388 IconRef iconRef ;
389 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1 , iconFamily, &iconRef ) ;
390 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
391 info.contentType = kControlContentIconRef ;
392 info.u.iconRef = iconRef ;
393 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
394 sizeof( info ), (Ptr)&info );
395 wxASSERT_MSG( err == noErr , "Error when setting icon on tab" ) ;
396 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
397 ReleaseIconRef( iconRef ) ;
398 DisposeHandle( (Handle) iconFamily ) ;
399 }
400#endif
c854c7d9
GD
401 }
402 Rect bounds;
76a5e5d2
SC
403 GetControlBounds((ControlHandle)m_macControl, &bounds);
404 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
ee6b1d97
SC
405}
406
407// ----------------------------------------------------------------------------
408// wxNotebook callbacks
409// ----------------------------------------------------------------------------
410
411// @@@ OnSize() is used for setting the font when it's called for the first
412// time because doing it in ::Create() doesn't work (for unknown reasons)
413void wxNotebook::OnSize(wxSizeEvent& event)
414{
ee6b1d97
SC
415 // emulate page change (it's esp. important to do it first time because
416 // otherwise our page would stay invisible)
417 int nSel = m_nSelection;
418 m_nSelection = -1;
419 SetSelection(nSel);
420
421 // fit the notebook page to the tab control's display area
422 int w, h;
423 GetSize(&w, &h);
424
90b959ae 425 unsigned int nCount = m_pages.Count();
ee6b1d97 426 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
90b959ae 427 wxNotebookPage *pPage = m_pages[nPage];
c854c7d9
GD
428 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin,
429 w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
430 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
431 if ( pPage->GetAutoLayout() ) {
ee6b1d97 432 pPage->Layout();
c854c7d9 433 }
ee6b1d97
SC
434 }
435
436 // Processing continues to next OnSize
437 event.Skip();
438}
439
440void wxNotebook::OnSelChange(wxNotebookEvent& event)
441{
442 // is it our tab control?
443 if ( event.GetEventObject() == this )
444 ChangePage(event.GetOldSelection(), event.GetSelection());
445
446 // we want to give others a chance to process this message as well
447 event.Skip();
448}
449
450void wxNotebook::OnSetFocus(wxFocusEvent& event)
451{
452 // set focus to the currently selected page if any
453 if ( m_nSelection != -1 )
90b959ae 454 m_pages[m_nSelection]->SetFocus();
ee6b1d97
SC
455
456 event.Skip();
457}
458
459void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
460{
461 if ( event.IsWindowChange() ) {
462 // change pages
463 AdvanceSelection(event.GetDirection());
464 }
465 else {
466 // pass to the parent
467 if ( GetParent() ) {
468 event.SetCurrentFocus(this);
469 GetParent()->ProcessEvent(event);
470 }
471 }
472}
473
474// ----------------------------------------------------------------------------
475// wxNotebook base class virtuals
476// ----------------------------------------------------------------------------
477
478// override these 2 functions to do nothing: everything is done in OnSize
479
480void wxNotebook::SetConstraintSizes(bool /* recurse */)
481{
482 // don't set the sizes of the pages - their correct size is not yet known
483 wxControl::SetConstraintSizes(FALSE);
484}
485
486bool wxNotebook::DoPhase(int /* nPhase */)
487{
488 return TRUE;
489}
490
491void wxNotebook::Command(wxCommandEvent& event)
492{
493 wxFAIL_MSG("wxNotebook::Command not implemented");
494}
495
496// ----------------------------------------------------------------------------
497// wxNotebook helper functions
498// ----------------------------------------------------------------------------
499
500// hide the currently active panel and show the new one
501void wxNotebook::ChangePage(int nOldSel, int nSel)
502{
c854c7d9
GD
503 // it's not an error (the message may be generated by the tab control itself)
504 // and it may happen - just do nothing
505 if ( nSel == nOldSel )
506 {
90b959ae 507 wxNotebookPage *pPage = m_pages[nSel];
c854c7d9
GD
508 pPage->Show(FALSE);
509 pPage->Show(TRUE);
510 pPage->SetFocus();
511 return;
512 }
ee6b1d97 513
c854c7d9 514 // Hide previous page
ee6b1d97 515 if ( nOldSel != -1 ) {
90b959ae 516 m_pages[nOldSel]->Show(FALSE);
ee6b1d97
SC
517 }
518
90b959ae 519 wxNotebookPage *pPage = m_pages[nSel];
ee6b1d97
SC
520 pPage->Show(TRUE);
521 pPage->SetFocus();
522
523 m_nSelection = nSel;
524}
525
799690a0
SC
526
527void wxNotebook::OnMouse( wxMouseEvent &event )
528{
529 if ( (ControlHandle) m_macControl == NULL )
530 {
531 event.Skip() ;
532 return ;
533 }
534
535 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
536 {
537 int x = event.m_x ;
538 int y = event.m_y ;
539
540 MacClientToRootWindow( &x , &y ) ;
541
542 ControlHandle control ;
543 Point localwhere ;
544 SInt16 controlpart ;
545
546 localwhere.h = x ;
547 localwhere.v = y ;
548
549 short modifiers = 0;
550
551 if ( !event.m_leftDown && !event.m_rightDown )
552 modifiers |= btnState ;
553
554 if ( event.m_shiftDown )
555 modifiers |= shiftKey ;
556
557 if ( event.m_controlDown )
558 modifiers |= controlKey ;
559
560 if ( event.m_altDown )
561 modifiers |= optionKey ;
562
563 if ( event.m_metaDown )
564 modifiers |= cmdKey ;
565
566 control = (ControlHandle) m_macControl ;
567 if ( control && ::IsControlActive( control ) )
568 {
569 {
570 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
467e3168 571 ::GetControl32BitValue(control) - 1, m_nSelection);
799690a0
SC
572 changing.SetEventObject(this);
573 ProcessEvent(changing);
574
575 if(changing.IsAllowed())
576 {
577 controlpart = ::HandleControlClick(control, localwhere, modifiers,
578 (ControlActionUPP) -1);
579 wxTheApp->s_lastMouseDown = 0 ;
580
581 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
467e3168 582 ::GetControl32BitValue(control) - 1, m_nSelection);
799690a0
SC
583 event.SetEventObject(this);
584
585 ProcessEvent(event);
586 }
587 }
588 }
589 }
590}
591
592
76a5e5d2 593void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
ee6b1d97 594{
799690a0 595#if 0
467e3168 596 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
ee6b1d97
SC
597 event.SetEventObject(this);
598
599 ProcessEvent(event);
799690a0 600#endif
ee6b1d97
SC
601}
602