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