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