Avoid events when implicitly selecting first wxBookCtrl page.
[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 DoSetSelectionAfterInsertion(nPage, bSelect);
244
245 InvalidateBestSize();
246
247 return true;
248 }
249
250 int wxNotebook::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(flags)) const
251 {
252 int resultV = wxNOT_FOUND;
253 #if 0
254 const int countPages = GetPageCount();
255
256 // we have to convert from Client to Window relative coordinates
257 wxPoint adjustedPt = pt + GetClientAreaOrigin();
258 // and now to HIView native ones
259 adjustedPt.x -= MacGetLeftBorderSize() ;
260 adjustedPt.y -= MacGetTopBorderSize() ;
261
262 HIPoint hipoint= { adjustedPt.x , adjustedPt.y } ;
263 HIViewPartCode outPart = 0 ;
264 OSStatus err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
265
266 int max = m_peer->GetMaximum() ;
267 if ( outPart == 0 && max > 0 )
268 {
269 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
270 // so we have to go some extra miles to make sure we select something different
271 // and try again ..
272 int val = m_peer->GetValue() ;
273 int maxval = max ;
274 if ( max == 1 )
275 {
276 m_peer->SetMaximum( 2 ) ;
277 maxval = 2 ;
278 }
279
280 if ( val == 1 )
281 m_peer->SetValue( maxval ) ;
282 else
283 m_peer->SetValue( 1 ) ;
284
285 err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
286
287 m_peer->SetValue( val ) ;
288 if ( max == 1 )
289 m_peer->SetMaximum( 1 ) ;
290 }
291
292 if ( outPart >= 1 && outPart <= countPages )
293 resultV = outPart - 1 ;
294
295 if (flags != NULL)
296 {
297 *flags = 0;
298
299 // we cannot differentiate better
300 if (resultV >= 0)
301 *flags |= wxBK_HITTEST_ONLABEL;
302 else
303 *flags |= wxBK_HITTEST_NOWHERE;
304 }
305 #endif
306 return resultV;
307 }
308
309 // Added by Mark Newsam
310 // When a page is added or deleted to the notebook this function updates
311 // information held in the control so that it matches the order
312 // the user would expect.
313 //
314 void wxNotebook::MacSetupTabs()
315 {
316 m_peer->SetupTabs(*this);
317 Refresh();
318 }
319
320 wxRect wxNotebook::GetPageRect() const
321 {
322 wxSize size = GetClientSize() ;
323
324 return wxRect( 0 , 0 , size.x , size.y ) ;
325 }
326
327 // ----------------------------------------------------------------------------
328 // wxNotebook callbacks
329 // ----------------------------------------------------------------------------
330
331 // @@@ OnSize() is used for setting the font when it's called for the first
332 // time because doing it in ::Create() doesn't work (for unknown reasons)
333 void wxNotebook::OnSize(wxSizeEvent& event)
334 {
335 unsigned int nCount = m_pages.Count();
336 wxRect rect = GetPageRect() ;
337
338 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
339 {
340 wxNotebookPage *pPage = m_pages[nPage];
341 pPage->SetSize(rect, wxSIZE_FORCE_EVENT);
342 }
343
344 // If the selected page is hidden at this point, the notebook
345 // has become visible for the first time after creation, and
346 // we postponed showing the page in ChangePage().
347 // So show the selected page now.
348 if ( m_selection != wxNOT_FOUND )
349 {
350 wxNotebookPage *pPage = m_pages[m_selection];
351 if ( !pPage->IsShown() )
352 {
353 pPage->Show( true );
354 pPage->SetFocus();
355 }
356 }
357
358 // Processing continues to next OnSize
359 event.Skip();
360 }
361
362 void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
363 {
364 // is it our tab control?
365 if ( event.GetEventObject() == this )
366 ChangePage(event.GetOldSelection(), event.GetSelection());
367
368 // we want to give others a chance to process this message as well
369 event.Skip();
370 }
371
372 void wxNotebook::OnSetFocus(wxFocusEvent& event)
373 {
374 // set focus to the currently selected page if any
375 if ( m_selection != wxNOT_FOUND )
376 m_pages[m_selection]->SetFocus();
377
378 event.Skip();
379 }
380
381 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
382 {
383 if ( event.IsWindowChange() )
384 {
385 // change pages
386 AdvanceSelection( event.GetDirection() );
387 }
388 else
389 {
390 // we get this event in 2 cases
391 //
392 // a) one of our pages might have generated it because the user TABbed
393 // out from it in which case we should propagate the event upwards and
394 // our parent will take care of setting the focus to prev/next sibling
395 //
396 // or
397 //
398 // b) the parent panel wants to give the focus to us so that we
399 // forward it to our selected page. We can't deal with this in
400 // OnSetFocus() because we don't know which direction the focus came
401 // from in this case and so can't choose between setting the focus to
402 // first or last panel child
403 wxWindow *parent = GetParent();
404
405 // the cast is here to fix a GCC ICE
406 if ( ((wxWindow*)event.GetEventObject()) == parent )
407 {
408 // no, it doesn't come from child, case (b): forward to a page
409 if ( m_selection != wxNOT_FOUND )
410 {
411 // so that the page knows that the event comes from it's parent
412 // and is being propagated downwards
413 event.SetEventObject( this );
414
415 wxWindow *page = m_pages[m_selection];
416 if ( !page->HandleWindowEvent( event ) )
417 {
418 page->SetFocus();
419 }
420 //else: page manages focus inside it itself
421 }
422 else
423 {
424 // we have no pages - still have to give focus to _something_
425 SetFocus();
426 }
427 }
428 else
429 {
430 // it comes from our child, case (a), pass to the parent
431 if ( parent )
432 {
433 event.SetCurrentFocus( this );
434 parent->HandleWindowEvent( event );
435 }
436 }
437 }
438 }
439
440 // ----------------------------------------------------------------------------
441 // wxNotebook base class virtuals
442 // ----------------------------------------------------------------------------
443
444 #if wxUSE_CONSTRAINTS
445
446 // override these 2 functions to do nothing: everything is done in OnSize
447
448 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
449 {
450 // don't set the sizes of the pages - their correct size is not yet known
451 wxControl::SetConstraintSizes( false );
452 }
453
454 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
455 {
456 return true;
457 }
458
459 #endif // wxUSE_CONSTRAINTS
460
461 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
462 {
463 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
464 }
465
466 // ----------------------------------------------------------------------------
467 // wxNotebook helper functions
468 // ----------------------------------------------------------------------------
469
470 // hide the currently active panel and show the new one
471 void wxNotebook::ChangePage(int nOldSel, int nSel)
472 {
473 if (nOldSel == nSel)
474 return;
475
476 if ( nOldSel != wxNOT_FOUND )
477 m_pages[nOldSel]->Show( false );
478
479 if ( nSel != wxNOT_FOUND )
480 {
481 wxNotebookPage *pPage = m_pages[nSel];
482 if ( IsShownOnScreen() )
483 {
484 pPage->Show( true );
485 pPage->SetFocus();
486 }
487 else
488 {
489 // Postpone Show() until the control is actually shown.
490 // Otherwise this forces the containing toplevel window
491 // to show, even if it's just being created and called
492 // AddPage() without intent to show the window yet.
493 // We Show() the selected page in our OnSize handler,
494 // unless it already is shown.
495 }
496 }
497
498 m_selection = nSel;
499 m_peer->SetValue( m_selection + 1 ) ;
500 }
501
502 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec) )
503 {
504 bool status = false ;
505
506 SInt32 newSel = m_peer->GetValue() - 1 ;
507 if ( newSel != m_selection )
508 {
509 wxBookCtrlEvent changing(
510 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
511 newSel , m_selection );
512 changing.SetEventObject( this );
513 HandleWindowEvent( changing );
514
515 if ( changing.IsAllowed() )
516 {
517 wxBookCtrlEvent event(
518 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
519 newSel, m_selection );
520 event.SetEventObject( this );
521 HandleWindowEvent( event );
522 }
523 else
524 {
525 m_peer->SetValue( m_selection + 1 ) ;
526 }
527
528 status = true ;
529 }
530
531 return status ;
532 }
533
534 #endif