]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
Warning fix.
[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/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 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
48 EVT_SIZE(wxNotebook::OnSize)
49 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
50 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
51 END_EVENT_TABLE()
52
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
54 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxNotebook construction
62 // ----------------------------------------------------------------------------
63
64 // common part of all ctors
65 void wxNotebook::Init()
66 {
67 m_nSelection = -1;
68 }
69
70 // default for dynamic class
71 wxNotebook::wxNotebook()
72 {
73 Init();
74 }
75
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow *parent,
78 wxWindowID id,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxString& name)
83 {
84 Init();
85
86 Create(parent, id, pos, size, style, name);
87 }
88
89 // Create() function
90 bool wxNotebook::Create(wxWindow *parent,
91 wxWindowID id,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 m_macIsUserPane = FALSE ;
98
99 if ( !wxNotebookBase::Create(parent, id, pos, size, style, name) )
100 return false;
101
102 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
103
104 if( bounds.right <= bounds.left )
105 bounds.right = bounds.left + 100 ;
106 if ( bounds.bottom <= bounds.top )
107 bounds.bottom = bounds.top + 100 ;
108
109 UInt16 tabstyle = kControlTabDirectionNorth ;
110 if ( HasFlag(wxNB_LEFT) )
111 tabstyle = kControlTabDirectionWest ;
112 else if ( HasFlag( wxNB_RIGHT ) )
113 tabstyle = kControlTabDirectionEast ;
114 else if ( HasFlag( wxNB_BOTTOM ) )
115 tabstyle = kControlTabDirectionSouth ;
116
117 ControlTabSize tabsize = kControlTabSizeLarge ;
118 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
119 tabsize = kControlTabSizeSmall ;
120 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
121 {
122 if (UMAGetSystemVersion() >= 0x1030 )
123 tabsize = 3 ;
124 else
125 tabsize = kControlSizeSmall;
126 }
127
128 m_peer = new wxMacControl() ;
129 verify_noerr ( CreateTabsControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
130 tabsize , tabstyle, 0, NULL, m_peer->GetControlRefAddr() ) );
131
132
133 MacPostControlCreate(pos,size) ;
134 return TRUE ;
135 }
136
137 // dtor
138 wxNotebook::~wxNotebook()
139 {
140 }
141
142 // ----------------------------------------------------------------------------
143 // wxNotebook accessors
144 // ----------------------------------------------------------------------------
145
146 void wxNotebook::SetPadding(const wxSize& padding)
147 {
148 // unsupported by OS
149 }
150
151 void wxNotebook::SetTabSize(const wxSize& sz)
152 {
153 // unsupported by OS
154 }
155
156 void wxNotebook::SetPageSize(const wxSize& size)
157 {
158 SetSize( CalcSizeFromPage( size ) );
159 }
160
161 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
162 {
163 return DoGetSizeFromClientSize( sizePage ) ;
164 }
165
166 int wxNotebook::SetSelection(size_t nPage)
167 {
168 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
169
170 if ( int(nPage) != m_nSelection )
171 {
172 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
173 event.SetSelection(nPage);
174 event.SetOldSelection(m_nSelection);
175 event.SetEventObject(this);
176 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
177 {
178 // program allows the page change
179 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
180 (void)GetEventHandler()->ProcessEvent(event);
181
182 ChangePage(m_nSelection, nPage);
183 }
184 }
185
186 return m_nSelection;
187 }
188
189 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
190 {
191 wxASSERT( IS_VALID_PAGE(nPage) );
192
193 wxNotebookPage *page = m_pages[nPage];
194 page->SetLabel(strText);
195 MacSetupTabs();
196
197 return true;
198 }
199
200 wxString wxNotebook::GetPageText(size_t nPage) const
201 {
202 wxASSERT( IS_VALID_PAGE(nPage) );
203
204 wxNotebookPage *page = m_pages[nPage];
205 return page->GetLabel();
206 }
207
208 int wxNotebook::GetPageImage(size_t nPage) const
209 {
210 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
211
212 return m_images[nPage];
213 }
214
215 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
216 {
217 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
218
219 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
220 _T("invalid image index in SetPageImage()") );
221
222 if ( nImage != m_images[nPage] )
223 {
224 // if the item didn't have an icon before or, on the contrary, did have
225 // it but has lost it now, its size will change - but if the icon just
226 // changes, it won't
227 m_images[nPage] = nImage;
228
229 MacSetupTabs() ;
230 }
231
232 return TRUE;
233 }
234
235 // ----------------------------------------------------------------------------
236 // wxNotebook operations
237 // ----------------------------------------------------------------------------
238
239 // remove one page from the notebook, without deleting the window
240 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
241 {
242 wxCHECK( IS_VALID_PAGE(nPage), NULL );
243 wxNotebookPage* page = m_pages[nPage] ;
244 m_pages.RemoveAt(nPage);
245
246 MacSetupTabs();
247
248 if(m_nSelection >= (int)GetPageCount()) {
249 m_nSelection = GetPageCount() - 1;
250 }
251 if(m_nSelection >= 0) {
252 m_pages[m_nSelection]->Show(true);
253 }
254 InvalidateBestSize();
255 return page;
256 }
257
258 // remove all pages
259 bool wxNotebook::DeleteAllPages()
260 {
261 WX_CLEAR_ARRAY(m_pages) ;
262 MacSetupTabs();
263 m_nSelection = -1 ;
264 InvalidateBestSize();
265 return TRUE;
266 }
267
268
269 // same as AddPage() but does it at given position
270 bool wxNotebook::InsertPage(size_t nPage,
271 wxNotebookPage *pPage,
272 const wxString& strText,
273 bool bSelect,
274 int imageId)
275 {
276 if ( !wxNotebookBase::InsertPage(nPage, pPage, strText, bSelect, imageId) )
277 return false;
278
279 wxASSERT_MSG( pPage->GetParent() == this,
280 _T("notebook pages must have notebook as parent") );
281
282 // don't show pages by default (we'll need to adjust their size first)
283 pPage->Show( false ) ;
284
285 pPage->SetLabel(strText);
286
287 m_images.Insert(imageId, nPage);
288
289 MacSetupTabs();
290
291 wxRect rect = GetPageRect() ;
292 pPage->SetSize(rect);
293 if ( pPage->GetAutoLayout() ) {
294 pPage->Layout();
295 }
296
297
298 // now deal with the selection
299 // ---------------------------
300
301 // if the inserted page is before the selected one, we must update the
302 // index of the selected page
303
304 if ( int(nPage) <= m_nSelection )
305 {
306 m_nSelection++;
307 // while this still is the same page showing, we need to update the tabs
308 m_peer->SetValue( m_nSelection + 1 ) ;
309 }
310
311 // some page should be selected: either this one or the first one if there
312 // is still no selection
313 int selNew = -1;
314 if ( bSelect )
315 selNew = nPage;
316 else if ( m_nSelection == -1 )
317 selNew = 0;
318
319 if ( selNew != -1 )
320 SetSelection(selNew);
321
322 InvalidateBestSize();
323 return true;
324 }
325
326 /* Added by Mark Newsam
327 * When a page is added or deleted to the notebook this function updates
328 * information held in the control so that it matches the order
329 * the user would expect.
330 */
331 void wxNotebook::MacSetupTabs()
332 {
333 m_peer->SetMaximum( GetPageCount() ) ;
334
335 wxNotebookPage *page;
336 ControlTabInfoRec info;
337
338 const size_t countPages = GetPageCount();
339 for(size_t ii = 0; ii < countPages; ii++)
340 {
341 page = m_pages[ii];
342 info.version = 0;
343 info.iconSuiteID = 0;
344 wxMacStringToPascal( page->GetLabel() , info.name ) ;
345 m_peer->SetData<ControlTabInfoRec>( ii+1, kControlTabInfoTag, &info ) ;
346 m_peer->SetTabEnabled( ii + 1 , true ) ;
347 #if TARGET_CARBON
348 if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 )
349 {
350 // tab controls only support very specific types of images, therefore we are doing an odyssee
351 // accross the icon worlds (even Apple DTS did not find a shorter path)
352 // in order not to pollute the icon registry we put every icon into (OSType) 1 and immediately
353 // afterwards Unregister it (IconRef is ref counted, so it will stay on the tab even if we
354 // unregister it) in case this will ever lead to having the same icon everywhere add some kind
355 // of static counter
356 const wxBitmap* bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ;
357 if ( bmap )
358 {
359 wxBitmap scaledBitmap ;
360 if ( bmap->GetWidth() != 16 || bmap->GetHeight() != 16 )
361 {
362 scaledBitmap = wxBitmap( bmap->ConvertToImage().Scale(16,16) ) ;
363 bmap = &scaledBitmap ;
364 }
365 ControlButtonContentInfo info ;
366 wxMacCreateBitmapButton( &info , *bmap , kControlContentPictHandle) ;
367 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(0) ;
368 OSErr err = SetIconFamilyData( iconFamily, 'PICT' , (Handle) info.u.picture ) ;
369 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
370 IconRef iconRef ;
371 err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) 1, iconFamily, &iconRef ) ;
372 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
373 info.contentType = kControlContentIconRef ;
374 info.u.iconRef = iconRef ;
375 m_peer->SetData<ControlButtonContentInfo>( ii+1,kControlTabImageContentTag, &info );
376 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
377 if ( UMAGetSystemVersion() < 0x1030 )
378 {
379 UnregisterIconRef( 'WXNG' , (OSType) 1 ) ;
380 }
381
382 ReleaseIconRef( iconRef ) ;
383 DisposeHandle( (Handle) iconFamily ) ;
384 }
385 }
386 #endif
387 }
388 Rect bounds;
389 m_peer->GetRectInWindowCoords( &bounds ) ;
390 InvalWindowRect((WindowRef)MacGetTopLevelWindowRef(), &bounds);
391 }
392
393 wxRect wxNotebook::GetPageRect() const
394 {
395 wxSize size = GetClientSize() ;
396 return wxRect( 0 , 0 , size.x , size.y ) ;
397 }
398 // ----------------------------------------------------------------------------
399 // wxNotebook callbacks
400 // ----------------------------------------------------------------------------
401
402 // @@@ OnSize() is used for setting the font when it's called for the first
403 // time because doing it in ::Create() doesn't work (for unknown reasons)
404 void wxNotebook::OnSize(wxSizeEvent& event)
405 {
406
407 unsigned int nCount = m_pages.Count();
408 wxRect rect = GetPageRect() ;
409 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
410 wxNotebookPage *pPage = m_pages[nPage];
411 pPage->SetSize(rect);
412 if ( pPage->GetAutoLayout() ) {
413 pPage->Layout();
414 }
415 }
416
417 // Processing continues to next OnSize
418 event.Skip();
419 }
420
421 void wxNotebook::OnSelChange(wxNotebookEvent& event)
422 {
423 // is it our tab control?
424 if ( event.GetEventObject() == this )
425 ChangePage(event.GetOldSelection(), event.GetSelection());
426
427 // we want to give others a chance to process this message as well
428 event.Skip();
429 }
430
431 void wxNotebook::OnSetFocus(wxFocusEvent& event)
432 {
433 // set focus to the currently selected page if any
434 if ( m_nSelection != -1 )
435 m_pages[m_nSelection]->SetFocus();
436
437 event.Skip();
438 }
439
440 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
441 {
442 if ( event.IsWindowChange() ) {
443 // change pages
444 AdvanceSelection(event.GetDirection());
445 }
446 else {
447 // we get this event in 2 cases
448 //
449 // a) one of our pages might have generated it because the user TABbed
450 // out from it in which case we should propagate the event upwards and
451 // our parent will take care of setting the focus to prev/next sibling
452 //
453 // or
454 //
455 // b) the parent panel wants to give the focus to us so that we
456 // forward it to our selected page. We can't deal with this in
457 // OnSetFocus() because we don't know which direction the focus came
458 // from in this case and so can't choose between setting the focus to
459 // first or last panel child
460 wxWindow *parent = GetParent();
461 // the cast is here to fic a GCC ICE
462 if ( ((wxWindow*)event.GetEventObject()) == parent )
463 {
464 // no, it doesn't come from child, case (b): forward to a page
465 if ( m_nSelection != -1 )
466 {
467 // so that the page knows that the event comes from it's parent
468 // and is being propagated downwards
469 event.SetEventObject(this);
470
471 wxWindow *page = m_pages[m_nSelection];
472 if ( !page->GetEventHandler()->ProcessEvent(event) )
473 {
474 page->SetFocus();
475 }
476 //else: page manages focus inside it itself
477 }
478 else
479 {
480 // we have no pages - still have to give focus to _something_
481 SetFocus();
482 }
483 }
484 else
485 {
486 // it comes from our child, case (a), pass to the parent
487 if ( parent ) {
488 event.SetCurrentFocus(this);
489 parent->GetEventHandler()->ProcessEvent(event);
490 }
491 }
492 }
493 }
494
495 // ----------------------------------------------------------------------------
496 // wxNotebook base class virtuals
497 // ----------------------------------------------------------------------------
498
499 #if wxUSE_CONSTRAINTS
500
501 // override these 2 functions to do nothing: everything is done in OnSize
502
503 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
504 {
505 // don't set the sizes of the pages - their correct size is not yet known
506 wxControl::SetConstraintSizes(FALSE);
507 }
508
509 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
510 {
511 return TRUE;
512 }
513
514 #endif // wxUSE_CONSTRAINTS
515
516 void wxNotebook::Command(wxCommandEvent& event)
517 {
518 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
519 }
520
521 // ----------------------------------------------------------------------------
522 // wxNotebook helper functions
523 // ----------------------------------------------------------------------------
524
525 // hide the currently active panel and show the new one
526 void wxNotebook::ChangePage(int nOldSel, int nSel)
527 {
528 if ( nOldSel != -1 )
529 {
530 m_pages[nOldSel]->Show(FALSE);
531 }
532
533 if ( nSel != -1 )
534 {
535 wxNotebookPage *pPage = m_pages[nSel];
536 pPage->Show(TRUE);
537 pPage->SetFocus();
538 }
539
540 m_nSelection = nSel;
541 m_peer->SetValue( m_nSelection + 1 ) ;
542 }
543
544 wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
545 {
546 OSStatus status = eventNotHandledErr ;
547
548 SInt32 newSel = m_peer->GetValue() - 1 ;
549 if ( newSel != m_nSelection )
550 {
551 wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
552 newSel , m_nSelection);
553 changing.SetEventObject(this);
554 GetEventHandler()->ProcessEvent(changing);
555
556 if(changing.IsAllowed())
557 {
558 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
559 newSel, m_nSelection);
560 event.SetEventObject(this);
561
562 GetEventHandler()->ProcessEvent(event);
563 }
564 else
565 {
566 m_peer->SetValue( m_nSelection + 1 ) ;
567 }
568 status = noErr ;
569 }
570 return status ;
571 }
572