1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/uma.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;
36 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
38 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
39 EVT_PAINT( wxToolBar::OnPaint
)
44 #pragma mark Tool Implementation
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
53 // when embedding native controls in the native toolbar we must make sure the
54 // control does not get deleted behind our backs, so the retain count gets increased
55 // (after creation it is 1), first be the creation of the custom HIToolbarItem wrapper
56 // object, and second by the code 'creating' the custom HIView (which is the same as the
57 // already existing native control, therefore we just increase the ref count)
58 // when this view is removed from the native toolbar its count gets decremented again
59 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
60 // so in the end the control lives with a refcount of one and can be disposed of by the
61 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
62 // so we can only test against a range, not a specific value of the refcount.
64 class wxToolBarTool
: public wxToolBarToolBase
70 const wxString
& label
,
71 const wxBitmap
& bmpNormal
,
72 const wxBitmap
& bmpDisabled
,
75 const wxString
& shortHelp
,
76 const wxString
& longHelp
);
78 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
79 : wxToolBarToolBase(tbar
, control
, label
)
83 SetControlHandle( (ControlRef
) control
->GetHandle() );
86 virtual ~wxToolBarTool()
91 WXWidget
GetControlHandle()
93 return (WXWidget
) m_controlHandle
;
96 void SetControlHandle( ControlRef handle
)
98 m_controlHandle
= handle
;
101 void SetPosition( const wxPoint
& position
);
105 if ( m_controlHandle
)
108 DisposeControl( m_controlHandle
);
111 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
112 // proper wxControl destructor
114 m_controlHandle
= NULL
;
117 #if wxOSX_USE_NATIVE_TOOLBAR
118 if ( m_toolbarItemRef
)
120 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
121 // different behaviour under Leopard
122 if ( UMAGetSystemVersion() < 0x1050 )
126 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
129 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
130 CFRelease(m_toolbarItemRef
);
131 m_toolbarItemRef
= NULL
;
133 #endif // wxOSX_USE_NATIVE_TOOLBAR
136 wxSize
GetSize() const
142 curSize
= GetControl()->GetSize();
144 else if ( IsButton() )
146 curSize
= GetToolBar()->GetToolSize();
151 curSize
= GetToolBar()->GetToolSize();
152 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
161 wxPoint
GetPosition() const
163 return wxPoint( m_x
, m_y
);
166 bool DoEnable( bool enable
);
168 void UpdateToggleImage( bool toggle
);
170 #if wxOSX_USE_NATIVE_TOOLBAR
171 void SetToolbarItemRef( HIToolbarItemRef ref
)
173 if ( m_controlHandle
)
174 HideControl( m_controlHandle
);
175 if ( m_toolbarItemRef
)
176 CFRelease( m_toolbarItemRef
);
178 m_toolbarItemRef
= ref
;
179 if ( m_toolbarItemRef
)
181 wxFontEncoding enc
= GetToolBarFontEncoding();
183 HIToolbarItemSetHelpText(
185 wxCFStringRef( GetShortHelp(), enc
),
186 wxCFStringRef( GetLongHelp(), enc
) );
190 HIToolbarItemRef
GetToolbarItemRef() const
192 return m_toolbarItemRef
;
195 void SetIndex( CFIndex idx
)
200 CFIndex
GetIndex() const
205 virtual void SetLabel(const wxString
& label
)
207 wxToolBarToolBase::SetLabel(label
);
209 if ( m_toolbarItemRef
)
211 // strip mnemonics from the label for compatibility with the usual
212 // labels in wxStaticText sense
213 wxString labelStr
= wxStripMenuCodes(label
);
215 HIToolbarItemSetLabel(
217 wxCFStringRef(labelStr
, GetToolBarFontEncoding()) );
220 #endif // wxOSX_USE_NATIVE_TOOLBAR
223 #if wxOSX_USE_NATIVE_TOOLBAR
224 wxFontEncoding
GetToolBarFontEncoding() const
229 f
= GetToolBar()->GetFont();
230 return f
.IsOk() ? f
.GetEncoding() : wxFont::GetDefaultEncoding();
232 #endif // wxOSX_USE_NATIVE_TOOLBAR
236 m_controlHandle
= NULL
;
238 #if wxOSX_USE_NATIVE_TOOLBAR
239 m_toolbarItemRef
= NULL
;
244 ControlRef m_controlHandle
;
248 #if wxOSX_USE_NATIVE_TOOLBAR
249 HIToolbarItemRef m_toolbarItemRef
;
250 // position in its toolbar, -1 means not inserted
255 static const EventTypeSpec eventList
[] =
257 { kEventClassControl
, kEventControlHit
},
258 { kEventClassControl
, kEventControlHitTest
},
261 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
263 OSStatus result
= eventNotHandledErr
;
264 ControlRef controlRef
;
265 wxMacCarbonEvent
cEvent( event
);
267 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
269 switch ( GetEventKind( event
) )
271 case kEventControlHit
:
273 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
274 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
275 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
279 shouldToggle
= !tbartool
->IsToggled();
281 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
284 if (tbartool
!= NULL
)
285 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
290 case kEventControlHitTest
:
292 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
294 HIViewGetBounds( controlRef
, &rect
);
296 ControlPartCode pc
= kControlNoPart
;
297 if ( CGRectContainsPoint( rect
, pt
) )
298 pc
= kControlIconPart
;
299 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
311 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
313 OSStatus result
= eventNotHandledErr
;
315 switch ( GetEventClass( event
) )
317 case kEventClassControl
:
318 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
328 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
330 #if wxOSX_USE_NATIVE_TOOLBAR
332 static const EventTypeSpec toolBarEventList
[] =
334 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
337 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
339 OSStatus result
= eventNotHandledErr
;
341 switch ( GetEventKind( event
) )
343 case kEventToolbarItemPerformAction
:
345 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
346 if ( tbartool
!= NULL
)
348 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
349 int toolID
= tbartool
->GetId();
351 if ( tbartool
->CanBeToggled() )
354 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
358 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
371 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
373 OSStatus result
= eventNotHandledErr
;
375 switch ( GetEventClass( event
) )
377 case kEventClassToolbarItem
:
378 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
388 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
392 bool wxToolBarTool::DoEnable( bool enable
)
396 GetControl()->Enable( enable
);
398 else if ( IsButton() )
400 #if wxOSX_USE_NATIVE_TOOLBAR
401 if ( m_toolbarItemRef
!= NULL
)
402 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
405 if ( m_controlHandle
!= NULL
)
408 EnableControl( m_controlHandle
);
410 DisableControl( m_controlHandle
);
417 void wxToolBarTool::SetPosition( const wxPoint
& position
)
422 int mac_x
= position
.x
;
423 int mac_y
= position
.y
;
428 GetControlBounds( m_controlHandle
, &contrlRect
);
429 int former_mac_x
= contrlRect
.left
;
430 int former_mac_y
= contrlRect
.top
;
431 GetToolBar()->GetToolSize();
433 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
435 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
438 else if ( IsControl() )
440 // embedded native controls are moved by the OS
441 #if wxOSX_USE_NATIVE_TOOLBAR
442 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
445 GetControl()->Move( position
);
452 GetControlBounds( m_controlHandle
, &contrlRect
);
453 int former_mac_x
= contrlRect
.left
;
454 int former_mac_y
= contrlRect
.top
;
456 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
457 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
461 void wxToolBarTool::UpdateToggleImage( bool toggle
)
465 int w
= m_bmpNormal
.GetWidth();
466 int h
= m_bmpNormal
.GetHeight();
467 wxBitmap
bmp( w
, h
);
470 dc
.SelectObject( bmp
);
471 dc
.SetPen( wxPen(*wxBLACK
) );
472 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
473 dc
.DrawRectangle( 0, 0, w
, h
);
474 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
475 dc
.SelectObject( wxNullBitmap
);
476 ControlButtonContentInfo info
;
477 wxMacCreateBitmapButton( &info
, bmp
);
478 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
479 #if wxOSX_USE_NATIVE_TOOLBAR
480 if (m_toolbarItemRef
!= NULL
)
482 ControlButtonContentInfo info2
;
483 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
484 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
485 wxMacReleaseBitmapButton( &info2
);
488 wxMacReleaseBitmapButton( &info
);
492 ControlButtonContentInfo info
;
493 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
494 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
495 #if wxOSX_USE_NATIVE_TOOLBAR
496 if (m_toolbarItemRef
!= NULL
)
498 ControlButtonContentInfo info2
;
499 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
500 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
501 wxMacReleaseBitmapButton( &info2
);
504 wxMacReleaseBitmapButton( &info
);
507 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
509 m_controlHandle
, 0, kControlIconTransformTag
,
510 sizeof(transform
), (Ptr
)&transform
);
511 HIViewSetNeedsDisplay( m_controlHandle
, true );
515 wxToolBarTool::wxToolBarTool(
518 const wxString
& label
,
519 const wxBitmap
& bmpNormal
,
520 const wxBitmap
& bmpDisabled
,
522 wxObject
*clientData
,
523 const wxString
& shortHelp
,
524 const wxString
& longHelp
)
527 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
528 clientData
, shortHelp
, longHelp
)
534 #pragma mark Toolbar Implementation
536 wxToolBarToolBase
*wxToolBar::CreateTool(
538 const wxString
& label
,
539 const wxBitmap
& bmpNormal
,
540 const wxBitmap
& bmpDisabled
,
542 wxObject
*clientData
,
543 const wxString
& shortHelp
,
544 const wxString
& longHelp
)
546 return new wxToolBarTool(
547 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
548 clientData
, shortHelp
, longHelp
);
552 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
554 return new wxToolBarTool(this, control
, label
);
557 void wxToolBar::Init()
561 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
562 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
564 #if wxOSX_USE_NATIVE_TOOLBAR
565 m_macHIToolbarRef
= NULL
;
566 m_macUsesNativeToolbar
= false;
570 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
572 const EventTypeSpec kEvents
[] =
574 { kEventClassHIObject
, kEventHIObjectConstruct
},
575 { kEventClassHIObject
, kEventHIObjectInitialize
},
576 { kEventClassHIObject
, kEventHIObjectDestruct
},
578 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
581 const EventTypeSpec kViewEvents
[] =
583 { kEventClassControl
, kEventControlGetSizeConstraints
}
586 struct ControlToolbarItem
588 HIToolbarItemRef toolbarItem
;
590 wxSize lastValidSize
;
593 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
595 OSStatus result
= eventNotHandledErr
;
596 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
598 switch ( GetEventClass( inEvent
) )
600 case kEventClassHIObject
:
601 switch ( GetEventKind( inEvent
) )
603 case kEventHIObjectConstruct
:
605 HIObjectRef toolbarItem
;
606 ControlToolbarItem
* item
;
608 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
609 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
611 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
612 item
->toolbarItem
= toolbarItem
;
613 item
->lastValidSize
= wxSize(-1,-1);
614 item
->viewRef
= NULL
;
616 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
622 case kEventHIObjectInitialize
:
623 result
= CallNextEventHandler( inCallRef
, inEvent
);
624 if ( result
== noErr
)
627 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
628 sizeof( CFTypeRef
), NULL
, &data
);
632 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
633 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
635 object
->viewRef
= (HIViewRef
) viewRef
;
636 // make sure we keep that control during our lifetime
637 CFRetain( object
->viewRef
) ;
639 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
640 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
645 case kEventHIObjectDestruct
:
647 HIViewRef viewRef
= object
->viewRef
;
648 if( viewRef
&& IsValidControlHandle( viewRef
) )
650 // depending whether the wxControl corresponding to this HIView has already been destroyed or
651 // not, ref counts differ, so we cannot assert a special value
652 CFIndex count
= CFGetRetainCount( viewRef
) ;
655 wxFAIL_MSG("Reference count of native tool was illegal before removal");
657 CFRelease( viewRef
) ;
667 case kEventClassToolbarItem
:
668 switch ( GetEventKind( inEvent
) )
670 case kEventToolbarItemCreateCustomView
:
672 HIViewRef viewRef
= object
->viewRef
;
673 HIViewRemoveFromSuperview( viewRef
) ;
674 HIViewSetVisible(viewRef
, true) ;
675 CFRetain( viewRef
) ;
676 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
682 case kEventClassControl
:
683 switch ( GetEventKind( inEvent
) )
685 case kEventControlGetSizeConstraints
:
687 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
690 // during toolbar layout the native window sometimes gets negative sizes,
691 // sometimes it just gets shrunk behind our back, so in order to avoid
692 // ever shrinking more, once a valid size is captured, we keep it
694 wxSize sz
= object
->lastValidSize
;
695 if ( sz
.x
<= 0 || sz
.y
<= 0 )
697 sz
= wxwindow
->GetSize() ;
698 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
699 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
700 if ( sz
.x
> 0 && sz
.y
> 0 )
701 object
->lastValidSize
= sz
;
706 // Extra width to avoid edge of combobox being cut off
710 min
.width
= max
.width
= sz
.x
;
711 min
.height
= max
.height
= sz
.y
;
713 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
714 sizeof( HISize
), &min
);
716 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
717 sizeof( HISize
), &max
);
729 void RegisterControlToolbarItemClass()
731 static bool sRegistered
;
735 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
736 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
742 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
744 RegisterControlToolbarItemClass();
748 UInt32 options
= kHIToolbarItemAllowDuplicates
;
749 HIToolbarItemRef result
= NULL
;
751 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
752 require_noerr( err
, CantCreateEvent
);
754 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
755 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
758 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
760 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
763 ReleaseEvent( event
);
768 #if wxOSX_USE_NATIVE_TOOLBAR
769 static const EventTypeSpec kToolbarEvents
[] =
771 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
772 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
773 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
776 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
778 void* WXUNUSED(inUserData
))
780 OSStatus result
= eventNotHandledErr
;
782 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
783 CFMutableArrayRef array
;
785 switch ( GetEventKind( inEvent
) )
787 case kEventToolbarGetDefaultIdentifiers
:
789 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
790 sizeof( CFMutableArrayRef
), NULL
, &array
);
791 // not implemented yet
792 // GetToolbarDefaultItems( array );
797 case kEventToolbarGetAllowedIdentifiers
:
799 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
800 sizeof( CFMutableArrayRef
), NULL
, &array
);
801 // not implemented yet
802 // GetToolbarAllowedItems( array );
806 case kEventToolbarCreateItemWithIdentifier
:
808 HIToolbarItemRef item
= NULL
;
809 CFTypeRef data
= NULL
;
810 CFStringRef identifier
= NULL
;
812 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
813 sizeof( CFStringRef
), NULL
, &identifier
);
815 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
816 sizeof( CFTypeRef
), NULL
, &data
);
818 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
820 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
823 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
824 sizeof( HIToolbarItemRef
), &item
);
834 #endif // wxOSX_USE_NATIVE_TOOLBAR
836 // also for the toolbar we have the dual implementation:
837 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
839 bool wxToolBar::Create(
845 const wxString
& name
)
847 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
852 OSStatus err
= noErr
;
854 #if wxOSX_USE_NATIVE_TOOLBAR
855 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
857 wxString labelStr
= wxString::Format( wxT("%p"), this );
858 err
= HIToolbarCreate(
859 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
860 (HIToolbarRef
*) &m_macHIToolbarRef
);
862 if (m_macHIToolbarRef
!= NULL
)
864 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macHIToolbarRef
), ToolbarDelegateHandler
,
865 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
867 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
868 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
870 if ( style
& wxTB_NOICONS
)
871 mode
= kHIToolbarDisplayModeLabelOnly
;
872 else if ( style
& wxTB_TEXT
)
873 mode
= kHIToolbarDisplayModeIconAndLabel
;
875 mode
= kHIToolbarDisplayModeIconOnly
;
877 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
878 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
881 #endif // wxOSX_USE_NATIVE_TOOLBAR
883 return (err
== noErr
);
886 wxToolBar::~wxToolBar()
888 #if wxOSX_USE_NATIVE_TOOLBAR
889 if (m_macHIToolbarRef
!= NULL
)
891 // if this is the installed toolbar, then deinstall it
892 if (m_macUsesNativeToolbar
)
893 MacInstallNativeToolbar( false );
895 CFIndex count
= CFGetRetainCount( m_macHIToolbarRef
) ;
896 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
897 if ( UMAGetSystemVersion() < 0x1050 )
901 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
904 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
905 m_macHIToolbarRef
= NULL
;
910 bool wxToolBar::Show( bool show
)
912 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
913 bool bResult
= (tlw
!= NULL
);
917 #if wxOSX_USE_NATIVE_TOOLBAR
918 bool ownToolbarInstalled
= false;
919 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
920 if (ownToolbarInstalled
)
922 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
924 ShowHideWindowToolbar( tlw
, show
, false );
927 bResult
= wxToolBarBase::Show( show
);
930 bResult
= wxToolBarBase::Show( show
);
937 bool wxToolBar::IsShown() const
941 #if wxOSX_USE_NATIVE_TOOLBAR
942 bool ownToolbarInstalled
;
944 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
945 if (ownToolbarInstalled
)
947 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
948 bResult
= IsWindowToolbarVisible( tlw
);
951 bResult
= wxToolBarBase::IsShown();
954 bResult
= wxToolBarBase::IsShown();
960 void wxToolBar::DoGetSize( int *width
, int *height
) const
962 #if wxOSX_USE_NATIVE_TOOLBAR
964 bool ownToolbarInstalled
;
966 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
967 if ( ownToolbarInstalled
)
969 // TODO: is this really a control ?
970 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
972 *width
= boundsR
.right
- boundsR
.left
;
973 if ( height
!= NULL
)
974 *height
= boundsR
.bottom
- boundsR
.top
;
977 wxToolBarBase::DoGetSize( width
, height
);
980 wxToolBarBase::DoGetSize( width
, height
);
984 wxSize
wxToolBar::DoGetBestSize() const
988 DoGetSize( &width
, &height
);
990 return wxSize( width
, height
);
993 void wxToolBar::SetWindowStyleFlag( long style
)
995 wxToolBarBase::SetWindowStyleFlag( style
);
997 #if wxOSX_USE_NATIVE_TOOLBAR
998 if (m_macHIToolbarRef
!= NULL
)
1000 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
1002 if ( style
& wxTB_NOICONS
)
1003 mode
= kHIToolbarDisplayModeLabelOnly
;
1004 else if ( style
& wxTB_TEXT
)
1005 mode
= kHIToolbarDisplayModeIconAndLabel
;
1007 mode
= kHIToolbarDisplayModeIconOnly
;
1009 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
1014 #if wxOSX_USE_NATIVE_TOOLBAR
1015 bool wxToolBar::MacWantsNativeToolbar()
1017 return m_macUsesNativeToolbar
;
1020 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1022 bool bResultV
= false;
1024 if (ownToolbarInstalled
!= NULL
)
1025 *ownToolbarInstalled
= false;
1027 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1030 HIToolbarRef curToolbarRef
= NULL
;
1031 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1032 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1033 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1034 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
1040 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1042 bool bResult
= false;
1044 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
1047 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
1050 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1054 // check the existing toolbar
1055 HIToolbarRef curToolbarRef
= NULL
;
1056 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1058 curToolbarRef
= NULL
;
1060 m_macUsesNativeToolbar
= usesNative
;
1062 if (m_macUsesNativeToolbar
)
1064 // only install toolbar if there isn't one installed already
1065 if (curToolbarRef
== NULL
)
1069 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
1070 ShowHideWindowToolbar( tlw
, true, false );
1071 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1072 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1074 m_peer
->Move(0,0,0,0 );
1075 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1076 m_peer
->SetVisibility( false );
1077 wxToolBarBase::Show( false );
1082 // only deinstall toolbar if this is the installed one
1083 if (m_macHIToolbarRef
== curToolbarRef
)
1087 ShowHideWindowToolbar( tlw
, false, false );
1088 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1089 SetWindowToolbar( tlw
, NULL
);
1091 m_peer
->SetVisibility( true );
1096 InvalidateBestSize();
1098 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1103 bool wxToolBar::Realize()
1105 if (m_tools
.GetCount() == 0)
1111 int maxToolWidth
= 0;
1112 int maxToolHeight
= 0;
1114 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1115 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1118 GetSize( &tw
, &th
);
1120 // find the maximum tool width and height
1121 wxToolBarTool
*tool
;
1122 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1125 tool
= (wxToolBarTool
*) node
->GetData();
1128 wxSize sz
= tool
->GetSize();
1130 if ( sz
.x
> maxToolWidth
)
1131 maxToolWidth
= sz
.x
;
1132 if ( sz
.y
> maxToolHeight
)
1133 maxToolHeight
= sz
.y
;
1136 node
= node
->GetNext();
1139 bool lastIsRadio
= false;
1140 bool curIsRadio
= false;
1142 #if wxOSX_USE_NATIVE_TOOLBAR
1143 CFIndex currentPosition
= 0;
1144 bool insertAll
= false;
1146 HIToolbarRef refTB
= (HIToolbarRef
)m_macHIToolbarRef
;
1151 enc
= f
.GetEncoding();
1153 enc
= wxFont::GetDefaultEncoding();
1156 node
= m_tools
.GetFirst();
1159 tool
= (wxToolBarTool
*) node
->GetData();
1162 node
= node
->GetNext();
1166 // set tool position:
1167 // for the moment just perform a single row/column alignment
1168 wxSize cursize
= tool
->GetSize();
1169 if ( x
+ cursize
.x
> maxWidth
)
1170 maxWidth
= x
+ cursize
.x
;
1171 if ( y
+ cursize
.y
> maxHeight
)
1172 maxHeight
= y
+ cursize
.y
;
1174 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1176 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1177 tool
->SetPosition( wxPoint(x1
, y
) );
1181 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1182 tool
->SetPosition( wxPoint(x
, y1
) );
1185 // update the item positioning state
1186 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1187 y
+= cursize
.y
+ kwxMacToolSpacing
;
1189 x
+= cursize
.x
+ kwxMacToolSpacing
;
1191 #if wxOSX_USE_NATIVE_TOOLBAR
1192 // install in native HIToolbar
1195 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1196 if ( hiItemRef
!= NULL
)
1198 // since setting the help texts is non-virtual we have to update
1200 HIToolbarItemSetHelpText( hiItemRef
,
1201 wxCFStringRef( tool
->GetShortHelp(), enc
),
1202 wxCFStringRef( tool
->GetLongHelp(), enc
) );
1204 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1206 OSStatus err
= noErr
;
1211 // if this is the first tool that gets newly inserted or repositioned
1212 // first remove all 'old' tools from here to the right, because of this
1213 // all following tools will have to be reinserted (insertAll).
1214 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1216 node2
= node2
->GetPrevious() )
1218 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1220 const long idx
= tool2
->GetIndex();
1223 if ( tool2
->IsControl() )
1225 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1226 if ( count
!= 3 && count
!= 2 )
1228 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1231 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1233 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1236 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1239 if ( tool2
->IsControl() )
1241 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1244 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1247 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1250 tool2
->SetIndex(-1);
1255 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1258 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1261 tool
->SetIndex( currentPosition
);
1262 if ( tool
->IsControl() )
1264 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1265 if ( count
!= 3 && count
!= 2 )
1267 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1269 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1278 // update radio button (and group) state
1279 lastIsRadio
= curIsRadio
;
1280 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1284 if ( tool
->IsToggled() )
1285 DoToggleTool( tool
, true );
1291 if ( tool
->Toggle( true ) )
1293 DoToggleTool( tool
, true );
1296 else if ( tool
->IsToggled() )
1298 if ( tool
->IsToggled() )
1299 DoToggleTool( tool
, true );
1301 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1304 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1305 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1308 if ( toggleTool
->Toggle( false ) )
1309 DoToggleTool( toggleTool
, false );
1311 nodePrev
= nodePrev
->GetPrevious();
1316 node
= node
->GetNext();
1319 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1321 // if not set yet, only one row
1322 if ( m_maxRows
<= 0 )
1325 m_minWidth
= maxWidth
;
1327 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1328 m_minHeight
= m_maxHeight
= maxHeight
;
1332 // if not set yet, have one column
1333 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1334 SetRows( GetToolsCount() );
1336 m_minHeight
= maxHeight
;
1338 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1339 m_minWidth
= m_maxWidth
= maxWidth
;
1343 // FIXME: should this be OSX-only?
1345 bool wantNativeToolbar
, ownToolbarInstalled
;
1347 // attempt to install the native toolbar
1348 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1349 MacInstallNativeToolbar( wantNativeToolbar
);
1350 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1351 if (!ownToolbarInstalled
)
1353 SetSize( maxWidth
, maxHeight
);
1354 InvalidateBestSize();
1358 SetSize( maxWidth
, maxHeight
);
1359 InvalidateBestSize();
1367 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1369 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1370 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1372 #if wxOSX_USE_NATIVE_TOOLBAR
1373 if (m_macHIToolbarRef
!= NULL
)
1375 int maxs
= wxMax( size
.x
, size
.y
);
1376 HIToolbarDisplaySize sizeSpec
;
1378 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1379 else if ( maxs
> 24 )
1380 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1382 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1384 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1389 // The button size is bigger than the bitmap size
1390 wxSize
wxToolBar::GetToolSize() const
1392 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1395 void wxToolBar::SetRows(int nRows
)
1397 // avoid resizing the frame uselessly
1398 if ( nRows
!= m_maxRows
)
1402 void wxToolBar::MacSuperChangedPosition()
1404 wxWindow::MacSuperChangedPosition();
1406 #if wxOSX_USE_NATIVE_TOOLBAR
1407 if (! m_macUsesNativeToolbar
)
1415 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1417 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1420 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1422 tool
->SetNormalBitmap(bitmap
);
1424 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1425 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1429 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1431 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1434 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1436 tool
->SetDisabledBitmap(bitmap
);
1438 // TODO: what to do for this one?
1442 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1444 wxToolBarTool
*tool
;
1445 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1448 tool
= (wxToolBarTool
*)node
->GetData();
1451 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1452 if ( r
.Contains( wxPoint( x
, y
) ) )
1456 node
= node
->GetNext();
1459 return (wxToolBarToolBase
*)NULL
;
1462 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1464 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1466 return tool
->GetShortHelp();
1468 return wxEmptyString
;
1471 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1474 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1477 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1479 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1480 if ( ( tool
!= NULL
) && tool
->IsButton() )
1481 tool
->UpdateToggleImage( toggle
);
1484 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1486 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1490 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1491 wxSize toolSize
= GetToolSize();
1492 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1493 ControlRef controlHandle
= NULL
;
1496 #if wxOSX_USE_NATIVE_TOOLBAR
1497 wxString label
= tool
->GetLabel();
1498 if (m_macHIToolbarRef
&& !label
.empty() )
1500 // strip mnemonics from the label for compatibility
1501 // with the usual labels in wxStaticText sense
1502 label
= wxStripMenuCodes(label
);
1504 #endif // wxOSX_USE_NATIVE_TOOLBAR
1506 switch (tool
->GetStyle())
1508 case wxTOOL_STYLE_SEPARATOR
:
1510 wxASSERT( tool
->GetControlHandle() == NULL
);
1513 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1514 toolrect
.bottom
= toolSize
.y
;
1516 toolrect
.right
= toolSize
.x
;
1518 // in flat style we need a visual separator
1519 #if wxOSX_USE_NATIVE_TOOLBAR
1520 if (m_macHIToolbarRef
!= NULL
)
1522 HIToolbarItemRef item
;
1523 err
= HIToolbarItemCreate(
1524 kHIToolbarSeparatorIdentifier
,
1525 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1528 tool
->SetToolbarItemRef( item
);
1532 #endif // wxOSX_USE_NATIVE_TOOLBAR
1534 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1535 tool
->SetControlHandle( controlHandle
);
1539 case wxTOOL_STYLE_BUTTON
:
1541 wxASSERT( tool
->GetControlHandle() == NULL
);
1542 ControlButtonContentInfo info
;
1543 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1545 if ( UMAGetSystemVersion() >= 0x1000)
1547 // contrary to the docs this control only works with iconrefs
1548 ControlButtonContentInfo info
;
1549 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1550 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1551 wxMacReleaseBitmapButton( &info
);
1555 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1556 if ( tool
->CanBeToggled() )
1557 behaviour
|= kControlBehaviorToggles
;
1558 err
= CreateBevelButtonControl( window
,
1559 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1560 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1563 #if wxOSX_USE_NATIVE_TOOLBAR
1564 if (m_macHIToolbarRef
!= NULL
)
1566 HIToolbarItemRef item
;
1567 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1568 err
= HIToolbarItemCreate(
1569 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1570 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1573 ControlButtonContentInfo info2
;
1574 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1576 InstallEventHandler(
1577 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1578 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1579 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1580 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1581 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1582 tool
->SetToolbarItemRef( item
);
1584 wxMacReleaseBitmapButton( &info2
);
1589 #endif // wxOSX_USE_NATIVE_TOOLBAR
1591 wxMacReleaseBitmapButton( &info
);
1594 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1595 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1598 InstallControlEventHandler(
1599 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1600 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1602 tool
->SetControlHandle( controlHandle
);
1606 case wxTOOL_STYLE_CONTROL
:
1608 #if wxOSX_USE_NATIVE_TOOLBAR
1609 if (m_macHIToolbarRef
!= NULL
)
1611 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1612 HIToolbarItemRef item
;
1613 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1614 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1615 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macHIToolbarRef
,kControlToolbarItemClassID
,
1620 tool
->SetToolbarItemRef( item
);
1630 // right now there's nothing to do here
1640 if ( controlHandle
)
1642 ControlRef container
= (ControlRef
) GetHandle();
1643 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1645 SetControlVisibility( controlHandle
, true, true );
1646 ::EmbedControl( controlHandle
, container
);
1649 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1650 tool
->UpdateToggleImage( true );
1652 // nothing special to do here - we relayout in Realize() later
1653 InvalidateBestSize();
1657 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1660 return (err
== noErr
);
1663 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1665 wxFAIL_MSG( wxT("not implemented") );
1668 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1670 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1671 wxToolBarToolsList::compatibility_iterator node
;
1672 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1674 wxToolBarToolBase
*tool2
= node
->GetData();
1675 if ( tool2
== tool
)
1677 // let node point to the next node in the list
1678 node
= node
->GetNext();
1684 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1686 #if wxOSX_USE_NATIVE_TOOLBAR
1687 CFIndex removeIndex
= tool
->GetIndex();
1690 #if wxOSX_USE_NATIVE_TOOLBAR
1691 if (m_macHIToolbarRef
!= NULL
)
1693 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1695 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1696 tool
->SetIndex( -1 );
1701 tool
->ClearControl();
1703 // and finally reposition all the controls after this one
1705 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1707 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1708 wxPoint pt
= tool2
->GetPosition();
1710 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1715 tool2
->SetPosition( pt
);
1717 #if wxOSX_USE_NATIVE_TOOLBAR
1718 if (m_macHIToolbarRef
!= NULL
)
1720 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1721 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1726 InvalidateBestSize();
1731 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1733 #if wxOSX_USE_NATIVE_TOOLBAR
1734 if ( m_macUsesNativeToolbar
)
1746 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1748 if ( !drawMetalTheme
)
1750 HIThemePlacardDrawInfo info
;
1751 memset( &info
, 0, sizeof(info
) );
1753 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1755 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1756 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1757 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1761 // leave the background as it is (striped or metal)
1767 #endif // wxUSE_TOOLBAR