]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
removed superfluous casts
[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 static bool constantsSet = false ;
37
38 short kwxMacTabLeftMargin = 0 ;
39 short kwxMacTabTopMargin = 0 ;
40 short kwxMacTabRightMargin = 0 ;
41 short kwxMacTabBottomMargin = 0 ;
42
43 // ----------------------------------------------------------------------------
44 // event table
45 // ----------------------------------------------------------------------------
46
47 #if !USE_SHARED_LIBRARIES
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
50
51 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
52 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
53 EVT_MOUSE_EVENTS(wxNotebook::OnMouse)
54
55 EVT_SIZE(wxNotebook::OnSize)
56 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
57 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
58 END_EVENT_TABLE()
59
60 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
62 #endif
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // wxNotebook construction
70 // ----------------------------------------------------------------------------
71
72 // common part of all ctors
73 void wxNotebook::Init()
74 {
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
100 m_nSelection = -1;
101 }
102
103 // default for dynamic class
104 wxNotebook::wxNotebook()
105 {
106 Init();
107 }
108
109 // the same arguments as for wxControl
110 wxNotebook::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
123 bool 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 , wxEmptyString , 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 ;
149 }
150
151 // dtor
152 wxNotebook::~wxNotebook()
153 {
154 m_macControl = NULL ;
155 }
156
157 wxSize 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
192 // ----------------------------------------------------------------------------
193 // wxNotebook accessors
194 // ----------------------------------------------------------------------------
195
196 void wxNotebook::SetPadding(const wxSize& padding)
197 {
198 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
199 }
200
201 void wxNotebook::SetTabSize(const wxSize& sz)
202 {
203 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
204 }
205
206 void wxNotebook::SetPageSize(const wxSize& size)
207 {
208 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
209 }
210
211 int wxNotebook::SetSelection(int nPage)
212 {
213 if( !IS_VALID_PAGE(nPage) )
214 return m_nSelection ;
215
216 ChangePage(m_nSelection, nPage);
217 SetControl32BitValue( (ControlHandle) m_macControl , m_nSelection + 1 ) ;
218
219 Refresh();
220 return m_nSelection;
221 }
222
223 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
224 {
225 wxASSERT( IS_VALID_PAGE(nPage) );
226
227 wxNotebookPage *page = m_pages[nPage];
228 page->SetLabel(strText);
229 MacSetupTabs();
230
231 return true;
232 }
233
234 wxString wxNotebook::GetPageText(int nPage) const
235 {
236 wxASSERT( IS_VALID_PAGE(nPage) );
237
238 wxNotebookPage *page = m_pages[nPage];
239 return page->GetLabel();
240 }
241
242 int wxNotebook::GetPageImage(int nPage) const
243 {
244 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
245
246 return m_images[nPage];
247 }
248
249 bool wxNotebook::SetPageImage(int nPage, int nImage)
250 {
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()") );
255
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;
267 }
268
269 // ----------------------------------------------------------------------------
270 // wxNotebook operations
271 // ----------------------------------------------------------------------------
272
273 // remove one page from the notebook, without deleting the window
274 wxNotebookPage* wxNotebook::DoRemovePage(int nPage)
275 {
276 wxCHECK( IS_VALID_PAGE(nPage), NULL );
277 wxNotebookPage* page = m_pages[nPage] ;
278 m_pages.RemoveAt(nPage);
279
280 MacSetupTabs();
281
282 if(m_nSelection >= GetPageCount()) {
283 m_nSelection = GetPageCount() - 1;
284 }
285 if(m_nSelection >= 0) {
286 m_pages[m_nSelection]->Show(true);
287 }
288 return page;
289 }
290
291 // remove all pages
292 bool wxNotebook::DeleteAllPages()
293 {
294 // TODO: delete native widget pages
295
296 WX_CLEAR_ARRAY(m_pages) ;
297 MacSetupTabs();
298
299 return TRUE;
300 }
301
302
303 // same as AddPage() but does it at given position
304 bool 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
313 pPage->SetLabel(strText);
314
315 // save the pointer to the page
316 m_pages.Insert(pPage, nPage);
317
318 m_images.Insert(imageId, nPage);
319
320 MacSetupTabs();
321
322 if ( bSelect ) {
323 m_nSelection = nPage;
324 }
325 else if ( m_nSelection == -1 ) {
326 m_nSelection = 0;
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 ) ;
333
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 }
342
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 */
351 void wxNotebook::MacSetupTabs()
352 {
353 SetControl32BitMaximum( (ControlHandle) m_macControl , GetPageCount() ) ;
354
355 wxNotebookPage *page;
356 ControlTabInfoRec info;
357
358 for(int ii = 0; ii < GetPageCount(); ii++)
359 {
360 page = m_pages[ii];
361 info.version = 0;
362 info.iconSuiteID = 0;
363 wxMacStringToPascal( page->GetLabel() , info.name ) ;
364
365 SetControlData( (ControlHandle) m_macControl, ii+1, kControlTabInfoTag,
366 sizeof( ControlTabInfoRec) , (char*) &info ) ;
367 SetTabEnabled( (ControlHandle) m_macControl , ii+1 , true ) ;
368 #if 0 // TARGET_CARBON
369 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
370 {
371 // tab controls only support very specific types of images, therefore we are doing an odyssee
372 // accross the icon worlds (even Apple DTS did not find a shorter path)
373 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
374 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
375 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
376 // of static counter
377 ControlButtonContentInfo info ;
378 wxMacCreateBitmapButton( &info , *GetImageList()->GetBitmap( GetPageImage(ii ) ) , kControlContentPictHandle) ;
379 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
380 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
381 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
382 IconRef iconRef ;
383 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1 , iconFamily, &iconRef ) ;
384 wxASSERT_MSG( err == noErr , "Error when adding bitmap" ) ;
385 info.contentType = kControlContentIconRef ;
386 info.u.iconRef = iconRef ;
387 SetControlData( (ControlHandle) m_macControl, ii+1,kControlTabImageContentTag,
388 sizeof( info ), (Ptr)&info );
389 wxASSERT_MSG( err == noErr , "Error when setting icon on tab" ) ;
390 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
391 ReleaseIconRef( iconRef ) ;
392 DisposeHandle( (Handle) iconFamily ) ;
393 }
394 #endif
395 }
396 Rect bounds;
397 GetControlBounds((ControlHandle)m_macControl, &bounds);
398 InvalWindowRect((WindowRef)MacGetRootWindow(), &bounds);
399 }
400
401 // ----------------------------------------------------------------------------
402 // wxNotebook callbacks
403 // ----------------------------------------------------------------------------
404
405 // @@@ OnSize() is used for setting the font when it's called for the first
406 // time because doing it in ::Create() doesn't work (for unknown reasons)
407 void wxNotebook::OnSize(wxSizeEvent& event)
408 {
409 // emulate page change (it's esp. important to do it first time because
410 // otherwise our page would stay invisible)
411 int nSel = m_nSelection;
412 m_nSelection = -1;
413 SetSelection(nSel);
414
415 // fit the notebook page to the tab control's display area
416 int w, h;
417 GetSize(&w, &h);
418
419 unsigned int nCount = m_pages.Count();
420 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
421 wxNotebookPage *pPage = m_pages[nPage];
422 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin,
423 w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
424 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
425 if ( pPage->GetAutoLayout() ) {
426 pPage->Layout();
427 }
428 }
429
430 // Processing continues to next OnSize
431 event.Skip();
432 }
433
434 void wxNotebook::OnSelChange(wxNotebookEvent& event)
435 {
436 // is it our tab control?
437 if ( event.GetEventObject() == this )
438 ChangePage(event.GetOldSelection(), event.GetSelection());
439
440 // we want to give others a chance to process this message as well
441 event.Skip();
442 }
443
444 void wxNotebook::OnSetFocus(wxFocusEvent& event)
445 {
446 // set focus to the currently selected page if any
447 if ( m_nSelection != -1 )
448 m_pages[m_nSelection]->SetFocus();
449
450 event.Skip();
451 }
452
453 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
454 {
455 if ( event.IsWindowChange() ) {
456 // change pages
457 AdvanceSelection(event.GetDirection());
458 }
459 else {
460 // pass to the parent
461 if ( GetParent() ) {
462 event.SetCurrentFocus(this);
463 GetParent()->ProcessEvent(event);
464 }
465 }
466 }
467
468 // ----------------------------------------------------------------------------
469 // wxNotebook base class virtuals
470 // ----------------------------------------------------------------------------
471
472 // override these 2 functions to do nothing: everything is done in OnSize
473
474 void wxNotebook::SetConstraintSizes(bool /* recurse */)
475 {
476 // don't set the sizes of the pages - their correct size is not yet known
477 wxControl::SetConstraintSizes(FALSE);
478 }
479
480 bool wxNotebook::DoPhase(int /* nPhase */)
481 {
482 return TRUE;
483 }
484
485 void wxNotebook::Command(wxCommandEvent& event)
486 {
487 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
488 }
489
490 // ----------------------------------------------------------------------------
491 // wxNotebook helper functions
492 // ----------------------------------------------------------------------------
493
494 // hide the currently active panel and show the new one
495 void wxNotebook::ChangePage(int nOldSel, int nSel)
496 {
497 // it's not an error (the message may be generated by the tab control itself)
498 // and it may happen - just do nothing
499 if ( nSel == nOldSel )
500 {
501 wxNotebookPage *pPage = m_pages[nSel];
502 pPage->Show(FALSE);
503 pPage->Show(TRUE);
504 pPage->SetFocus();
505 return;
506 }
507
508 // Hide previous page
509 if ( nOldSel != -1 ) {
510 m_pages[nOldSel]->Show(FALSE);
511 }
512
513 wxNotebookPage *pPage = m_pages[nSel];
514 pPage->Show(TRUE);
515 pPage->SetFocus();
516
517 m_nSelection = nSel;
518 }
519
520
521 void wxNotebook::OnMouse( wxMouseEvent &event )
522 {
523 if ( (ControlHandle) m_macControl == NULL )
524 {
525 event.Skip() ;
526 return ;
527 }
528
529 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
530 {
531 int x = event.m_x ;
532 int y = event.m_y ;
533
534 MacClientToRootWindow( &x , &y ) ;
535
536 ControlHandle control ;
537 Point localwhere ;
538 SInt16 controlpart ;
539
540 localwhere.h = x ;
541 localwhere.v = y ;
542
543 short modifiers = 0;
544
545 if ( !event.m_leftDown && !event.m_rightDown )
546 modifiers |= btnState ;
547
548 if ( event.m_shiftDown )
549 modifiers |= shiftKey ;
550
551 if ( event.m_controlDown )
552 modifiers |= controlKey ;
553
554 if ( event.m_altDown )
555 modifiers |= optionKey ;
556
557 if ( event.m_metaDown )
558 modifiers |= cmdKey ;
559
560 control = (ControlHandle) m_macControl ;
561 if ( control && ::IsControlActive( control ) )
562 {
563 {
564 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
565 ::GetControl32BitValue(control) - 1, m_nSelection);
566 changing.SetEventObject(this);
567 ProcessEvent(changing);
568
569 if(changing.IsAllowed())
570 {
571 controlpart = ::HandleControlClick(control, localwhere, modifiers,
572 (ControlActionUPP) -1);
573 wxTheApp->s_lastMouseDown = 0 ;
574
575 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
576 ::GetControl32BitValue(control) - 1, m_nSelection);
577 event.SetEventObject(this);
578
579 ProcessEvent(event);
580 }
581 }
582 }
583 }
584 }
585
586
587 void wxNotebook::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
588 {
589 #if 0
590 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControl32BitValue((ControlHandle)m_macControl) - 1, m_nSelection);
591 event.SetEventObject(this);
592
593 ProcessEvent(event);
594 #endif
595 }
596