]> git.saurik.com Git - wxWidgets.git/blob - src/mac/notebmac.cpp
selection for DeleteAllPages corrected, images scaleing in order to avoid crashes...
[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/image.h"
28 #include "wx/notebook.h"
29 #include "wx/mac/uma.h"
30 // ----------------------------------------------------------------------------
31 // macros
32 // ----------------------------------------------------------------------------
33
34 // check that the page index is valid
35 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
36
37
38 // ----------------------------------------------------------------------------
39 // event table
40 // ----------------------------------------------------------------------------
41
42 #if !USE_SHARED_LIBRARIES
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
45
46 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
47 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
48 EVT_MOUSE_EVENTS(wxNotebook::OnMouse)
49
50 EVT_SIZE(wxNotebook::OnSize)
51 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
52 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
53 END_EVENT_TABLE()
54
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
56 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
57 #endif
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // The Appearance Manager docs show using tab controls in either edge to edge
64 // mode, or inset. I think edge to edge conforms better to the other ports,
65 // and inset mode is better accomplished with space around the wxNotebook rather
66 // than within it. --Robin
67 #define wxMAC_EDGE_TO_EDGE 1
68
69 static inline int wxMacTabMargin(long nbStyle, long side)
70 {
71 static int tabMargin = -1;
72 static int otherMargin = -1;
73
74 if ( tabMargin == -1)
75 {
76 if ( UMAHasAquaLayout() )
77 {
78 tabMargin = 26; // From Appearance Manager docs for small tab control dimensions
79 #if wxMAC_EDGE_TO_EDGE
80 otherMargin = 0;
81 #else
82 otherMargin = 20;
83 #endif
84 }
85 else
86 {
87 tabMargin = 30;
88 #if wxMAC_EDGE_TO_EDGE
89 otherMargin = 0;
90 #else
91 otherMargin = 16;
92 #endif
93 }
94 }
95
96 // If the style matches the side asked for then return the tab margin,
97 // but we have to special case wxNB_TOP since it is zero...
98 if ( side == wxNB_TOP)
99 {
100 if ( nbStyle != 0 && nbStyle & (wxNB_LEFT|wxNB_RIGHT|wxNB_BOTTOM))
101 {
102 return otherMargin;
103 }
104 else
105 {
106 return tabMargin;
107 }
108 }
109 else if ( nbStyle & side)
110 return tabMargin;
111 else
112 return otherMargin;
113 }
114
115 static inline int wxMacTabLeftMargin(long style)
116 {
117 return wxMacTabMargin(style, wxNB_LEFT);
118 }
119
120 static inline int wxMacTabTopMargin(long style)
121 {
122 return wxMacTabMargin(style, wxNB_TOP);
123 }
124
125 static inline int wxMacTabRightMargin(long style)
126 {
127 return wxMacTabMargin(style, wxNB_RIGHT);
128 }
129
130 static inline int wxMacTabBottomMargin(long style)
131 {
132 return wxMacTabMargin(style, wxNB_BOTTOM);
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxNotebook construction
137 // ----------------------------------------------------------------------------
138
139 // common part of all ctors
140 void wxNotebook::Init()
141 {
142 if ( UMAHasAquaLayout() )
143 {
144 // Should these depend on wxMAC_EDGE_TO_EDGE too?
145 m_macHorizontalBorder = 7;
146 m_macVerticalBorder = 8;
147 }
148
149 m_nSelection = -1;
150 }
151
152 // default for dynamic class
153 wxNotebook::wxNotebook()
154 {
155 Init();
156 }
157
158 // the same arguments as for wxControl
159 wxNotebook::wxNotebook(wxWindow *parent,
160 wxWindowID id,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
165 {
166 Init();
167
168 Create(parent, id, pos, size, style, name);
169 }
170
171 // Create() function
172 bool wxNotebook::Create(wxWindow *parent,
173 wxWindowID id,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxString& name)
178 {
179 if ( !wxNotebookBase::Create(parent, id, pos, size, style, name) )
180 return false;
181
182 Rect bounds ;
183 Str255 title ;
184
185 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, wxDefaultValidator , name , &bounds , title ) ;
186
187 int tabstyle = kControlTabSmallNorthProc ;
188 if ( HasFlag(wxNB_LEFT) )
189 tabstyle = kControlTabSmallWestProc ;
190 else if ( HasFlag( wxNB_RIGHT ) )
191 tabstyle = kControlTabSmallEastProc ;
192 else if ( HasFlag( wxNB_BOTTOM ) )
193 tabstyle = kControlTabSmallSouthProc ;
194
195
196 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
197 tabstyle , (long) this ) ;
198
199 MacPostControlCreate() ;
200 return TRUE ;
201 }
202
203 // dtor
204 wxNotebook::~wxNotebook()
205 {
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(size_t 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(size_t 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(size_t 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(size_t 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(size_t 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(size_t 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 >= (int)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 WX_CLEAR_ARRAY(m_pages) ;
346 MacSetupTabs();
347 m_nSelection = -1 ;
348 return TRUE;
349 }
350
351
352 // same as AddPage() but does it at given position
353 bool wxNotebook::InsertPage(size_t nPage,
354 wxNotebookPage *pPage,
355 const wxString& strText,
356 bool bSelect,
357 int imageId)
358 {
359 if ( !wxNotebookBase::InsertPage(nPage, pPage, strText, bSelect, imageId) )
360 return false;
361
362 pPage->SetLabel(strText);
363
364 m_images.Insert(imageId, nPage);
365
366 MacSetupTabs();
367
368 if ( bSelect ) {
369 m_nSelection = nPage;
370 }
371 else if ( m_nSelection == -1 ) {
372 m_nSelection = 0;
373 }
374 else if ((size_t)m_nSelection >= nPage) {
375 m_nSelection++;
376 }
377 // don't show pages by default (we'll need to adjust their size first)
378 pPage->Show( false ) ;
379
380 int h, w;
381 GetSize(&w, &h);
382 pPage->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder,
383 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder,
384 w - wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder,
385 h - wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder);
386 if ( pPage->GetAutoLayout() ) {
387 pPage->Layout();
388 }
389
390 return true;
391 }
392
393 /* Added by Mark Newsam
394 * When a page is added or deleted to the notebook this function updates
395 * information held in the m_macControl so that it matches the order
396 * the user would expect.
397 */
398 void wxNotebook::MacSetupTabs()
399 {
400 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
401
402 wxNotebookPage *page;
403 ControlTabInfoRec info;
404
405 const size_t countPages = GetPageCount();
406 for(size_t ii = 0; ii < countPages; ii++)
407 {
408 page = m_pages[ii];
409 info.version = 0;
410 info.iconSuiteID = 0;
411 wxMacStringToPascal( page->GetLabel() , info.name ) ;
412
413 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
414 sizeof( ControlTabInfoRec) , (char*) &info ) ;
415 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
416 #if TARGET_CARBON
417 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
418 {
419 // tab controls only support very specific types of images, therefore we are doing an odyssee
420 // accross the icon worlds (even Apple DTS did not find a shorter path)
421 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
422 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
423 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
424 // of static counter
425 wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ;
426 if ( bmap )
427 {
428 wxBitmap scaledBitmap ;
429 if ( bmap->GetWidth() != 16 || bmap->GetHeight() != 16 )
430 {
431 scaledBitmap = wxBitmap( bmap->ConvertToImage().Scale(16,16) ) ;
432 bmap = &scaledBitmap ;
433 }
434 ControlButtonContentInfo info ;
435 wxMacCreateBitmapButton( &info , *bmap , kControlContentPictHandle) ;
436 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
437 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
438 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
439 IconRef iconRef ;
440 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1 , iconFamily, &iconRef ) ;
441 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
442 info.contentType = kControlContentIconRef ;
443 info.u.iconRef = iconRef ;
444 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
445 sizeof( info ), (Ptr)&info );
446 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
447 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
448 ReleaseIconRef( iconRef ) ;
449 DisposeHandle( (Handle) iconFamily ) ;
450 }
451 }
452 #endif
453 }
454 Rect bounds;
455 GetControlBounds((ControlHandle)m_macControl, &bounds);
456 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
457 }
458
459 // ----------------------------------------------------------------------------
460 // wxNotebook callbacks
461 // ----------------------------------------------------------------------------
462
463 // @@@ OnSize() is used for setting the font when it's called for the first
464 // time because doing it in ::Create() doesn't work (for unknown reasons)
465 void wxNotebook::OnSize(wxSizeEvent& event)
466 {
467 // emulate page change (it's esp. important to do it first time because
468 // otherwise our page would stay invisible)
469 int nSel = m_nSelection;
470 m_nSelection = -1;
471 SetSelection(nSel);
472
473 // fit the notebook page to the tab control's display area
474 int w, h;
475 GetSize(&w, &h);
476
477 unsigned int nCount = m_pages.Count();
478 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
479 wxNotebookPage *pPage = m_pages[nPage];
480 pPage->SetSize(wxMacTabLeftMargin(GetWindowStyle()) + m_macHorizontalBorder,
481 wxMacTabTopMargin(GetWindowStyle()) + m_macVerticalBorder,
482 w - wxMacTabLeftMargin(GetWindowStyle()) - wxMacTabRightMargin(GetWindowStyle()) - 2*m_macHorizontalBorder,
483 h - wxMacTabTopMargin(GetWindowStyle()) - wxMacTabBottomMargin(GetWindowStyle()) - 2*m_macVerticalBorder);
484 if ( pPage->GetAutoLayout() ) {
485 pPage->Layout();
486 }
487 }
488
489 // Processing continues to next OnSize
490 event.Skip();
491 }
492
493 void wxNotebook::OnSelChange(wxNotebookEvent& event)
494 {
495 // is it our tab control?
496 if ( event.GetEventObject() == this )
497 ChangePage(event.GetOldSelection(), event.GetSelection());
498
499 // we want to give others a chance to process this message as well
500 event.Skip();
501 }
502
503 void wxNotebook::OnSetFocus(wxFocusEvent& event)
504 {
505 // set focus to the currently selected page if any
506 if ( m_nSelection != -1 )
507 m_pages[m_nSelection]->SetFocus();
508
509 event.Skip();
510 }
511
512 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
513 {
514 if ( event.IsWindowChange() ) {
515 // change pages
516 AdvanceSelection(event.GetDirection());
517 }
518 else {
519 // pass to the parent
520 if ( GetParent() ) {
521 event.SetCurrentFocus(this);
522 GetParent()->ProcessEvent(event);
523 }
524 }
525 }
526
527 // ----------------------------------------------------------------------------
528 // wxNotebook base class virtuals
529 // ----------------------------------------------------------------------------
530
531 // override these 2 functions to do nothing: everything is done in OnSize
532
533 void wxNotebook::SetConstraintSizes(bool /* recurse */)
534 {
535 // don't set the sizes of the pages - their correct size is not yet known
536 wxControl::SetConstraintSizes(FALSE);
537 }
538
539 bool wxNotebook::DoPhase(int /* nPhase */)
540 {
541 return TRUE;
542 }
543
544 void wxNotebook::Command(wxCommandEvent& event)
545 {
546 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
547 }
548
549 // ----------------------------------------------------------------------------
550 // wxNotebook helper functions
551 // ----------------------------------------------------------------------------
552
553 // hide the currently active panel and show the new one
554 void wxNotebook::ChangePage(int nOldSel, int nSel)
555 {
556 // it's not an error (the message may be generated by the tab control itself)
557 // and it may happen - just do nothing
558 if ( nSel == nOldSel )
559 {
560 wxNotebookPage *pPage = m_pages[nSel];
561 pPage->Show(FALSE);
562 pPage->Show(TRUE);
563 pPage->SetFocus();
564 return;
565 }
566
567 // Hide previous page
568 if ( nOldSel != -1 ) {
569 m_pages[nOldSel]->Show(FALSE);
570 }
571
572 wxNotebookPage *pPage = m_pages[nSel];
573 pPage->Show(TRUE);
574 pPage->SetFocus();
575
576 m_nSelection = nSel;
577 }
578
579
580 void wxNotebook::OnMouse( wxMouseEvent &event )
581 {
582 if ( (ControlHandle) m_macControl == NULL )
583 {
584 event.Skip() ;
585 return ;
586 }
587
588 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
589 {
590 int x = event.m_x ;
591 int y = event.m_y ;
592
593 MacClientToRootWindow( &x , &y ) ;
594
595 ControlHandle control ;
596 Point localwhere ;
597 SInt16 controlpart ;
598
599 localwhere.h = x ;
600 localwhere.v = y ;
601
602 short modifiers = 0;
603
604 if ( !event.m_leftDown && !event.m_rightDown )
605 modifiers |= btnState ;
606
607 if ( event.m_shiftDown )
608 modifiers |= shiftKey ;
609
610 if ( event.m_controlDown )
611 modifiers |= controlKey ;
612
613 if ( event.m_altDown )
614 modifiers |= optionKey ;
615
616 if ( event.m_metaDown )
617 modifiers |= cmdKey ;
618
619 control = (ControlHandle) m_macControl ;
620 if ( control && ::IsControlActive( control ) )
621 {
622 {
623 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
624 ::GetControl32BitValue(control) - 1, m_nSelection);
625 changing.SetEventObject(this);
626 GetEventHandler()->ProcessEvent(changing);
627
628 if(changing.IsAllowed())
629 {
630 controlpart = ::HandleControlClick(control, localwhere, modifiers,
631 (ControlActionUPP) -1);
632 wxTheApp->s_lastMouseDown = 0 ;
633
634 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
635 ::GetControl32BitValue(control) - 1, m_nSelection);
636 event.SetEventObject(this);
637
638 GetEventHandler()->ProcessEvent(event);
639 }
640 }
641 }
642 }
643 }
644
645
646 void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
647 {
648 #if 0
649 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
650 event.SetEventObject(this);
651
652 ProcessEvent(event);
653 #endif
654 }
655