]> git.saurik.com Git - wxWidgets.git/blob - src/osx/notebook_osx.cpp
making sure images are in synch with the pages
[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)GetPageCount())
181 m_selection = GetPageCount() - 1;
182
183 if (m_selection >= 0)
184 m_pages[m_selection]->Show(true);
185
186 InvalidateBestSize();
187
188 return page;
189 }
190
191 // remove all pages
192 bool wxNotebook::DeleteAllPages()
193 {
194 WX_CLEAR_ARRAY(m_pages);
195 m_images.clear();
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 GetPeer()->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& pt, long *flags) const
251 {
252 return GetPeer()->TabHitTest(pt,flags);
253 }
254
255 // Added by Mark Newsam
256 // When a page is added or deleted to the notebook this function updates
257 // information held in the control so that it matches the order
258 // the user would expect.
259 //
260 void wxNotebook::MacSetupTabs()
261 {
262 GetPeer()->SetupTabs(*this);
263 Refresh();
264 }
265
266 wxRect wxNotebook::GetPageRect() const
267 {
268 wxSize size = GetClientSize() ;
269
270 return wxRect( 0 , 0 , size.x , size.y ) ;
271 }
272
273 // ----------------------------------------------------------------------------
274 // wxNotebook callbacks
275 // ----------------------------------------------------------------------------
276
277 // @@@ OnSize() is used for setting the font when it's called for the first
278 // time because doing it in ::Create() doesn't work (for unknown reasons)
279 void wxNotebook::OnSize(wxSizeEvent& event)
280 {
281 unsigned int nCount = m_pages.Count();
282 wxRect rect = GetPageRect() ;
283
284 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
285 {
286 wxNotebookPage *pPage = m_pages[nPage];
287 pPage->SetSize(rect, wxSIZE_FORCE_EVENT);
288 }
289
290 #if 0 // deactivate r65078 for the moment
291 // If the selected page is hidden at this point, the notebook
292 // has become visible for the first time after creation, and
293 // we postponed showing the page in ChangePage().
294 // So show the selected page now.
295 if ( m_selection != wxNOT_FOUND )
296 {
297 wxNotebookPage *pPage = m_pages[m_selection];
298 if ( !pPage->IsShown() )
299 {
300 pPage->Show( true );
301 pPage->SetFocus();
302 }
303 }
304 #endif
305
306 // Processing continues to next OnSize
307 event.Skip();
308 }
309
310 void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
311 {
312 // is it our tab control?
313 if ( event.GetEventObject() == this )
314 ChangePage(event.GetOldSelection(), event.GetSelection());
315
316 // we want to give others a chance to process this message as well
317 event.Skip();
318 }
319
320 void wxNotebook::OnSetFocus(wxFocusEvent& event)
321 {
322 // set focus to the currently selected page if any
323 if ( m_selection != wxNOT_FOUND )
324 m_pages[m_selection]->SetFocus();
325
326 event.Skip();
327 }
328
329 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
330 {
331 if ( event.IsWindowChange() )
332 {
333 // change pages
334 AdvanceSelection( event.GetDirection() );
335 }
336 else
337 {
338 // we get this event in 2 cases
339 //
340 // a) one of our pages might have generated it because the user TABbed
341 // out from it in which case we should propagate the event upwards and
342 // our parent will take care of setting the focus to prev/next sibling
343 //
344 // or
345 //
346 // b) the parent panel wants to give the focus to us so that we
347 // forward it to our selected page. We can't deal with this in
348 // OnSetFocus() because we don't know which direction the focus came
349 // from in this case and so can't choose between setting the focus to
350 // first or last panel child
351 wxWindow *parent = GetParent();
352
353 // the cast is here to fix a GCC ICE
354 if ( ((wxWindow*)event.GetEventObject()) == parent )
355 {
356 // no, it doesn't come from child, case (b): forward to a page
357 if ( m_selection != wxNOT_FOUND )
358 {
359 // so that the page knows that the event comes from it's parent
360 // and is being propagated downwards
361 event.SetEventObject( this );
362
363 wxWindow *page = m_pages[m_selection];
364 if ( !page->HandleWindowEvent( event ) )
365 {
366 page->SetFocus();
367 }
368 //else: page manages focus inside it itself
369 }
370 else
371 {
372 // we have no pages - still have to give focus to _something_
373 SetFocus();
374 }
375 }
376 else
377 {
378 // it comes from our child, case (a), pass to the parent
379 if ( parent )
380 {
381 event.SetCurrentFocus( this );
382 parent->HandleWindowEvent( event );
383 }
384 }
385 }
386 }
387
388 // ----------------------------------------------------------------------------
389 // wxNotebook base class virtuals
390 // ----------------------------------------------------------------------------
391
392 #if wxUSE_CONSTRAINTS
393
394 // override these 2 functions to do nothing: everything is done in OnSize
395
396 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
397 {
398 // don't set the sizes of the pages - their correct size is not yet known
399 wxControl::SetConstraintSizes( false );
400 }
401
402 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
403 {
404 return true;
405 }
406
407 #endif // wxUSE_CONSTRAINTS
408
409 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
410 {
411 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
412 }
413
414 // ----------------------------------------------------------------------------
415 // wxNotebook helper functions
416 // ----------------------------------------------------------------------------
417
418 // hide the currently active panel and show the new one
419 void wxNotebook::ChangePage(int nOldSel, int nSel)
420 {
421 if (nOldSel == nSel)
422 return;
423
424 if ( nOldSel != wxNOT_FOUND )
425 m_pages[nOldSel]->Show( false );
426
427 if ( nSel != wxNOT_FOUND )
428 {
429 wxNotebookPage *pPage = m_pages[nSel];
430 #if 0 // deactivate r65078 for the moment
431 if ( IsShownOnScreen() )
432 {
433 pPage->Show( true );
434 pPage->SetFocus();
435 }
436 else
437 {
438 // Postpone Show() until the control is actually shown.
439 // Otherwise this forces the containing toplevel window
440 // to show, even if it's just being created and called
441 // AddPage() without intent to show the window yet.
442 // We Show() the selected page in our OnSize handler,
443 // unless it already is shown.
444 }
445 #else
446 pPage->Show( true );
447 pPage->SetFocus();
448 #endif
449 }
450
451 m_selection = nSel;
452 GetPeer()->SetValue( m_selection + 1 ) ;
453 }
454
455 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec) )
456 {
457 bool status = false ;
458
459 SInt32 newSel = GetPeer()->GetValue() - 1 ;
460 if ( newSel != m_selection )
461 {
462 wxBookCtrlEvent changing(
463 wxEVT_NOTEBOOK_PAGE_CHANGING, m_windowId,
464 newSel , m_selection );
465 changing.SetEventObject( this );
466 HandleWindowEvent( changing );
467
468 if ( changing.IsAllowed() )
469 {
470 wxBookCtrlEvent event(
471 wxEVT_NOTEBOOK_PAGE_CHANGED, m_windowId,
472 newSel, m_selection );
473 event.SetEventObject( this );
474 HandleWindowEvent( event );
475
476 m_selection = newSel;
477 }
478 else
479 {
480 GetPeer()->SetValue( m_selection + 1 ) ;
481 }
482
483 status = true ;
484 }
485
486 return status ;
487 }
488
489 #endif