]> git.saurik.com Git - wxWidgets.git/blob - src/mac/notebmac.cpp
94f142fb03dad9c0eb7d2c949825ffdb1825ea9e
[wxWidgets.git] / src / mac / 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 }
206
207 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
208 {
209 wxSize sizeTotal = sizePage;
210
211 int major,minor;
212 wxGetOsVersion( &major, &minor );
213
214 // Mac has large notebook borders. Aqua even more so.
215
216 if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) )
217 {
218 sizeTotal.x += 90;
219
220 if (major >= 10)
221 sizeTotal.y += 28;
222 else
223 sizeTotal.y += 20;
224 }
225 else
226 {
227 if (major >= 10)
228 {
229 sizeTotal.x += 34;
230 sizeTotal.y += 46;
231 }
232 else
233 {
234 sizeTotal.x += 22;
235 sizeTotal.y += 44;
236 }
237 }
238
239 return sizeTotal;
240 }
241
242 // ----------------------------------------------------------------------------
243 // wxNotebook accessors
244 // ----------------------------------------------------------------------------
245
246 void wxNotebook::SetPadding(const wxSize& padding)
247 {
248 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
249 }
250
251 void wxNotebook::SetTabSize(const wxSize& sz)
252 {
253 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
254 }
255
256 void wxNotebook::SetPageSize(const wxSize& size)
257 {
258 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
259 }
260
261 int wxNotebook::SetSelection(size_t nPage)
262 {
263 if( !IS_VALID_PAGE(nPage) )
264 return m_nSelection ;
265
266 ChangePage(m_nSelection, nPage);
267 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
268
269 Refresh();
270 return m_nSelection;
271 }
272
273 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
274 {
275 wxASSERT( IS_VALID_PAGE(nPage) );
276
277 wxNotebookPage *page = m_pages[nPage];
278 page->SetLabel(strText);
279 MacSetupTabs();
280
281 return true;
282 }
283
284 wxString wxNotebook::GetPageText(size_t nPage) const
285 {
286 wxASSERT( IS_VALID_PAGE(nPage) );
287
288 wxNotebookPage *page = m_pages[nPage];
289 return page->GetLabel();
290 }
291
292 int wxNotebook::GetPageImage(size_t nPage) const
293 {
294 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
295
296 return m_images[nPage];
297 }
298
299 bool wxNotebook::SetPageImage(size_t nPage , int nImage)
300 {
301 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
302
303 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
304 _T("invalid image index in SetPageImage()") );
305
306 if ( nImage != m_images[nPage] )
307 {
308 // if the item didn't have an icon before or, on the contrary, did have
309 // it but has lost it now, its size will change - but if the icon just
310 // changes, it won't
311 m_images[nPage] = nImage;
312
313 MacSetupTabs() ;
314 }
315
316 return TRUE;
317 }
318
319 // ----------------------------------------------------------------------------
320 // wxNotebook operations
321 // ----------------------------------------------------------------------------
322
323 // remove one page from the notebook, without deleting the window
324 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
325 {
326 wxCHECK( IS_VALID_PAGE(nPage), NULL );
327 wxNotebookPage* page = m_pages[nPage] ;
328 m_pages.RemoveAt(nPage);
329
330 MacSetupTabs();
331
332 if(m_nSelection >= GetPageCount()) {
333 m_nSelection = GetPageCount() - 1;
334 }
335 if(m_nSelection >= 0) {
336 m_pages[m_nSelection]->Show(true);
337 }
338 return page;
339 }
340
341 // remove all pages
342 bool wxNotebook::DeleteAllPages()
343 {
344 // TODO: delete native widget pages
345
346 WX_CLEAR_ARRAY(m_pages) ;
347 MacSetupTabs();
348
349 return TRUE;
350 }
351
352
353 // same as AddPage() but does it at given position
354 bool wxNotebook::InsertPage(size_t nPage,
355 wxNotebookPage *pPage,
356 const wxString& strText,
357 bool bSelect,
358 int imageId)
359 {
360 wxASSERT( pPage != NULL );
361 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
362
363 pPage->SetLabel(strText);
364
365 // save the pointer to the page
366 m_pages.Insert(pPage, nPage);
367
368 m_images.Insert(imageId, nPage);
369
370 MacSetupTabs();
371
372 if ( bSelect ) {
373 m_nSelection = nPage;
374 }
375 else if ( m_nSelection == -1 ) {
376 m_nSelection = 0;
377 }
378 else if (m_nSelection >= nPage) {
379 m_nSelection++;
380 }
381 // don't show pages by default (we'll need to adjust their size first)
382 pPage->Show( false ) ;
383
384 int h, w;
385 GetSize(&w, &h);
386 pPage->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder,
387 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder,
388 w - wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder,
389 h - wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder);
390 if ( pPage->GetAutoLayout() ) {
391 pPage->Layout();
392 }
393
394 return true;
395 }
396
397 /* Added by Mark Newsam
398 * When a page is added or deleted to the notebook this function updates
399 * information held in the m_macControl so that it matches the order
400 * the user would expect.
401 */
402 void wxNotebook::MacSetupTabs()
403 {
404 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
405
406 wxNotebookPage *page;
407 ControlTabInfoRec info;
408
409 for(int ii = 0; ii < GetPageCount(); ii++)
410 {
411 page = m_pages[ii];
412 info.version = 0;
413 info.iconSuiteID = 0;
414 wxMacStringToPascal( page->GetLabel() , info.name ) ;
415
416 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
417 sizeof( ControlTabInfoRec) , (char*) &info ) ;
418 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
419 #if TARGET_CARBON
420 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
421 {
422 // tab controls only support very specific types of images, therefore we are doing an odyssee
423 // accross the icon worlds (even Apple DTS did not find a shorter path)
424 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
425 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
426 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
427 // of static counter
428 ControlButtonContentInfo info ;
429 wxMacCreateBitmapButton( &info , *GetImageList()->GetBitmap( GetPageImage(ii ) ) , kControlContentPictHandle) ;
430 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
431 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
432 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
433 IconRef iconRef ;
434 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1 , iconFamily, &iconRef ) ;
435 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
436 info.contentType = kControlContentIconRef ;
437 info.u.iconRef = iconRef ;
438 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
439 sizeof( info ), (Ptr)&info );
440 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
441 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
442 ReleaseIconRef( iconRef ) ;
443 DisposeHandle( (Handle) iconFamily ) ;
444 }
445 #endif
446 }
447 Rect bounds;
448 GetControlBounds((ControlHandle)m_macControl, &bounds);
449 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
450 }
451
452 // ----------------------------------------------------------------------------
453 // wxNotebook callbacks
454 // ----------------------------------------------------------------------------
455
456 // @@@ OnSize() is used for setting the font when it's called for the first
457 // time because doing it in ::Create() doesn't work (for unknown reasons)
458 void wxNotebook::OnSize(wxSizeEvent& event)
459 {
460 // emulate page change (it's esp. important to do it first time because
461 // otherwise our page would stay invisible)
462 int nSel = m_nSelection;
463 m_nSelection = -1;
464 SetSelection(nSel);
465
466 // fit the notebook page to the tab control's display area
467 int w, h;
468 GetSize(&w, &h);
469
470 unsigned int nCount = m_pages.Count();
471 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
472 wxNotebookPage *pPage = m_pages[nPage];
473 pPage->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder,
474 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder,
475 w - wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder,
476 h - wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder);
477 if ( pPage->GetAutoLayout() ) {
478 pPage->Layout();
479 }
480 }
481
482 // Processing continues to next OnSize
483 event.Skip();
484 }
485
486 void wxNotebook::OnSelChange(wxNotebookEvent& event)
487 {
488 // is it our tab control?
489 if ( event.GetEventObject() == this )
490 ChangePage(event.GetOldSelection(), event.GetSelection());
491
492 // we want to give others a chance to process this message as well
493 event.Skip();
494 }
495
496 void wxNotebook::OnSetFocus(wxFocusEvent& event)
497 {
498 // set focus to the currently selected page if any
499 if ( m_nSelection != -1 )
500 m_pages[m_nSelection]->SetFocus();
501
502 event.Skip();
503 }
504
505 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
506 {
507 if ( event.IsWindowChange() ) {
508 // change pages
509 AdvanceSelection(event.GetDirection());
510 }
511 else {
512 // pass to the parent
513 if ( GetParent() ) {
514 event.SetCurrentFocus(this);
515 GetParent()->ProcessEvent(event);
516 }
517 }
518 }
519
520 // ----------------------------------------------------------------------------
521 // wxNotebook base class virtuals
522 // ----------------------------------------------------------------------------
523
524 // override these 2 functions to do nothing: everything is done in OnSize
525
526 void wxNotebook::SetConstraintSizes(bool /* recurse */)
527 {
528 // don't set the sizes of the pages - their correct size is not yet known
529 wxControl::SetConstraintSizes(FALSE);
530 }
531
532 bool wxNotebook::DoPhase(int /* nPhase */)
533 {
534 return TRUE;
535 }
536
537 void wxNotebook::Command(wxCommandEvent& event)
538 {
539 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
540 }
541
542 // ----------------------------------------------------------------------------
543 // wxNotebook helper functions
544 // ----------------------------------------------------------------------------
545
546 // hide the currently active panel and show the new one
547 void wxNotebook::ChangePage(int nOldSel, int nSel)
548 {
549 // it's not an error (the message may be generated by the tab control itself)
550 // and it may happen - just do nothing
551 if ( nSel == nOldSel )
552 {
553 wxNotebookPage *pPage = m_pages[nSel];
554 pPage->Show(FALSE);
555 pPage->Show(TRUE);
556 pPage->SetFocus();
557 return;
558 }
559
560 // Hide previous page
561 if ( nOldSel != -1 ) {
562 m_pages[nOldSel]->Show(FALSE);
563 }
564
565 wxNotebookPage *pPage = m_pages[nSel];
566 pPage->Show(TRUE);
567 pPage->SetFocus();
568
569 m_nSelection = nSel;
570 }
571
572
573 void wxNotebook::OnMouse( wxMouseEvent &event )
574 {
575 if ( (ControlHandle) m_macControl == NULL )
576 {
577 event.Skip() ;
578 return ;
579 }
580
581 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
582 {
583 int x = event.m_x ;
584 int y = event.m_y ;
585
586 MacClientToRootWindow( &x , &y ) ;
587
588 ControlHandle control ;
589 Point localwhere ;
590 SInt16 controlpart ;
591
592 localwhere.h = x ;
593 localwhere.v = y ;
594
595 short modifiers = 0;
596
597 if ( !event.m_leftDown && !event.m_rightDown )
598 modifiers |= btnState ;
599
600 if ( event.m_shiftDown )
601 modifiers |= shiftKey ;
602
603 if ( event.m_controlDown )
604 modifiers |= controlKey ;
605
606 if ( event.m_altDown )
607 modifiers |= optionKey ;
608
609 if ( event.m_metaDown )
610 modifiers |= cmdKey ;
611
612 control = (ControlHandle) m_macControl ;
613 if ( control && ::IsControlActive( control ) )
614 {
615 {
616 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
617 ::GetControl32BitValue(control) - 1, m_nSelection);
618 changing.SetEventObject(this);
619 GetEventHandler()->ProcessEvent(changing);
620
621 if(changing.IsAllowed())
622 {
623 controlpart = ::HandleControlClick(control, localwhere, modifiers,
624 (ControlActionUPP) -1);
625 wxTheApp->s_lastMouseDown = 0 ;
626
627 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
628 ::GetControl32BitValue(control) - 1, m_nSelection);
629 event.SetEventObject(this);
630
631 GetEventHandler()->ProcessEvent(event);
632 }
633 }
634 }
635 }
636 }
637
638
639 void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
640 {
641 #if 0
642 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
643 event.SetEventObject(this);
644
645 ProcessEvent(event);
646 #endif
647 }
648