]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/toolbar.cpp
supporting disabled items, closes #11130
[wxWidgets.git] / src / osx / carbon / toolbar.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/toolbar.cpp
489468fe
SC
3// Purpose: wxToolBar
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_TOOLBAR
15
16#include "wx/toolbar.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif
21
22#include "wx/app.h"
524c47aa 23#include "wx/osx/private.h"
489468fe
SC
24#include "wx/geometry.h"
25#include "wx/sysopt.h"
26
27
28const short kwxMacToolBarToolDefaultWidth = 16;
29const short kwxMacToolBarToolDefaultHeight = 16;
30const short kwxMacToolBarTopMargin = 4;
31const short kwxMacToolBarLeftMargin = 4;
32const short kwxMacToolBorder = 0;
33const short kwxMacToolSpacing = 6;
34
489468fe
SC
35BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
36 EVT_PAINT( wxToolBar::OnPaint )
37END_EVENT_TABLE()
38
39
40#pragma mark -
41#pragma mark Tool Implementation
42
489468fe
SC
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
48
49// when embedding native controls in the native toolbar we must make sure the
50// control does not get deleted behind our backs, so the retain count gets increased
51// (after creation it is 1), first be the creation of the custom HIToolbarItem wrapper
52// object, and second by the code 'creating' the custom HIView (which is the same as the
53// already existing native control, therefore we just increase the ref count)
54// when this view is removed from the native toolbar its count gets decremented again
55// and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
56// so in the end the control lives with a refcount of one and can be disposed of by the
57// wxControl code. For embedded controls on a non-native toolbar this ref count is less
58// so we can only test against a range, not a specific value of the refcount.
59
60class wxToolBarTool : public wxToolBarToolBase
61{
62public:
63 wxToolBarTool(
64 wxToolBar *tbar,
65 int id,
66 const wxString& label,
67 const wxBitmap& bmpNormal,
68 const wxBitmap& bmpDisabled,
69 wxItemKind kind,
70 wxObject *clientData,
71 const wxString& shortHelp,
72 const wxString& longHelp );
73
74 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
75 : wxToolBarToolBase(tbar, control, label)
76 {
77 Init();
78 if (control != NULL)
79 SetControlHandle( (ControlRef) control->GetHandle() );
80 }
81
82 virtual ~wxToolBarTool()
83 {
84 ClearControl();
85 }
86
87 WXWidget GetControlHandle()
88 {
89 return (WXWidget) m_controlHandle;
90 }
91
92 void SetControlHandle( ControlRef handle )
93 {
94 m_controlHandle = handle;
95 }
96
97 void SetPosition( const wxPoint& position );
98
99 void ClearControl()
100 {
101 if ( m_controlHandle )
102 {
103 if ( !IsControl() )
104 DisposeControl( m_controlHandle );
105 else
106 {
107 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
108 // proper wxControl destructor
109 }
110 m_controlHandle = NULL ;
111 }
112
292e5e1f 113#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
114 if ( m_toolbarItemRef )
115 {
116 CFIndex count = CFGetRetainCount( m_toolbarItemRef ) ;
117 // different behaviour under Leopard
118 if ( UMAGetSystemVersion() < 0x1050 )
119 {
120 if ( count != 1 )
121 {
122 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
123 }
124 }
125 wxTheApp->MacAddToAutorelease(m_toolbarItemRef);
126 CFRelease(m_toolbarItemRef);
127 m_toolbarItemRef = NULL;
128 }
292e5e1f 129#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
130 }
131
132 wxSize GetSize() const
133 {
134 wxSize curSize;
135
136 if ( IsControl() )
137 {
138 curSize = GetControl()->GetSize();
139 }
140 else if ( IsButton() )
141 {
142 curSize = GetToolBar()->GetToolSize();
143 }
144 else
145 {
146 // separator size
147 curSize = GetToolBar()->GetToolSize();
f0ceebb7 148 if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
149 curSize.y /= 4;
150 else
151 curSize.x /= 4;
152 }
153
154 return curSize;
155 }
156
157 wxPoint GetPosition() const
158 {
159 return wxPoint( m_x, m_y );
160 }
161
2bed17c8 162 virtual bool Enable( bool enable );
489468fe
SC
163
164 void UpdateToggleImage( bool toggle );
165
2bed17c8 166 virtual bool Toggle(bool toggle)
489468fe 167 {
2bed17c8
SC
168 if ( wxToolBarToolBase::Toggle( toggle ) == false )
169 return false;
bb2212e6 170
2bed17c8
SC
171 UpdateToggleImage(toggle);
172 return true;
173 }
bb2212e6 174
2bed17c8
SC
175 void UpdateHelpStrings()
176 {
bb2212e6 177#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
178 if ( m_toolbarItemRef )
179 {
402c3347 180 wxFontEncoding enc = GetToolBarFontEncoding();
489468fe
SC
181
182 HIToolbarItemSetHelpText(
183 m_toolbarItemRef,
184 wxCFStringRef( GetShortHelp(), enc ),
185 wxCFStringRef( GetLongHelp(), enc ) );
186 }
2bed17c8 187#endif
489468fe 188 }
bb2212e6 189
2bed17c8 190 virtual bool SetShortHelp(const wxString& help)
489468fe 191 {
2bed17c8
SC
192 if ( wxToolBarToolBase::SetShortHelp( help ) == false )
193 return false;
bb2212e6
VZ
194
195 UpdateHelpStrings();
2bed17c8 196 return true;
489468fe 197 }
bb2212e6 198
2bed17c8 199 virtual bool SetLongHelp(const wxString& help)
489468fe 200 {
2bed17c8
SC
201 if ( wxToolBarToolBase::SetLongHelp( help ) == false )
202 return false;
203
204 UpdateHelpStrings();
205 return true;
489468fe
SC
206 }
207
bb2212e6 208 virtual void SetNormalBitmap(const wxBitmap& bmp)
489468fe 209 {
2bed17c8
SC
210 wxToolBarToolBase::SetNormalBitmap(bmp);
211 UpdateToggleImage(CanBeToggled() && IsToggled());
489468fe 212 }
bb2212e6 213
e3e061ee
VZ
214 virtual void SetLabel(const wxString& label)
215 {
216 wxToolBarToolBase::SetLabel(label);
2bed17c8 217#if wxOSX_USE_NATIVE_TOOLBAR
e3e061ee
VZ
218 if ( m_toolbarItemRef )
219 {
e3e061ee
VZ
220 // strip mnemonics from the label for compatibility with the usual
221 // labels in wxStaticText sense
222 wxString labelStr = wxStripMenuCodes(label);
223
402c3347
VZ
224 HIToolbarItemSetLabel(
225 m_toolbarItemRef,
226 wxCFStringRef(labelStr, GetToolBarFontEncoding()) );
e3e061ee 227 }
2bed17c8
SC
228#endif
229 }
230
231#if wxOSX_USE_NATIVE_TOOLBAR
232 void SetToolbarItemRef( HIToolbarItemRef ref )
233 {
234 if ( m_controlHandle )
235 HideControl( m_controlHandle );
236 if ( m_toolbarItemRef )
237 CFRelease( m_toolbarItemRef );
238
239 m_toolbarItemRef = ref;
240 UpdateHelpStrings();
241 }
242
243 HIToolbarItemRef GetToolbarItemRef() const
244 {
245 return m_toolbarItemRef;
246 }
247
248 void SetIndex( CFIndex idx )
249 {
250 m_index = idx;
251 }
252
253 CFIndex GetIndex() const
254 {
255 return m_index;
e3e061ee
VZ
256 }
257#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
258
259private:
402c3347
VZ
260#if wxOSX_USE_NATIVE_TOOLBAR
261 wxFontEncoding GetToolBarFontEncoding() const
262 {
263 wxFont f;
402c3347
VZ
264 if ( GetToolBar() )
265 f = GetToolBar()->GetFont();
266 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
267 }
268#endif // wxOSX_USE_NATIVE_TOOLBAR
269
489468fe
SC
270 void Init()
271 {
272 m_controlHandle = NULL;
273
292e5e1f 274#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
275 m_toolbarItemRef = NULL;
276 m_index = -1;
277#endif
278 }
279
280 ControlRef m_controlHandle;
281 wxCoord m_x;
282 wxCoord m_y;
283
292e5e1f 284#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
285 HIToolbarItemRef m_toolbarItemRef;
286 // position in its toolbar, -1 means not inserted
287 CFIndex m_index;
288#endif
289};
290
291static const EventTypeSpec eventList[] =
292{
293 { kEventClassControl, kEventControlHit },
294 { kEventClassControl, kEventControlHitTest },
295};
296
297static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
298{
299 OSStatus result = eventNotHandledErr;
300 ControlRef controlRef;
301 wxMacCarbonEvent cEvent( event );
302
303 cEvent.GetParameter( kEventParamDirectObject, &controlRef );
304
305 switch ( GetEventKind( event ) )
306 {
307 case kEventControlHit:
308 {
309 wxToolBarTool *tbartool = (wxToolBarTool*)data;
310 wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL;
311 if ((tbartool != NULL) && tbartool->CanBeToggled())
312 {
313 bool shouldToggle;
314
315 shouldToggle = !tbartool->IsToggled();
316
317 tbar->ToggleTool( tbartool->GetId(), shouldToggle );
318 }
319
320 if (tbartool != NULL)
321 tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
322 result = noErr;
323 }
324 break;
325
326 case kEventControlHitTest:
327 {
328 HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation);
329 HIRect rect;
330 HIViewGetBounds( controlRef, &rect );
331
332 ControlPartCode pc = kControlNoPart;
333 if ( CGRectContainsPoint( rect, pt ) )
334 pc = kControlIconPart;
335 cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc );
336 result = noErr;
337 }
338 break;
339
340 default:
341 break;
342 }
343
344 return result;
345}
346
347static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
348{
349 OSStatus result = eventNotHandledErr;
350
351 switch ( GetEventClass( event ) )
352 {
353 case kEventClassControl:
354 result = wxMacToolBarToolControlEventHandler( handler, event, data );
355 break;
356
357 default:
358 break;
359 }
360
361 return result;
362}
363
364DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
365
292e5e1f 366#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
367
368static const EventTypeSpec toolBarEventList[] =
369{
370 { kEventClassToolbarItem, kEventToolbarItemPerformAction },
371};
372
373static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
374{
375 OSStatus result = eventNotHandledErr;
376
377 switch ( GetEventKind( event ) )
378 {
379 case kEventToolbarItemPerformAction:
380 {
381 wxToolBarTool* tbartool = (wxToolBarTool*) data;
382 if ( tbartool != NULL )
383 {
384 wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar());
385 int toolID = tbartool->GetId();
386
387 if ( tbartool->CanBeToggled() )
388 {
389 if ( tbar != NULL )
390 tbar->ToggleTool(toolID, !tbartool->IsToggled() );
391 }
392
393 if ( tbar != NULL )
394 tbar->OnLeftClick( toolID, tbartool->IsToggled() );
395 result = noErr;
396 }
397 }
398 break;
399
400 default:
401 break;
402 }
403
404 return result;
405}
406
407static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
408{
409 OSStatus result = eventNotHandledErr;
410
411 switch ( GetEventClass( event ) )
412 {
413 case kEventClassToolbarItem:
414 result = wxMacToolBarCommandEventHandler( handler, event, data );
415 break;
416
417 default:
418 break;
419 }
420
421 return result;
422}
423
424DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
425
426#endif
427
2bed17c8 428bool wxToolBarTool::Enable( bool enable )
489468fe 429{
2bed17c8
SC
430 if ( wxToolBarToolBase::Enable( enable ) == false )
431 return false;
bb2212e6 432
489468fe
SC
433 if ( IsControl() )
434 {
435 GetControl()->Enable( enable );
436 }
437 else if ( IsButton() )
438 {
292e5e1f 439#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
440 if ( m_toolbarItemRef != NULL )
441 HIToolbarItemSetEnabled( m_toolbarItemRef, enable );
442#endif
443
444 if ( m_controlHandle != NULL )
445 {
446 if ( enable )
447 EnableControl( m_controlHandle );
448 else
449 DisableControl( m_controlHandle );
450 }
451 }
452
453 return true;
454}
455
456void wxToolBarTool::SetPosition( const wxPoint& position )
457{
458 m_x = position.x;
459 m_y = position.y;
460
461 int mac_x = position.x;
462 int mac_y = position.y;
463
464 if ( IsButton() )
465 {
466 Rect contrlRect;
467 GetControlBounds( m_controlHandle, &contrlRect );
468 int former_mac_x = contrlRect.left;
469 int former_mac_y = contrlRect.top;
470 GetToolBar()->GetToolSize();
471
472 if ( mac_x != former_mac_x || mac_y != former_mac_y )
473 {
474 ::MoveControl( m_controlHandle, mac_x, mac_y );
475 }
476 }
477 else if ( IsControl() )
478 {
479 // embedded native controls are moved by the OS
292e5e1f 480#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
481 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
482#endif
483 {
484 GetControl()->Move( position );
485 }
486 }
487 else
488 {
489 // separator
490 Rect contrlRect;
491 GetControlBounds( m_controlHandle, &contrlRect );
492 int former_mac_x = contrlRect.left;
493 int former_mac_y = contrlRect.top;
494
495 if ( mac_x != former_mac_x || mac_y != former_mac_y )
496 ::MoveControl( m_controlHandle, mac_x, mac_y );
497 }
498}
499
500void wxToolBarTool::UpdateToggleImage( bool toggle )
501{
502 if ( toggle )
503 {
f656d379
RR
504 int w = m_bmpNormal.GetWidth() + 6;
505 int h = m_bmpNormal.GetHeight() + 6;
489468fe
SC
506 wxBitmap bmp( w, h );
507 wxMemoryDC dc;
508
509 dc.SelectObject( bmp );
f656d379
RR
510 wxColour mid_grey_75 = wxColour(128, 128, 128, 196);
511 wxColour light_grey_75 = wxColour(196, 196, 196, 196);
512 dc.GradientFillLinear( wxRect(1, 1, w - 1, h-1),
513 light_grey_75, mid_grey_75, wxNORTH);
514 wxColour black_50 = wxColour(0, 0, 0, 127);
515 dc.SetPen( wxPen(black_50) );
516 dc.DrawRoundedRectangle( 0, 0, w, h, 1.5 );
517 dc.DrawBitmap( m_bmpNormal, 3, 3, true );
489468fe
SC
518 dc.SelectObject( wxNullBitmap );
519 ControlButtonContentInfo info;
520 wxMacCreateBitmapButton( &info, bmp );
521 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
292e5e1f 522#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
523 if (m_toolbarItemRef != NULL)
524 {
525 ControlButtonContentInfo info2;
526 wxMacCreateBitmapButton( &info2, bmp, kControlContentCGImageRef);
527 HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
528 wxMacReleaseBitmapButton( &info2 );
529 }
530#endif
531 wxMacReleaseBitmapButton( &info );
532 }
533 else
534 {
535 ControlButtonContentInfo info;
536 wxMacCreateBitmapButton( &info, m_bmpNormal );
537 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
292e5e1f 538#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
539 if (m_toolbarItemRef != NULL)
540 {
541 ControlButtonContentInfo info2;
542 wxMacCreateBitmapButton( &info2, m_bmpNormal, kControlContentCGImageRef);
543 HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
544 wxMacReleaseBitmapButton( &info2 );
545 }
546#endif
547 wxMacReleaseBitmapButton( &info );
548 }
549
550 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
551 SetControlData(
552 m_controlHandle, 0, kControlIconTransformTag,
553 sizeof(transform), (Ptr)&transform );
554 HIViewSetNeedsDisplay( m_controlHandle, true );
555
556}
557
558wxToolBarTool::wxToolBarTool(
559 wxToolBar *tbar,
560 int id,
561 const wxString& label,
562 const wxBitmap& bmpNormal,
563 const wxBitmap& bmpDisabled,
564 wxItemKind kind,
565 wxObject *clientData,
566 const wxString& shortHelp,
567 const wxString& longHelp )
568 :
569 wxToolBarToolBase(
570 tbar, id, label, bmpNormal, bmpDisabled, kind,
571 clientData, shortHelp, longHelp )
572{
573 Init();
574}
575
576#pragma mark -
577#pragma mark Toolbar Implementation
578
579wxToolBarToolBase *wxToolBar::CreateTool(
580 int id,
581 const wxString& label,
582 const wxBitmap& bmpNormal,
583 const wxBitmap& bmpDisabled,
584 wxItemKind kind,
585 wxObject *clientData,
586 const wxString& shortHelp,
587 const wxString& longHelp )
588{
589 return new wxToolBarTool(
590 this, id, label, bmpNormal, bmpDisabled, kind,
591 clientData, shortHelp, longHelp );
592}
593
594wxToolBarToolBase *
595wxToolBar::CreateTool(wxControl *control, const wxString& label)
596{
597 return new wxToolBarTool(this, control, label);
598}
599
600void wxToolBar::Init()
601{
602 m_maxWidth = -1;
603 m_maxHeight = -1;
604 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
605 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
606
292e5e1f 607#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 608 m_macToolbar = NULL;
489468fe
SC
609 m_macUsesNativeToolbar = false;
610#endif
611}
612
613#define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
614
615const EventTypeSpec kEvents[] =
616{
617 { kEventClassHIObject, kEventHIObjectConstruct },
618 { kEventClassHIObject, kEventHIObjectInitialize },
619 { kEventClassHIObject, kEventHIObjectDestruct },
620
621 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
622};
623
624const EventTypeSpec kViewEvents[] =
625{
626 { kEventClassControl, kEventControlGetSizeConstraints }
627};
628
629struct ControlToolbarItem
630{
631 HIToolbarItemRef toolbarItem;
632 HIViewRef viewRef;
633 wxSize lastValidSize ;
634};
635
636static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
637{
638 OSStatus result = eventNotHandledErr;
639 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
640
641 switch ( GetEventClass( inEvent ) )
642 {
643 case kEventClassHIObject:
644 switch ( GetEventKind( inEvent ) )
645 {
646 case kEventHIObjectConstruct:
647 {
648 HIObjectRef toolbarItem;
649 ControlToolbarItem* item;
650
651 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
652 sizeof( HIObjectRef ), NULL, &toolbarItem );
653
654 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
655 item->toolbarItem = toolbarItem ;
656 item->lastValidSize = wxSize(-1,-1);
657 item->viewRef = NULL ;
658
659 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
660
661 result = noErr ;
662 }
663 break;
664
665 case kEventHIObjectInitialize:
666 result = CallNextEventHandler( inCallRef, inEvent );
667 if ( result == noErr )
668 {
669 CFDataRef data;
670 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
671 sizeof( CFTypeRef ), NULL, &data );
672
673 HIViewRef viewRef ;
674
675 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
676 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
677
678 object->viewRef = (HIViewRef) viewRef ;
679 // make sure we keep that control during our lifetime
680 CFRetain( object->viewRef ) ;
681
682 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
683 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL ));
684 result = noErr ;
685 }
686 break;
687
688 case kEventHIObjectDestruct:
689 {
690 HIViewRef viewRef = object->viewRef ;
691 if( viewRef && IsValidControlHandle( viewRef) )
692 {
693 // depending whether the wxControl corresponding to this HIView has already been destroyed or
694 // not, ref counts differ, so we cannot assert a special value
695 CFIndex count = CFGetRetainCount( viewRef ) ;
696 if ( count >= 1 )
697 {
489468fe
SC
698 CFRelease( viewRef ) ;
699 }
700 }
701 free( object ) ;
702 result = noErr;
703 }
704 break;
705 }
706 break;
707
708 case kEventClassToolbarItem:
709 switch ( GetEventKind( inEvent ) )
710 {
711 case kEventToolbarItemCreateCustomView:
712 {
713 HIViewRef viewRef = object->viewRef ;
714 HIViewRemoveFromSuperview( viewRef ) ;
715 HIViewSetVisible(viewRef, true) ;
716 CFRetain( viewRef ) ;
717 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
718 }
719 break;
720 }
721 break;
722
723 case kEventClassControl:
724 switch ( GetEventKind( inEvent ) )
725 {
726 case kEventControlGetSizeConstraints:
727 {
b2680ced 728 wxWindow* wxwindow = wxFindWindowFromWXWidget( (WXWidget) object->viewRef ) ;
489468fe
SC
729 if ( wxwindow )
730 {
731 // during toolbar layout the native window sometimes gets negative sizes,
732 // sometimes it just gets shrunk behind our back, so in order to avoid
733 // ever shrinking more, once a valid size is captured, we keep it
734
735 wxSize sz = object->lastValidSize;
736 if ( sz.x <= 0 || sz.y <= 0 )
737 {
738 sz = wxwindow->GetSize() ;
739 sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
740 sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
741 if ( sz.x > 0 && sz.y > 0 )
742 object->lastValidSize = sz ;
743 else
744 sz = wxSize(0,0) ;
745 }
746
747 // Extra width to avoid edge of combobox being cut off
748 sz.x += 3;
749
750 HISize min, max;
751 min.width = max.width = sz.x ;
752 min.height = max.height = sz.y ;
753
754 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
755 sizeof( HISize ), &min );
756
757 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
758 sizeof( HISize ), &max );
759 result = noErr ;
760 }
761 }
762 break;
763 }
764 break;
765 }
766
767 return result;
768}
769
770void RegisterControlToolbarItemClass()
771{
772 static bool sRegistered;
773
774 if ( !sRegistered )
775 {
776 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
777 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
778
779 sRegistered = true;
780 }
781}
782
783HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
784{
785 RegisterControlToolbarItemClass();
786
787 OSStatus err;
788 EventRef event;
789 UInt32 options = kHIToolbarItemAllowDuplicates;
790 HIToolbarItemRef result = NULL;
791
792 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
793 require_noerr( err, CantCreateEvent );
794
795 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
796 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
797
798 if ( inConfigData )
799 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
800
801 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
802 check_noerr( err );
803
804 ReleaseEvent( event );
805CantCreateEvent :
806 return result ;
807}
808
292e5e1f 809#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
810static const EventTypeSpec kToolbarEvents[] =
811{
812 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
813 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
814 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
815};
816
817static OSStatus ToolbarDelegateHandler(EventHandlerCallRef WXUNUSED(inCallRef),
818 EventRef inEvent,
819 void* WXUNUSED(inUserData))
820{
821 OSStatus result = eventNotHandledErr;
822 // Not yet needed
823 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
824 CFMutableArrayRef array;
825
826 switch ( GetEventKind( inEvent ) )
827 {
828 case kEventToolbarGetDefaultIdentifiers:
829 {
830 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
831 sizeof( CFMutableArrayRef ), NULL, &array );
832 // not implemented yet
833 // GetToolbarDefaultItems( array );
834 result = noErr;
835 }
836 break;
837
838 case kEventToolbarGetAllowedIdentifiers:
839 {
840 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
841 sizeof( CFMutableArrayRef ), NULL, &array );
842 // not implemented yet
843 // GetToolbarAllowedItems( array );
844 result = noErr;
845 }
846 break;
847 case kEventToolbarCreateItemWithIdentifier:
848 {
849 HIToolbarItemRef item = NULL;
850 CFTypeRef data = NULL;
851 CFStringRef identifier = NULL ;
852
853 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
854 sizeof( CFStringRef ), NULL, &identifier );
855
856 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
857 sizeof( CFTypeRef ), NULL, &data );
858
859 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
860 {
861 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
862 if ( item )
863 {
864 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
865 sizeof( HIToolbarItemRef ), &item );
866 result = noErr;
867 }
868 }
869
870 }
871 break;
872 }
873 return result ;
874}
292e5e1f 875#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
876
877// also for the toolbar we have the dual implementation:
878// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
879
880bool wxToolBar::Create(
881 wxWindow *parent,
882 wxWindowID id,
883 const wxPoint& pos,
884 const wxSize& size,
885 long style,
886 const wxString& name )
887{
888 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
889 return false;
890
891 FixupStyle();
892
893 OSStatus err = noErr;
894
292e5e1f 895#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
896 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
897 {
898 wxString labelStr = wxString::Format( wxT("%p"), this );
899 err = HIToolbarCreate(
900 wxCFStringRef( labelStr, wxFont::GetDefaultEncoding() ), 0,
524c47aa 901 (HIToolbarRef*) &m_macToolbar );
489468fe 902
524c47aa 903 if (m_macToolbar != NULL)
489468fe 904 {
524c47aa 905 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macToolbar ), ToolbarDelegateHandler,
489468fe
SC
906 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
907
908 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
909 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
910
911 if ( style & wxTB_NOICONS )
912 mode = kHIToolbarDisplayModeLabelOnly;
913 else if ( style & wxTB_TEXT )
914 mode = kHIToolbarDisplayModeIconAndLabel;
915 else
916 mode = kHIToolbarDisplayModeIconOnly;
917
524c47aa
SC
918 HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode );
919 HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, displaySize );
489468fe
SC
920 }
921 }
292e5e1f 922#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
923
924 return (err == noErr);
925}
926
927wxToolBar::~wxToolBar()
928{
292e5e1f 929#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 930 if (m_macToolbar != NULL)
489468fe
SC
931 {
932 // if this is the installed toolbar, then deinstall it
933 if (m_macUsesNativeToolbar)
934 MacInstallNativeToolbar( false );
935
524c47aa 936 CFIndex count = CFGetRetainCount( m_macToolbar ) ;
489468fe
SC
937 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
938 if ( UMAGetSystemVersion() < 0x1050 )
939 {
940 if ( count != 1 )
941 {
942 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
943 }
944 }
524c47aa
SC
945 CFRelease( (HIToolbarRef)m_macToolbar );
946 m_macToolbar = NULL;
489468fe
SC
947 }
948#endif
949}
950
951bool wxToolBar::Show( bool show )
952{
953 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
954 bool bResult = (tlw != NULL);
955
956 if (bResult)
957 {
292e5e1f 958#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
959 bool ownToolbarInstalled = false;
960 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
961 if (ownToolbarInstalled)
962 {
963 bResult = (IsWindowToolbarVisible( tlw ) != show);
964 if ( bResult )
965 ShowHideWindowToolbar( tlw, show, false );
966 }
967 else
968 bResult = wxToolBarBase::Show( show );
969#else
970
971 bResult = wxToolBarBase::Show( show );
972#endif
973 }
974
975 return bResult;
976}
977
978bool wxToolBar::IsShown() const
979{
980 bool bResult;
981
292e5e1f 982#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
983 bool ownToolbarInstalled;
984
985 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
986 if (ownToolbarInstalled)
987 {
988 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
989 bResult = IsWindowToolbarVisible( tlw );
990 }
991 else
992 bResult = wxToolBarBase::IsShown();
993#else
994
995 bResult = wxToolBarBase::IsShown();
996#endif
997
998 return bResult;
999}
1000
1001void wxToolBar::DoGetSize( int *width, int *height ) const
1002{
292e5e1f 1003#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1004 Rect boundsR;
1005 bool ownToolbarInstalled;
1006
1007 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1008 if ( ownToolbarInstalled )
1009 {
1010 // TODO: is this really a control ?
524c47aa 1011 GetControlBounds( (ControlRef) m_macToolbar, &boundsR );
489468fe
SC
1012 if ( width != NULL )
1013 *width = boundsR.right - boundsR.left;
1014 if ( height != NULL )
1015 *height = boundsR.bottom - boundsR.top;
1016 }
1017 else
1018 wxToolBarBase::DoGetSize( width, height );
1019
1020#else
1021 wxToolBarBase::DoGetSize( width, height );
1022#endif
1023}
1024
1025wxSize wxToolBar::DoGetBestSize() const
1026{
1027 int width, height;
1028
1029 DoGetSize( &width, &height );
1030
1031 return wxSize( width, height );
1032}
1033
1034void wxToolBar::SetWindowStyleFlag( long style )
1035{
1036 wxToolBarBase::SetWindowStyleFlag( style );
1037
292e5e1f 1038#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1039 if (m_macToolbar != NULL)
489468fe
SC
1040 {
1041 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
1042
1043 if ( style & wxTB_NOICONS )
1044 mode = kHIToolbarDisplayModeLabelOnly;
1045 else if ( style & wxTB_TEXT )
1046 mode = kHIToolbarDisplayModeIconAndLabel;
1047 else
1048 mode = kHIToolbarDisplayModeIconOnly;
1049
524c47aa 1050 HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode );
489468fe
SC
1051 }
1052#endif
1053}
1054
292e5e1f 1055#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1056bool wxToolBar::MacWantsNativeToolbar()
1057{
1058 return m_macUsesNativeToolbar;
1059}
1060
1061bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
1062{
1063 bool bResultV = false;
1064
1065 if (ownToolbarInstalled != NULL)
1066 *ownToolbarInstalled = false;
1067
1068 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1069 if (tlw != NULL)
1070 {
1071 HIToolbarRef curToolbarRef = NULL;
1072 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1073 bResultV = ((err == noErr) && (curToolbarRef != NULL));
1074 if (bResultV && (ownToolbarInstalled != NULL))
524c47aa 1075 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
489468fe
SC
1076 }
1077
1078 return bResultV;
1079}
1080
1081bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
1082{
1083 bool bResult = false;
1084
524c47aa 1085 if (usesNative && (m_macToolbar == NULL))
489468fe
SC
1086 return bResult;
1087
f0ceebb7 1088 if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0))
489468fe
SC
1089 return bResult;
1090
1091 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1092 if (tlw == NULL)
1093 return bResult;
1094
1095 // check the existing toolbar
1096 HIToolbarRef curToolbarRef = NULL;
1097 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1098 if (err != noErr)
1099 curToolbarRef = NULL;
1100
1101 m_macUsesNativeToolbar = usesNative;
1102
1103 if (m_macUsesNativeToolbar)
1104 {
1105 // only install toolbar if there isn't one installed already
1106 if (curToolbarRef == NULL)
1107 {
1108 bResult = true;
1109
524c47aa 1110 SetWindowToolbar( tlw, (HIToolbarRef) m_macToolbar );
bb2212e6 1111
b0184e59
RR
1112 // ShowHideWindowToolbar will make the wxFrame grow
1113 // which we don't want in this case
1114 wxSize sz = GetParent()->GetSize();
489468fe 1115 ShowHideWindowToolbar( tlw, true, false );
b0184e59
RR
1116 // Restore the orginal size
1117 GetParent()->SetSize( sz );
bb2212e6 1118
489468fe 1119 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
bb2212e6 1120
489468fe 1121 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
bb2212e6 1122
b2680ced 1123 m_peer->Move(0,0,0,0 );
489468fe 1124 SetSize( wxSIZE_AUTO_WIDTH, 0 );
b2680ced 1125 m_peer->SetVisibility( false );
489468fe
SC
1126 wxToolBarBase::Show( false );
1127 }
1128 }
1129 else
1130 {
1131 // only deinstall toolbar if this is the installed one
524c47aa 1132 if (m_macToolbar == curToolbarRef)
489468fe
SC
1133 {
1134 bResult = true;
1135
1136 ShowHideWindowToolbar( tlw, false, false );
1137 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
1138 SetWindowToolbar( tlw, NULL );
1139
b2680ced 1140 m_peer->SetVisibility( true );
489468fe
SC
1141 }
1142 }
1143
1144 if (bResult)
1145 InvalidateBestSize();
1146
1147// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1148 return bResult;
1149}
1150#endif
1151
1152bool wxToolBar::Realize()
1153{
bb2212e6 1154 if ( !wxToolBarBase::Realize() )
489468fe 1155 return false;
bb2212e6 1156
b0184e59 1157 wxSize tlw_sz = GetParent()->GetSize();
489468fe
SC
1158
1159 int maxWidth = 0;
1160 int maxHeight = 0;
1161
1162 int maxToolWidth = 0;
1163 int maxToolHeight = 0;
1164
1165 int x = m_xMargin + kwxMacToolBarLeftMargin;
1166 int y = m_yMargin + kwxMacToolBarTopMargin;
1167
1168 int tw, th;
1169 GetSize( &tw, &th );
1170
1171 // find the maximum tool width and height
1172 wxToolBarTool *tool;
1173 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1174 while ( node )
1175 {
1176 tool = (wxToolBarTool *) node->GetData();
1177 if ( tool != NULL )
1178 {
1179 wxSize sz = tool->GetSize();
1180
1181 if ( sz.x > maxToolWidth )
1182 maxToolWidth = sz.x;
1183 if ( sz.y > maxToolHeight )
1184 maxToolHeight = sz.y;
1185 }
1186
1187 node = node->GetNext();
1188 }
1189
1190 bool lastIsRadio = false;
1191 bool curIsRadio = false;
1192
292e5e1f 1193#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1194 CFIndex currentPosition = 0;
1195 bool insertAll = false;
1196
524c47aa 1197 HIToolbarRef refTB = (HIToolbarRef)m_macToolbar;
b2680ced
SC
1198 wxFont f;
1199 wxFontEncoding enc;
1200 f = GetFont();
1201 if ( f.IsOk() )
1202 enc = f.GetEncoding();
1203 else
1204 enc = wxFont::GetDefaultEncoding();
489468fe
SC
1205#endif
1206
1207 node = m_tools.GetFirst();
1208 while ( node )
1209 {
1210 tool = (wxToolBarTool*) node->GetData();
1211 if ( tool == NULL )
1212 {
1213 node = node->GetNext();
1214 continue;
1215 }
1216
1217 // set tool position:
1218 // for the moment just perform a single row/column alignment
1219 wxSize cursize = tool->GetSize();
1220 if ( x + cursize.x > maxWidth )
1221 maxWidth = x + cursize.x;
1222 if ( y + cursize.y > maxHeight )
1223 maxHeight = y + cursize.y;
1224
f0ceebb7 1225 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1226 {
1227 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1228 tool->SetPosition( wxPoint(x1, y) );
1229 }
1230 else
1231 {
1232 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1233 tool->SetPosition( wxPoint(x, y1) );
1234 }
1235
1236 // update the item positioning state
f0ceebb7 1237 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1238 y += cursize.y + kwxMacToolSpacing;
1239 else
1240 x += cursize.x + kwxMacToolSpacing;
1241
292e5e1f 1242#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1243 // install in native HIToolbar
1244 if ( refTB )
1245 {
1246 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1247 if ( hiItemRef != NULL )
1248 {
b2680ced
SC
1249 // since setting the help texts is non-virtual we have to update
1250 // the strings now
489468fe
SC
1251 if ( insertAll || (tool->GetIndex() != currentPosition) )
1252 {
1253 OSStatus err = noErr;
1254 if ( !insertAll )
1255 {
1256 insertAll = true;
1257
1258 // if this is the first tool that gets newly inserted or repositioned
1259 // first remove all 'old' tools from here to the right, because of this
1260 // all following tools will have to be reinserted (insertAll).
1261 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1262 node2 != node;
1263 node2 = node2->GetPrevious() )
1264 {
1265 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1266
1267 const long idx = tool2->GetIndex();
1268 if ( idx != -1 )
1269 {
1270 if ( tool2->IsControl() )
1271 {
1272 CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
1273 if ( count != 3 && count != 2 )
1274 {
1275 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1276 }
1277
1278 wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
1279 }
1280 err = HIToolbarRemoveItemAtIndex(refTB, idx);
1281 if ( err != noErr )
1282 {
1283 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1284 idx, (long)err);
1285 }
1286 if ( tool2->IsControl() )
1287 {
1288 CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
1289 if ( count != 2 )
1290 {
1291 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1292 }
1293
1294 wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
1295 }
1296
1297 tool2->SetIndex(-1);
1298 }
1299 }
1300 }
1301
1302 err = HIToolbarInsertItemAtIndex( refTB, hiItemRef, currentPosition );
1303 if (err != noErr)
1304 {
1305 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
1306 }
1307
1308 tool->SetIndex( currentPosition );
1309 if ( tool->IsControl() )
1310 {
1311 CFIndex count = CFGetRetainCount( tool->GetControl()->GetPeer()->GetControlRef() ) ;
1312 if ( count != 3 && count != 2 )
1313 {
1314 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1315 }
1316 wxASSERT( IsValidControlHandle(tool->GetControl()->GetPeer()->GetControlRef() )) ;
1317 }
1318 }
1319
1320 currentPosition++;
1321 }
1322 }
1323#endif
1324
1325 // update radio button (and group) state
1326 lastIsRadio = curIsRadio;
1327 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1328
1329 if ( !curIsRadio )
1330 {
1331 if ( tool->IsToggled() )
1332 DoToggleTool( tool, true );
1333 }
1334 else
1335 {
1336 if ( !lastIsRadio )
1337 {
1338 if ( tool->Toggle( true ) )
1339 {
1340 DoToggleTool( tool, true );
1341 }
1342 }
1343 else if ( tool->IsToggled() )
1344 {
1345 if ( tool->IsToggled() )
1346 DoToggleTool( tool, true );
1347
1348 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1349 while ( nodePrev )
1350 {
1351 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1352 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1353 break;
1354
1355 if ( toggleTool->Toggle( false ) )
1356 DoToggleTool( toggleTool, false );
1357
1358 nodePrev = nodePrev->GetPrevious();
1359 }
1360 }
1361 }
1362
1363 node = node->GetNext();
1364 }
1365
b0184e59
RR
1366 if (m_macUsesNativeToolbar)
1367 GetParent()->SetSize( tlw_sz );
bb2212e6 1368
f0ceebb7 1369 if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) )
489468fe
SC
1370 {
1371 // if not set yet, only one row
1372 if ( m_maxRows <= 0 )
1373 SetRows( 1 );
1374
1375 m_minWidth = maxWidth;
489468fe
SC
1376 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1377 m_minHeight = m_maxHeight = maxHeight;
1378 }
1379 else
1380 {
1381 // if not set yet, have one column
1382 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1383 SetRows( GetToolsCount() );
1384
1385 m_minHeight = maxHeight;
489468fe
SC
1386 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1387 m_minWidth = m_maxWidth = maxWidth;
1388 }
1389
1390#if 0
1391 // FIXME: should this be OSX-only?
1392 {
1393 bool wantNativeToolbar, ownToolbarInstalled;
1394
1395 // attempt to install the native toolbar
f0ceebb7 1396 wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0);
489468fe
SC
1397 MacInstallNativeToolbar( wantNativeToolbar );
1398 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1399 if (!ownToolbarInstalled)
1400 {
1401 SetSize( maxWidth, maxHeight );
1402 InvalidateBestSize();
1403 }
1404 }
1405#else
1406 SetSize( maxWidth, maxHeight );
1407 InvalidateBestSize();
1408#endif
1409
1410 SetInitialSize();
1411
1412 return true;
1413}
1414
1415void wxToolBar::SetToolBitmapSize(const wxSize& size)
1416{
1417 m_defaultWidth = size.x + kwxMacToolBorder;
1418 m_defaultHeight = size.y + kwxMacToolBorder;
1419
292e5e1f 1420#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1421 if (m_macToolbar != NULL)
489468fe
SC
1422 {
1423 int maxs = wxMax( size.x, size.y );
1424 HIToolbarDisplaySize sizeSpec;
1425 if ( maxs > 32 )
1426 sizeSpec = kHIToolbarDisplaySizeNormal;
1427 else if ( maxs > 24 )
1428 sizeSpec = kHIToolbarDisplaySizeDefault;
1429 else
1430 sizeSpec = kHIToolbarDisplaySizeSmall;
1431
524c47aa 1432 HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, sizeSpec );
489468fe
SC
1433 }
1434#endif
1435}
1436
1437// The button size is bigger than the bitmap size
1438wxSize wxToolBar::GetToolSize() const
1439{
1440 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1441}
1442
1443void wxToolBar::SetRows(int nRows)
1444{
1445 // avoid resizing the frame uselessly
1446 if ( nRows != m_maxRows )
1447 m_maxRows = nRows;
1448}
1449
1450void wxToolBar::MacSuperChangedPosition()
1451{
1452 wxWindow::MacSuperChangedPosition();
1453
292e5e1f 1454#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1455 if (! m_macUsesNativeToolbar )
1456 Realize();
1457#else
1458
1459 Realize();
1460#endif
1461}
1462
1463void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1464{
5c33522f 1465 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
489468fe
SC
1466 if ( tool )
1467 {
1468 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1469
1470 tool->SetNormalBitmap(bitmap);
1471
1472 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1473 tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
1474 }
1475}
1476
1477void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1478{
5c33522f 1479 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
489468fe
SC
1480 if ( tool )
1481 {
1482 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1483
1484 tool->SetDisabledBitmap(bitmap);
1485
1486 // TODO: what to do for this one?
1487 }
1488}
1489
1490wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1491{
1492 wxToolBarTool *tool;
1493 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1494 while ( node )
1495 {
1496 tool = (wxToolBarTool *)node->GetData();
1497 if (tool != NULL)
1498 {
1499 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1500 if ( r.Contains( wxPoint( x, y ) ) )
1501 return tool;
1502 }
1503
1504 node = node->GetNext();
1505 }
1506
d3b9f782 1507 return NULL;
489468fe
SC
1508}
1509
1510wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1511{
1512 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1513 if ( tool != NULL )
1514 return tool->GetShortHelp();
1515
1516 return wxEmptyString;
1517}
1518
2bed17c8 1519void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(enable))
489468fe 1520{
2bed17c8 1521 // everything already done in the tool's implementation
489468fe
SC
1522}
1523
a4fec5b4 1524void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(toggle))
489468fe 1525{
2bed17c8 1526 // everything already done in the tool's implementation
489468fe
SC
1527}
1528
1529bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1530{
5c33522f 1531 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
489468fe
SC
1532 if (tool == NULL)
1533 return false;
1534
1535 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1536 wxSize toolSize = GetToolSize();
1537 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
1538 ControlRef controlHandle = NULL;
1539 OSStatus err = 0;
1540
292e5e1f 1541#if wxOSX_USE_NATIVE_TOOLBAR
489468fe 1542 wxString label = tool->GetLabel();
524c47aa 1543 if (m_macToolbar && !label.empty() )
489468fe
SC
1544 {
1545 // strip mnemonics from the label for compatibility
1546 // with the usual labels in wxStaticText sense
1547 label = wxStripMenuCodes(label);
1548 }
292e5e1f 1549#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1550
1551 switch (tool->GetStyle())
1552 {
1553 case wxTOOL_STYLE_SEPARATOR:
1554 {
1555 wxASSERT( tool->GetControlHandle() == NULL );
1556 toolSize.x /= 4;
1557 toolSize.y /= 4;
f0ceebb7 1558 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1559 toolrect.bottom = toolSize.y;
1560 else
1561 toolrect.right = toolSize.x;
1562
1563 // in flat style we need a visual separator
292e5e1f 1564#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1565 if (m_macToolbar != NULL)
489468fe
SC
1566 {
1567 HIToolbarItemRef item;
1568 err = HIToolbarItemCreate(
1569 kHIToolbarSeparatorIdentifier,
1570 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1571 &item );
1572 if (err == noErr)
1573 tool->SetToolbarItemRef( item );
1574 }
1575 else
1576 err = noErr;
292e5e1f 1577#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1578
1579 CreateSeparatorControl( window, &toolrect, &controlHandle );
1580 tool->SetControlHandle( controlHandle );
1581 }
1582 break;
1583
1584 case wxTOOL_STYLE_BUTTON:
1585 {
1586 wxASSERT( tool->GetControlHandle() == NULL );
1587 ControlButtonContentInfo info;
1588 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap() );
1589
1590 if ( UMAGetSystemVersion() >= 0x1000)
1591 {
1592 // contrary to the docs this control only works with iconrefs
1593 ControlButtonContentInfo info;
1594 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
1595 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1596 wxMacReleaseBitmapButton( &info );
1597 }
1598 else
1599 {
1600 SInt16 behaviour = kControlBehaviorOffsetContents;
1601 if ( tool->CanBeToggled() )
1602 behaviour |= kControlBehaviorToggles;
1603 err = CreateBevelButtonControl( window,
1604 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1605 behaviour, &info, 0, 0, 0, &controlHandle );
1606 }
1607
292e5e1f 1608#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1609 if (m_macToolbar != NULL)
489468fe
SC
1610 {
1611 HIToolbarItemRef item;
1612 wxString labelStr = wxString::Format(wxT("%p"), tool);
1613 err = HIToolbarItemCreate(
1614 wxCFStringRef(labelStr, wxFont::GetDefaultEncoding()),
1615 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1616 if (err == noErr)
1617 {
1618 ControlButtonContentInfo info2;
1619 wxMacCreateBitmapButton( &info2, tool->GetNormalBitmap(), kControlContentCGImageRef);
1620
1621 InstallEventHandler(
1622 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1623 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
1624 HIToolbarItemSetLabel( item, wxCFStringRef(label, GetFont().GetEncoding()) );
1625 HIToolbarItemSetImage( item, info2.u.imageRef );
1626 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
1627 tool->SetToolbarItemRef( item );
1628
1629 wxMacReleaseBitmapButton( &info2 );
1630 }
1631 }
1632 else
1633 err = noErr;
292e5e1f 1634#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1635
1636 wxMacReleaseBitmapButton( &info );
1637
1638#if 0
1639 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1640 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1641#endif
1642
1643 InstallControlEventHandler(
1644 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1645 GetEventTypeCount(eventList), eventList, tool, NULL );
1646
1647 tool->SetControlHandle( controlHandle );
1648 }
1649 break;
1650
1651 case wxTOOL_STYLE_CONTROL:
1652
292e5e1f 1653#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1654 if (m_macToolbar != NULL)
489468fe 1655 {
9a83f860 1656 wxCHECK_MSG( tool->GetControl(), false, wxT("control must be non-NULL") );
489468fe
SC
1657 HIToolbarItemRef item;
1658 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
1659 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
524c47aa 1660 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macToolbar,kControlToolbarItemClassID,
489468fe
SC
1661 data , &item ) ;
1662
1663 if (err == noErr)
1664 {
1665 tool->SetToolbarItemRef( item );
1666 }
1667 CFRelease( data ) ;
1668 }
1669 else
1670 {
1671 err = noErr;
1672 break;
1673 }
1674#else
1675 // right now there's nothing to do here
1676#endif
1677 break;
1678
1679 default:
1680 break;
1681 }
1682
1683 if ( err == noErr )
1684 {
1685 if ( controlHandle )
1686 {
1687 ControlRef container = (ControlRef) GetHandle();
1688 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1689
1690 SetControlVisibility( controlHandle, true, true );
1691 ::EmbedControl( controlHandle, container );
1692 }
1693
1694 if ( tool->CanBeToggled() && tool->IsToggled() )
1695 tool->UpdateToggleImage( true );
1696
1697 // nothing special to do here - we relayout in Realize() later
1698 InvalidateBestSize();
1699 }
1700 else
1701 {
1702 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err ) );
1703 }
1704
1705 return (err == noErr);
1706}
1707
1708void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1709{
2bed17c8 1710 // nothing to do
489468fe
SC
1711}
1712
1713bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1714{
5c33522f 1715 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
489468fe
SC
1716 wxToolBarToolsList::compatibility_iterator node;
1717 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1718 {
1719 wxToolBarToolBase *tool2 = node->GetData();
1720 if ( tool2 == tool )
1721 {
1722 // let node point to the next node in the list
1723 node = node->GetNext();
1724
1725 break;
1726 }
1727 }
1728
1729 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1730
292e5e1f 1731#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1732 CFIndex removeIndex = tool->GetIndex();
1733#endif
1734
292e5e1f 1735#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1736 if (m_macToolbar != NULL)
489468fe 1737 {
524c47aa 1738 if ( removeIndex != -1 && m_macToolbar )
489468fe 1739 {
524c47aa 1740 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macToolbar, removeIndex );
489468fe
SC
1741 tool->SetIndex( -1 );
1742 }
1743 }
1744#endif
1745
1746 tool->ClearControl();
1747
1748 // and finally reposition all the controls after this one
1749
1750 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1751 {
1752 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1753 wxPoint pt = tool2->GetPosition();
1754
f0ceebb7 1755 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1756 pt.y -= sz.y;
1757 else
1758 pt.x -= sz.x;
1759
1760 tool2->SetPosition( pt );
1761
292e5e1f 1762#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1763 if (m_macToolbar != NULL)
489468fe
SC
1764 {
1765 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1766 tool2->SetIndex( tool2->GetIndex() - 1 );
1767 }
1768#endif
1769 }
1770
1771 InvalidateBestSize();
1772
1773 return true;
1774}
1775
1776void wxToolBar::OnPaint(wxPaintEvent& event)
1777{
292e5e1f 1778#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1779 if ( m_macUsesNativeToolbar )
1780 {
1781 event.Skip(true);
1782 return;
1783 }
1784#endif
1785
1786 wxPaintDC dc(this);
1787
1788 int w, h;
1789 GetSize( &w, &h );
1790
b2680ced 1791 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
489468fe
SC
1792
1793 if ( !drawMetalTheme )
1794 {
1795 HIThemePlacardDrawInfo info;
1796 memset( &info, 0, sizeof(info) );
1797 info.version = 0;
1798 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1799
1800 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1801 HIRect rect = CGRectMake( 0, 0, w, h );
1802 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1803 }
1804 else
1805 {
1806 // leave the background as it is (striped or metal)
1807 }
1808
1809 event.Skip();
1810}
1811
1812#endif // wxUSE_TOOLBAR