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_LEFT
|wxTB_RIGHT
) )
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() + 6;
505 int h
= m_bmpNormal
.GetHeight() + 6;
506 wxBitmap
bmp( w
, h
);
509 dc
.SelectObject( bmp
);
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 );
518 dc
.SelectObject( wxNullBitmap
);
519 ControlButtonContentInfo info
;
520 wxMacCreateBitmapButton( &info
, bmp
);
521 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
522 #if wxOSX_USE_NATIVE_TOOLBAR
523 if (m_toolbarItemRef
!= NULL
)
525 ControlButtonContentInfo info2
;
526 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
527 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
528 wxMacReleaseBitmapButton( &info2
);
531 wxMacReleaseBitmapButton( &info
);
535 ControlButtonContentInfo info
;
536 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
537 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
538 #if wxOSX_USE_NATIVE_TOOLBAR
539 if (m_toolbarItemRef
!= NULL
)
541 ControlButtonContentInfo info2
;
542 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
543 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
544 wxMacReleaseBitmapButton( &info2
);
547 wxMacReleaseBitmapButton( &info
);
550 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
552 m_controlHandle
, 0, kControlIconTransformTag
,
553 sizeof(transform
), (Ptr
)&transform
);
554 HIViewSetNeedsDisplay( m_controlHandle
, true );
558 wxToolBarTool::wxToolBarTool(
561 const wxString
& label
,
562 const wxBitmap
& bmpNormal
,
563 const wxBitmap
& bmpDisabled
,
565 wxObject
*clientData
,
566 const wxString
& shortHelp
,
567 const wxString
& longHelp
)
570 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
571 clientData
, shortHelp
, longHelp
)
577 #pragma mark Toolbar Implementation
579 wxToolBarToolBase
*wxToolBar::CreateTool(
581 const wxString
& label
,
582 const wxBitmap
& bmpNormal
,
583 const wxBitmap
& bmpDisabled
,
585 wxObject
*clientData
,
586 const wxString
& shortHelp
,
587 const wxString
& longHelp
)
589 return new wxToolBarTool(
590 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
591 clientData
, shortHelp
, longHelp
);
595 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
597 return new wxToolBarTool(this, control
, label
);
600 void wxToolBar::Init()
604 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
605 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
607 #if wxOSX_USE_NATIVE_TOOLBAR
609 m_macUsesNativeToolbar
= false;
613 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
615 const EventTypeSpec kEvents
[] =
617 { kEventClassHIObject
, kEventHIObjectConstruct
},
618 { kEventClassHIObject
, kEventHIObjectInitialize
},
619 { kEventClassHIObject
, kEventHIObjectDestruct
},
621 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
624 const EventTypeSpec kViewEvents
[] =
626 { kEventClassControl
, kEventControlGetSizeConstraints
}
629 struct ControlToolbarItem
631 HIToolbarItemRef toolbarItem
;
633 wxSize lastValidSize
;
636 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
638 OSStatus result
= eventNotHandledErr
;
639 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
641 switch ( GetEventClass( inEvent
) )
643 case kEventClassHIObject
:
644 switch ( GetEventKind( inEvent
) )
646 case kEventHIObjectConstruct
:
648 HIObjectRef toolbarItem
;
649 ControlToolbarItem
* item
;
651 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
652 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
654 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
655 item
->toolbarItem
= toolbarItem
;
656 item
->lastValidSize
= wxSize(-1,-1);
657 item
->viewRef
= NULL
;
659 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
665 case kEventHIObjectInitialize
:
666 result
= CallNextEventHandler( inCallRef
, inEvent
);
667 if ( result
== noErr
)
670 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
671 sizeof( CFTypeRef
), NULL
, &data
);
675 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
676 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
678 object
->viewRef
= (HIViewRef
) viewRef
;
679 // make sure we keep that control during our lifetime
680 CFRetain( object
->viewRef
) ;
682 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
683 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
688 case kEventHIObjectDestruct
:
690 HIViewRef viewRef
= object
->viewRef
;
691 if( viewRef
&& IsValidControlHandle( viewRef
) )
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
) ;
698 CFRelease( viewRef
) ;
708 case kEventClassToolbarItem
:
709 switch ( GetEventKind( inEvent
) )
711 case kEventToolbarItemCreateCustomView
:
713 HIViewRef viewRef
= object
->viewRef
;
714 HIViewRemoveFromSuperview( viewRef
) ;
715 HIViewSetVisible(viewRef
, true) ;
716 CFRetain( viewRef
) ;
717 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
723 case kEventClassControl
:
724 switch ( GetEventKind( inEvent
) )
726 case kEventControlGetSizeConstraints
:
728 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
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
735 wxSize sz
= object
->lastValidSize
;
736 if ( sz
.x
<= 0 || sz
.y
<= 0 )
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
;
747 // Extra width to avoid edge of combobox being cut off
751 min
.width
= max
.width
= sz
.x
;
752 min
.height
= max
.height
= sz
.y
;
754 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
755 sizeof( HISize
), &min
);
757 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
758 sizeof( HISize
), &max
);
770 void RegisterControlToolbarItemClass()
772 static bool sRegistered
;
776 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
777 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
783 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
785 RegisterControlToolbarItemClass();
789 UInt32 options
= kHIToolbarItemAllowDuplicates
;
790 HIToolbarItemRef result
= NULL
;
792 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
793 require_noerr( err
, CantCreateEvent
);
795 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
796 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
799 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
801 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
804 ReleaseEvent( event
);
809 #if wxOSX_USE_NATIVE_TOOLBAR
810 static const EventTypeSpec kToolbarEvents
[] =
812 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
813 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
814 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
817 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
819 void* WXUNUSED(inUserData
))
821 OSStatus result
= eventNotHandledErr
;
823 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
824 CFMutableArrayRef array
;
826 switch ( GetEventKind( inEvent
) )
828 case kEventToolbarGetDefaultIdentifiers
:
830 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
831 sizeof( CFMutableArrayRef
), NULL
, &array
);
832 // not implemented yet
833 // GetToolbarDefaultItems( array );
838 case kEventToolbarGetAllowedIdentifiers
:
840 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
841 sizeof( CFMutableArrayRef
), NULL
, &array
);
842 // not implemented yet
843 // GetToolbarAllowedItems( array );
847 case kEventToolbarCreateItemWithIdentifier
:
849 HIToolbarItemRef item
= NULL
;
850 CFTypeRef data
= NULL
;
851 CFStringRef identifier
= NULL
;
853 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
854 sizeof( CFStringRef
), NULL
, &identifier
);
856 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
857 sizeof( CFTypeRef
), NULL
, &data
);
859 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
861 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
864 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
865 sizeof( HIToolbarItemRef
), &item
);
875 #endif // wxOSX_USE_NATIVE_TOOLBAR
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
880 bool wxToolBar::Create(
886 const wxString
& name
)
888 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
893 OSStatus err
= noErr
;
895 #if wxOSX_USE_NATIVE_TOOLBAR
896 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
898 wxString labelStr
= wxString::Format( wxT("%p"), this );
899 err
= HIToolbarCreate(
900 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
901 (HIToolbarRef
*) &m_macToolbar
);
903 if (m_macToolbar
!= NULL
)
905 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macToolbar
), ToolbarDelegateHandler
,
906 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
908 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
909 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
911 if ( style
& wxTB_NOICONS
)
912 mode
= kHIToolbarDisplayModeLabelOnly
;
913 else if ( style
& wxTB_TEXT
)
914 mode
= kHIToolbarDisplayModeIconAndLabel
;
916 mode
= kHIToolbarDisplayModeIconOnly
;
918 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
919 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, displaySize
);
922 #endif // wxOSX_USE_NATIVE_TOOLBAR
924 return (err
== noErr
);
927 wxToolBar::~wxToolBar()
929 #if wxOSX_USE_NATIVE_TOOLBAR
930 // We could be not using a native tool bar at all, this happens when we're
931 // created with something other than the frame as parent for example.
935 CFIndex count
= CFGetRetainCount( m_macToolbar
) ;
936 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
937 if ( UMAGetSystemVersion() < 0x1050 )
941 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
944 CFRelease( (HIToolbarRef
)m_macToolbar
);
946 #endif // wxOSX_USE_NATIVE_TOOLBAR
949 bool wxToolBar::Show( bool show
)
951 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
952 bool bResult
= (tlw
!= NULL
);
956 #if wxOSX_USE_NATIVE_TOOLBAR
957 bool ownToolbarInstalled
= false;
958 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
959 if (ownToolbarInstalled
)
961 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
963 ShowHideWindowToolbar( tlw
, show
, false );
966 bResult
= wxToolBarBase::Show( show
);
969 bResult
= wxToolBarBase::Show( show
);
976 bool wxToolBar::IsShown() const
980 #if wxOSX_USE_NATIVE_TOOLBAR
981 bool ownToolbarInstalled
;
983 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
984 if (ownToolbarInstalled
)
986 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
987 bResult
= IsWindowToolbarVisible( tlw
);
990 bResult
= wxToolBarBase::IsShown();
993 bResult
= wxToolBarBase::IsShown();
999 void wxToolBar::DoGetSize( int *width
, int *height
) const
1001 #if wxOSX_USE_NATIVE_TOOLBAR
1003 bool ownToolbarInstalled
;
1005 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1006 if ( ownToolbarInstalled
)
1008 // TODO: is this really a control ?
1009 GetControlBounds( (ControlRef
) m_macToolbar
, &boundsR
);
1010 if ( width
!= NULL
)
1011 *width
= boundsR
.right
- boundsR
.left
;
1012 if ( height
!= NULL
)
1013 *height
= boundsR
.bottom
- boundsR
.top
;
1016 wxToolBarBase::DoGetSize( width
, height
);
1019 wxToolBarBase::DoGetSize( width
, height
);
1023 wxSize
wxToolBar::DoGetBestSize() const
1027 DoGetSize( &width
, &height
);
1029 return wxSize( width
, height
);
1032 void wxToolBar::SetWindowStyleFlag( long style
)
1034 wxToolBarBase::SetWindowStyleFlag( style
);
1036 #if wxOSX_USE_NATIVE_TOOLBAR
1037 if (m_macToolbar
!= NULL
)
1039 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
1041 if ( style
& wxTB_NOICONS
)
1042 mode
= kHIToolbarDisplayModeLabelOnly
;
1043 else if ( style
& wxTB_TEXT
)
1044 mode
= kHIToolbarDisplayModeIconAndLabel
;
1046 mode
= kHIToolbarDisplayModeIconOnly
;
1048 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
1053 #if wxOSX_USE_NATIVE_TOOLBAR
1054 bool wxToolBar::MacWantsNativeToolbar()
1056 return m_macUsesNativeToolbar
;
1059 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1061 bool bResultV
= false;
1063 if (ownToolbarInstalled
!= NULL
)
1064 *ownToolbarInstalled
= false;
1066 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1069 HIToolbarRef curToolbarRef
= NULL
;
1070 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1071 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1072 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1073 *ownToolbarInstalled
= (curToolbarRef
== m_macToolbar
);
1079 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1081 bool bResult
= false;
1083 if (usesNative
&& (m_macToolbar
== NULL
))
1086 if (usesNative
&& ((GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
|wxTB_BOTTOM
)) != 0))
1089 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1093 // check the existing toolbar
1094 HIToolbarRef curToolbarRef
= NULL
;
1095 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1097 curToolbarRef
= NULL
;
1099 m_macUsesNativeToolbar
= usesNative
;
1101 if (m_macUsesNativeToolbar
)
1103 // only install toolbar if there isn't one installed already
1104 if (curToolbarRef
== NULL
)
1108 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macToolbar
);
1110 // ShowHideWindowToolbar will make the wxFrame grow
1111 // which we don't want in this case
1112 wxSize sz
= GetParent()->GetSize();
1113 ShowHideWindowToolbar( tlw
, true, false );
1114 // Restore the orginal size
1115 GetParent()->SetSize( sz
);
1117 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1119 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1121 m_peer
->Move(0,0,0,0 );
1122 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1123 m_peer
->SetVisibility( false );
1124 wxToolBarBase::Show( false );
1129 // only deinstall toolbar if this is the installed one
1130 if (m_macToolbar
== curToolbarRef
)
1134 ShowHideWindowToolbar( tlw
, false, false );
1135 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1136 MacUninstallNativeToolbar();
1138 m_peer
->SetVisibility( true );
1143 InvalidateBestSize();
1145 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1149 void wxToolBar::MacUninstallNativeToolbar()
1154 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1156 SetWindowToolbar( tlw
, NULL
);
1160 bool wxToolBar::Realize()
1162 if ( !wxToolBarBase::Realize() )
1165 wxSize tlw_sz
= GetParent()->GetSize();
1170 int maxToolWidth
= 0;
1171 int maxToolHeight
= 0;
1173 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1174 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1177 GetSize( &tw
, &th
);
1179 // find the maximum tool width and height
1180 wxToolBarTool
*tool
;
1181 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1184 tool
= (wxToolBarTool
*) node
->GetData();
1187 wxSize sz
= tool
->GetSize();
1189 if ( sz
.x
> maxToolWidth
)
1190 maxToolWidth
= sz
.x
;
1191 if ( sz
.y
> maxToolHeight
)
1192 maxToolHeight
= sz
.y
;
1195 node
= node
->GetNext();
1198 bool lastIsRadio
= false;
1199 bool curIsRadio
= false;
1201 #if wxOSX_USE_NATIVE_TOOLBAR
1202 CFIndex currentPosition
= 0;
1203 bool insertAll
= false;
1205 HIToolbarRef refTB
= (HIToolbarRef
)m_macToolbar
;
1210 enc
= f
.GetEncoding();
1212 enc
= wxFont::GetDefaultEncoding();
1215 node
= m_tools
.GetFirst();
1218 tool
= (wxToolBarTool
*) node
->GetData();
1221 node
= node
->GetNext();
1225 // set tool position:
1226 // for the moment just perform a single row/column alignment
1227 wxSize cursize
= tool
->GetSize();
1228 if ( x
+ cursize
.x
> maxWidth
)
1229 maxWidth
= x
+ cursize
.x
;
1230 if ( y
+ cursize
.y
> maxHeight
)
1231 maxHeight
= y
+ cursize
.y
;
1233 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1235 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1236 tool
->SetPosition( wxPoint(x1
, y
) );
1240 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1241 tool
->SetPosition( wxPoint(x
, y1
) );
1244 // update the item positioning state
1245 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1246 y
+= cursize
.y
+ kwxMacToolSpacing
;
1248 x
+= cursize
.x
+ kwxMacToolSpacing
;
1250 #if wxOSX_USE_NATIVE_TOOLBAR
1251 // install in native HIToolbar
1254 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1255 if ( hiItemRef
!= NULL
)
1257 // since setting the help texts is non-virtual we have to update
1259 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1261 OSStatus err
= noErr
;
1266 // if this is the first tool that gets newly inserted or repositioned
1267 // first remove all 'old' tools from here to the right, because of this
1268 // all following tools will have to be reinserted (insertAll).
1269 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1271 node2
= node2
->GetPrevious() )
1273 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1275 const long idx
= tool2
->GetIndex();
1278 if ( tool2
->IsControl() )
1280 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1281 if ( count
!= 3 && count
!= 2 )
1283 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1286 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1288 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1291 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1294 if ( tool2
->IsControl() )
1296 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1299 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1302 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1305 tool2
->SetIndex(-1);
1310 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1313 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1316 tool
->SetIndex( currentPosition
);
1317 if ( tool
->IsControl() )
1319 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1320 if ( count
!= 3 && count
!= 2 )
1322 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1324 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1326 wxString label
= tool
->GetLabel();
1327 if ( !label
.empty() )
1328 HIToolbarItemSetLabel( hiItemRef
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1337 // update radio button (and group) state
1338 lastIsRadio
= curIsRadio
;
1339 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1343 if ( tool
->IsToggled() )
1344 DoToggleTool( tool
, true );
1350 if ( tool
->Toggle( true ) )
1352 DoToggleTool( tool
, true );
1355 else if ( tool
->IsToggled() )
1357 if ( tool
->IsToggled() )
1358 DoToggleTool( tool
, true );
1360 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1363 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1364 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1367 if ( toggleTool
->Toggle( false ) )
1368 DoToggleTool( toggleTool
, false );
1370 nodePrev
= nodePrev
->GetPrevious();
1375 node
= node
->GetNext();
1378 if (m_macUsesNativeToolbar
)
1379 GetParent()->SetSize( tlw_sz
);
1381 if ( GetWindowStyleFlag() & (wxTB_TOP
|wxTB_BOTTOM
) )
1383 // if not set yet, only one row
1384 if ( m_maxRows
<= 0 )
1387 m_minWidth
= maxWidth
;
1388 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1389 m_minHeight
= m_maxHeight
= maxHeight
;
1393 // if not set yet, have one column
1394 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1395 SetRows( GetToolsCount() );
1397 m_minHeight
= maxHeight
;
1398 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1399 m_minWidth
= m_maxWidth
= maxWidth
;
1403 // FIXME: should this be OSX-only?
1405 bool wantNativeToolbar
, ownToolbarInstalled
;
1407 // attempt to install the native toolbar
1408 wantNativeToolbar
= ((GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_BOTTOM
|wxTB_RIGHT
)) == 0);
1409 MacInstallNativeToolbar( wantNativeToolbar
);
1410 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1411 if (!ownToolbarInstalled
)
1413 SetSize( maxWidth
, maxHeight
);
1414 InvalidateBestSize();
1418 SetSize( maxWidth
, maxHeight
);
1419 InvalidateBestSize();
1427 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1429 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1430 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1432 #if wxOSX_USE_NATIVE_TOOLBAR
1433 if (m_macToolbar
!= NULL
)
1435 int maxs
= wxMax( size
.x
, size
.y
);
1436 HIToolbarDisplaySize sizeSpec
;
1438 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1439 else if ( maxs
> 24 )
1440 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1442 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1444 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, sizeSpec
);
1449 // The button size is bigger than the bitmap size
1450 wxSize
wxToolBar::GetToolSize() const
1452 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1455 void wxToolBar::SetRows(int nRows
)
1457 // avoid resizing the frame uselessly
1458 if ( nRows
!= m_maxRows
)
1462 void wxToolBar::MacSuperChangedPosition()
1464 wxWindow::MacSuperChangedPosition();
1466 #if wxOSX_USE_NATIVE_TOOLBAR
1467 if (! m_macUsesNativeToolbar
)
1475 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1477 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1480 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1482 tool
->SetNormalBitmap(bitmap
);
1484 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1485 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1489 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1491 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1494 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1496 tool
->SetDisabledBitmap(bitmap
);
1498 // TODO: what to do for this one?
1502 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1504 wxToolBarTool
*tool
;
1505 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1508 tool
= (wxToolBarTool
*)node
->GetData();
1511 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1512 if ( r
.Contains( wxPoint( x
, y
) ) )
1516 node
= node
->GetNext();
1522 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1524 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1526 return tool
->GetShortHelp();
1528 return wxEmptyString
;
1531 void wxToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(t
), bool WXUNUSED(enable
))
1533 // everything already done in the tool's implementation
1536 void wxToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(t
), bool WXUNUSED(toggle
))
1538 // everything already done in the tool's implementation
1541 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1543 wxToolBarTool
*tool
= static_cast< wxToolBarTool
*>(toolBase
);
1547 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1548 wxSize toolSize
= GetToolSize();
1549 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1550 ControlRef controlHandle
= NULL
;
1553 #if wxOSX_USE_NATIVE_TOOLBAR
1554 wxString label
= tool
->GetLabel();
1555 if (m_macToolbar
&& !label
.empty() )
1557 // strip mnemonics from the label for compatibility
1558 // with the usual labels in wxStaticText sense
1559 label
= wxStripMenuCodes(label
);
1561 #endif // wxOSX_USE_NATIVE_TOOLBAR
1563 switch (tool
->GetStyle())
1565 case wxTOOL_STYLE_SEPARATOR
:
1567 wxASSERT( tool
->GetControlHandle() == NULL
);
1570 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1571 toolrect
.bottom
= toolSize
.y
;
1573 toolrect
.right
= toolSize
.x
;
1575 // in flat style we need a visual separator
1576 #if wxOSX_USE_NATIVE_TOOLBAR
1577 if (m_macToolbar
!= NULL
)
1579 HIToolbarItemRef item
;
1580 err
= HIToolbarItemCreate(
1581 kHIToolbarSeparatorIdentifier
,
1582 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1585 tool
->SetToolbarItemRef( item
);
1589 #endif // wxOSX_USE_NATIVE_TOOLBAR
1591 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1592 tool
->SetControlHandle( controlHandle
);
1596 case wxTOOL_STYLE_BUTTON
:
1598 wxASSERT( tool
->GetControlHandle() == NULL
);
1599 ControlButtonContentInfo info
;
1600 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1602 if ( UMAGetSystemVersion() >= 0x1000)
1604 // contrary to the docs this control only works with iconrefs
1605 ControlButtonContentInfo info
;
1606 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1607 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1608 wxMacReleaseBitmapButton( &info
);
1612 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1613 if ( tool
->CanBeToggled() )
1614 behaviour
|= kControlBehaviorToggles
;
1615 err
= CreateBevelButtonControl( window
,
1616 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1617 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1620 #if wxOSX_USE_NATIVE_TOOLBAR
1621 if (m_macToolbar
!= NULL
)
1623 HIToolbarItemRef item
;
1624 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1625 err
= HIToolbarItemCreate(
1626 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1627 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1630 ControlButtonContentInfo info2
;
1631 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1633 InstallEventHandler(
1634 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1635 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1636 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1637 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1638 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1639 tool
->SetToolbarItemRef( item
);
1641 wxMacReleaseBitmapButton( &info2
);
1646 #endif // wxOSX_USE_NATIVE_TOOLBAR
1648 wxMacReleaseBitmapButton( &info
);
1651 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1652 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1655 InstallControlEventHandler(
1656 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1657 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1659 tool
->SetControlHandle( controlHandle
);
1663 case wxTOOL_STYLE_CONTROL
:
1665 #if wxOSX_USE_NATIVE_TOOLBAR
1666 if (m_macToolbar
!= NULL
)
1668 wxCHECK_MSG( tool
->GetControl(), false, wxT("control must be non-NULL") );
1669 HIToolbarItemRef item
;
1670 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1671 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1672 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macToolbar
,kControlToolbarItemClassID
,
1677 tool
->SetToolbarItemRef( item
);
1687 // right now there's nothing to do here
1697 if ( controlHandle
)
1699 ControlRef container
= (ControlRef
) GetHandle();
1700 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1702 SetControlVisibility( controlHandle
, true, true );
1703 ::EmbedControl( controlHandle
, container
);
1706 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1707 tool
->UpdateToggleImage( true );
1709 // nothing special to do here - we relayout in Realize() later
1710 InvalidateBestSize();
1714 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1717 return (err
== noErr
);
1720 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1725 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1727 wxToolBarTool
* tool
= static_cast< wxToolBarTool
*>(toolbase
);
1728 wxToolBarToolsList::compatibility_iterator node
;
1729 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1731 wxToolBarToolBase
*tool2
= node
->GetData();
1732 if ( tool2
== tool
)
1734 // let node point to the next node in the list
1735 node
= node
->GetNext();
1741 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1743 #if wxOSX_USE_NATIVE_TOOLBAR
1744 CFIndex removeIndex
= tool
->GetIndex();
1747 #if wxOSX_USE_NATIVE_TOOLBAR
1748 if (m_macToolbar
!= NULL
)
1750 if ( removeIndex
!= -1 && m_macToolbar
)
1752 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macToolbar
, removeIndex
);
1753 tool
->SetIndex( -1 );
1758 tool
->ClearControl();
1760 // and finally reposition all the controls after this one
1762 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1764 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1765 wxPoint pt
= tool2
->GetPosition();
1767 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1772 tool2
->SetPosition( pt
);
1774 #if wxOSX_USE_NATIVE_TOOLBAR
1775 if (m_macToolbar
!= NULL
)
1777 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1778 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1783 InvalidateBestSize();
1788 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1790 #if wxOSX_USE_NATIVE_TOOLBAR
1791 if ( m_macUsesNativeToolbar
)
1803 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1805 if ( !drawMetalTheme
)
1807 HIThemePlacardDrawInfo info
;
1808 memset( &info
, 0, sizeof(info
) );
1810 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1812 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1813 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1814 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1818 // leave the background as it is (striped or metal)
1824 #endif // wxUSE_TOOLBAR