1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
28 const short kwxMacToolBarToolDefaultWidth
= 16;
29 const short kwxMacToolBarToolDefaultHeight
= 16;
30 const short kwxMacToolBarTopMargin
= 4;
31 const short kwxMacToolBarLeftMargin
= 4;
32 const short kwxMacToolBorder
= 0;
33 const short kwxMacToolSpacing
= 6;
35 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
36 EVT_PAINT( wxToolBar::OnPaint
)
41 #pragma mark Tool Implementation
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
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.
60 class wxToolBarTool
: public wxToolBarToolBase
66 const wxString
& label
,
67 const wxBitmap
& bmpNormal
,
68 const wxBitmap
& bmpDisabled
,
71 const wxString
& shortHelp
,
72 const wxString
& longHelp
);
74 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
75 : wxToolBarToolBase(tbar
, control
, label
)
79 SetControlHandle( (ControlRef
) control
->GetHandle() );
82 virtual ~wxToolBarTool()
87 WXWidget
GetControlHandle()
89 return (WXWidget
) m_controlHandle
;
92 void SetControlHandle( ControlRef handle
)
94 m_controlHandle
= handle
;
97 void SetPosition( const wxPoint
& position
);
101 if ( m_controlHandle
)
104 DisposeControl( m_controlHandle
);
107 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
108 // proper wxControl destructor
110 m_controlHandle
= NULL
;
113 #if wxOSX_USE_NATIVE_TOOLBAR
114 if ( m_toolbarItemRef
)
116 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
117 // different behaviour under Leopard
118 if ( UMAGetSystemVersion() < 0x1050 )
122 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
125 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
126 CFRelease(m_toolbarItemRef
);
127 m_toolbarItemRef
= NULL
;
129 #endif // wxOSX_USE_NATIVE_TOOLBAR
132 wxSize
GetSize() const
138 curSize
= GetControl()->GetSize();
140 else if ( IsButton() )
142 curSize
= GetToolBar()->GetToolSize();
147 curSize
= GetToolBar()->GetToolSize();
148 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
157 wxPoint
GetPosition() const
159 return wxPoint( m_x
, m_y
);
162 virtual bool Enable( bool enable
);
164 void UpdateToggleImage( bool toggle
);
166 virtual bool Toggle(bool toggle
)
168 if ( wxToolBarToolBase::Toggle( toggle
) == false )
171 UpdateToggleImage(toggle
);
175 void UpdateHelpStrings()
177 #if wxOSX_USE_NATIVE_TOOLBAR
178 if ( m_toolbarItemRef
)
180 wxFontEncoding enc
= GetToolBarFontEncoding();
182 HIToolbarItemSetHelpText(
184 wxCFStringRef( GetShortHelp(), enc
),
185 wxCFStringRef( GetLongHelp(), enc
) );
190 virtual bool SetShortHelp(const wxString
& help
)
192 if ( wxToolBarToolBase::SetShortHelp( help
) == false )
199 virtual bool SetLongHelp(const wxString
& help
)
201 if ( wxToolBarToolBase::SetLongHelp( help
) == false )
208 virtual void SetNormalBitmap(const wxBitmap
& bmp
)
210 wxToolBarToolBase::SetNormalBitmap(bmp
);
211 UpdateToggleImage(CanBeToggled() && IsToggled());
214 virtual void SetLabel(const wxString
& label
)
216 wxToolBarToolBase::SetLabel(label
);
217 #if wxOSX_USE_NATIVE_TOOLBAR
218 if ( m_toolbarItemRef
)
220 // strip mnemonics from the label for compatibility with the usual
221 // labels in wxStaticText sense
222 wxString labelStr
= wxStripMenuCodes(label
);
224 HIToolbarItemSetLabel(
226 wxCFStringRef(labelStr
, GetToolBarFontEncoding()) );
231 #if wxOSX_USE_NATIVE_TOOLBAR
232 void SetToolbarItemRef( HIToolbarItemRef ref
)
234 if ( m_controlHandle
)
235 HideControl( m_controlHandle
);
236 if ( m_toolbarItemRef
)
237 CFRelease( m_toolbarItemRef
);
239 m_toolbarItemRef
= ref
;
243 HIToolbarItemRef
GetToolbarItemRef() const
245 return m_toolbarItemRef
;
248 void SetIndex( CFIndex idx
)
253 CFIndex
GetIndex() const
257 #endif // wxOSX_USE_NATIVE_TOOLBAR
260 #if wxOSX_USE_NATIVE_TOOLBAR
261 wxFontEncoding
GetToolBarFontEncoding() const
265 f
= GetToolBar()->GetFont();
266 return f
.IsOk() ? f
.GetEncoding() : wxFont::GetDefaultEncoding();
268 #endif // wxOSX_USE_NATIVE_TOOLBAR
272 m_controlHandle
= NULL
;
274 #if wxOSX_USE_NATIVE_TOOLBAR
275 m_toolbarItemRef
= NULL
;
280 ControlRef m_controlHandle
;
284 #if wxOSX_USE_NATIVE_TOOLBAR
285 HIToolbarItemRef m_toolbarItemRef
;
286 // position in its toolbar, -1 means not inserted
291 static const EventTypeSpec eventList
[] =
293 { kEventClassControl
, kEventControlHit
},
294 { kEventClassControl
, kEventControlHitTest
},
297 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
299 OSStatus result
= eventNotHandledErr
;
300 ControlRef controlRef
;
301 wxMacCarbonEvent
cEvent( event
);
303 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
305 switch ( GetEventKind( event
) )
307 case kEventControlHit
:
309 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
310 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
311 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
315 shouldToggle
= !tbartool
->IsToggled();
317 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
320 if (tbartool
!= NULL
)
321 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
326 case kEventControlHitTest
:
328 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
330 HIViewGetBounds( controlRef
, &rect
);
332 ControlPartCode pc
= kControlNoPart
;
333 if ( CGRectContainsPoint( rect
, pt
) )
334 pc
= kControlIconPart
;
335 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
347 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
349 OSStatus result
= eventNotHandledErr
;
351 switch ( GetEventClass( event
) )
353 case kEventClassControl
:
354 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
364 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
366 #if wxOSX_USE_NATIVE_TOOLBAR
368 static const EventTypeSpec toolBarEventList
[] =
370 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
373 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
375 OSStatus result
= eventNotHandledErr
;
377 switch ( GetEventKind( event
) )
379 case kEventToolbarItemPerformAction
:
381 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
382 if ( tbartool
!= NULL
)
384 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
385 int toolID
= tbartool
->GetId();
387 if ( tbartool
->CanBeToggled() )
390 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
394 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
407 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
409 OSStatus result
= eventNotHandledErr
;
411 switch ( GetEventClass( event
) )
413 case kEventClassToolbarItem
:
414 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
424 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
428 bool wxToolBarTool::Enable( bool enable
)
430 if ( wxToolBarToolBase::Enable( enable
) == false )
435 GetControl()->Enable( enable
);
437 else if ( IsButton() )
439 #if wxOSX_USE_NATIVE_TOOLBAR
440 if ( m_toolbarItemRef
!= NULL
)
441 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
444 if ( m_controlHandle
!= NULL
)
447 EnableControl( m_controlHandle
);
449 DisableControl( m_controlHandle
);
456 void wxToolBarTool::SetPosition( const wxPoint
& position
)
461 int mac_x
= position
.x
;
462 int mac_y
= position
.y
;
467 GetControlBounds( m_controlHandle
, &contrlRect
);
468 int former_mac_x
= contrlRect
.left
;
469 int former_mac_y
= contrlRect
.top
;
470 GetToolBar()->GetToolSize();
472 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
474 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
477 else if ( IsControl() )
479 // embedded native controls are moved by the OS
480 #if wxOSX_USE_NATIVE_TOOLBAR
481 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
484 GetControl()->Move( position
);
491 GetControlBounds( m_controlHandle
, &contrlRect
);
492 int former_mac_x
= contrlRect
.left
;
493 int former_mac_y
= contrlRect
.top
;
495 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
496 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
500 void wxToolBarTool::UpdateToggleImage( bool toggle
)
504 int w
= m_bmpNormal
.GetWidth();
505 int h
= m_bmpNormal
.GetHeight();
506 wxBitmap
bmp( w
, h
);
509 dc
.SelectObject( bmp
);
510 dc
.SetPen( wxPen(*wxBLACK
) );
511 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
512 dc
.DrawRectangle( 0, 0, w
, h
);
513 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
514 dc
.SelectObject( wxNullBitmap
);
515 ControlButtonContentInfo info
;
516 wxMacCreateBitmapButton( &info
, bmp
);
517 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
518 #if wxOSX_USE_NATIVE_TOOLBAR
519 if (m_toolbarItemRef
!= NULL
)
521 ControlButtonContentInfo info2
;
522 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
523 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
524 wxMacReleaseBitmapButton( &info2
);
527 wxMacReleaseBitmapButton( &info
);
531 ControlButtonContentInfo info
;
532 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
533 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
534 #if wxOSX_USE_NATIVE_TOOLBAR
535 if (m_toolbarItemRef
!= NULL
)
537 ControlButtonContentInfo info2
;
538 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
539 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
540 wxMacReleaseBitmapButton( &info2
);
543 wxMacReleaseBitmapButton( &info
);
546 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
548 m_controlHandle
, 0, kControlIconTransformTag
,
549 sizeof(transform
), (Ptr
)&transform
);
550 HIViewSetNeedsDisplay( m_controlHandle
, true );
554 wxToolBarTool::wxToolBarTool(
557 const wxString
& label
,
558 const wxBitmap
& bmpNormal
,
559 const wxBitmap
& bmpDisabled
,
561 wxObject
*clientData
,
562 const wxString
& shortHelp
,
563 const wxString
& longHelp
)
566 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
567 clientData
, shortHelp
, longHelp
)
573 #pragma mark Toolbar Implementation
575 wxToolBarToolBase
*wxToolBar::CreateTool(
577 const wxString
& label
,
578 const wxBitmap
& bmpNormal
,
579 const wxBitmap
& bmpDisabled
,
581 wxObject
*clientData
,
582 const wxString
& shortHelp
,
583 const wxString
& longHelp
)
585 return new wxToolBarTool(
586 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
587 clientData
, shortHelp
, longHelp
);
591 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
593 return new wxToolBarTool(this, control
, label
);
596 void wxToolBar::Init()
600 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
601 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
603 #if wxOSX_USE_NATIVE_TOOLBAR
605 m_macUsesNativeToolbar
= false;
609 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
611 const EventTypeSpec kEvents
[] =
613 { kEventClassHIObject
, kEventHIObjectConstruct
},
614 { kEventClassHIObject
, kEventHIObjectInitialize
},
615 { kEventClassHIObject
, kEventHIObjectDestruct
},
617 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
620 const EventTypeSpec kViewEvents
[] =
622 { kEventClassControl
, kEventControlGetSizeConstraints
}
625 struct ControlToolbarItem
627 HIToolbarItemRef toolbarItem
;
629 wxSize lastValidSize
;
632 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
634 OSStatus result
= eventNotHandledErr
;
635 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
637 switch ( GetEventClass( inEvent
) )
639 case kEventClassHIObject
:
640 switch ( GetEventKind( inEvent
) )
642 case kEventHIObjectConstruct
:
644 HIObjectRef toolbarItem
;
645 ControlToolbarItem
* item
;
647 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
648 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
650 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
651 item
->toolbarItem
= toolbarItem
;
652 item
->lastValidSize
= wxSize(-1,-1);
653 item
->viewRef
= NULL
;
655 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
661 case kEventHIObjectInitialize
:
662 result
= CallNextEventHandler( inCallRef
, inEvent
);
663 if ( result
== noErr
)
666 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
667 sizeof( CFTypeRef
), NULL
, &data
);
671 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
672 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
674 object
->viewRef
= (HIViewRef
) viewRef
;
675 // make sure we keep that control during our lifetime
676 CFRetain( object
->viewRef
) ;
678 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
679 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
684 case kEventHIObjectDestruct
:
686 HIViewRef viewRef
= object
->viewRef
;
687 if( viewRef
&& IsValidControlHandle( viewRef
) )
689 // depending whether the wxControl corresponding to this HIView has already been destroyed or
690 // not, ref counts differ, so we cannot assert a special value
691 CFIndex count
= CFGetRetainCount( viewRef
) ;
694 CFRelease( viewRef
) ;
704 case kEventClassToolbarItem
:
705 switch ( GetEventKind( inEvent
) )
707 case kEventToolbarItemCreateCustomView
:
709 HIViewRef viewRef
= object
->viewRef
;
710 HIViewRemoveFromSuperview( viewRef
) ;
711 HIViewSetVisible(viewRef
, true) ;
712 CFRetain( viewRef
) ;
713 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
719 case kEventClassControl
:
720 switch ( GetEventKind( inEvent
) )
722 case kEventControlGetSizeConstraints
:
724 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
727 // during toolbar layout the native window sometimes gets negative sizes,
728 // sometimes it just gets shrunk behind our back, so in order to avoid
729 // ever shrinking more, once a valid size is captured, we keep it
731 wxSize sz
= object
->lastValidSize
;
732 if ( sz
.x
<= 0 || sz
.y
<= 0 )
734 sz
= wxwindow
->GetSize() ;
735 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
736 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
737 if ( sz
.x
> 0 && sz
.y
> 0 )
738 object
->lastValidSize
= sz
;
743 // Extra width to avoid edge of combobox being cut off
747 min
.width
= max
.width
= sz
.x
;
748 min
.height
= max
.height
= sz
.y
;
750 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
751 sizeof( HISize
), &min
);
753 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
754 sizeof( HISize
), &max
);
766 void RegisterControlToolbarItemClass()
768 static bool sRegistered
;
772 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
773 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
779 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
781 RegisterControlToolbarItemClass();
785 UInt32 options
= kHIToolbarItemAllowDuplicates
;
786 HIToolbarItemRef result
= NULL
;
788 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
789 require_noerr( err
, CantCreateEvent
);
791 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
792 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
795 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
797 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
800 ReleaseEvent( event
);
805 #if wxOSX_USE_NATIVE_TOOLBAR
806 static const EventTypeSpec kToolbarEvents
[] =
808 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
809 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
810 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
813 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
815 void* WXUNUSED(inUserData
))
817 OSStatus result
= eventNotHandledErr
;
819 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
820 CFMutableArrayRef array
;
822 switch ( GetEventKind( inEvent
) )
824 case kEventToolbarGetDefaultIdentifiers
:
826 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
827 sizeof( CFMutableArrayRef
), NULL
, &array
);
828 // not implemented yet
829 // GetToolbarDefaultItems( array );
834 case kEventToolbarGetAllowedIdentifiers
:
836 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
837 sizeof( CFMutableArrayRef
), NULL
, &array
);
838 // not implemented yet
839 // GetToolbarAllowedItems( array );
843 case kEventToolbarCreateItemWithIdentifier
:
845 HIToolbarItemRef item
= NULL
;
846 CFTypeRef data
= NULL
;
847 CFStringRef identifier
= NULL
;
849 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
850 sizeof( CFStringRef
), NULL
, &identifier
);
852 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
853 sizeof( CFTypeRef
), NULL
, &data
);
855 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
857 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
860 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
861 sizeof( HIToolbarItemRef
), &item
);
871 #endif // wxOSX_USE_NATIVE_TOOLBAR
873 // also for the toolbar we have the dual implementation:
874 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
876 bool wxToolBar::Create(
882 const wxString
& name
)
884 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
889 OSStatus err
= noErr
;
891 #if wxOSX_USE_NATIVE_TOOLBAR
892 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
894 wxString labelStr
= wxString::Format( wxT("%p"), this );
895 err
= HIToolbarCreate(
896 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
897 (HIToolbarRef
*) &m_macToolbar
);
899 if (m_macToolbar
!= NULL
)
901 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macToolbar
), ToolbarDelegateHandler
,
902 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
904 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
905 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
907 if ( style
& wxTB_NOICONS
)
908 mode
= kHIToolbarDisplayModeLabelOnly
;
909 else if ( style
& wxTB_TEXT
)
910 mode
= kHIToolbarDisplayModeIconAndLabel
;
912 mode
= kHIToolbarDisplayModeIconOnly
;
914 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
915 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, displaySize
);
918 #endif // wxOSX_USE_NATIVE_TOOLBAR
920 return (err
== noErr
);
923 wxToolBar::~wxToolBar()
925 #if wxOSX_USE_NATIVE_TOOLBAR
926 if (m_macToolbar
!= NULL
)
928 // if this is the installed toolbar, then deinstall it
929 if (m_macUsesNativeToolbar
)
930 MacInstallNativeToolbar( false );
932 CFIndex count
= CFGetRetainCount( m_macToolbar
) ;
933 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
934 if ( UMAGetSystemVersion() < 0x1050 )
938 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
941 CFRelease( (HIToolbarRef
)m_macToolbar
);
947 bool wxToolBar::Show( bool show
)
949 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
950 bool bResult
= (tlw
!= NULL
);
954 #if wxOSX_USE_NATIVE_TOOLBAR
955 bool ownToolbarInstalled
= false;
956 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
957 if (ownToolbarInstalled
)
959 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
961 ShowHideWindowToolbar( tlw
, show
, false );
964 bResult
= wxToolBarBase::Show( show
);
967 bResult
= wxToolBarBase::Show( show
);
974 bool wxToolBar::IsShown() const
978 #if wxOSX_USE_NATIVE_TOOLBAR
979 bool ownToolbarInstalled
;
981 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
982 if (ownToolbarInstalled
)
984 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
985 bResult
= IsWindowToolbarVisible( tlw
);
988 bResult
= wxToolBarBase::IsShown();
991 bResult
= wxToolBarBase::IsShown();
997 void wxToolBar::DoGetSize( int *width
, int *height
) const
999 #if wxOSX_USE_NATIVE_TOOLBAR
1001 bool ownToolbarInstalled
;
1003 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1004 if ( ownToolbarInstalled
)
1006 // TODO: is this really a control ?
1007 GetControlBounds( (ControlRef
) m_macToolbar
, &boundsR
);
1008 if ( width
!= NULL
)
1009 *width
= boundsR
.right
- boundsR
.left
;
1010 if ( height
!= NULL
)
1011 *height
= boundsR
.bottom
- boundsR
.top
;
1014 wxToolBarBase::DoGetSize( width
, height
);
1017 wxToolBarBase::DoGetSize( width
, height
);
1021 wxSize
wxToolBar::DoGetBestSize() const
1025 DoGetSize( &width
, &height
);
1027 return wxSize( width
, height
);
1030 void wxToolBar::SetWindowStyleFlag( long style
)
1032 wxToolBarBase::SetWindowStyleFlag( style
);
1034 #if wxOSX_USE_NATIVE_TOOLBAR
1035 if (m_macToolbar
!= NULL
)
1037 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
1039 if ( style
& wxTB_NOICONS
)
1040 mode
= kHIToolbarDisplayModeLabelOnly
;
1041 else if ( style
& wxTB_TEXT
)
1042 mode
= kHIToolbarDisplayModeIconAndLabel
;
1044 mode
= kHIToolbarDisplayModeIconOnly
;
1046 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
1051 #if wxOSX_USE_NATIVE_TOOLBAR
1052 bool wxToolBar::MacWantsNativeToolbar()
1054 return m_macUsesNativeToolbar
;
1057 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1059 bool bResultV
= false;
1061 if (ownToolbarInstalled
!= NULL
)
1062 *ownToolbarInstalled
= false;
1064 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1067 HIToolbarRef curToolbarRef
= NULL
;
1068 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1069 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1070 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1071 *ownToolbarInstalled
= (curToolbarRef
== m_macToolbar
);
1077 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1079 bool bResult
= false;
1081 if (usesNative
&& (m_macToolbar
== NULL
))
1084 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
1087 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1091 // check the existing toolbar
1092 HIToolbarRef curToolbarRef
= NULL
;
1093 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1095 curToolbarRef
= NULL
;
1097 m_macUsesNativeToolbar
= usesNative
;
1099 if (m_macUsesNativeToolbar
)
1101 // only install toolbar if there isn't one installed already
1102 if (curToolbarRef
== NULL
)
1106 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macToolbar
);
1107 ShowHideWindowToolbar( tlw
, true, false );
1108 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1109 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1111 m_peer
->Move(0,0,0,0 );
1112 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1113 m_peer
->SetVisibility( false );
1114 wxToolBarBase::Show( false );
1119 // only deinstall toolbar if this is the installed one
1120 if (m_macToolbar
== curToolbarRef
)
1124 ShowHideWindowToolbar( tlw
, false, false );
1125 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1126 SetWindowToolbar( tlw
, NULL
);
1128 m_peer
->SetVisibility( true );
1133 InvalidateBestSize();
1135 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1140 bool wxToolBar::Realize()
1142 if (m_tools
.GetCount() == 0)
1148 int maxToolWidth
= 0;
1149 int maxToolHeight
= 0;
1151 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1152 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1155 GetSize( &tw
, &th
);
1157 // find the maximum tool width and height
1158 wxToolBarTool
*tool
;
1159 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1162 tool
= (wxToolBarTool
*) node
->GetData();
1165 wxSize sz
= tool
->GetSize();
1167 if ( sz
.x
> maxToolWidth
)
1168 maxToolWidth
= sz
.x
;
1169 if ( sz
.y
> maxToolHeight
)
1170 maxToolHeight
= sz
.y
;
1173 node
= node
->GetNext();
1176 bool lastIsRadio
= false;
1177 bool curIsRadio
= false;
1179 #if wxOSX_USE_NATIVE_TOOLBAR
1180 CFIndex currentPosition
= 0;
1181 bool insertAll
= false;
1183 HIToolbarRef refTB
= (HIToolbarRef
)m_macToolbar
;
1188 enc
= f
.GetEncoding();
1190 enc
= wxFont::GetDefaultEncoding();
1193 node
= m_tools
.GetFirst();
1196 tool
= (wxToolBarTool
*) node
->GetData();
1199 node
= node
->GetNext();
1203 // set tool position:
1204 // for the moment just perform a single row/column alignment
1205 wxSize cursize
= tool
->GetSize();
1206 if ( x
+ cursize
.x
> maxWidth
)
1207 maxWidth
= x
+ cursize
.x
;
1208 if ( y
+ cursize
.y
> maxHeight
)
1209 maxHeight
= y
+ cursize
.y
;
1211 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1213 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1214 tool
->SetPosition( wxPoint(x1
, y
) );
1218 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1219 tool
->SetPosition( wxPoint(x
, y1
) );
1222 // update the item positioning state
1223 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1224 y
+= cursize
.y
+ kwxMacToolSpacing
;
1226 x
+= cursize
.x
+ kwxMacToolSpacing
;
1228 #if wxOSX_USE_NATIVE_TOOLBAR
1229 // install in native HIToolbar
1232 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1233 if ( hiItemRef
!= NULL
)
1235 // since setting the help texts is non-virtual we have to update
1237 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1239 OSStatus err
= noErr
;
1244 // if this is the first tool that gets newly inserted or repositioned
1245 // first remove all 'old' tools from here to the right, because of this
1246 // all following tools will have to be reinserted (insertAll).
1247 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1249 node2
= node2
->GetPrevious() )
1251 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1253 const long idx
= tool2
->GetIndex();
1256 if ( tool2
->IsControl() )
1258 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1259 if ( count
!= 3 && count
!= 2 )
1261 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1264 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1266 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1269 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1272 if ( tool2
->IsControl() )
1274 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1277 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1280 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1283 tool2
->SetIndex(-1);
1288 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1291 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1294 tool
->SetIndex( currentPosition
);
1295 if ( tool
->IsControl() )
1297 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1298 if ( count
!= 3 && count
!= 2 )
1300 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1302 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1311 // update radio button (and group) state
1312 lastIsRadio
= curIsRadio
;
1313 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1317 if ( tool
->IsToggled() )
1318 DoToggleTool( tool
, true );
1324 if ( tool
->Toggle( true ) )
1326 DoToggleTool( tool
, true );
1329 else if ( tool
->IsToggled() )
1331 if ( tool
->IsToggled() )
1332 DoToggleTool( tool
, true );
1334 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1337 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1338 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1341 if ( toggleTool
->Toggle( false ) )
1342 DoToggleTool( toggleTool
, false );
1344 nodePrev
= nodePrev
->GetPrevious();
1349 node
= node
->GetNext();
1352 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1354 // if not set yet, only one row
1355 if ( m_maxRows
<= 0 )
1358 m_minWidth
= maxWidth
;
1360 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1361 m_minHeight
= m_maxHeight
= maxHeight
;
1365 // if not set yet, have one column
1366 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1367 SetRows( GetToolsCount() );
1369 m_minHeight
= maxHeight
;
1371 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1372 m_minWidth
= m_maxWidth
= maxWidth
;
1376 // FIXME: should this be OSX-only?
1378 bool wantNativeToolbar
, ownToolbarInstalled
;
1380 // attempt to install the native toolbar
1381 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1382 MacInstallNativeToolbar( wantNativeToolbar
);
1383 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1384 if (!ownToolbarInstalled
)
1386 SetSize( maxWidth
, maxHeight
);
1387 InvalidateBestSize();
1391 SetSize( maxWidth
, maxHeight
);
1392 InvalidateBestSize();
1400 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1402 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1403 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1405 #if wxOSX_USE_NATIVE_TOOLBAR
1406 if (m_macToolbar
!= NULL
)
1408 int maxs
= wxMax( size
.x
, size
.y
);
1409 HIToolbarDisplaySize sizeSpec
;
1411 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1412 else if ( maxs
> 24 )
1413 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1415 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1417 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, sizeSpec
);
1422 // The button size is bigger than the bitmap size
1423 wxSize
wxToolBar::GetToolSize() const
1425 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1428 void wxToolBar::SetRows(int nRows
)
1430 // avoid resizing the frame uselessly
1431 if ( nRows
!= m_maxRows
)
1435 void wxToolBar::MacSuperChangedPosition()
1437 wxWindow::MacSuperChangedPosition();
1439 #if wxOSX_USE_NATIVE_TOOLBAR
1440 if (! m_macUsesNativeToolbar
)
1448 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1450 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1453 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1455 tool
->SetNormalBitmap(bitmap
);
1457 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1458 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1462 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1464 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1467 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1469 tool
->SetDisabledBitmap(bitmap
);
1471 // TODO: what to do for this one?
1475 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1477 wxToolBarTool
*tool
;
1478 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1481 tool
= (wxToolBarTool
*)node
->GetData();
1484 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1485 if ( r
.Contains( wxPoint( x
, y
) ) )
1489 node
= node
->GetNext();
1492 return (wxToolBarToolBase
*)NULL
;
1495 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1497 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1499 return tool
->GetShortHelp();
1501 return wxEmptyString
;
1504 void wxToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(t
), bool WXUNUSED(enable
))
1506 // everything already done in the tool's implementation
1509 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1511 // everything already done in the tool's implementation
1514 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1516 wxToolBarTool
*tool
= static_cast< wxToolBarTool
*>(toolBase
);
1520 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1521 wxSize toolSize
= GetToolSize();
1522 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1523 ControlRef controlHandle
= NULL
;
1526 #if wxOSX_USE_NATIVE_TOOLBAR
1527 wxString label
= tool
->GetLabel();
1528 if (m_macToolbar
&& !label
.empty() )
1530 // strip mnemonics from the label for compatibility
1531 // with the usual labels in wxStaticText sense
1532 label
= wxStripMenuCodes(label
);
1534 #endif // wxOSX_USE_NATIVE_TOOLBAR
1536 switch (tool
->GetStyle())
1538 case wxTOOL_STYLE_SEPARATOR
:
1540 wxASSERT( tool
->GetControlHandle() == NULL
);
1543 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1544 toolrect
.bottom
= toolSize
.y
;
1546 toolrect
.right
= toolSize
.x
;
1548 // in flat style we need a visual separator
1549 #if wxOSX_USE_NATIVE_TOOLBAR
1550 if (m_macToolbar
!= NULL
)
1552 HIToolbarItemRef item
;
1553 err
= HIToolbarItemCreate(
1554 kHIToolbarSeparatorIdentifier
,
1555 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1558 tool
->SetToolbarItemRef( item
);
1562 #endif // wxOSX_USE_NATIVE_TOOLBAR
1564 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1565 tool
->SetControlHandle( controlHandle
);
1569 case wxTOOL_STYLE_BUTTON
:
1571 wxASSERT( tool
->GetControlHandle() == NULL
);
1572 ControlButtonContentInfo info
;
1573 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1575 if ( UMAGetSystemVersion() >= 0x1000)
1577 // contrary to the docs this control only works with iconrefs
1578 ControlButtonContentInfo info
;
1579 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1580 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1581 wxMacReleaseBitmapButton( &info
);
1585 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1586 if ( tool
->CanBeToggled() )
1587 behaviour
|= kControlBehaviorToggles
;
1588 err
= CreateBevelButtonControl( window
,
1589 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1590 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1593 #if wxOSX_USE_NATIVE_TOOLBAR
1594 if (m_macToolbar
!= NULL
)
1596 HIToolbarItemRef item
;
1597 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1598 err
= HIToolbarItemCreate(
1599 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1600 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1603 ControlButtonContentInfo info2
;
1604 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1606 InstallEventHandler(
1607 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1608 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1609 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1610 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1611 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1612 tool
->SetToolbarItemRef( item
);
1614 wxMacReleaseBitmapButton( &info2
);
1619 #endif // wxOSX_USE_NATIVE_TOOLBAR
1621 wxMacReleaseBitmapButton( &info
);
1624 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1625 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1628 InstallControlEventHandler(
1629 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1630 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1632 tool
->SetControlHandle( controlHandle
);
1636 case wxTOOL_STYLE_CONTROL
:
1638 #if wxOSX_USE_NATIVE_TOOLBAR
1639 if (m_macToolbar
!= NULL
)
1641 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1642 HIToolbarItemRef item
;
1643 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1644 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1645 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macToolbar
,kControlToolbarItemClassID
,
1650 tool
->SetToolbarItemRef( item
);
1660 // right now there's nothing to do here
1670 if ( controlHandle
)
1672 ControlRef container
= (ControlRef
) GetHandle();
1673 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1675 SetControlVisibility( controlHandle
, true, true );
1676 ::EmbedControl( controlHandle
, container
);
1679 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1680 tool
->UpdateToggleImage( true );
1682 // nothing special to do here - we relayout in Realize() later
1683 InvalidateBestSize();
1687 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1690 return (err
== noErr
);
1693 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1698 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1700 wxToolBarTool
* tool
= static_cast< wxToolBarTool
*>(toolbase
);
1701 wxToolBarToolsList::compatibility_iterator node
;
1702 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1704 wxToolBarToolBase
*tool2
= node
->GetData();
1705 if ( tool2
== tool
)
1707 // let node point to the next node in the list
1708 node
= node
->GetNext();
1714 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1716 #if wxOSX_USE_NATIVE_TOOLBAR
1717 CFIndex removeIndex
= tool
->GetIndex();
1720 #if wxOSX_USE_NATIVE_TOOLBAR
1721 if (m_macToolbar
!= NULL
)
1723 if ( removeIndex
!= -1 && m_macToolbar
)
1725 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macToolbar
, removeIndex
);
1726 tool
->SetIndex( -1 );
1731 tool
->ClearControl();
1733 // and finally reposition all the controls after this one
1735 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1737 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1738 wxPoint pt
= tool2
->GetPosition();
1740 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1745 tool2
->SetPosition( pt
);
1747 #if wxOSX_USE_NATIVE_TOOLBAR
1748 if (m_macToolbar
!= NULL
)
1750 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1751 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1756 InvalidateBestSize();
1761 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1763 #if wxOSX_USE_NATIVE_TOOLBAR
1764 if ( m_macUsesNativeToolbar
)
1776 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1778 if ( !drawMetalTheme
)
1780 HIThemePlacardDrawInfo info
;
1781 memset( &info
, 0, sizeof(info
) );
1783 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1785 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1786 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1787 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1791 // leave the background as it is (striped or metal)
1797 #endif // wxUSE_TOOLBAR