1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/toolbar.h"
22 #include "wx/osx/private.h"
23 #include "wx/geometry.h"
24 #include "wx/sysopt.h"
27 const short kwxMacToolBarToolDefaultWidth
= 16;
28 const short kwxMacToolBarToolDefaultHeight
= 16;
29 const short kwxMacToolBarTopMargin
= 4;
30 const short kwxMacToolBarLeftMargin
= 4;
31 const short kwxMacToolBorder
= 0;
32 const short kwxMacToolSpacing
= 6;
34 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
35 EVT_PAINT( wxToolBar::OnPaint
)
40 #pragma mark Tool Implementation
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
48 // when embedding native controls in the native toolbar we must make sure the
49 // control does not get deleted behind our backs, so the retain count gets increased
50 // (after creation it is 1), first be the creation of the custom HIToolbarItem wrapper
51 // object, and second by the code 'creating' the custom HIView (which is the same as the
52 // already existing native control, therefore we just increase the ref count)
53 // when this view is removed from the native toolbar its count gets decremented again
54 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
55 // so in the end the control lives with a refcount of one and can be disposed of by the
56 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
57 // so we can only test against a range, not a specific value of the refcount.
59 class wxToolBarTool
: public wxToolBarToolBase
65 const wxString
& label
,
66 const wxBitmap
& bmpNormal
,
67 const wxBitmap
& bmpDisabled
,
70 const wxString
& shortHelp
,
71 const wxString
& longHelp
);
73 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
74 : wxToolBarToolBase(tbar
, control
, label
)
78 SetControlHandle( (ControlRef
) control
->GetHandle() );
81 virtual ~wxToolBarTool()
86 WXWidget
GetControlHandle()
88 return (WXWidget
) m_controlHandle
;
91 void SetControlHandle( ControlRef handle
)
93 m_controlHandle
= handle
;
96 void SetPosition( const wxPoint
& position
);
100 if ( m_controlHandle
)
103 DisposeControl( m_controlHandle
);
106 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
107 // proper wxControl destructor
109 m_controlHandle
= NULL
;
112 #if wxOSX_USE_NATIVE_TOOLBAR
113 if ( m_toolbarItemRef
)
115 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
116 // different behaviour under Leopard
117 if ( UMAGetSystemVersion() < 0x1050 )
121 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
124 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
125 CFRelease(m_toolbarItemRef
);
126 m_toolbarItemRef
= NULL
;
128 #endif // wxOSX_USE_NATIVE_TOOLBAR
131 wxSize
GetSize() const
137 curSize
= GetControl()->GetSize();
139 else if ( IsButton() )
141 curSize
= GetToolBar()->GetToolSize();
146 curSize
= GetToolBar()->GetToolSize();
147 if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
156 wxPoint
GetPosition() const
158 return wxPoint( m_x
, m_y
);
161 virtual bool Enable( bool enable
);
163 void UpdateToggleImage( bool toggle
);
165 virtual bool Toggle(bool toggle
)
167 if ( wxToolBarToolBase::Toggle( toggle
) == false )
170 UpdateToggleImage(toggle
);
174 void UpdateHelpStrings()
176 #if wxOSX_USE_NATIVE_TOOLBAR
177 if ( m_toolbarItemRef
)
179 wxFontEncoding enc
= GetToolBarFontEncoding();
181 HIToolbarItemSetHelpText(
183 wxCFStringRef( GetShortHelp(), enc
),
184 wxCFStringRef( GetLongHelp(), enc
) );
189 virtual bool SetShortHelp(const wxString
& help
)
191 if ( wxToolBarToolBase::SetShortHelp( help
) == false )
198 virtual bool SetLongHelp(const wxString
& help
)
200 if ( wxToolBarToolBase::SetLongHelp( help
) == false )
207 virtual void SetNormalBitmap(const wxBitmap
& bmp
)
209 wxToolBarToolBase::SetNormalBitmap(bmp
);
210 UpdateToggleImage(CanBeToggled() && IsToggled());
213 virtual void SetLabel(const wxString
& label
)
215 wxToolBarToolBase::SetLabel(label
);
216 #if wxOSX_USE_NATIVE_TOOLBAR
217 if ( m_toolbarItemRef
)
219 // strip mnemonics from the label for compatibility with the usual
220 // labels in wxStaticText sense
221 wxString labelStr
= wxStripMenuCodes(label
);
223 HIToolbarItemSetLabel(
225 wxCFStringRef(labelStr
, GetToolBarFontEncoding()) );
230 #if wxOSX_USE_NATIVE_TOOLBAR
231 void SetToolbarItemRef( HIToolbarItemRef ref
)
233 if ( m_controlHandle
)
234 HideControl( m_controlHandle
);
235 if ( m_toolbarItemRef
)
236 CFRelease( m_toolbarItemRef
);
238 m_toolbarItemRef
= ref
;
242 HIToolbarItemRef
GetToolbarItemRef() const
244 return m_toolbarItemRef
;
247 void SetIndex( CFIndex idx
)
252 CFIndex
GetIndex() const
256 #endif // wxOSX_USE_NATIVE_TOOLBAR
259 #if wxOSX_USE_NATIVE_TOOLBAR
260 wxFontEncoding
GetToolBarFontEncoding() const
264 f
= GetToolBar()->GetFont();
265 return f
.IsOk() ? f
.GetEncoding() : wxFont::GetDefaultEncoding();
267 #endif // wxOSX_USE_NATIVE_TOOLBAR
271 m_controlHandle
= NULL
;
273 #if wxOSX_USE_NATIVE_TOOLBAR
274 m_toolbarItemRef
= NULL
;
279 ControlRef m_controlHandle
;
283 #if wxOSX_USE_NATIVE_TOOLBAR
284 HIToolbarItemRef m_toolbarItemRef
;
285 // position in its toolbar, -1 means not inserted
290 static const EventTypeSpec eventList
[] =
292 { kEventClassControl
, kEventControlHit
},
293 { kEventClassControl
, kEventControlHitTest
},
296 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
298 OSStatus result
= eventNotHandledErr
;
299 ControlRef controlRef
;
300 wxMacCarbonEvent
cEvent( event
);
302 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
304 switch ( GetEventKind( event
) )
306 case kEventControlHit
:
308 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
309 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
310 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
314 shouldToggle
= !tbartool
->IsToggled();
316 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
319 if (tbartool
!= NULL
)
320 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
325 case kEventControlHitTest
:
327 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
329 HIViewGetBounds( controlRef
, &rect
);
331 ControlPartCode pc
= kControlNoPart
;
332 if ( CGRectContainsPoint( rect
, pt
) )
333 pc
= kControlIconPart
;
334 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
346 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
348 OSStatus result
= eventNotHandledErr
;
350 switch ( GetEventClass( event
) )
352 case kEventClassControl
:
353 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
363 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
365 #if wxOSX_USE_NATIVE_TOOLBAR
367 static const EventTypeSpec toolBarEventList
[] =
369 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
372 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
374 OSStatus result
= eventNotHandledErr
;
376 switch ( GetEventKind( event
) )
378 case kEventToolbarItemPerformAction
:
380 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
381 if ( tbartool
!= NULL
)
383 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
384 int toolID
= tbartool
->GetId();
386 if ( tbartool
->CanBeToggled() )
389 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
393 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
406 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
408 OSStatus result
= eventNotHandledErr
;
410 switch ( GetEventClass( event
) )
412 case kEventClassToolbarItem
:
413 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
423 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
427 bool wxToolBarTool::Enable( bool enable
)
429 if ( wxToolBarToolBase::Enable( enable
) == false )
434 GetControl()->Enable( enable
);
436 else if ( IsButton() )
438 #if wxOSX_USE_NATIVE_TOOLBAR
439 if ( m_toolbarItemRef
!= NULL
)
440 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
443 if ( m_controlHandle
!= NULL
)
446 EnableControl( m_controlHandle
);
448 DisableControl( m_controlHandle
);
455 void wxToolBarTool::SetPosition( const wxPoint
& position
)
460 int mac_x
= position
.x
;
461 int mac_y
= position
.y
;
466 GetControlBounds( m_controlHandle
, &contrlRect
);
467 int former_mac_x
= contrlRect
.left
;
468 int former_mac_y
= contrlRect
.top
;
469 GetToolBar()->GetToolSize();
471 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
473 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
476 else if ( IsControl() )
478 // embedded native controls are moved by the OS
479 #if wxOSX_USE_NATIVE_TOOLBAR
480 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
483 GetControl()->Move( position
);
490 GetControlBounds( m_controlHandle
, &contrlRect
);
491 int former_mac_x
= contrlRect
.left
;
492 int former_mac_y
= contrlRect
.top
;
494 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
495 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
499 void wxToolBarTool::UpdateToggleImage( bool toggle
)
503 int w
= m_bmpNormal
.GetWidth() + 6;
504 int h
= m_bmpNormal
.GetHeight() + 6;
505 wxBitmap
bmp( w
, h
);
508 dc
.SelectObject( bmp
);
509 wxColour mid_grey_75
= wxColour(128, 128, 128, 196);
510 wxColour light_grey_75
= wxColour(196, 196, 196, 196);
511 dc
.GradientFillLinear( wxRect(1, 1, w
- 1, h
-1),
512 light_grey_75
, mid_grey_75
, wxNORTH
);
513 wxColour black_50
= wxColour(0, 0, 0, 127);
514 dc
.SetPen( wxPen(black_50
) );
515 dc
.DrawRoundedRectangle( 0, 0, w
, h
, 1.5 );
516 dc
.DrawBitmap( m_bmpNormal
, 3, 3, true );
517 dc
.SelectObject( wxNullBitmap
);
518 ControlButtonContentInfo info
;
519 wxMacCreateBitmapButton( &info
, bmp
);
520 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
521 #if wxOSX_USE_NATIVE_TOOLBAR
522 if (m_toolbarItemRef
!= NULL
)
524 ControlButtonContentInfo info2
;
525 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
526 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
527 wxMacReleaseBitmapButton( &info2
);
530 wxMacReleaseBitmapButton( &info
);
534 ControlButtonContentInfo info
;
535 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
536 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
537 #if wxOSX_USE_NATIVE_TOOLBAR
538 if (m_toolbarItemRef
!= NULL
)
540 ControlButtonContentInfo info2
;
541 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
542 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
543 wxMacReleaseBitmapButton( &info2
);
546 wxMacReleaseBitmapButton( &info
);
549 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
551 m_controlHandle
, 0, kControlIconTransformTag
,
552 sizeof(transform
), (Ptr
)&transform
);
553 HIViewSetNeedsDisplay( m_controlHandle
, true );
557 wxToolBarTool::wxToolBarTool(
560 const wxString
& label
,
561 const wxBitmap
& bmpNormal
,
562 const wxBitmap
& bmpDisabled
,
564 wxObject
*clientData
,
565 const wxString
& shortHelp
,
566 const wxString
& longHelp
)
569 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
570 clientData
, shortHelp
, longHelp
)
576 #pragma mark Toolbar Implementation
578 wxToolBarToolBase
*wxToolBar::CreateTool(
580 const wxString
& label
,
581 const wxBitmap
& bmpNormal
,
582 const wxBitmap
& bmpDisabled
,
584 wxObject
*clientData
,
585 const wxString
& shortHelp
,
586 const wxString
& longHelp
)
588 return new wxToolBarTool(
589 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
590 clientData
, shortHelp
, longHelp
);
594 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
596 return new wxToolBarTool(this, control
, label
);
599 void wxToolBar::Init()
603 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
604 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
606 #if wxOSX_USE_NATIVE_TOOLBAR
608 m_macUsesNativeToolbar
= false;
612 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
614 const EventTypeSpec kEvents
[] =
616 { kEventClassHIObject
, kEventHIObjectConstruct
},
617 { kEventClassHIObject
, kEventHIObjectInitialize
},
618 { kEventClassHIObject
, kEventHIObjectDestruct
},
620 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
623 const EventTypeSpec kViewEvents
[] =
625 { kEventClassControl
, kEventControlGetSizeConstraints
}
628 struct ControlToolbarItem
630 HIToolbarItemRef toolbarItem
;
632 wxSize lastValidSize
;
635 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
637 OSStatus result
= eventNotHandledErr
;
638 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
640 switch ( GetEventClass( inEvent
) )
642 case kEventClassHIObject
:
643 switch ( GetEventKind( inEvent
) )
645 case kEventHIObjectConstruct
:
647 HIObjectRef toolbarItem
;
648 ControlToolbarItem
* item
;
650 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
651 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
653 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
654 item
->toolbarItem
= toolbarItem
;
655 item
->lastValidSize
= wxSize(-1,-1);
656 item
->viewRef
= NULL
;
658 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
664 case kEventHIObjectInitialize
:
665 result
= CallNextEventHandler( inCallRef
, inEvent
);
666 if ( result
== noErr
)
669 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
670 sizeof( CFTypeRef
), NULL
, &data
);
674 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
675 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
677 object
->viewRef
= (HIViewRef
) viewRef
;
678 // make sure we keep that control during our lifetime
679 CFRetain( object
->viewRef
) ;
681 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
682 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
687 case kEventHIObjectDestruct
:
689 HIViewRef viewRef
= object
->viewRef
;
690 if( viewRef
&& IsValidControlHandle( viewRef
) )
692 // depending whether the wxControl corresponding to this HIView has already been destroyed or
693 // not, ref counts differ, so we cannot assert a special value
694 CFIndex count
= CFGetRetainCount( viewRef
) ;
697 CFRelease( viewRef
) ;
707 case kEventClassToolbarItem
:
708 switch ( GetEventKind( inEvent
) )
710 case kEventToolbarItemCreateCustomView
:
712 HIViewRef viewRef
= object
->viewRef
;
713 HIViewRemoveFromSuperview( viewRef
) ;
714 HIViewSetVisible(viewRef
, true) ;
715 CFRetain( viewRef
) ;
716 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
722 case kEventClassControl
:
723 switch ( GetEventKind( inEvent
) )
725 case kEventControlGetSizeConstraints
:
727 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
730 // during toolbar layout the native window sometimes gets negative sizes,
731 // sometimes it just gets shrunk behind our back, so in order to avoid
732 // ever shrinking more, once a valid size is captured, we keep it
734 wxSize sz
= object
->lastValidSize
;
735 if ( sz
.x
<= 0 || sz
.y
<= 0 )
737 sz
= wxwindow
->GetSize() ;
738 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
739 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
740 if ( sz
.x
> 0 && sz
.y
> 0 )
741 object
->lastValidSize
= sz
;
746 // Extra width to avoid edge of combobox being cut off
750 min
.width
= max
.width
= sz
.x
;
751 min
.height
= max
.height
= sz
.y
;
753 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
754 sizeof( HISize
), &min
);
756 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
757 sizeof( HISize
), &max
);
769 void RegisterControlToolbarItemClass()
771 static bool sRegistered
;
775 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
776 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
782 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
784 RegisterControlToolbarItemClass();
788 UInt32 options
= kHIToolbarItemAllowDuplicates
;
789 HIToolbarItemRef result
= NULL
;
791 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
792 require_noerr( err
, CantCreateEvent
);
794 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
795 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
798 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
800 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
803 ReleaseEvent( event
);
808 #if wxOSX_USE_NATIVE_TOOLBAR
809 static const EventTypeSpec kToolbarEvents
[] =
811 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
812 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
813 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
816 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
818 void* WXUNUSED(inUserData
))
820 OSStatus result
= eventNotHandledErr
;
822 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
823 CFMutableArrayRef array
;
825 switch ( GetEventKind( inEvent
) )
827 case kEventToolbarGetDefaultIdentifiers
:
829 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
830 sizeof( CFMutableArrayRef
), NULL
, &array
);
831 // not implemented yet
832 // GetToolbarDefaultItems( array );
837 case kEventToolbarGetAllowedIdentifiers
:
839 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
840 sizeof( CFMutableArrayRef
), NULL
, &array
);
841 // not implemented yet
842 // GetToolbarAllowedItems( array );
846 case kEventToolbarCreateItemWithIdentifier
:
848 HIToolbarItemRef item
= NULL
;
849 CFTypeRef data
= NULL
;
850 CFStringRef identifier
= NULL
;
852 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
853 sizeof( CFStringRef
), NULL
, &identifier
);
855 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
856 sizeof( CFTypeRef
), NULL
, &data
);
858 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
860 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
863 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
864 sizeof( HIToolbarItemRef
), &item
);
874 #endif // wxOSX_USE_NATIVE_TOOLBAR
876 // also for the toolbar we have the dual implementation:
877 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
879 bool wxToolBar::Create(
885 const wxString
& name
)
887 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
892 OSStatus err
= noErr
;
894 #if wxOSX_USE_NATIVE_TOOLBAR
895 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
897 wxString labelStr
= wxString::Format( wxT("%p"), this );
898 err
= HIToolbarCreate(
899 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
900 (HIToolbarRef
*) &m_macToolbar
);
902 if (m_macToolbar
!= NULL
)
904 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macToolbar
), ToolbarDelegateHandler
,
905 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
907 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
908 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
910 if ( style
& wxTB_NOICONS
)
911 mode
= kHIToolbarDisplayModeLabelOnly
;
912 else if ( style
& wxTB_TEXT
)
913 mode
= kHIToolbarDisplayModeIconAndLabel
;
915 mode
= kHIToolbarDisplayModeIconOnly
;
917 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
918 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, displaySize
);
921 #endif // wxOSX_USE_NATIVE_TOOLBAR
923 return (err
== noErr
);
926 wxToolBar::~wxToolBar()
928 #if wxOSX_USE_NATIVE_TOOLBAR
929 // We could be not using a native tool bar at all, this happens when we're
930 // created with something other than the frame as parent for example.
934 // it might already have been uninstalled due to a previous call to Destroy, but in case
935 // wasn't, do so now, otherwise redraw events may occur for deleted objects
936 bool ownToolbarInstalled
= false;
937 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
938 if (ownToolbarInstalled
)
940 MacUninstallNativeToolbar();
943 CFIndex count
= CFGetRetainCount( m_macToolbar
) ;
944 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
945 if ( UMAGetSystemVersion() < 0x1050 )
949 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
952 CFRelease( (HIToolbarRef
)m_macToolbar
);
954 #endif // wxOSX_USE_NATIVE_TOOLBAR
957 bool wxToolBar::Show( bool show
)
959 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
960 bool bResult
= (tlw
!= NULL
);
964 #if wxOSX_USE_NATIVE_TOOLBAR
965 bool ownToolbarInstalled
= false;
966 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
967 if (ownToolbarInstalled
)
969 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
971 ShowHideWindowToolbar( tlw
, show
, false );
974 bResult
= wxToolBarBase::Show( show
);
977 bResult
= wxToolBarBase::Show( show
);
984 bool wxToolBar::IsShown() const
988 #if wxOSX_USE_NATIVE_TOOLBAR
989 bool ownToolbarInstalled
;
991 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
992 if (ownToolbarInstalled
)
994 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
995 bResult
= IsWindowToolbarVisible( tlw
);
998 bResult
= wxToolBarBase::IsShown();
1001 bResult
= wxToolBarBase::IsShown();
1007 void wxToolBar::DoGetSize( int *width
, int *height
) const
1009 #if wxOSX_USE_NATIVE_TOOLBAR
1011 bool ownToolbarInstalled
;
1013 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1014 if ( ownToolbarInstalled
)
1016 // TODO: is this really a control ?
1017 GetControlBounds( (ControlRef
) m_macToolbar
, &boundsR
);
1018 if ( width
!= NULL
)
1019 *width
= boundsR
.right
- boundsR
.left
;
1020 if ( height
!= NULL
)
1021 *height
= boundsR
.bottom
- boundsR
.top
;
1024 wxToolBarBase::DoGetSize( width
, height
);
1027 wxToolBarBase::DoGetSize( width
, height
);
1031 wxSize
wxToolBar::DoGetBestSize() const
1035 DoGetSize( &width
, &height
);
1037 return wxSize( width
, height
);
1040 void wxToolBar::SetWindowStyleFlag( long style
)
1042 wxToolBarBase::SetWindowStyleFlag( style
);
1044 #if wxOSX_USE_NATIVE_TOOLBAR
1045 if (m_macToolbar
!= NULL
)
1047 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
1049 if ( style
& wxTB_NOICONS
)
1050 mode
= kHIToolbarDisplayModeLabelOnly
;
1051 else if ( style
& wxTB_TEXT
)
1052 mode
= kHIToolbarDisplayModeIconAndLabel
;
1054 mode
= kHIToolbarDisplayModeIconOnly
;
1056 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
1061 #if wxOSX_USE_NATIVE_TOOLBAR
1062 bool wxToolBar::MacWantsNativeToolbar()
1064 return m_macUsesNativeToolbar
;
1067 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1069 bool bResultV
= false;
1071 if (ownToolbarInstalled
!= NULL
)
1072 *ownToolbarInstalled
= false;
1074 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1077 HIToolbarRef curToolbarRef
= NULL
;
1078 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1079 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1080 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1081 *ownToolbarInstalled
= (curToolbarRef
== m_macToolbar
);
1087 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1089 bool bResult
= false;
1091 if (usesNative
&& (m_macToolbar
== NULL
))
1094 if (usesNative
&& ((GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
|wxTB_BOTTOM
)) != 0))
1097 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1101 // check the existing toolbar
1102 HIToolbarRef curToolbarRef
= NULL
;
1103 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1105 curToolbarRef
= NULL
;
1107 m_macUsesNativeToolbar
= usesNative
;
1109 if (m_macUsesNativeToolbar
)
1111 // only install toolbar if there isn't one installed already
1112 if (curToolbarRef
== NULL
)
1116 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macToolbar
);
1118 // ShowHideWindowToolbar will make the wxFrame grow
1119 // which we don't want in this case
1120 wxSize sz
= GetParent()->GetSize();
1121 ShowHideWindowToolbar( tlw
, true, false );
1122 // Restore the original size
1123 GetParent()->SetSize( sz
);
1125 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1127 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1129 GetPeer()->Move(0,0,0,0 );
1130 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1131 GetPeer()->SetVisibility( false );
1132 wxToolBarBase::Show( false );
1137 // only deinstall toolbar if this is the installed one
1138 if (m_macToolbar
== curToolbarRef
)
1142 ShowHideWindowToolbar( tlw
, false, false );
1143 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1144 MacUninstallNativeToolbar();
1146 GetPeer()->SetVisibility( true );
1151 InvalidateBestSize();
1153 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1157 void wxToolBar::MacUninstallNativeToolbar()
1162 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1164 SetWindowToolbar( tlw
, NULL
);
1168 bool wxToolBar::Realize()
1170 if ( !wxToolBarBase::Realize() )
1173 wxSize tlw_sz
= GetParent()->GetSize();
1178 int maxToolWidth
= 0;
1179 int maxToolHeight
= 0;
1181 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1182 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1185 GetSize( &tw
, &th
);
1187 // find the maximum tool width and height
1188 wxToolBarTool
*tool
;
1189 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1192 tool
= (wxToolBarTool
*) node
->GetData();
1195 wxSize sz
= tool
->GetSize();
1197 if ( sz
.x
> maxToolWidth
)
1198 maxToolWidth
= sz
.x
;
1199 if ( sz
.y
> maxToolHeight
)
1200 maxToolHeight
= sz
.y
;
1203 node
= node
->GetNext();
1206 bool lastIsRadio
= false;
1207 bool curIsRadio
= false;
1209 #if wxOSX_USE_NATIVE_TOOLBAR
1210 CFIndex currentPosition
= 0;
1211 bool insertAll
= false;
1213 HIToolbarRef refTB
= (HIToolbarRef
)m_macToolbar
;
1218 enc
= f
.GetEncoding();
1220 enc
= wxFont::GetDefaultEncoding();
1223 node
= m_tools
.GetFirst();
1226 tool
= (wxToolBarTool
*) node
->GetData();
1229 node
= node
->GetNext();
1233 // set tool position:
1234 // for the moment just perform a single row/column alignment
1235 wxSize cursize
= tool
->GetSize();
1236 if ( x
+ cursize
.x
> maxWidth
)
1237 maxWidth
= x
+ cursize
.x
;
1238 if ( y
+ cursize
.y
> maxHeight
)
1239 maxHeight
= y
+ cursize
.y
;
1241 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1243 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1244 tool
->SetPosition( wxPoint(x1
, y
) );
1248 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1249 tool
->SetPosition( wxPoint(x
, y1
) );
1252 // update the item positioning state
1253 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1254 y
+= cursize
.y
+ kwxMacToolSpacing
;
1256 x
+= cursize
.x
+ kwxMacToolSpacing
;
1258 #if wxOSX_USE_NATIVE_TOOLBAR
1259 // install in native HIToolbar
1262 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1263 if ( hiItemRef
!= NULL
)
1265 // since setting the help texts is non-virtual we have to update
1267 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1269 OSStatus err
= noErr
;
1274 // if this is the first tool that gets newly inserted or repositioned
1275 // first remove all 'old' tools from here to the right, because of this
1276 // all following tools will have to be reinserted (insertAll).
1277 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1279 node2
= node2
->GetPrevious() )
1281 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1283 const long idx
= tool2
->GetIndex();
1286 if ( tool2
->IsControl() )
1288 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1289 if ( count
!= 3 && count
!= 2 )
1291 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1294 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1296 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1299 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1302 if ( tool2
->IsControl() )
1304 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1307 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1310 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1313 tool2
->SetIndex(-1);
1318 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1321 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1324 tool
->SetIndex( currentPosition
);
1325 if ( tool
->IsControl() )
1327 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1328 if ( count
!= 3 && count
!= 2 )
1330 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1332 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1334 wxString label
= tool
->GetLabel();
1335 if ( !label
.empty() )
1336 HIToolbarItemSetLabel( hiItemRef
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1345 // update radio button (and group) state
1346 lastIsRadio
= curIsRadio
;
1347 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1351 if ( tool
->IsToggled() )
1352 DoToggleTool( tool
, true );
1358 if ( tool
->Toggle( true ) )
1360 DoToggleTool( tool
, true );
1363 else if ( tool
->IsToggled() )
1365 if ( tool
->IsToggled() )
1366 DoToggleTool( tool
, true );
1368 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1371 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1372 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1375 if ( toggleTool
->Toggle( false ) )
1376 DoToggleTool( toggleTool
, false );
1378 nodePrev
= nodePrev
->GetPrevious();
1383 node
= node
->GetNext();
1386 if (m_macUsesNativeToolbar
)
1387 GetParent()->SetSize( tlw_sz
);
1389 if ( GetWindowStyleFlag() & (wxTB_TOP
|wxTB_BOTTOM
) )
1391 // if not set yet, only one row
1392 if ( m_maxRows
<= 0 )
1395 m_minWidth
= maxWidth
;
1396 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1397 m_minHeight
= m_maxHeight
= maxHeight
;
1401 // if not set yet, have one column
1402 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1403 SetRows( GetToolsCount() );
1405 m_minHeight
= maxHeight
;
1406 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1407 m_minWidth
= m_maxWidth
= maxWidth
;
1411 // FIXME: should this be OSX-only?
1413 bool wantNativeToolbar
, ownToolbarInstalled
;
1415 // attempt to install the native toolbar
1416 wantNativeToolbar
= ((GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_BOTTOM
|wxTB_RIGHT
)) == 0);
1417 MacInstallNativeToolbar( wantNativeToolbar
);
1418 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1419 if (!ownToolbarInstalled
)
1421 SetSize( maxWidth
, maxHeight
);
1422 InvalidateBestSize();
1426 SetSize( maxWidth
, maxHeight
);
1427 InvalidateBestSize();
1435 void wxToolBar::DoLayout()
1437 // TODO port back osx_cocoa layout solution
1440 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1442 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1447 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1449 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1450 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1452 #if wxOSX_USE_NATIVE_TOOLBAR
1453 if (m_macToolbar
!= NULL
)
1455 int maxs
= wxMax( size
.x
, size
.y
);
1456 HIToolbarDisplaySize sizeSpec
;
1458 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1459 else if ( maxs
> 24 )
1460 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1462 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1464 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, sizeSpec
);
1469 // The button size is bigger than the bitmap size
1470 wxSize
wxToolBar::GetToolSize() const
1472 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1475 void wxToolBar::SetRows(int nRows
)
1477 // avoid resizing the frame uselessly
1478 if ( nRows
!= m_maxRows
)
1482 void wxToolBar::MacSuperChangedPosition()
1484 wxWindow::MacSuperChangedPosition();
1486 #if wxOSX_USE_NATIVE_TOOLBAR
1487 if (! m_macUsesNativeToolbar
)
1495 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1497 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1500 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1502 tool
->SetNormalBitmap(bitmap
);
1504 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1505 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1509 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1511 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1514 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1516 tool
->SetDisabledBitmap(bitmap
);
1518 // TODO: what to do for this one?
1522 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1524 wxToolBarTool
*tool
;
1525 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1528 tool
= (wxToolBarTool
*)node
->GetData();
1531 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1532 if ( r
.Contains( wxPoint( x
, y
) ) )
1536 node
= node
->GetNext();
1542 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1544 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1546 return tool
->GetShortHelp();
1548 return wxEmptyString
;
1551 void wxToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(t
), bool WXUNUSED(enable
))
1553 // everything already done in the tool's implementation
1556 void wxToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(t
), bool WXUNUSED(toggle
))
1558 // everything already done in the tool's implementation
1561 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1563 wxToolBarTool
*tool
= static_cast< wxToolBarTool
*>(toolBase
);
1567 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1568 wxSize toolSize
= GetToolSize();
1569 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1570 ControlRef controlHandle
= NULL
;
1573 #if wxOSX_USE_NATIVE_TOOLBAR
1574 wxString label
= tool
->GetLabel();
1575 if (m_macToolbar
&& !label
.empty() )
1577 // strip mnemonics from the label for compatibility
1578 // with the usual labels in wxStaticText sense
1579 label
= wxStripMenuCodes(label
);
1581 #endif // wxOSX_USE_NATIVE_TOOLBAR
1583 switch (tool
->GetStyle())
1585 case wxTOOL_STYLE_SEPARATOR
:
1587 wxASSERT( tool
->GetControlHandle() == NULL
);
1590 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1591 toolrect
.bottom
= toolSize
.y
;
1593 toolrect
.right
= toolSize
.x
;
1595 // in flat style we need a visual separator
1596 #if wxOSX_USE_NATIVE_TOOLBAR
1597 if (m_macToolbar
!= NULL
)
1599 HIToolbarItemRef item
;
1600 err
= HIToolbarItemCreate(
1601 kHIToolbarSeparatorIdentifier
,
1602 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1605 tool
->SetToolbarItemRef( item
);
1609 #endif // wxOSX_USE_NATIVE_TOOLBAR
1611 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1612 tool
->SetControlHandle( controlHandle
);
1616 case wxTOOL_STYLE_BUTTON
:
1618 wxASSERT( tool
->GetControlHandle() == NULL
);
1619 ControlButtonContentInfo info
;
1620 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1622 if ( UMAGetSystemVersion() >= 0x1000)
1624 // contrary to the docs this control only works with iconrefs
1625 ControlButtonContentInfo info
;
1626 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1627 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1628 wxMacReleaseBitmapButton( &info
);
1632 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1633 if ( tool
->CanBeToggled() )
1634 behaviour
|= kControlBehaviorToggles
;
1635 err
= CreateBevelButtonControl( window
,
1636 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1637 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1640 #if wxOSX_USE_NATIVE_TOOLBAR
1641 if (m_macToolbar
!= NULL
)
1643 HIToolbarItemRef item
;
1644 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1645 err
= HIToolbarItemCreate(
1646 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1647 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1650 ControlButtonContentInfo info2
;
1651 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1653 InstallEventHandler(
1654 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1655 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1656 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1657 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1658 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1659 tool
->SetToolbarItemRef( item
);
1661 wxMacReleaseBitmapButton( &info2
);
1666 #endif // wxOSX_USE_NATIVE_TOOLBAR
1668 wxMacReleaseBitmapButton( &info
);
1671 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1672 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1675 InstallControlEventHandler(
1676 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1677 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1679 tool
->SetControlHandle( controlHandle
);
1683 case wxTOOL_STYLE_CONTROL
:
1685 #if wxOSX_USE_NATIVE_TOOLBAR
1686 if (m_macToolbar
!= NULL
)
1688 wxCHECK_MSG( tool
->GetControl(), false, wxT("control must be non-NULL") );
1689 HIToolbarItemRef item
;
1690 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1691 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1692 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macToolbar
,kControlToolbarItemClassID
,
1697 tool
->SetToolbarItemRef( item
);
1707 // right now there's nothing to do here
1717 if ( controlHandle
)
1719 ControlRef container
= (ControlRef
) GetHandle();
1720 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1722 SetControlVisibility( controlHandle
, true, true );
1723 ::EmbedControl( controlHandle
, container
);
1726 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1727 tool
->UpdateToggleImage( true );
1729 // nothing special to do here - we relayout in Realize() later
1730 InvalidateBestSize();
1734 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1737 return (err
== noErr
);
1740 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1745 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1747 wxToolBarTool
* tool
= static_cast< wxToolBarTool
*>(toolbase
);
1748 wxToolBarToolsList::compatibility_iterator node
;
1749 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1751 wxToolBarToolBase
*tool2
= node
->GetData();
1752 if ( tool2
== tool
)
1754 // let node point to the next node in the list
1755 node
= node
->GetNext();
1761 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1763 #if wxOSX_USE_NATIVE_TOOLBAR
1764 CFIndex removeIndex
= tool
->GetIndex();
1767 #if wxOSX_USE_NATIVE_TOOLBAR
1768 if (m_macToolbar
!= NULL
)
1770 if ( removeIndex
!= -1 && m_macToolbar
)
1772 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macToolbar
, removeIndex
);
1773 tool
->SetIndex( -1 );
1778 tool
->ClearControl();
1780 // and finally reposition all the controls after this one
1782 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1784 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1785 wxPoint pt
= tool2
->GetPosition();
1787 if ( GetWindowStyleFlag() & (wxTB_LEFT
|wxTB_RIGHT
) )
1792 tool2
->SetPosition( pt
);
1794 #if wxOSX_USE_NATIVE_TOOLBAR
1795 if (m_macToolbar
!= NULL
)
1797 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1798 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1803 InvalidateBestSize();
1808 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1810 #if wxOSX_USE_NATIVE_TOOLBAR
1811 if ( m_macUsesNativeToolbar
)
1823 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1825 if ( !drawMetalTheme
)
1827 HIThemePlacardDrawInfo info
;
1828 memset( &info
, 0, sizeof(info
) );
1830 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1832 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1833 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1834 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1838 // leave the background as it is (striped or metal)
1844 #endif // wxUSE_TOOLBAR