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