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