1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
28 const short kwxMacToolBarToolDefaultWidth
= 16;
29 const short kwxMacToolBarToolDefaultHeight
= 16;
30 const short kwxMacToolBarTopMargin
= 4;
31 const short kwxMacToolBarLeftMargin
= 4;
32 const short kwxMacToolBorder
= 0;
33 const short kwxMacToolSpacing
= 6;
35 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
36 EVT_PAINT( wxToolBar::OnPaint
)
41 #pragma mark Tool Implementation
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
49 // when embedding native controls in the native toolbar we must make sure the
50 // control does not get deleted behind our backs, so the retain count gets increased
51 // (after creation it is 1), first be the creation of the custom HIToolbarItem wrapper
52 // object, and second by the code 'creating' the custom HIView (which is the same as the
53 // already existing native control, therefore we just increase the ref count)
54 // when this view is removed from the native toolbar its count gets decremented again
55 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
56 // so in the end the control lives with a refcount of one and can be disposed of by the
57 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
58 // so we can only test against a range, not a specific value of the refcount.
60 class wxToolBarTool
: public wxToolBarToolBase
66 const wxString
& label
,
67 const wxBitmap
& bmpNormal
,
68 const wxBitmap
& bmpDisabled
,
71 const wxString
& shortHelp
,
72 const wxString
& longHelp
);
74 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
75 : wxToolBarToolBase(tbar
, control
, label
)
79 SetControlHandle( (ControlRef
) control
->GetHandle() );
82 virtual ~wxToolBarTool()
87 WXWidget
GetControlHandle()
89 return (WXWidget
) m_controlHandle
;
92 void SetControlHandle( ControlRef handle
)
94 m_controlHandle
= handle
;
97 void SetPosition( const wxPoint
& position
);
101 if ( m_controlHandle
)
104 DisposeControl( m_controlHandle
);
107 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
108 // proper wxControl destructor
110 m_controlHandle
= NULL
;
113 #if wxOSX_USE_NATIVE_TOOLBAR
114 if ( m_toolbarItemRef
)
116 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
117 // different behaviour under Leopard
118 if ( UMAGetSystemVersion() < 0x1050 )
122 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
125 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
126 CFRelease(m_toolbarItemRef
);
127 m_toolbarItemRef
= NULL
;
129 #endif // wxOSX_USE_NATIVE_TOOLBAR
132 wxSize
GetSize() const
138 curSize
= GetControl()->GetSize();
140 else if ( IsButton() )
142 curSize
= GetToolBar()->GetToolSize();
147 curSize
= GetToolBar()->GetToolSize();
148 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
157 wxPoint
GetPosition() const
159 return wxPoint( m_x
, m_y
);
162 bool DoEnable( bool enable
);
164 void UpdateToggleImage( bool toggle
);
166 #if wxOSX_USE_NATIVE_TOOLBAR
167 void SetToolbarItemRef( HIToolbarItemRef ref
)
169 if ( m_controlHandle
)
170 HideControl( m_controlHandle
);
171 if ( m_toolbarItemRef
)
172 CFRelease( m_toolbarItemRef
);
174 m_toolbarItemRef
= ref
;
175 if ( m_toolbarItemRef
)
177 wxFontEncoding enc
= GetToolBarFontEncoding();
179 HIToolbarItemSetHelpText(
181 wxCFStringRef( GetShortHelp(), enc
),
182 wxCFStringRef( GetLongHelp(), enc
) );
186 HIToolbarItemRef
GetToolbarItemRef() const
188 return m_toolbarItemRef
;
191 void SetIndex( CFIndex idx
)
196 CFIndex
GetIndex() const
201 virtual void SetLabel(const wxString
& label
)
203 wxToolBarToolBase::SetLabel(label
);
205 if ( m_toolbarItemRef
)
207 // strip mnemonics from the label for compatibility with the usual
208 // labels in wxStaticText sense
209 wxString labelStr
= wxStripMenuCodes(label
);
211 HIToolbarItemSetLabel(
213 wxCFStringRef(labelStr
, GetToolBarFontEncoding()) );
216 #endif // wxOSX_USE_NATIVE_TOOLBAR
219 #if wxOSX_USE_NATIVE_TOOLBAR
220 wxFontEncoding
GetToolBarFontEncoding() const
224 f
= GetToolBar()->GetFont();
225 return f
.IsOk() ? f
.GetEncoding() : wxFont::GetDefaultEncoding();
227 #endif // wxOSX_USE_NATIVE_TOOLBAR
231 m_controlHandle
= NULL
;
233 #if wxOSX_USE_NATIVE_TOOLBAR
234 m_toolbarItemRef
= NULL
;
239 ControlRef m_controlHandle
;
243 #if wxOSX_USE_NATIVE_TOOLBAR
244 HIToolbarItemRef m_toolbarItemRef
;
245 // position in its toolbar, -1 means not inserted
250 static const EventTypeSpec eventList
[] =
252 { kEventClassControl
, kEventControlHit
},
253 { kEventClassControl
, kEventControlHitTest
},
256 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
258 OSStatus result
= eventNotHandledErr
;
259 ControlRef controlRef
;
260 wxMacCarbonEvent
cEvent( event
);
262 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
264 switch ( GetEventKind( event
) )
266 case kEventControlHit
:
268 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
269 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
270 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
274 shouldToggle
= !tbartool
->IsToggled();
276 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
279 if (tbartool
!= NULL
)
280 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
285 case kEventControlHitTest
:
287 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
289 HIViewGetBounds( controlRef
, &rect
);
291 ControlPartCode pc
= kControlNoPart
;
292 if ( CGRectContainsPoint( rect
, pt
) )
293 pc
= kControlIconPart
;
294 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
306 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
308 OSStatus result
= eventNotHandledErr
;
310 switch ( GetEventClass( event
) )
312 case kEventClassControl
:
313 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
323 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
325 #if wxOSX_USE_NATIVE_TOOLBAR
327 static const EventTypeSpec toolBarEventList
[] =
329 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
332 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
334 OSStatus result
= eventNotHandledErr
;
336 switch ( GetEventKind( event
) )
338 case kEventToolbarItemPerformAction
:
340 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
341 if ( tbartool
!= NULL
)
343 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
344 int toolID
= tbartool
->GetId();
346 if ( tbartool
->CanBeToggled() )
349 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
353 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
366 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
368 OSStatus result
= eventNotHandledErr
;
370 switch ( GetEventClass( event
) )
372 case kEventClassToolbarItem
:
373 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
383 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
387 bool wxToolBarTool::DoEnable( bool enable
)
391 GetControl()->Enable( enable
);
393 else if ( IsButton() )
395 #if wxOSX_USE_NATIVE_TOOLBAR
396 if ( m_toolbarItemRef
!= NULL
)
397 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
400 if ( m_controlHandle
!= NULL
)
403 EnableControl( m_controlHandle
);
405 DisableControl( m_controlHandle
);
412 void wxToolBarTool::SetPosition( const wxPoint
& position
)
417 int mac_x
= position
.x
;
418 int mac_y
= position
.y
;
423 GetControlBounds( m_controlHandle
, &contrlRect
);
424 int former_mac_x
= contrlRect
.left
;
425 int former_mac_y
= contrlRect
.top
;
426 GetToolBar()->GetToolSize();
428 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
430 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
433 else if ( IsControl() )
435 // embedded native controls are moved by the OS
436 #if wxOSX_USE_NATIVE_TOOLBAR
437 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
440 GetControl()->Move( position
);
447 GetControlBounds( m_controlHandle
, &contrlRect
);
448 int former_mac_x
= contrlRect
.left
;
449 int former_mac_y
= contrlRect
.top
;
451 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
452 ::MoveControl( m_controlHandle
, mac_x
, mac_y
);
456 void wxToolBarTool::UpdateToggleImage( bool toggle
)
460 int w
= m_bmpNormal
.GetWidth();
461 int h
= m_bmpNormal
.GetHeight();
462 wxBitmap
bmp( w
, h
);
465 dc
.SelectObject( bmp
);
466 dc
.SetPen( wxPen(*wxBLACK
) );
467 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
468 dc
.DrawRectangle( 0, 0, w
, h
);
469 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
470 dc
.SelectObject( wxNullBitmap
);
471 ControlButtonContentInfo info
;
472 wxMacCreateBitmapButton( &info
, bmp
);
473 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
474 #if wxOSX_USE_NATIVE_TOOLBAR
475 if (m_toolbarItemRef
!= NULL
)
477 ControlButtonContentInfo info2
;
478 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
479 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
480 wxMacReleaseBitmapButton( &info2
);
483 wxMacReleaseBitmapButton( &info
);
487 ControlButtonContentInfo info
;
488 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
489 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
490 #if wxOSX_USE_NATIVE_TOOLBAR
491 if (m_toolbarItemRef
!= NULL
)
493 ControlButtonContentInfo info2
;
494 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
495 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
496 wxMacReleaseBitmapButton( &info2
);
499 wxMacReleaseBitmapButton( &info
);
502 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
504 m_controlHandle
, 0, kControlIconTransformTag
,
505 sizeof(transform
), (Ptr
)&transform
);
506 HIViewSetNeedsDisplay( m_controlHandle
, true );
510 wxToolBarTool::wxToolBarTool(
513 const wxString
& label
,
514 const wxBitmap
& bmpNormal
,
515 const wxBitmap
& bmpDisabled
,
517 wxObject
*clientData
,
518 const wxString
& shortHelp
,
519 const wxString
& longHelp
)
522 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
523 clientData
, shortHelp
, longHelp
)
529 #pragma mark Toolbar Implementation
531 wxToolBarToolBase
*wxToolBar::CreateTool(
533 const wxString
& label
,
534 const wxBitmap
& bmpNormal
,
535 const wxBitmap
& bmpDisabled
,
537 wxObject
*clientData
,
538 const wxString
& shortHelp
,
539 const wxString
& longHelp
)
541 return new wxToolBarTool(
542 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
543 clientData
, shortHelp
, longHelp
);
547 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
549 return new wxToolBarTool(this, control
, label
);
552 void wxToolBar::Init()
556 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
557 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
559 #if wxOSX_USE_NATIVE_TOOLBAR
561 m_macUsesNativeToolbar
= false;
565 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
567 const EventTypeSpec kEvents
[] =
569 { kEventClassHIObject
, kEventHIObjectConstruct
},
570 { kEventClassHIObject
, kEventHIObjectInitialize
},
571 { kEventClassHIObject
, kEventHIObjectDestruct
},
573 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
576 const EventTypeSpec kViewEvents
[] =
578 { kEventClassControl
, kEventControlGetSizeConstraints
}
581 struct ControlToolbarItem
583 HIToolbarItemRef toolbarItem
;
585 wxSize lastValidSize
;
588 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
590 OSStatus result
= eventNotHandledErr
;
591 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
593 switch ( GetEventClass( inEvent
) )
595 case kEventClassHIObject
:
596 switch ( GetEventKind( inEvent
) )
598 case kEventHIObjectConstruct
:
600 HIObjectRef toolbarItem
;
601 ControlToolbarItem
* item
;
603 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
604 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
606 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
607 item
->toolbarItem
= toolbarItem
;
608 item
->lastValidSize
= wxSize(-1,-1);
609 item
->viewRef
= NULL
;
611 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
617 case kEventHIObjectInitialize
:
618 result
= CallNextEventHandler( inCallRef
, inEvent
);
619 if ( result
== noErr
)
622 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
623 sizeof( CFTypeRef
), NULL
, &data
);
627 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
628 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
630 object
->viewRef
= (HIViewRef
) viewRef
;
631 // make sure we keep that control during our lifetime
632 CFRetain( object
->viewRef
) ;
634 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
635 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
640 case kEventHIObjectDestruct
:
642 HIViewRef viewRef
= object
->viewRef
;
643 if( viewRef
&& IsValidControlHandle( viewRef
) )
645 // depending whether the wxControl corresponding to this HIView has already been destroyed or
646 // not, ref counts differ, so we cannot assert a special value
647 CFIndex count
= CFGetRetainCount( viewRef
) ;
650 CFRelease( viewRef
) ;
660 case kEventClassToolbarItem
:
661 switch ( GetEventKind( inEvent
) )
663 case kEventToolbarItemCreateCustomView
:
665 HIViewRef viewRef
= object
->viewRef
;
666 HIViewRemoveFromSuperview( viewRef
) ;
667 HIViewSetVisible(viewRef
, true) ;
668 CFRetain( viewRef
) ;
669 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
675 case kEventClassControl
:
676 switch ( GetEventKind( inEvent
) )
678 case kEventControlGetSizeConstraints
:
680 wxWindow
* wxwindow
= wxFindWindowFromWXWidget( (WXWidget
) object
->viewRef
) ;
683 // during toolbar layout the native window sometimes gets negative sizes,
684 // sometimes it just gets shrunk behind our back, so in order to avoid
685 // ever shrinking more, once a valid size is captured, we keep it
687 wxSize sz
= object
->lastValidSize
;
688 if ( sz
.x
<= 0 || sz
.y
<= 0 )
690 sz
= wxwindow
->GetSize() ;
691 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
692 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
693 if ( sz
.x
> 0 && sz
.y
> 0 )
694 object
->lastValidSize
= sz
;
699 // Extra width to avoid edge of combobox being cut off
703 min
.width
= max
.width
= sz
.x
;
704 min
.height
= max
.height
= sz
.y
;
706 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
707 sizeof( HISize
), &min
);
709 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
710 sizeof( HISize
), &max
);
722 void RegisterControlToolbarItemClass()
724 static bool sRegistered
;
728 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
729 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
735 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
737 RegisterControlToolbarItemClass();
741 UInt32 options
= kHIToolbarItemAllowDuplicates
;
742 HIToolbarItemRef result
= NULL
;
744 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
745 require_noerr( err
, CantCreateEvent
);
747 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
748 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
751 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
753 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
756 ReleaseEvent( event
);
761 #if wxOSX_USE_NATIVE_TOOLBAR
762 static const EventTypeSpec kToolbarEvents
[] =
764 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
765 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
766 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
769 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
771 void* WXUNUSED(inUserData
))
773 OSStatus result
= eventNotHandledErr
;
775 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
776 CFMutableArrayRef array
;
778 switch ( GetEventKind( inEvent
) )
780 case kEventToolbarGetDefaultIdentifiers
:
782 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
783 sizeof( CFMutableArrayRef
), NULL
, &array
);
784 // not implemented yet
785 // GetToolbarDefaultItems( array );
790 case kEventToolbarGetAllowedIdentifiers
:
792 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
793 sizeof( CFMutableArrayRef
), NULL
, &array
);
794 // not implemented yet
795 // GetToolbarAllowedItems( array );
799 case kEventToolbarCreateItemWithIdentifier
:
801 HIToolbarItemRef item
= NULL
;
802 CFTypeRef data
= NULL
;
803 CFStringRef identifier
= NULL
;
805 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
806 sizeof( CFStringRef
), NULL
, &identifier
);
808 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
809 sizeof( CFTypeRef
), NULL
, &data
);
811 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
813 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
816 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
817 sizeof( HIToolbarItemRef
), &item
);
827 #endif // wxOSX_USE_NATIVE_TOOLBAR
829 // also for the toolbar we have the dual implementation:
830 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
832 bool wxToolBar::Create(
838 const wxString
& name
)
840 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
845 OSStatus err
= noErr
;
847 #if wxOSX_USE_NATIVE_TOOLBAR
848 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
850 wxString labelStr
= wxString::Format( wxT("%p"), this );
851 err
= HIToolbarCreate(
852 wxCFStringRef( labelStr
, wxFont::GetDefaultEncoding() ), 0,
853 (HIToolbarRef
*) &m_macToolbar
);
855 if (m_macToolbar
!= NULL
)
857 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macToolbar
), ToolbarDelegateHandler
,
858 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
860 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
861 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
863 if ( style
& wxTB_NOICONS
)
864 mode
= kHIToolbarDisplayModeLabelOnly
;
865 else if ( style
& wxTB_TEXT
)
866 mode
= kHIToolbarDisplayModeIconAndLabel
;
868 mode
= kHIToolbarDisplayModeIconOnly
;
870 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
871 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, displaySize
);
874 #endif // wxOSX_USE_NATIVE_TOOLBAR
876 return (err
== noErr
);
879 wxToolBar::~wxToolBar()
881 #if wxOSX_USE_NATIVE_TOOLBAR
882 if (m_macToolbar
!= NULL
)
884 // if this is the installed toolbar, then deinstall it
885 if (m_macUsesNativeToolbar
)
886 MacInstallNativeToolbar( false );
888 CFIndex count
= CFGetRetainCount( m_macToolbar
) ;
889 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
890 if ( UMAGetSystemVersion() < 0x1050 )
894 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
897 CFRelease( (HIToolbarRef
)m_macToolbar
);
903 bool wxToolBar::Show( bool show
)
905 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
906 bool bResult
= (tlw
!= NULL
);
910 #if wxOSX_USE_NATIVE_TOOLBAR
911 bool ownToolbarInstalled
= false;
912 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
913 if (ownToolbarInstalled
)
915 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
917 ShowHideWindowToolbar( tlw
, show
, false );
920 bResult
= wxToolBarBase::Show( show
);
923 bResult
= wxToolBarBase::Show( show
);
930 bool wxToolBar::IsShown() const
934 #if wxOSX_USE_NATIVE_TOOLBAR
935 bool ownToolbarInstalled
;
937 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
938 if (ownToolbarInstalled
)
940 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
941 bResult
= IsWindowToolbarVisible( tlw
);
944 bResult
= wxToolBarBase::IsShown();
947 bResult
= wxToolBarBase::IsShown();
953 void wxToolBar::DoGetSize( int *width
, int *height
) const
955 #if wxOSX_USE_NATIVE_TOOLBAR
957 bool ownToolbarInstalled
;
959 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
960 if ( ownToolbarInstalled
)
962 // TODO: is this really a control ?
963 GetControlBounds( (ControlRef
) m_macToolbar
, &boundsR
);
965 *width
= boundsR
.right
- boundsR
.left
;
966 if ( height
!= NULL
)
967 *height
= boundsR
.bottom
- boundsR
.top
;
970 wxToolBarBase::DoGetSize( width
, height
);
973 wxToolBarBase::DoGetSize( width
, height
);
977 wxSize
wxToolBar::DoGetBestSize() const
981 DoGetSize( &width
, &height
);
983 return wxSize( width
, height
);
986 void wxToolBar::SetWindowStyleFlag( long style
)
988 wxToolBarBase::SetWindowStyleFlag( style
);
990 #if wxOSX_USE_NATIVE_TOOLBAR
991 if (m_macToolbar
!= NULL
)
993 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
995 if ( style
& wxTB_NOICONS
)
996 mode
= kHIToolbarDisplayModeLabelOnly
;
997 else if ( style
& wxTB_TEXT
)
998 mode
= kHIToolbarDisplayModeIconAndLabel
;
1000 mode
= kHIToolbarDisplayModeIconOnly
;
1002 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macToolbar
, mode
);
1007 #if wxOSX_USE_NATIVE_TOOLBAR
1008 bool wxToolBar::MacWantsNativeToolbar()
1010 return m_macUsesNativeToolbar
;
1013 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1015 bool bResultV
= false;
1017 if (ownToolbarInstalled
!= NULL
)
1018 *ownToolbarInstalled
= false;
1020 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1023 HIToolbarRef curToolbarRef
= NULL
;
1024 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1025 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1026 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1027 *ownToolbarInstalled
= (curToolbarRef
== m_macToolbar
);
1033 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1035 bool bResult
= false;
1037 if (usesNative
&& (m_macToolbar
== NULL
))
1040 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
1043 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1047 // check the existing toolbar
1048 HIToolbarRef curToolbarRef
= NULL
;
1049 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1051 curToolbarRef
= NULL
;
1053 m_macUsesNativeToolbar
= usesNative
;
1055 if (m_macUsesNativeToolbar
)
1057 // only install toolbar if there isn't one installed already
1058 if (curToolbarRef
== NULL
)
1062 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macToolbar
);
1063 ShowHideWindowToolbar( tlw
, true, false );
1064 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1065 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1067 m_peer
->Move(0,0,0,0 );
1068 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1069 m_peer
->SetVisibility( false );
1070 wxToolBarBase::Show( false );
1075 // only deinstall toolbar if this is the installed one
1076 if (m_macToolbar
== curToolbarRef
)
1080 ShowHideWindowToolbar( tlw
, false, false );
1081 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1082 SetWindowToolbar( tlw
, NULL
);
1084 m_peer
->SetVisibility( true );
1089 InvalidateBestSize();
1091 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1096 bool wxToolBar::Realize()
1098 if (m_tools
.GetCount() == 0)
1104 int maxToolWidth
= 0;
1105 int maxToolHeight
= 0;
1107 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1108 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1111 GetSize( &tw
, &th
);
1113 // find the maximum tool width and height
1114 wxToolBarTool
*tool
;
1115 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1118 tool
= (wxToolBarTool
*) node
->GetData();
1121 wxSize sz
= tool
->GetSize();
1123 if ( sz
.x
> maxToolWidth
)
1124 maxToolWidth
= sz
.x
;
1125 if ( sz
.y
> maxToolHeight
)
1126 maxToolHeight
= sz
.y
;
1129 node
= node
->GetNext();
1132 bool lastIsRadio
= false;
1133 bool curIsRadio
= false;
1135 #if wxOSX_USE_NATIVE_TOOLBAR
1136 CFIndex currentPosition
= 0;
1137 bool insertAll
= false;
1139 HIToolbarRef refTB
= (HIToolbarRef
)m_macToolbar
;
1144 enc
= f
.GetEncoding();
1146 enc
= wxFont::GetDefaultEncoding();
1149 node
= m_tools
.GetFirst();
1152 tool
= (wxToolBarTool
*) node
->GetData();
1155 node
= node
->GetNext();
1159 // set tool position:
1160 // for the moment just perform a single row/column alignment
1161 wxSize cursize
= tool
->GetSize();
1162 if ( x
+ cursize
.x
> maxWidth
)
1163 maxWidth
= x
+ cursize
.x
;
1164 if ( y
+ cursize
.y
> maxHeight
)
1165 maxHeight
= y
+ cursize
.y
;
1167 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1169 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1170 tool
->SetPosition( wxPoint(x1
, y
) );
1174 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1175 tool
->SetPosition( wxPoint(x
, y1
) );
1178 // update the item positioning state
1179 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1180 y
+= cursize
.y
+ kwxMacToolSpacing
;
1182 x
+= cursize
.x
+ kwxMacToolSpacing
;
1184 #if wxOSX_USE_NATIVE_TOOLBAR
1185 // install in native HIToolbar
1188 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1189 if ( hiItemRef
!= NULL
)
1191 // since setting the help texts is non-virtual we have to update
1193 HIToolbarItemSetHelpText( hiItemRef
,
1194 wxCFStringRef( tool
->GetShortHelp(), enc
),
1195 wxCFStringRef( tool
->GetLongHelp(), enc
) );
1197 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1199 OSStatus err
= noErr
;
1204 // if this is the first tool that gets newly inserted or repositioned
1205 // first remove all 'old' tools from here to the right, because of this
1206 // all following tools will have to be reinserted (insertAll).
1207 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1209 node2
= node2
->GetPrevious() )
1211 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1213 const long idx
= tool2
->GetIndex();
1216 if ( tool2
->IsControl() )
1218 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1219 if ( count
!= 3 && count
!= 2 )
1221 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1224 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1226 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1229 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1232 if ( tool2
->IsControl() )
1234 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1237 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1240 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1243 tool2
->SetIndex(-1);
1248 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1251 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1254 tool
->SetIndex( currentPosition
);
1255 if ( tool
->IsControl() )
1257 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1258 if ( count
!= 3 && count
!= 2 )
1260 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1262 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1271 // update radio button (and group) state
1272 lastIsRadio
= curIsRadio
;
1273 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1277 if ( tool
->IsToggled() )
1278 DoToggleTool( tool
, true );
1284 if ( tool
->Toggle( true ) )
1286 DoToggleTool( tool
, true );
1289 else if ( tool
->IsToggled() )
1291 if ( tool
->IsToggled() )
1292 DoToggleTool( tool
, true );
1294 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1297 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1298 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1301 if ( toggleTool
->Toggle( false ) )
1302 DoToggleTool( toggleTool
, false );
1304 nodePrev
= nodePrev
->GetPrevious();
1309 node
= node
->GetNext();
1312 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1314 // if not set yet, only one row
1315 if ( m_maxRows
<= 0 )
1318 m_minWidth
= maxWidth
;
1320 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1321 m_minHeight
= m_maxHeight
= maxHeight
;
1325 // if not set yet, have one column
1326 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1327 SetRows( GetToolsCount() );
1329 m_minHeight
= maxHeight
;
1331 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1332 m_minWidth
= m_maxWidth
= maxWidth
;
1336 // FIXME: should this be OSX-only?
1338 bool wantNativeToolbar
, ownToolbarInstalled
;
1340 // attempt to install the native toolbar
1341 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1342 MacInstallNativeToolbar( wantNativeToolbar
);
1343 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1344 if (!ownToolbarInstalled
)
1346 SetSize( maxWidth
, maxHeight
);
1347 InvalidateBestSize();
1351 SetSize( maxWidth
, maxHeight
);
1352 InvalidateBestSize();
1360 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1362 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1363 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1365 #if wxOSX_USE_NATIVE_TOOLBAR
1366 if (m_macToolbar
!= NULL
)
1368 int maxs
= wxMax( size
.x
, size
.y
);
1369 HIToolbarDisplaySize sizeSpec
;
1371 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1372 else if ( maxs
> 24 )
1373 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1375 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1377 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macToolbar
, sizeSpec
);
1382 // The button size is bigger than the bitmap size
1383 wxSize
wxToolBar::GetToolSize() const
1385 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1388 void wxToolBar::SetRows(int nRows
)
1390 // avoid resizing the frame uselessly
1391 if ( nRows
!= m_maxRows
)
1395 void wxToolBar::MacSuperChangedPosition()
1397 wxWindow::MacSuperChangedPosition();
1399 #if wxOSX_USE_NATIVE_TOOLBAR
1400 if (! m_macUsesNativeToolbar
)
1408 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1410 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1413 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1415 tool
->SetNormalBitmap(bitmap
);
1417 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1418 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1422 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1424 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1427 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1429 tool
->SetDisabledBitmap(bitmap
);
1431 // TODO: what to do for this one?
1435 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1437 wxToolBarTool
*tool
;
1438 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1441 tool
= (wxToolBarTool
*)node
->GetData();
1444 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1445 if ( r
.Contains( wxPoint( x
, y
) ) )
1449 node
= node
->GetNext();
1452 return (wxToolBarToolBase
*)NULL
;
1455 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1457 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1459 return tool
->GetShortHelp();
1461 return wxEmptyString
;
1464 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1467 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1470 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1472 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1473 if ( ( tool
!= NULL
) && tool
->IsButton() )
1474 tool
->UpdateToggleImage( toggle
);
1477 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1479 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1483 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1484 wxSize toolSize
= GetToolSize();
1485 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1486 ControlRef controlHandle
= NULL
;
1489 #if wxOSX_USE_NATIVE_TOOLBAR
1490 wxString label
= tool
->GetLabel();
1491 if (m_macToolbar
&& !label
.empty() )
1493 // strip mnemonics from the label for compatibility
1494 // with the usual labels in wxStaticText sense
1495 label
= wxStripMenuCodes(label
);
1497 #endif // wxOSX_USE_NATIVE_TOOLBAR
1499 switch (tool
->GetStyle())
1501 case wxTOOL_STYLE_SEPARATOR
:
1503 wxASSERT( tool
->GetControlHandle() == NULL
);
1506 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1507 toolrect
.bottom
= toolSize
.y
;
1509 toolrect
.right
= toolSize
.x
;
1511 // in flat style we need a visual separator
1512 #if wxOSX_USE_NATIVE_TOOLBAR
1513 if (m_macToolbar
!= NULL
)
1515 HIToolbarItemRef item
;
1516 err
= HIToolbarItemCreate(
1517 kHIToolbarSeparatorIdentifier
,
1518 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1521 tool
->SetToolbarItemRef( item
);
1525 #endif // wxOSX_USE_NATIVE_TOOLBAR
1527 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1528 tool
->SetControlHandle( controlHandle
);
1532 case wxTOOL_STYLE_BUTTON
:
1534 wxASSERT( tool
->GetControlHandle() == NULL
);
1535 ControlButtonContentInfo info
;
1536 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1538 if ( UMAGetSystemVersion() >= 0x1000)
1540 // contrary to the docs this control only works with iconrefs
1541 ControlButtonContentInfo info
;
1542 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1543 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1544 wxMacReleaseBitmapButton( &info
);
1548 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1549 if ( tool
->CanBeToggled() )
1550 behaviour
|= kControlBehaviorToggles
;
1551 err
= CreateBevelButtonControl( window
,
1552 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1553 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1556 #if wxOSX_USE_NATIVE_TOOLBAR
1557 if (m_macToolbar
!= NULL
)
1559 HIToolbarItemRef item
;
1560 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1561 err
= HIToolbarItemCreate(
1562 wxCFStringRef(labelStr
, wxFont::GetDefaultEncoding()),
1563 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1566 ControlButtonContentInfo info2
;
1567 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1569 InstallEventHandler(
1570 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1571 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1572 HIToolbarItemSetLabel( item
, wxCFStringRef(label
, GetFont().GetEncoding()) );
1573 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1574 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1575 tool
->SetToolbarItemRef( item
);
1577 wxMacReleaseBitmapButton( &info2
);
1582 #endif // wxOSX_USE_NATIVE_TOOLBAR
1584 wxMacReleaseBitmapButton( &info
);
1587 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1588 SetControlTitleWithCFString( m_controlHandle
, wxCFStringRef( label
, wxFont::GetDefaultEncoding() );
1591 InstallControlEventHandler(
1592 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1593 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1595 tool
->SetControlHandle( controlHandle
);
1599 case wxTOOL_STYLE_CONTROL
:
1601 #if wxOSX_USE_NATIVE_TOOLBAR
1602 if (m_macToolbar
!= NULL
)
1604 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1605 HIToolbarItemRef item
;
1606 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1607 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1608 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macToolbar
,kControlToolbarItemClassID
,
1613 tool
->SetToolbarItemRef( item
);
1623 // right now there's nothing to do here
1633 if ( controlHandle
)
1635 ControlRef container
= (ControlRef
) GetHandle();
1636 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1638 SetControlVisibility( controlHandle
, true, true );
1639 ::EmbedControl( controlHandle
, container
);
1642 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1643 tool
->UpdateToggleImage( true );
1645 // nothing special to do here - we relayout in Realize() later
1646 InvalidateBestSize();
1650 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1653 return (err
== noErr
);
1656 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1658 wxFAIL_MSG( wxT("not implemented") );
1661 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1663 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1664 wxToolBarToolsList::compatibility_iterator node
;
1665 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1667 wxToolBarToolBase
*tool2
= node
->GetData();
1668 if ( tool2
== tool
)
1670 // let node point to the next node in the list
1671 node
= node
->GetNext();
1677 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1679 #if wxOSX_USE_NATIVE_TOOLBAR
1680 CFIndex removeIndex
= tool
->GetIndex();
1683 #if wxOSX_USE_NATIVE_TOOLBAR
1684 if (m_macToolbar
!= NULL
)
1686 if ( removeIndex
!= -1 && m_macToolbar
)
1688 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macToolbar
, removeIndex
);
1689 tool
->SetIndex( -1 );
1694 tool
->ClearControl();
1696 // and finally reposition all the controls after this one
1698 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1700 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1701 wxPoint pt
= tool2
->GetPosition();
1703 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1708 tool2
->SetPosition( pt
);
1710 #if wxOSX_USE_NATIVE_TOOLBAR
1711 if (m_macToolbar
!= NULL
)
1713 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1714 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1719 InvalidateBestSize();
1724 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1726 #if wxOSX_USE_NATIVE_TOOLBAR
1727 if ( m_macUsesNativeToolbar
)
1739 bool drawMetalTheme
= MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL
;
1741 if ( !drawMetalTheme
)
1743 HIThemePlacardDrawInfo info
;
1744 memset( &info
, 0, sizeof(info
) );
1746 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1748 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1749 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1750 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1754 // leave the background as it is (striped or metal)
1760 #endif // wxUSE_TOOLBAR