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