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