]> git.saurik.com Git - wxWidgets.git/blob - src/osx/notebook_osx.cpp
wxRTC text box layout fixes
[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
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& pt, long *flags) const
249 {
250 int resultV = wxNOT_FOUND;
251 #ifdef __WXOSX_CARBON__
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 #else
304 wxUnusedVar(pt);
305 wxUnusedVar(flags);
306 #endif
307 return resultV;
308 }
309
310 // Added by Mark Newsam
311 // When a page is added or deleted to the notebook this function updates
312 // information held in the control so that it matches the order
313 // the user would expect.
314 //
315 void wxNotebook::MacSetupTabs()
316 {
317 GetPeer()->SetupTabs(*this);
318 Refresh();
319 }
320
321 wxRect wxNotebook::GetPageRect() const
322 {
323 wxSize size = GetClientSize() ;
324
325 return wxRect( 0 , 0 , size.x , size.y ) ;
326 }
327
328 // ----------------------------------------------------------------------------
329 // wxNotebook callbacks
330 // ----------------------------------------------------------------------------
331
332 // @@@ OnSize() is used for setting the font when it's called for the first
333 // time because doing it in ::Create() doesn't work (for unknown reasons)
334 void wxNotebook::OnSize(wxSizeEvent& event)
335 {
336 unsigned int nCount = m_pages.Count();
337 wxRect rect = GetPageRect() ;
338
339 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
340 {
341 wxNotebookPage *pPage = m_pages[nPage];
342 pPage->SetSize(rect, wxSIZE_FORCE_EVENT);
343 }
344
345 #if 0 // deactivate r65078 for the moment
346 // If the selected page is hidden at this point, the notebook
347 // has become visible for the first time after creation, and
348 // we postponed showing the page in ChangePage().
349 // So show the selected page now.
350 if ( m_selection != wxNOT_FOUND )
351 {
352 wxNotebookPage *pPage = m_pages[m_selection];
353 if ( !pPage->IsShown() )
354 {
355 pPage->Show( true );
356 pPage->SetFocus();
357 }
358 }
359 #endif
360
361 // Processing continues to next OnSize
362 event.Skip();
363 }
364
365 void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
366 {
367 // is it our tab control?
368 if ( event.GetEventObject() == this )
369 ChangePage(event.GetOldSelection(), event.GetSelection());
370
371 // we want to give others a chance to process this message as well
372 event.Skip();
373 }
374
375 void wxNotebook::OnSetFocus(wxFocusEvent& event)
376 {
377 // set focus to the currently selected page if any
378 if ( m_selection != wxNOT_FOUND )
379 m_pages[m_selection]->SetFocus();
380
381 event.Skip();
382 }
383
384 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
385 {
386 if ( event.IsWindowChange() )
387 {
388 // change pages
389 AdvanceSelection( event.GetDirection() );
390 }
391 else
392 {
393 // we get this event in 2 cases
394 //
395 // a) one of our pages might have generated it because the user TABbed
396 // out from it in which case we should propagate the event upwards and
397 // our parent will take care of setting the focus to prev/next sibling
398 //
399 // or
400 //
401 // b) the parent panel wants to give the focus to us so that we
402 // forward it to our selected page. We can't deal with this in
403 // OnSetFocus() because we don't know which direction the focus came
404 // from in this case and so can't choose between setting the focus to
405 // first or last panel child
406 wxWindow *parent = GetParent();
407
408 // the cast is here to fix a GCC ICE
409 if ( ((wxWindow*)event.GetEventObject()) == parent )
410 {
411 // no, it doesn't come from child, case (b): forward to a page
412 if ( m_selection != wxNOT_FOUND )
413 {
414 // so that the page knows that the event comes from it's parent
415 // and is being propagated downwards
416 event.SetEventObject( this );
417
418 wxWindow *page = m_pages[m_selection];
419 if ( !page->HandleWindowEvent( event ) )
420 {
421 page->SetFocus();
422 }
423 //else: page manages focus inside it itself
424 }
425 else
426 {
427 // we have no pages - still have to give focus to _something_
428 SetFocus();
429 }
430 }
431 else
432 {
433 // it comes from our child, case (a), pass to the parent
434 if ( parent )
435 {
436 event.SetCurrentFocus( this );
437 parent->HandleWindowEvent( event );
438 }
439 }
440 }
441 }
442
443 // ----------------------------------------------------------------------------
444 // wxNotebook base class virtuals
445 // ----------------------------------------------------------------------------
446
447 #if wxUSE_CONSTRAINTS
448
449 // override these 2 functions to do nothing: everything is done in OnSize
450
451 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
452 {
453 // don't set the sizes of the pages - their correct size is not yet known
454 wxControl::SetConstraintSizes( false );
455 }
456
457 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
458 {
459 return true;
460 }
461
462 #endif // wxUSE_CONSTRAINTS
463
464 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
465 {
466 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
467 }
468
469 // ----------------------------------------------------------------------------
470 // wxNotebook helper functions
471 // ----------------------------------------------------------------------------
472
473 // hide the currently active panel and show the new one
474 void wxNotebook::ChangePage(int nOldSel, int nSel)
475 {
476 if (nOldSel == nSel)
477 return;
478
479 if ( nOldSel != wxNOT_FOUND )
480 m_pages[nOldSel]->Show( false );
481
482 if ( nSel != wxNOT_FOUND )
483 {
484 wxNotebookPage *pPage = m_pages[nSel];
485 #if 0 // deactivate r65078 for the moment
486 if ( IsShownOnScreen() )
487 {
488 pPage->Show( true );
489 pPage->SetFocus();
490 }
491 else
492 {
493 // Postpone Show() until the control is actually shown.
494 // Otherwise this forces the containing toplevel window
495 // to show, even if it's just being created and called
496 // AddPage() without intent to show the window yet.
497 // We Show() the selected page in our OnSize handler,
498 // unless it already is shown.
499 }
500 #else
501 pPage->Show( true );
502 pPage->SetFocus();
503 #endif
504 }
505
506 m_selection = nSel;
507 GetPeer()->SetValue( m_selection + 1 ) ;
508 }
509
510 bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec) )
511 {
512 bool status = false ;
513
514 SInt32 newSel = GetPeer()->GetValue() - 1 ;
515 if ( newSel != m_selection )
516 {
517 wxBookCtrlEvent changing(
518 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
519 newSel , m_selection );
520 changing.SetEventObject( this );
521 HandleWindowEvent( changing );
522
523 if ( changing.IsAllowed() )
524 {
525 wxBookCtrlEvent event(
526 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
527 newSel, m_selection );
528 event.SetEventObject( this );
529 HandleWindowEvent( event );
530
531 m_selection = newSel;
532 }
533 else
534 {
535 GetPeer()->SetValue( m_selection + 1 ) ;
536 }
537
538 status = true ;
539 }
540
541 return status ;
542 }
543
544 #endif