]>
Commit | Line | Data |
---|---|---|
e53b3d16 SC |
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 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
e53b3d16 SC |
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 | ||
e53b3d16 SC |
41 | bool wxNotebook::Create( wxWindow *parent, |
42 | wxWindowID id, | |
43 | const wxPoint& pos, | |
44 | const wxSize& size, | |
45 | long style, | |
46 | const wxString& name ) | |
d15694e8 SC |
47 | { |
48 | DontCreatePeer(); | |
49 | ||
e53b3d16 SC |
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 | ||
22756322 | 56 | SetPeer(wxWidgetImpl::CreateTabView(this,parent, id, pos, size, style, GetExtraStyle() )); |
03647350 | 57 | |
e53b3d16 SC |
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 | ||
681be2ef | 96 | if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection ) |
e53b3d16 SC |
97 | { |
98 | if ( flags & SetSelection_SendEvent ) | |
99 | { | |
100 | if ( !SendPageChangingEvent(nPage) ) | |
101 | { | |
102 | // vetoed by program | |
681be2ef | 103 | return m_selection; |
e53b3d16 SC |
104 | } |
105 | //else: program allows the page change | |
106 | ||
681be2ef | 107 | SendPageChangedEvent(m_selection, nPage); |
e53b3d16 SC |
108 | } |
109 | ||
681be2ef | 110 | ChangePage(m_selection, nPage); |
e53b3d16 SC |
111 | } |
112 | //else: no change | |
113 | ||
681be2ef | 114 | return m_selection; |
e53b3d16 SC |
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") ); | |
abfdefed | 148 | wxCHECK_MSG( HasImageList() && nImage < GetImageList()->GetImageCount(), false, |
e53b3d16 SC |
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 | ||
177 | MacSetupTabs(); | |
178 | ||
681be2ef VZ |
179 | if (m_selection >= (int)GetPageCount()) |
180 | m_selection = GetPageCount() - 1; | |
e53b3d16 | 181 | |
681be2ef VZ |
182 | if (m_selection >= 0) |
183 | m_pages[m_selection]->Show(true); | |
e53b3d16 SC |
184 | |
185 | InvalidateBestSize(); | |
186 | ||
187 | return page; | |
188 | } | |
189 | ||
190 | // remove all pages | |
191 | bool wxNotebook::DeleteAllPages() | |
192 | { | |
193 | WX_CLEAR_ARRAY(m_pages) ; | |
194 | MacSetupTabs(); | |
681be2ef | 195 | m_selection = wxNOT_FOUND ; |
e53b3d16 SC |
196 | InvalidateBestSize(); |
197 | ||
198 | return true; | |
199 | } | |
200 | ||
201 | // same as AddPage() but does it at given position | |
202 | bool wxNotebook::InsertPage(size_t nPage, | |
203 | wxNotebookPage *pPage, | |
204 | const wxString& strText, | |
205 | bool bSelect, | |
206 | int imageId ) | |
207 | { | |
208 | if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) ) | |
209 | return false; | |
210 | ||
211 | wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") ); | |
212 | ||
213 | // don't show pages by default (we'll need to adjust their size first) | |
214 | pPage->Show( false ) ; | |
215 | ||
216 | pPage->SetLabel( wxStripMenuCodes(strText) ); | |
217 | ||
218 | m_images.Insert( imageId, nPage ); | |
219 | ||
220 | MacSetupTabs(); | |
221 | ||
222 | wxRect rect = GetPageRect() ; | |
223 | pPage->SetSize( rect ); | |
224 | if ( pPage->GetAutoLayout() ) | |
225 | pPage->Layout(); | |
226 | ||
227 | // now deal with the selection | |
228 | // --------------------------- | |
229 | ||
230 | // if the inserted page is before the selected one, we must update the | |
231 | // index of the selected page | |
232 | ||
681be2ef | 233 | if ( int(nPage) <= m_selection ) |
e53b3d16 | 234 | { |
681be2ef | 235 | m_selection++; |
e53b3d16 SC |
236 | |
237 | // while this still is the same page showing, we need to update the tabs | |
22756322 | 238 | GetPeer()->SetValue( m_selection + 1 ) ; |
e53b3d16 SC |
239 | } |
240 | ||
60d5c563 | 241 | DoSetSelectionAfterInsertion(nPage, bSelect); |
e53b3d16 SC |
242 | |
243 | InvalidateBestSize(); | |
244 | ||
245 | return true; | |
246 | } | |
247 | ||
ddd7e430 | 248 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const |
e53b3d16 | 249 | { |
dfd0756f | 250 | return GetPeer()->TabHitTest(pt,flags); |
e53b3d16 SC |
251 | } |
252 | ||
253 | // Added by Mark Newsam | |
254 | // When a page is added or deleted to the notebook this function updates | |
255 | // information held in the control so that it matches the order | |
256 | // the user would expect. | |
257 | // | |
258 | void wxNotebook::MacSetupTabs() | |
259 | { | |
22756322 | 260 | GetPeer()->SetupTabs(*this); |
e53b3d16 SC |
261 | Refresh(); |
262 | } | |
263 | ||
264 | wxRect wxNotebook::GetPageRect() const | |
265 | { | |
266 | wxSize size = GetClientSize() ; | |
267 | ||
268 | return wxRect( 0 , 0 , size.x , size.y ) ; | |
269 | } | |
270 | ||
271 | // ---------------------------------------------------------------------------- | |
272 | // wxNotebook callbacks | |
273 | // ---------------------------------------------------------------------------- | |
274 | ||
275 | // @@@ OnSize() is used for setting the font when it's called for the first | |
276 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
277 | void wxNotebook::OnSize(wxSizeEvent& event) | |
278 | { | |
279 | unsigned int nCount = m_pages.Count(); | |
280 | wxRect rect = GetPageRect() ; | |
281 | ||
282 | for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) | |
283 | { | |
284 | wxNotebookPage *pPage = m_pages[nPage]; | |
ff94c563 | 285 | pPage->SetSize(rect, wxSIZE_FORCE_EVENT); |
e53b3d16 SC |
286 | } |
287 | ||
43321b80 | 288 | #if 0 // deactivate r65078 for the moment |
5bdeabaf VZ |
289 | // If the selected page is hidden at this point, the notebook |
290 | // has become visible for the first time after creation, and | |
291 | // we postponed showing the page in ChangePage(). | |
292 | // So show the selected page now. | |
681be2ef | 293 | if ( m_selection != wxNOT_FOUND ) |
5bdeabaf | 294 | { |
681be2ef | 295 | wxNotebookPage *pPage = m_pages[m_selection]; |
5bdeabaf VZ |
296 | if ( !pPage->IsShown() ) |
297 | { | |
298 | pPage->Show( true ); | |
299 | pPage->SetFocus(); | |
300 | } | |
301 | } | |
43321b80 | 302 | #endif |
5bdeabaf | 303 | |
e53b3d16 SC |
304 | // Processing continues to next OnSize |
305 | event.Skip(); | |
306 | } | |
307 | ||
308 | void wxNotebook::OnSelChange(wxBookCtrlEvent& event) | |
309 | { | |
310 | // is it our tab control? | |
311 | if ( event.GetEventObject() == this ) | |
312 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
313 | ||
314 | // we want to give others a chance to process this message as well | |
315 | event.Skip(); | |
316 | } | |
317 | ||
318 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
319 | { | |
320 | // set focus to the currently selected page if any | |
681be2ef VZ |
321 | if ( m_selection != wxNOT_FOUND ) |
322 | m_pages[m_selection]->SetFocus(); | |
e53b3d16 SC |
323 | |
324 | event.Skip(); | |
325 | } | |
326 | ||
327 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
328 | { | |
329 | if ( event.IsWindowChange() ) | |
330 | { | |
331 | // change pages | |
332 | AdvanceSelection( event.GetDirection() ); | |
333 | } | |
334 | else | |
335 | { | |
336 | // we get this event in 2 cases | |
337 | // | |
338 | // a) one of our pages might have generated it because the user TABbed | |
339 | // out from it in which case we should propagate the event upwards and | |
340 | // our parent will take care of setting the focus to prev/next sibling | |
341 | // | |
342 | // or | |
343 | // | |
344 | // b) the parent panel wants to give the focus to us so that we | |
345 | // forward it to our selected page. We can't deal with this in | |
346 | // OnSetFocus() because we don't know which direction the focus came | |
347 | // from in this case and so can't choose between setting the focus to | |
348 | // first or last panel child | |
349 | wxWindow *parent = GetParent(); | |
350 | ||
351 | // the cast is here to fix a GCC ICE | |
352 | if ( ((wxWindow*)event.GetEventObject()) == parent ) | |
353 | { | |
354 | // no, it doesn't come from child, case (b): forward to a page | |
681be2ef | 355 | if ( m_selection != wxNOT_FOUND ) |
e53b3d16 SC |
356 | { |
357 | // so that the page knows that the event comes from it's parent | |
358 | // and is being propagated downwards | |
359 | event.SetEventObject( this ); | |
360 | ||
681be2ef | 361 | wxWindow *page = m_pages[m_selection]; |
e53b3d16 SC |
362 | if ( !page->HandleWindowEvent( event ) ) |
363 | { | |
364 | page->SetFocus(); | |
365 | } | |
366 | //else: page manages focus inside it itself | |
367 | } | |
368 | else | |
369 | { | |
370 | // we have no pages - still have to give focus to _something_ | |
371 | SetFocus(); | |
372 | } | |
373 | } | |
374 | else | |
375 | { | |
376 | // it comes from our child, case (a), pass to the parent | |
377 | if ( parent ) | |
378 | { | |
379 | event.SetCurrentFocus( this ); | |
380 | parent->HandleWindowEvent( event ); | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // wxNotebook base class virtuals | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
390 | #if wxUSE_CONSTRAINTS | |
391 | ||
392 | // override these 2 functions to do nothing: everything is done in OnSize | |
393 | ||
394 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) | |
395 | { | |
396 | // don't set the sizes of the pages - their correct size is not yet known | |
397 | wxControl::SetConstraintSizes( false ); | |
398 | } | |
399 | ||
400 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) | |
401 | { | |
402 | return true; | |
403 | } | |
404 | ||
405 | #endif // wxUSE_CONSTRAINTS | |
406 | ||
407 | void wxNotebook::Command(wxCommandEvent& WXUNUSED(event)) | |
408 | { | |
409 | wxFAIL_MSG(wxT("wxNotebook::Command not implemented")); | |
410 | } | |
411 | ||
412 | // ---------------------------------------------------------------------------- | |
413 | // wxNotebook helper functions | |
414 | // ---------------------------------------------------------------------------- | |
415 | ||
416 | // hide the currently active panel and show the new one | |
417 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
418 | { | |
419 | if (nOldSel == nSel) | |
420 | return; | |
421 | ||
7e837615 | 422 | if ( nOldSel != wxNOT_FOUND ) |
e53b3d16 SC |
423 | m_pages[nOldSel]->Show( false ); |
424 | ||
7e837615 | 425 | if ( nSel != wxNOT_FOUND ) |
e53b3d16 SC |
426 | { |
427 | wxNotebookPage *pPage = m_pages[nSel]; | |
43321b80 | 428 | #if 0 // deactivate r65078 for the moment |
5bdeabaf VZ |
429 | if ( IsShownOnScreen() ) |
430 | { | |
431 | pPage->Show( true ); | |
432 | pPage->SetFocus(); | |
433 | } | |
434 | else | |
435 | { | |
436 | // Postpone Show() until the control is actually shown. | |
437 | // Otherwise this forces the containing toplevel window | |
438 | // to show, even if it's just being created and called | |
439 | // AddPage() without intent to show the window yet. | |
440 | // We Show() the selected page in our OnSize handler, | |
441 | // unless it already is shown. | |
442 | } | |
43321b80 SC |
443 | #else |
444 | pPage->Show( true ); | |
445 | pPage->SetFocus(); | |
446 | #endif | |
e53b3d16 SC |
447 | } |
448 | ||
681be2ef | 449 | m_selection = nSel; |
22756322 | 450 | GetPeer()->SetValue( m_selection + 1 ) ; |
e53b3d16 SC |
451 | } |
452 | ||
0faf03bf | 453 | bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
e53b3d16 SC |
454 | { |
455 | bool status = false ; | |
456 | ||
22756322 | 457 | SInt32 newSel = GetPeer()->GetValue() - 1 ; |
681be2ef | 458 | if ( newSel != m_selection ) |
e53b3d16 SC |
459 | { |
460 | wxBookCtrlEvent changing( | |
461 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId, | |
681be2ef | 462 | newSel , m_selection ); |
e53b3d16 SC |
463 | changing.SetEventObject( this ); |
464 | HandleWindowEvent( changing ); | |
465 | ||
466 | if ( changing.IsAllowed() ) | |
467 | { | |
468 | wxBookCtrlEvent event( | |
469 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId, | |
681be2ef | 470 | newSel, m_selection ); |
e53b3d16 SC |
471 | event.SetEventObject( this ); |
472 | HandleWindowEvent( event ); | |
821073e3 VZ |
473 | |
474 | m_selection = newSel; | |
e53b3d16 SC |
475 | } |
476 | else | |
477 | { | |
22756322 | 478 | GetPeer()->SetValue( m_selection + 1 ) ; |
e53b3d16 SC |
479 | } |
480 | ||
481 | status = true ; | |
482 | } | |
483 | ||
484 | return status ; | |
485 | } | |
486 | ||
487 | #endif |