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