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
)
184 f
= GetToolBar()->GetFont();
186 enc
= f
.GetEncoding();
188 enc
= wxFont::GetDefaultEncoding();
190 HIToolbarItemSetHelpText(
192 wxCFStringRef( GetShortHelp(), enc
),
193 wxCFStringRef( GetLongHelp(), enc
) );
197 HIToolbarItemRef
GetToolbarItemRef() const
199 return m_toolbarItemRef
;
202 void SetIndex( CFIndex idx
)
207 CFIndex
GetIndex() const
212 virtual void SetLabel(const wxString
& label
)
214 wxToolBarToolBase::SetLabel(label
);
216 if ( m_toolbarItemRef
)
221 f
= GetToolBar()->GetFont();
223 enc
= f
.GetEncoding();
225 enc
= wxFont::GetDefaultEncoding();
227 // strip mnemonics from the label for compatibility with the usual
228 // labels in wxStaticText sense
229 wxString labelStr
= wxStripMenuCodes(label
);
231 HIToolbarItemSetLabel( m_toolbarItemRef
, wxCFStringRef(labelStr
, enc
) );
234 #endif // wxOSX_USE_NATIVE_TOOLBAR
239 m_controlHandle
= NULL
;
241 #if wxOSX_USE_NATIVE_TOOLBAR
242 m_toolbarItemRef
= NULL
;
247 ControlRef m_controlHandle
;
251 #if wxOSX_USE_NATIVE_TOOLBAR
252 HIToolbarItemRef m_toolbarItemRef
;
253 // position in its toolbar, -1 means not inserted
258 static const EventTypeSpec eventList
[] =
260 { kEventClassControl
, kEventControlHit
},
261 { kEventClassControl
, kEventControlHitTest
},
264 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
266 OSStatus result
= eventNotHandledErr
;
267 ControlRef controlRef
;
268 wxMacCarbonEvent
cEvent( event
);
270 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
272 switch ( GetEventKind( event
) )
274 case kEventControlHit
:
276 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
277 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
278 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
282 shouldToggle
= !tbartool
->IsToggled();
284 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
287 if (tbartool
!= NULL
)
288 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
293 case kEventControlHitTest
:
295 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
297 HIViewGetBounds( controlRef
, &rect
);
299 ControlPartCode pc
= kControlNoPart
;
300 if ( CGRectContainsPoint( rect
, pt
) )
301 pc
= kControlIconPart
;
302 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
314 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
316 OSStatus result
= eventNotHandledErr
;
318 switch ( GetEventClass( event
) )
320 case kEventClassControl
:
321 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
331 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
333 #if wxOSX_USE_NATIVE_TOOLBAR
335 static const EventTypeSpec toolBarEventList
[] =
337 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
340 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
342 OSStatus result
= eventNotHandledErr
;
344 switch ( GetEventKind( event
) )
346 case kEventToolbarItemPerformAction
:
348 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
349 if ( tbartool
!= NULL
)
351 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
352 int toolID
= tbartool
->GetId();
354 if ( tbartool
->CanBeToggled() )
357 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
361 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
374 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
376 OSStatus result
= eventNotHandledErr
;
378 switch ( GetEventClass( event
) )
380 case kEventClassToolbarItem
:
381 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
391 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
395 bool wxToolBarTool::DoEnable( bool enable
)
399 GetControl()->Enable( enable
);
401 else if ( IsButton() )
403 #if wxOSX_USE_NATIVE_TOOLBAR
404 if ( m_toolbarItemRef
!= NULL
)
405 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
408 if ( m_controlHandle
!= NULL
)
411 EnableControl( m_controlHandle
);
413 DisableControl( m_controlHandle
);
420 void wxToolBarTool::SetPosition( const wxPoint
& position
)
425 int mac_x
= position
.x
;
426 int mac_y
= position
.y
;
431 GetControlBounds( m_controlHandle
, &contrlRect
);
432 int former_mac_x
= contrlRect
.left
;
433 int former_mac_y
= contrlRect
.top
;
434 GetToolBar()->GetToolSize();
436 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
438 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
441 else if ( IsControl() )
443 // embedded native controls are moved by the OS
444 #if wxOSX_USE_NATIVE_TOOLBAR
445 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
448 GetControl()->Move( position
);
455 GetControlBounds( m_controlHandle
, &contrlRect
);
456 int former_mac_x
= contrlRect
.left
;
457 int former_mac_y
= contrlRect
.top
;
459 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
460 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
464 void wxToolBarTool::UpdateToggleImage( bool toggle
)
468 int w
= m_bmpNormal
.GetWidth();
469 int h
= m_bmpNormal
.GetHeight();
470 wxBitmap
bmp( w
, h
);
473 dc
.SelectObject( bmp
);
474 dc
.SetPen( wxPen(*wxBLACK
) );
475 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
476 dc
.DrawRectangle( 0, 0, w
, h
);
477 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
478 dc
.SelectObject( wxNullBitmap
);
479 ControlButtonContentInfo info
;
480 wxMacCreateBitmapButton( &info
, bmp
);
481 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
482 #if wxOSX_USE_NATIVE_TOOLBAR
483 if (m_toolbarItemRef
!= NULL
)
485 ControlButtonContentInfo info2
;
486 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
487 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
488 wxMacReleaseBitmapButton( &info2
);
491 wxMacReleaseBitmapButton( &info
);
495 ControlButtonContentInfo info
;
496 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
497 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
498 #if wxOSX_USE_NATIVE_TOOLBAR
499 if (m_toolbarItemRef
!= NULL
)
501 ControlButtonContentInfo info2
;
502 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
503 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
504 wxMacReleaseBitmapButton( &info2
);
507 wxMacReleaseBitmapButton( &info
);
510 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
512 m_controlHandle
, 0, kControlIconTransformTag
,
513 sizeof(transform
), (Ptr
)&transform
);
514 HIViewSetNeedsDisplay( m_controlHandle
, true );
518 wxToolBarTool::wxToolBarTool(
521 const wxString
& label
,
522 const wxBitmap
& bmpNormal
,
523 const wxBitmap
& bmpDisabled
,
525 wxObject
*clientData
,
526 const wxString
& shortHelp
,
527 const wxString
& longHelp
)
530 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
531 clientData
, shortHelp
, longHelp
)
537 #pragma mark Toolbar Implementation
539 wxToolBarToolBase
*wxToolBar::CreateTool(
541 const wxString
& label
,
542 const wxBitmap
& bmpNormal
,
543 const wxBitmap
& bmpDisabled
,
545 wxObject
*clientData
,
546 const wxString
& shortHelp
,
547 const wxString
& longHelp
)
549 return new wxToolBarTool(
550 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
551 clientData
, shortHelp
, longHelp
);
555 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
557 return new wxToolBarTool(this, control
, label
);
560 void wxToolBar::Init()
564 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
565 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
567 #if wxOSX_USE_NATIVE_TOOLBAR
568 m_macHIToolbarRef
= NULL
;
569 m_macUsesNativeToolbar
= false;
573 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
575 const EventTypeSpec kEvents
[] =
577 { kEventClassHIObject
, kEventHIObjectConstruct
},
578 { kEventClassHIObject
, kEventHIObjectInitialize
},
579 { kEventClassHIObject
, kEventHIObjectDestruct
},
581 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
584 const EventTypeSpec kViewEvents
[] =
586 { kEventClassControl
, kEventControlGetSizeConstraints
}
589 struct ControlToolbarItem
591 HIToolbarItemRef toolbarItem
;
593 wxSize lastValidSize
;
596 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
598 OSStatus result
= eventNotHandledErr
;
599 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
601 switch ( GetEventClass( inEvent
) )
603 case kEventClassHIObject
:
604 switch ( GetEventKind( inEvent
) )
606 case kEventHIObjectConstruct
:
608 HIObjectRef toolbarItem
;
609 ControlToolbarItem
* item
;
611 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
612 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
614 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
615 item
->toolbarItem
= toolbarItem
;
616 item
->lastValidSize
= wxSize(-1,-1);
617 item
->viewRef
= NULL
;
619 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
625 case kEventHIObjectInitialize
:
626 result
= CallNextEventHandler( inCallRef
, inEvent
);
627 if ( result
== noErr
)
630 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
631 sizeof( CFTypeRef
), NULL
, &data
);
635 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
636 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
638 object
->viewRef
= (HIViewRef
) viewRef
;
639 // make sure we keep that control during our lifetime
640 CFRetain( object
->viewRef
) ;
642 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
643 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
648 case kEventHIObjectDestruct
:
650 HIViewRef viewRef
= object
->viewRef
;
651 if( viewRef
&& IsValidControlHandle( viewRef
) )
653 // depending whether the wxControl corresponding to this HIView has already been destroyed or
654 // not, ref counts differ, so we cannot assert a special value
655 CFIndex count
= CFGetRetainCount( viewRef
) ;
658 wxFAIL_MSG("Reference count of native tool was illegal before removal");
660 CFRelease( viewRef
) ;
670 case kEventClassToolbarItem
:
671 switch ( GetEventKind( inEvent
) )
673 case kEventToolbarItemCreateCustomView
:
675 HIViewRef viewRef
= object
->viewRef
;
676 HIViewRemoveFromSuperview( viewRef
) ;
677 HIViewSetVisible(viewRef
, true) ;
678 CFRetain( viewRef
) ;
679 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
685 case kEventClassControl
:
686 switch ( GetEventKind( inEvent
) )
688 case kEventControlGetSizeConstraints
:
690 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
693 // during toolbar layout the native window sometimes gets negative sizes,
694 // sometimes it just gets shrunk behind our back, so in order to avoid
695 // ever shrinking more, once a valid size is captured, we keep it
697 wxSize sz
= object
->lastValidSize
;
698 if ( sz
.x
<= 0 || sz
.y
<= 0 )
700 sz
= wxwindow
->GetSize() ;
701 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
702 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
703 if ( sz
.x
> 0 && sz
.y
> 0 )
704 object
->lastValidSize
= sz
;
709 // Extra width to avoid edge of combobox being cut off
713 min
.width
= max
.width
= sz
.x
;
714 min
.height
= max
.height
= sz
.y
;
716 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
717 sizeof( HISize
), &min
);
719 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
720 sizeof( HISize
), &max
);
732 void RegisterControlToolbarItemClass()
734 static bool sRegistered
;
738 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
739 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
745 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
747 RegisterControlToolbarItemClass();
751 UInt32 options
= kHIToolbarItemAllowDuplicates
;
752 HIToolbarItemRef result
= NULL
;
754 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
755 require_noerr( err
, CantCreateEvent
);
757 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
758 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
761 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
763 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
766 ReleaseEvent( event
);
771 #if wxOSX_USE_NATIVE_TOOLBAR
772 static const EventTypeSpec kToolbarEvents
[] =
774 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
775 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
776 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
779 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
781 void* WXUNUSED(inUserData
))
783 OSStatus result
= eventNotHandledErr
;
785 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
786 CFMutableArrayRef array
;
788 switch ( GetEventKind( inEvent
) )
790 case kEventToolbarGetDefaultIdentifiers
:
792 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
793 sizeof( CFMutableArrayRef
), NULL
, &array
);
794 // not implemented yet
795 // GetToolbarDefaultItems( array );
800 case kEventToolbarGetAllowedIdentifiers
:
802 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
803 sizeof( CFMutableArrayRef
), NULL
, &array
);
804 // not implemented yet
805 // GetToolbarAllowedItems( array );
809 case kEventToolbarCreateItemWithIdentifier
:
811 HIToolbarItemRef item
= NULL
;
812 CFTypeRef data
= NULL
;
813 CFStringRef identifier
= NULL
;
815 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
816 sizeof( CFStringRef
), NULL
, &identifier
);
818 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
819 sizeof( CFTypeRef
), NULL
, &data
);
821 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
823 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
826 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
827 sizeof( HIToolbarItemRef
), &item
);
837 #endif // wxOSX_USE_NATIVE_TOOLBAR
839 // also for the toolbar we have the dual implementation:
840 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
842 bool wxToolBar::Create(
848 const wxString
& name
)
850 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
855 OSStatus err
= noErr
;
857 #if wxOSX_USE_NATIVE_TOOLBAR
858 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
860 wxString labelStr
= wxString::Format( wxT("%p"), this );
861 err
= HIToolbarCreate(
862 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
863 (HIToolbarRef
*) &m_macHIToolbarRef
);
865 if (m_macHIToolbarRef
!= NULL
)
867 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macHIToolbarRef
), ToolbarDelegateHandler
,
868 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
870 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
871 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
873 if ( style
& wxTB_NOICONS
)
874 mode
= kHIToolbarDisplayModeLabelOnly
;
875 else if ( style
& wxTB_TEXT
)
876 mode
= kHIToolbarDisplayModeIconAndLabel
;
878 mode
= kHIToolbarDisplayModeIconOnly
;
880 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
881 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
884 #endif // wxOSX_USE_NATIVE_TOOLBAR
886 return (err
== noErr
);
889 wxToolBar::~wxToolBar()
891 #if wxOSX_USE_NATIVE_TOOLBAR
892 if (m_macHIToolbarRef
!= NULL
)
894 // if this is the installed toolbar, then deinstall it
895 if (m_macUsesNativeToolbar
)
896 MacInstallNativeToolbar( false );
898 CFIndex count
= CFGetRetainCount( m_macHIToolbarRef
) ;
899 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
900 if ( UMAGetSystemVersion() < 0x1050 )
904 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
907 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
908 m_macHIToolbarRef
= NULL
;
913 bool wxToolBar::Show( bool show
)
915 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
916 bool bResult
= (tlw
!= NULL
);
920 #if wxOSX_USE_NATIVE_TOOLBAR
921 bool ownToolbarInstalled
= false;
922 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
923 if (ownToolbarInstalled
)
925 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
927 ShowHideWindowToolbar( tlw
, show
, false );
930 bResult
= wxToolBarBase::Show( show
);
933 bResult
= wxToolBarBase::Show( show
);
940 bool wxToolBar::IsShown() const
944 #if wxOSX_USE_NATIVE_TOOLBAR
945 bool ownToolbarInstalled
;
947 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
948 if (ownToolbarInstalled
)
950 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
951 bResult
= IsWindowToolbarVisible( tlw
);
954 bResult
= wxToolBarBase::IsShown();
957 bResult
= wxToolBarBase::IsShown();
963 void wxToolBar::DoGetSize( int *width
, int *height
) const
965 #if wxOSX_USE_NATIVE_TOOLBAR
967 bool ownToolbarInstalled
;
969 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
970 if ( ownToolbarInstalled
)
972 // TODO: is this really a control ?
973 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
975 *width
= boundsR
.right
- boundsR
.left
;
976 if ( height
!= NULL
)
977 *height
= boundsR
.bottom
- boundsR
.top
;
980 wxToolBarBase::DoGetSize( width
, height
);
983 wxToolBarBase::DoGetSize( width
, height
);
987 wxSize
wxToolBar::DoGetBestSize() const
991 DoGetSize( &width
, &height
);
993 return wxSize( width
, height
);
996 void wxToolBar::SetWindowStyleFlag( long style
)
998 wxToolBarBase::SetWindowStyleFlag( style
);
1000 #if wxOSX_USE_NATIVE_TOOLBAR
1001 if (m_macHIToolbarRef
!= NULL
)
1003 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
1005 if ( style
& wxTB_NOICONS
)
1006 mode
= kHIToolbarDisplayModeLabelOnly
;
1007 else if ( style
& wxTB_TEXT
)
1008 mode
= kHIToolbarDisplayModeIconAndLabel
;
1010 mode
= kHIToolbarDisplayModeIconOnly
;
1012 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
1017 #if wxOSX_USE_NATIVE_TOOLBAR
1018 bool wxToolBar::MacWantsNativeToolbar()
1020 return m_macUsesNativeToolbar
;
1023 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1025 bool bResultV
= false;
1027 if (ownToolbarInstalled
!= NULL
)
1028 *ownToolbarInstalled
= false;
1030 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1033 HIToolbarRef curToolbarRef
= NULL
;
1034 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1035 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1036 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1037 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
1043 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1045 bool bResult
= false;
1047 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
1050 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
1053 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1057 // check the existing toolbar
1058 HIToolbarRef curToolbarRef
= NULL
;
1059 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1061 curToolbarRef
= NULL
;
1063 m_macUsesNativeToolbar
= usesNative
;
1065 if (m_macUsesNativeToolbar
)
1067 // only install toolbar if there isn't one installed already
1068 if (curToolbarRef
== NULL
)
1072 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
1073 ShowHideWindowToolbar( tlw
, true, false );
1074 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1075 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1077 m_peer
->Move(0,0,0,0 );
1078 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1079 m_peer
->SetVisibility( false );
1080 wxToolBarBase::Show( false );
1085 // only deinstall toolbar if this is the installed one
1086 if (m_macHIToolbarRef
== curToolbarRef
)
1090 ShowHideWindowToolbar( tlw
, false, false );
1091 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1092 SetWindowToolbar( tlw
, NULL
);
1094 m_peer
->SetVisibility( true );
1099 InvalidateBestSize();
1101 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1106 bool wxToolBar::Realize()
1108 if (m_tools
.GetCount() == 0)
1114 int maxToolWidth
= 0;
1115 int maxToolHeight
= 0;
1117 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1118 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1121 GetSize( &tw
, &th
);
1123 // find the maximum tool width and height
1124 wxToolBarTool
*tool
;
1125 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1128 tool
= (wxToolBarTool
*) node
->GetData();
1131 wxSize sz
= tool
->GetSize();
1133 if ( sz
.x
> maxToolWidth
)
1134 maxToolWidth
= sz
.x
;
1135 if ( sz
.y
> maxToolHeight
)
1136 maxToolHeight
= sz
.y
;
1139 node
= node
->GetNext();
1142 bool lastIsRadio
= false;
1143 bool curIsRadio
= false;
1145 #if wxOSX_USE_NATIVE_TOOLBAR
1146 CFIndex currentPosition
= 0;
1147 bool insertAll
= false;
1149 HIToolbarRef refTB
= (HIToolbarRef
)m_macHIToolbarRef
;
1154 enc
= f
.GetEncoding();
1156 enc
= wxFont::GetDefaultEncoding();
1159 node
= m_tools
.GetFirst();
1162 tool
= (wxToolBarTool
*) node
->GetData();
1165 node
= node
->GetNext();
1169 // set tool position:
1170 // for the moment just perform a single row/column alignment
1171 wxSize cursize
= tool
->GetSize();
1172 if ( x
+ cursize
.x
> maxWidth
)
1173 maxWidth
= x
+ cursize
.x
;
1174 if ( y
+ cursize
.y
> maxHeight
)
1175 maxHeight
= y
+ cursize
.y
;
1177 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1179 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1180 tool
->SetPosition( wxPoint(x1
, y
) );
1184 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1185 tool
->SetPosition( wxPoint(x
, y1
) );
1188 // update the item positioning state
1189 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1190 y
+= cursize
.y
+ kwxMacToolSpacing
;
1192 x
+= cursize
.x
+ kwxMacToolSpacing
;
1194 #if wxOSX_USE_NATIVE_TOOLBAR
1195 // install in native HIToolbar
1198 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1199 if ( hiItemRef
!= NULL
)
1201 // since setting the help texts is non-virtual we have to update
1203 HIToolbarItemSetHelpText( hiItemRef
,
1204 wxCFStringRef( tool
->GetShortHelp(), enc
),
1205 wxCFStringRef( tool
->GetLongHelp(), enc
) );
1207 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1209 OSStatus err
= noErr
;
1214 // if this is the first tool that gets newly inserted or repositioned
1215 // first remove all 'old' tools from here to the right, because of this
1216 // all following tools will have to be reinserted (insertAll).
1217 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1219 node2
= node2
->GetPrevious() )
1221 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1223 const long idx
= tool2
->GetIndex();
1226 if ( tool2
->IsControl() )
1228 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1229 if ( count
!= 3 && count
!= 2 )
1231 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1234 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1236 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1239 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1242 if ( tool2
->IsControl() )
1244 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1247 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1250 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1253 tool2
->SetIndex(-1);
1258 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1261 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1264 tool
->SetIndex( currentPosition
);
1265 if ( tool
->IsControl() )
1267 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1268 if ( count
!= 3 && count
!= 2 )
1270 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1272 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1281 // update radio button (and group) state
1282 lastIsRadio
= curIsRadio
;
1283 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1287 if ( tool
->IsToggled() )
1288 DoToggleTool( tool
, true );
1294 if ( tool
->Toggle( true ) )
1296 DoToggleTool( tool
, true );
1299 else if ( tool
->IsToggled() )
1301 if ( tool
->IsToggled() )
1302 DoToggleTool( tool
, true );
1304 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1307 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1308 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1311 if ( toggleTool
->Toggle( false ) )
1312 DoToggleTool( toggleTool
, false );
1314 nodePrev
= nodePrev
->GetPrevious();
1319 node
= node
->GetNext();
1322 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1324 // if not set yet, only one row
1325 if ( m_maxRows
<= 0 )
1328 m_minWidth
= maxWidth
;
1330 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1331 m_minHeight
= m_maxHeight
= maxHeight
;
1335 // if not set yet, have one column
1336 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1337 SetRows( GetToolsCount() );
1339 m_minHeight
= maxHeight
;
1341 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1342 m_minWidth
= m_maxWidth
= maxWidth
;
1346 // FIXME: should this be OSX-only?
1348 bool wantNativeToolbar
, ownToolbarInstalled
;
1350 // attempt to install the native toolbar
1351 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1352 MacInstallNativeToolbar( wantNativeToolbar
);
1353 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1354 if (!ownToolbarInstalled
)
1356 SetSize( maxWidth
, maxHeight
);
1357 InvalidateBestSize();
1361 SetSize( maxWidth
, maxHeight
);
1362 InvalidateBestSize();
1370 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1372 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1373 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1375 #if wxOSX_USE_NATIVE_TOOLBAR
1376 if (m_macHIToolbarRef
!= NULL
)
1378 int maxs
= wxMax( size
.x
, size
.y
);
1379 HIToolbarDisplaySize sizeSpec
;
1381 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1382 else if ( maxs
> 24 )
1383 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1385 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1387 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1392 // The button size is bigger than the bitmap size
1393 wxSize
wxToolBar::GetToolSize() const
1395 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1398 void wxToolBar::SetRows(int nRows
)
1400 // avoid resizing the frame uselessly
1401 if ( nRows
!= m_maxRows
)
1405 void wxToolBar::MacSuperChangedPosition()
1407 wxWindow::MacSuperChangedPosition();
1409 #if wxOSX_USE_NATIVE_TOOLBAR
1410 if (! m_macUsesNativeToolbar
)
1418 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1420 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1423 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1425 tool
->SetNormalBitmap(bitmap
);
1427 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1428 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1432 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1434 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1437 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1439 tool
->SetDisabledBitmap(bitmap
);
1441 // TODO: what to do for this one?
1445 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1447 wxToolBarTool
*tool
;
1448 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1451 tool
= (wxToolBarTool
*)node
->GetData();
1454 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1455 if ( r
.Contains( wxPoint( x
, y
) ) )
1459 node
= node
->GetNext();
1462 return (wxToolBarToolBase
*)NULL
;
1465 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1467 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1469 return tool
->GetShortHelp();
1471 return wxEmptyString
;
1474 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1477 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1480 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1482 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1483 if ( ( tool
!= NULL
) && tool
->IsButton() )
1484 tool
->UpdateToggleImage( toggle
);
1487 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1489 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1493 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1494 wxSize toolSize
= GetToolSize();
1495 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1496 ControlRef controlHandle
= NULL
;
1499 #if wxOSX_USE_NATIVE_TOOLBAR
1500 wxString label
= tool
->GetLabel();
1501 if (m_macHIToolbarRef
&& !label
.empty() )
1503 // strip mnemonics from the label for compatibility
1504 // with the usual labels in wxStaticText sense
1505 label
= wxStripMenuCodes(label
);
1507 #endif // wxOSX_USE_NATIVE_TOOLBAR
1509 switch (tool
->GetStyle())
1511 case wxTOOL_STYLE_SEPARATOR
:
1513 wxASSERT( tool
->GetControlHandle() == NULL
);
1516 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1517 toolrect
.bottom
= toolSize
.y
;
1519 toolrect
.right
= toolSize
.x
;
1521 // in flat style we need a visual separator
1522 #if wxOSX_USE_NATIVE_TOOLBAR
1523 if (m_macHIToolbarRef
!= NULL
)
1525 HIToolbarItemRef item
;
1526 err
= HIToolbarItemCreate(
1527 kHIToolbarSeparatorIdentifier
,
1528 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1531 tool
->SetToolbarItemRef( item
);
1535 #endif // wxOSX_USE_NATIVE_TOOLBAR
1537 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1538 tool
->SetControlHandle( controlHandle
);
1542 case wxTOOL_STYLE_BUTTON
:
1544 wxASSERT( tool
->GetControlHandle() == NULL
);
1545 ControlButtonContentInfo info
;
1546 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1548 if ( UMAGetSystemVersion() >= 0x1000)
1550 // contrary to the docs this control only works with iconrefs
1551 ControlButtonContentInfo info
;
1552 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1553 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1554 wxMacReleaseBitmapButton( &info
);
1558 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1559 if ( tool
->CanBeToggled() )
1560 behaviour
|= kControlBehaviorToggles
;
1561 err
= CreateBevelButtonControl( window
,
1562 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1563 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1566 #if wxOSX_USE_NATIVE_TOOLBAR
1567 if (m_macHIToolbarRef
!= NULL
)
1569 HIToolbarItemRef item
;
1570 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1571 err
= HIToolbarItemCreate(
1572 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1573 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1576 ControlButtonContentInfo info2
;
1577 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1579 InstallEventHandler(
1580 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1581 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1582 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1583 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1584 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1585 tool
->SetToolbarItemRef( item
);
1587 wxMacReleaseBitmapButton( &info2
);
1592 #endif // wxOSX_USE_NATIVE_TOOLBAR
1594 wxMacReleaseBitmapButton( &info
);
1597 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1598 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1601 InstallControlEventHandler(
1602 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1603 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1605 tool
->SetControlHandle( controlHandle
);
1609 case wxTOOL_STYLE_CONTROL
:
1611 #if wxOSX_USE_NATIVE_TOOLBAR
1612 if (m_macHIToolbarRef
!= NULL
)
1614 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1615 HIToolbarItemRef item
;
1616 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1617 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1618 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macHIToolbarRef
,kControlToolbarItemClassID
,
1623 tool
->SetToolbarItemRef( item
);
1633 // right now there's nothing to do here
1643 if ( controlHandle
)
1645 ControlRef container
= (ControlRef
) GetHandle();
1646 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1648 SetControlVisibility( controlHandle
, true, true );
1649 ::EmbedControl( controlHandle
, container
);
1652 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1653 tool
->UpdateToggleImage( true );
1655 // nothing special to do here - we relayout in Realize() later
1656 InvalidateBestSize();
1660 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1663 return (err
== noErr
);
1666 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1668 wxFAIL_MSG( wxT("not implemented") );
1671 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1673 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1674 wxToolBarToolsList::compatibility_iterator node
;
1675 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1677 wxToolBarToolBase
*tool2
= node
->GetData();
1678 if ( tool2
== tool
)
1680 // let node point to the next node in the list
1681 node
= node
->GetNext();
1687 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1689 #if wxOSX_USE_NATIVE_TOOLBAR
1690 CFIndex removeIndex
= tool
->GetIndex();
1693 #if wxOSX_USE_NATIVE_TOOLBAR
1694 if (m_macHIToolbarRef
!= NULL
)
1696 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1698 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1699 tool
->SetIndex( -1 );
1704 tool
->ClearControl();
1706 // and finally reposition all the controls after this one
1708 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1710 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1711 wxPoint pt
= tool2
->GetPosition();
1713 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1718 tool2
->SetPosition( pt
);
1720 #if wxOSX_USE_NATIVE_TOOLBAR
1721 if (m_macHIToolbarRef
!= NULL
)
1723 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1724 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1729 InvalidateBestSize();
1734 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1736 #if wxOSX_USE_NATIVE_TOOLBAR
1737 if ( m_macUsesNativeToolbar
)
1749 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1751 if ( !drawMetalTheme
)
1753 HIThemePlacardDrawInfo info
;
1754 memset( &info
, 0, sizeof(info
) );
1756 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1758 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1759 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1760 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1764 // leave the background as it is (striped or metal)
1770 #endif // wxUSE_TOOLBAR