]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
don't encode collons in file:// URLs under Windows as IE is incapable of opening...
[wxWidgets.git] / src / mac / carbon / notebmac.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/notebmac.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/mac/uma.h"
28
29
30 // check that the page index is valid
31 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
32
33
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
36
37 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
38 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
39
40 EVT_SIZE(wxNotebook::OnSize)
41 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
43 END_EVENT_TABLE()
44
45 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase)
46 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
47
48
49 // common part of all ctors
50 void wxNotebook::Init()
51 {
52 m_nSelection = -1;
53 }
54
55 // default for dynamic class
56 wxNotebook::wxNotebook()
57 {
58 Init();
59 }
60
61 // the same arguments as for wxControl
62 wxNotebook::wxNotebook( wxWindow *parent,
63 wxWindowID id,
64 const wxPoint& pos,
65 const wxSize& size,
66 long style,
67 const wxString& name )
68 {
69 Init();
70
71 Create( parent, id, pos, size, style, name );
72 }
73
74 bool wxNotebook::Create( wxWindow *parent,
75 wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name )
80 {
81 m_macIsUserPane = false ;
82
83 if (! (style & wxBK_ALIGN_MASK))
84 style |= wxBK_TOP;
85
86 if ( !wxNotebookBase::Create( parent, id, pos, size, style, name ) )
87 return false;
88
89 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
90
91 if ( bounds.right <= bounds.left )
92 bounds.right = bounds.left + 100;
93 if ( bounds.bottom <= bounds.top )
94 bounds.bottom = bounds.top + 100;
95
96 UInt16 tabstyle = kControlTabDirectionNorth;
97 if ( HasFlag(wxBK_LEFT) )
98 tabstyle = kControlTabDirectionWest;
99 else if ( HasFlag( wxBK_RIGHT ) )
100 tabstyle = kControlTabDirectionEast;
101 else if ( HasFlag( wxBK_BOTTOM ) )
102 tabstyle = kControlTabDirectionSouth;
103
104 ControlTabSize tabsize;
105 switch (GetWindowVariant())
106 {
107 case wxWINDOW_VARIANT_MINI:
108 tabsize = 3 ;
109 break;
110
111 case wxWINDOW_VARIANT_SMALL:
112 tabsize = kControlTabSizeSmall;
113 break;
114
115 default:
116 tabsize = kControlTabSizeLarge;
117 break;
118 }
119
120 m_peer = new wxMacControl( this );
121 OSStatus err = CreateTabsControl(
122 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
123 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
124 verify_noerr( err );
125
126 MacPostControlCreate( pos, size );
127
128 return true ;
129 }
130
131 // dtor
132 wxNotebook::~wxNotebook()
133 {
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxNotebook accessors
138 // ----------------------------------------------------------------------------
139
140 void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding))
141 {
142 // unsupported by OS
143 }
144
145 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
146 {
147 // unsupported by OS
148 }
149
150 void wxNotebook::SetPageSize(const wxSize& size)
151 {
152 SetSize( CalcSizeFromPage( size ) );
153 }
154
155 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
156 {
157 return DoGetSizeFromClientSize( sizePage );
158 }
159
160 int wxNotebook::DoSetSelection(size_t nPage, int flags)
161 {
162 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("DoSetSelection: invalid notebook page") );
163
164 if ( m_nSelection == wxNOT_FOUND || nPage != (size_t)m_nSelection )
165 {
166 if ( flags & SetSelection_SendEvent )
167 {
168 if ( !SendPageChangingEvent(nPage) )
169 {
170 // vetoed by program
171 return m_nSelection;
172 }
173 //else: program allows the page change
174
175 SendPageChangedEvent(m_nSelection, nPage);
176 }
177
178 ChangePage(m_nSelection, nPage);
179 }
180 //else: no change
181
182 return m_nSelection;
183 }
184
185 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
186 {
187 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") );
188
189 wxNotebookPage *page = m_pages[nPage];
190 page->SetLabel(wxStripMenuCodes(strText));
191 MacSetupTabs();
192
193 return true;
194 }
195
196 wxString wxNotebook::GetPageText(size_t nPage) const
197 {
198 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") );
199
200 wxNotebookPage *page = m_pages[nPage];
201
202 return page->GetLabel();
203 }
204
205 int wxNotebook::GetPageImage(size_t nPage) const
206 {
207 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") );
208
209 return m_images[nPage];
210 }
211
212 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
213 {
214 wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
215 wxT("SetPageImage: invalid notebook page") );
216 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
217 wxT("SetPageImage: invalid image index") );
218
219 if ( nImage != m_images[nPage] )
220 {
221 // if the item didn't have an icon before or, on the contrary, did have
222 // it but has lost it now, its size will change - but if the icon just
223 // changes, it won't
224 m_images[nPage] = nImage;
225
226 MacSetupTabs() ;
227 }
228
229 return true;
230 }
231
232 // ----------------------------------------------------------------------------
233 // wxNotebook operations
234 // ----------------------------------------------------------------------------
235
236 // remove one page from the notebook, without deleting the window
237 wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
238 {
239 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL,
240 wxT("DoRemovePage: invalid notebook page") );
241
242 wxNotebookPage* page = m_pages[nPage] ;
243 m_pages.RemoveAt(nPage);
244
245 MacSetupTabs();
246
247 if (m_nSelection >= (int)GetPageCount())
248 m_nSelection = GetPageCount() - 1;
249
250 if (m_nSelection >= 0)
251 m_pages[m_nSelection]->Show(true);
252
253 InvalidateBestSize();
254
255 return page;
256 }
257
258 // remove all pages
259 bool wxNotebook::DeleteAllPages()
260 {
261 WX_CLEAR_ARRAY(m_pages) ;
262 MacSetupTabs();
263 m_nSelection = -1 ;
264 InvalidateBestSize();
265
266 return true;
267 }
268
269 // same as AddPage() but does it at given position
270 bool wxNotebook::InsertPage(size_t nPage,
271 wxNotebookPage *pPage,
272 const wxString& strText,
273 bool bSelect,
274 int imageId )
275 {
276 if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) )
277 return false;
278
279 wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") );
280
281 // don't show pages by default (we'll need to adjust their size first)
282 pPage->Show( false ) ;
283
284 pPage->SetLabel( wxStripMenuCodes(strText) );
285
286 m_images.Insert( imageId, nPage );
287
288 MacSetupTabs();
289
290 wxRect rect = GetPageRect() ;
291 pPage->SetSize( rect );
292 if ( pPage->GetAutoLayout() )
293 pPage->Layout();
294
295 // now deal with the selection
296 // ---------------------------
297
298 // if the inserted page is before the selected one, we must update the
299 // index of the selected page
300
301 if ( int(nPage) <= m_nSelection )
302 {
303 m_nSelection++;
304
305 // while this still is the same page showing, we need to update the tabs
306 m_peer->SetValue( m_nSelection + 1 ) ;
307 }
308
309 // some page should be selected: either this one or the first one if there
310 // is still no selection
311 int selNew = -1;
312 if ( bSelect )
313 selNew = nPage;
314 else if ( m_nSelection == -1 )
315 selNew = 0;
316
317 if ( selNew != -1 )
318 SetSelection( selNew );
319
320 InvalidateBestSize();
321
322 return true;
323 }
324
325 int wxNotebook::HitTest(const wxPoint& pt, long * flags) const
326 {
327 int resultV = wxNOT_FOUND;
328
329 const int countPages = GetPageCount();
330
331 // we have to convert from Client to Window relative coordinates
332 wxPoint adjustedPt = pt + GetClientAreaOrigin();
333 // and now to HIView native ones
334 adjustedPt.x -= MacGetLeftBorderSize() ;
335 adjustedPt.y -= MacGetTopBorderSize() ;
336
337 HIPoint hipoint= { adjustedPt.x , adjustedPt.y } ;
338 HIViewPartCode outPart = 0 ;
339 OSStatus err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
340
341 int max = m_peer->GetMaximum() ;
342 if ( outPart == 0 && max > 0 )
343 {
344 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
345 // so we have to go some extra miles to make sure we select something different
346 // and try again ..
347 int val = m_peer->GetValue() ;
348 int maxval = max ;
349 if ( max == 1 )
350 {
351 m_peer->SetMaximum( 2 ) ;
352 maxval = 2 ;
353 }
354
355 if ( val == 1 )
356 m_peer->SetValue( maxval ) ;
357 else
358 m_peer->SetValue( 1 ) ;
359
360 err = HIViewGetPartHit( m_peer->GetControlRef(), &hipoint, &outPart );
361
362 m_peer->SetValue( val ) ;
363 if ( max == 1 )
364 m_peer->SetMaximum( 1 ) ;
365 }
366
367 if ( outPart >= 1 && outPart <= countPages )
368 resultV = outPart - 1 ;
369
370 if (flags != NULL)
371 {
372 *flags = 0;
373
374 // we cannot differentiate better
375 if (resultV >= 0)
376 *flags |= wxBK_HITTEST_ONLABEL;
377 else
378 *flags |= wxBK_HITTEST_NOWHERE;
379 }
380
381 return resultV;
382 }
383
384 // Added by Mark Newsam
385 // When a page is added or deleted to the notebook this function updates
386 // information held in the control so that it matches the order
387 // the user would expect.
388 //
389 void wxNotebook::MacSetupTabs()
390 {
391 m_peer->SetMaximum( GetPageCount() ) ;
392
393 wxNotebookPage *page;
394 ControlTabInfoRecV1 info;
395
396 const size_t countPages = GetPageCount();
397 for (size_t ii = 0; ii < countPages; ii++)
398 {
399 page = m_pages[ii];
400 info.version = kControlTabInfoVersionOne;
401 info.iconSuiteID = 0;
402 wxCFStringRef cflabel( page->GetLabel(), m_font.GetEncoding() ) ;
403 info.name = cflabel ;
404 m_peer->SetData<ControlTabInfoRecV1>( ii + 1, kControlTabInfoTag, &info ) ;
405
406 if ( GetImageList() && GetPageImage(ii) >= 0 )
407 {
408 const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage( ii ) ) ;
409 if ( bmap.Ok() )
410 {
411 ControlButtonContentInfo info ;
412
413 wxMacCreateBitmapButton( &info, bmap ) ;
414
415 OSStatus err = m_peer->SetData<ControlButtonContentInfo>( ii + 1, kControlTabImageContentTag, &info );
416 wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ;
417
418 wxMacReleaseBitmapButton( &info ) ;
419 }
420 }
421
422 m_peer->SetTabEnabled( ii + 1, true ) ;
423 }
424
425 Refresh();
426 }
427
428 wxRect wxNotebook::GetPageRect() const
429 {
430 wxSize size = GetClientSize() ;
431
432 return wxRect( 0 , 0 , size.x , size.y ) ;
433 }
434
435 // ----------------------------------------------------------------------------
436 // wxNotebook callbacks
437 // ----------------------------------------------------------------------------
438
439 // @@@ OnSize() is used for setting the font when it's called for the first
440 // time because doing it in ::Create() doesn't work (for unknown reasons)
441 void wxNotebook::OnSize(wxSizeEvent& event)
442 {
443 unsigned int nCount = m_pages.Count();
444 wxRect rect = GetPageRect() ;
445
446 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
447 {
448 wxNotebookPage *pPage = m_pages[nPage];
449 pPage->SetSize(rect);
450 if ( pPage->GetAutoLayout() )
451 pPage->Layout();
452 }
453
454 // Processing continues to next OnSize
455 event.Skip();
456 }
457
458 void wxNotebook::OnSelChange(wxNotebookEvent& event)
459 {
460 // is it our tab control?
461 if ( event.GetEventObject() == this )
462 ChangePage(event.GetOldSelection(), event.GetSelection());
463
464 // we want to give others a chance to process this message as well
465 event.Skip();
466 }
467
468 void wxNotebook::OnSetFocus(wxFocusEvent& event)
469 {
470 // set focus to the currently selected page if any
471 if ( m_nSelection != -1 )
472 m_pages[m_nSelection]->SetFocus();
473
474 event.Skip();
475 }
476
477 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
478 {
479 if ( event.IsWindowChange() )
480 {
481 // change pages
482 AdvanceSelection( event.GetDirection() );
483 }
484 else
485 {
486 // we get this event in 2 cases
487 //
488 // a) one of our pages might have generated it because the user TABbed
489 // out from it in which case we should propagate the event upwards and
490 // our parent will take care of setting the focus to prev/next sibling
491 //
492 // or
493 //
494 // b) the parent panel wants to give the focus to us so that we
495 // forward it to our selected page. We can't deal with this in
496 // OnSetFocus() because we don't know which direction the focus came
497 // from in this case and so can't choose between setting the focus to
498 // first or last panel child
499 wxWindow *parent = GetParent();
500
501 // the cast is here to fix a GCC ICE
502 if ( ((wxWindow*)event.GetEventObject()) == parent )
503 {
504 // no, it doesn't come from child, case (b): forward to a page
505 if ( m_nSelection != -1 )
506 {
507 // so that the page knows that the event comes from it's parent
508 // and is being propagated downwards
509 event.SetEventObject( this );
510
511 wxWindow *page = m_pages[m_nSelection];
512 if ( !page->HandleWindowEvent( event ) )
513 {
514 page->SetFocus();
515 }
516 //else: page manages focus inside it itself
517 }
518 else
519 {
520 // we have no pages - still have to give focus to _something_
521 SetFocus();
522 }
523 }
524 else
525 {
526 // it comes from our child, case (a), pass to the parent
527 if ( parent )
528 {
529 event.SetCurrentFocus( this );
530 parent->HandleWindowEvent( event );
531 }
532 }
533 }
534 }
535
536 // ----------------------------------------------------------------------------
537 // wxNotebook base class virtuals
538 // ----------------------------------------------------------------------------
539
540 #if wxUSE_CONSTRAINTS
541
542 // override these 2 functions to do nothing: everything is done in OnSize
543
544 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
545 {
546 // don't set the sizes of the pages - their correct size is not yet known
547 wxControl::SetConstraintSizes( false );
548 }
549
550 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
551 {
552 return true;
553 }
554
555 #endif // wxUSE_CONSTRAINTS
556
557 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
558 {
559 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
560 }
561
562 // ----------------------------------------------------------------------------
563 // wxNotebook helper functions
564 // ----------------------------------------------------------------------------
565
566 // hide the currently active panel and show the new one
567 void wxNotebook::ChangePage(int nOldSel, int nSel)
568 {
569 if (nOldSel == nSel)
570 return;
571
572 if ( nOldSel != -1 )
573 m_pages[nOldSel]->Show( false );
574
575 if ( nSel != -1 )
576 {
577 wxNotebookPage *pPage = m_pages[nSel];
578 pPage->Show( true );
579 pPage->SetFocus();
580 }
581
582 m_nSelection = nSel;
583 m_peer->SetValue( m_nSelection + 1 ) ;
584 }
585
586 wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
587 {
588 OSStatus status = eventNotHandledErr ;
589
590 SInt32 newSel = m_peer->GetValue() - 1 ;
591 if ( newSel != m_nSelection )
592 {
593 wxNotebookEvent changing(
594 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId,
595 newSel , m_nSelection );
596 changing.SetEventObject( this );
597 HandleWindowEvent( changing );
598
599 if ( changing.IsAllowed() )
600 {
601 wxNotebookEvent event(
602 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId,
603 newSel, m_nSelection );
604 event.SetEventObject( this );
605 HandleWindowEvent( event );
606 }
607 else
608 {
609 m_peer->SetValue( m_nSelection + 1 ) ;
610 }
611
612 status = noErr ;
613 }
614
615 return (wxInt32)status ;
616 }
617
618 #endif