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/mac/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 will be disposed of in the
112 // proper wxControl destructor
113 wxASSERT( IsValidControlHandle(GetControl()->GetPeer()->GetControlRef() )) ;
115 m_controlHandle
= NULL
;
119 #if wxMAC_USE_NATIVE_TOOLBAR
120 if ( m_toolbarItemRef
)
122 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
123 // different behaviour under Leopard
124 if ( UMAGetSystemVersion() < 0x1050 )
126 wxASSERT_MSG( count
== 1 , wxT("Reference Count of native tool was not 1 in wxToolBarTool destructor") );
128 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
129 CFRelease(m_toolbarItemRef
);
130 m_toolbarItemRef
= NULL
;
135 wxSize
GetSize() const
141 curSize
= GetControl()->GetSize();
143 else if ( IsButton() )
145 curSize
= GetToolBar()->GetToolSize();
150 curSize
= GetToolBar()->GetToolSize();
151 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
160 wxPoint
GetPosition() const
162 return wxPoint( m_x
, m_y
);
165 bool DoEnable( bool enable
);
167 void UpdateToggleImage( bool toggle
);
169 #if wxMAC_USE_NATIVE_TOOLBAR
170 void SetToolbarItemRef( HIToolbarItemRef ref
)
172 if ( m_controlHandle
)
173 HideControl( m_controlHandle
);
174 if ( m_toolbarItemRef
)
175 CFRelease( m_toolbarItemRef
);
177 m_toolbarItemRef
= ref
;
178 if ( m_toolbarItemRef
)
183 f
= GetToolBar()->GetFont();
185 enc
= f
.GetEncoding();
187 enc
= wxFont::GetDefaultEncoding();
189 HIToolbarItemSetHelpText(
191 wxMacCFStringHolder( GetShortHelp(), enc
),
192 wxMacCFStringHolder( GetLongHelp(), enc
) );
196 HIToolbarItemRef
GetToolbarItemRef() const
198 return m_toolbarItemRef
;
201 void SetIndex( CFIndex idx
)
206 CFIndex
GetIndex() const
215 m_controlHandle
= NULL
;
217 #if wxMAC_USE_NATIVE_TOOLBAR
218 m_toolbarItemRef
= NULL
;
223 ControlRef m_controlHandle
;
227 #if wxMAC_USE_NATIVE_TOOLBAR
228 HIToolbarItemRef m_toolbarItemRef
;
229 // position in its toolbar, -1 means not inserted
234 static const EventTypeSpec eventList
[] =
236 { kEventClassControl
, kEventControlHit
},
237 { kEventClassControl
, kEventControlHitTest
},
240 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
242 OSStatus result
= eventNotHandledErr
;
243 ControlRef controlRef
;
244 wxMacCarbonEvent
cEvent( event
);
246 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
248 switch ( GetEventKind( event
) )
250 case kEventControlHit
:
252 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
253 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
254 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
258 shouldToggle
= !tbartool
->IsToggled();
260 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
263 if (tbartool
!= NULL
)
264 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
269 case kEventControlHitTest
:
271 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
273 HIViewGetBounds( controlRef
, &rect
);
275 ControlPartCode pc
= kControlNoPart
;
276 if ( CGRectContainsPoint( rect
, pt
) )
277 pc
= kControlIconPart
;
278 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
290 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
292 OSStatus result
= eventNotHandledErr
;
294 switch ( GetEventClass( event
) )
296 case kEventClassControl
:
297 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
307 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
309 #if wxMAC_USE_NATIVE_TOOLBAR
311 static const EventTypeSpec toolBarEventList
[] =
313 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
316 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
318 OSStatus result
= eventNotHandledErr
;
320 switch ( GetEventKind( event
) )
322 case kEventToolbarItemPerformAction
:
324 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
325 if ( tbartool
!= NULL
)
327 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
328 int toolID
= tbartool
->GetId();
330 if ( tbartool
->CanBeToggled() )
333 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
337 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
350 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
352 OSStatus result
= eventNotHandledErr
;
354 switch ( GetEventClass( event
) )
356 case kEventClassToolbarItem
:
357 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
367 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
371 bool wxToolBarTool::DoEnable( bool enable
)
375 GetControl()->Enable( enable
);
377 else if ( IsButton() )
379 #if wxMAC_USE_NATIVE_TOOLBAR
380 if ( m_toolbarItemRef
!= NULL
)
381 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
384 if ( m_controlHandle
!= NULL
)
386 #if TARGET_API_MAC_OSX
388 EnableControl( m_controlHandle
);
390 DisableControl( m_controlHandle
);
393 ActivateControl( m_controlHandle
);
395 DeactivateControl( m_controlHandle
);
403 void wxToolBarTool::SetPosition( const wxPoint
& position
)
408 int mac_x
= position
.x
;
409 int mac_y
= position
.y
;
414 GetControlBounds( m_controlHandle
, &contrlRect
);
415 int former_mac_x
= contrlRect
.left
;
416 int former_mac_y
= contrlRect
.top
;
417 GetToolBar()->GetToolSize();
419 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
421 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
424 else if ( IsControl() )
426 // embedded native controls are moved by the OS
427 #if wxMAC_USE_NATIVE_TOOLBAR
428 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
431 GetControl()->Move( position
);
438 GetControlBounds( m_controlHandle
, &contrlRect
);
439 int former_mac_x
= contrlRect
.left
;
440 int former_mac_y
= contrlRect
.top
;
442 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
443 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
447 void wxToolBarTool::UpdateToggleImage( bool toggle
)
451 int w
= m_bmpNormal
.GetWidth();
452 int h
= m_bmpNormal
.GetHeight();
453 wxBitmap
bmp( w
, h
);
456 dc
.SelectObject( bmp
);
457 dc
.SetPen( wxPen(*wxBLACK
) );
458 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
459 dc
.DrawRectangle( 0, 0, w
, h
);
460 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
461 dc
.SelectObject( wxNullBitmap
);
462 ControlButtonContentInfo info
;
463 wxMacCreateBitmapButton( &info
, bmp
);
464 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
465 #if wxMAC_USE_NATIVE_TOOLBAR
466 if (m_toolbarItemRef
!= NULL
)
468 ControlButtonContentInfo info2
;
469 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
470 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
471 wxMacReleaseBitmapButton( &info2
);
474 wxMacReleaseBitmapButton( &info
);
478 ControlButtonContentInfo info
;
479 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
480 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
481 #if wxMAC_USE_NATIVE_TOOLBAR
482 if (m_toolbarItemRef
!= NULL
)
484 ControlButtonContentInfo info2
;
485 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
486 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
487 wxMacReleaseBitmapButton( &info2
);
490 wxMacReleaseBitmapButton( &info
);
493 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
495 m_controlHandle
, 0, kControlIconTransformTag
,
496 sizeof(transform
), (Ptr
)&transform
);
497 HIViewSetNeedsDisplay( m_controlHandle
, true );
501 wxToolBarTool::wxToolBarTool(
504 const wxString
& label
,
505 const wxBitmap
& bmpNormal
,
506 const wxBitmap
& bmpDisabled
,
508 wxObject
*clientData
,
509 const wxString
& shortHelp
,
510 const wxString
& longHelp
)
513 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
514 clientData
, shortHelp
, longHelp
)
520 #pragma mark Toolbar Implementation
522 wxToolBarToolBase
*wxToolBar::CreateTool(
524 const wxString
& label
,
525 const wxBitmap
& bmpNormal
,
526 const wxBitmap
& bmpDisabled
,
528 wxObject
*clientData
,
529 const wxString
& shortHelp
,
530 const wxString
& longHelp
)
532 return new wxToolBarTool(
533 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
534 clientData
, shortHelp
, longHelp
);
538 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
540 return new wxToolBarTool(this, control
, label
);
543 void wxToolBar::Init()
547 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
548 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
550 #if wxMAC_USE_NATIVE_TOOLBAR
551 m_macHIToolbarRef
= NULL
;
552 m_macUsesNativeToolbar
= false;
556 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
558 const EventTypeSpec kEvents
[] =
560 { kEventClassHIObject
, kEventHIObjectConstruct
},
561 { kEventClassHIObject
, kEventHIObjectInitialize
},
562 { kEventClassHIObject
, kEventHIObjectDestruct
},
564 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
567 const EventTypeSpec kViewEvents
[] =
569 { kEventClassControl
, kEventControlGetSizeConstraints
}
572 struct ControlToolbarItem
574 HIToolbarItemRef toolbarItem
;
576 wxSize lastValidSize
;
579 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
581 OSStatus result
= eventNotHandledErr
;
582 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
584 switch ( GetEventClass( inEvent
) )
586 case kEventClassHIObject
:
587 switch ( GetEventKind( inEvent
) )
589 case kEventHIObjectConstruct
:
591 HIObjectRef toolbarItem
;
592 ControlToolbarItem
* item
;
594 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
595 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
597 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
598 item
->toolbarItem
= toolbarItem
;
599 item
->lastValidSize
= wxSize(-1,-1);
600 item
->viewRef
= NULL
;
602 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
608 case kEventHIObjectInitialize
:
609 result
= CallNextEventHandler( inCallRef
, inEvent
);
610 if ( result
== noErr
)
613 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
614 sizeof( CFTypeRef
), NULL
, &data
);
618 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
619 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
621 object
->viewRef
= (HIViewRef
) viewRef
;
622 // make sure we keep that control during our lifetime
623 CFRetain( object
->viewRef
) ;
625 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
626 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
631 case kEventHIObjectDestruct
:
633 HIViewRef viewRef
= object
->viewRef
;
634 if( viewRef
&& IsValidControlHandle( viewRef
) )
636 // depending whether the wxControl corresponding to this HIView has already been destroyed or
637 // not, ref counts differ, so we cannot assert a special value
638 CFIndex count
= CFGetRetainCount( viewRef
) ;
639 wxASSERT_MSG( count
>=1 , wxT("Reference Count of native tool was illegal before removal") );
641 CFRelease( viewRef
) ;
650 case kEventClassToolbarItem
:
651 switch ( GetEventKind( inEvent
) )
653 case kEventToolbarItemCreateCustomView
:
655 HIViewRef viewRef
= object
->viewRef
;
656 HIViewRemoveFromSuperview( viewRef
) ;
657 HIViewSetVisible(viewRef
, true) ;
658 CFRetain( viewRef
) ;
659 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
665 case kEventClassControl
:
666 switch ( GetEventKind( inEvent
) )
668 case kEventControlGetSizeConstraints
:
670 wxWindow
* wxwindow
= wxFindControlFromMacControl(object
->viewRef
) ;
673 // during toolbar layout the native window sometimes gets negative sizes,
674 // sometimes it just gets shrunk behind our back, so in order to avoid
675 // ever shrinking more, once a valid size is captured, we keep it
677 wxSize sz
= object
->lastValidSize
;
678 if ( sz
.x
<= 0 || sz
.y
<= 0 )
680 sz
= wxwindow
->GetSize() ;
681 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
682 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
683 if ( sz
.x
> 0 && sz
.y
> 0 )
684 object
->lastValidSize
= sz
;
689 // Extra width to avoid edge of combobox being cut off
693 min
.width
= max
.width
= sz
.x
;
694 min
.height
= max
.height
= sz
.y
;
696 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
697 sizeof( HISize
), &min
);
699 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
700 sizeof( HISize
), &max
);
712 void RegisterControlToolbarItemClass()
714 static bool sRegistered
;
718 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
719 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
725 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
727 RegisterControlToolbarItemClass();
731 UInt32 options
= kHIToolbarItemAllowDuplicates
;
732 HIToolbarItemRef result
= NULL
;
734 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
735 require_noerr( err
, CantCreateEvent
);
737 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
738 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
741 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
743 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
746 ReleaseEvent( event
);
751 #if wxMAC_USE_NATIVE_TOOLBAR
752 static const EventTypeSpec kToolbarEvents
[] =
754 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
755 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
756 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
759 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
761 void* WXUNUSED(inUserData
))
763 OSStatus result
= eventNotHandledErr
;
765 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
766 CFMutableArrayRef array
;
768 switch ( GetEventKind( inEvent
) )
770 case kEventToolbarGetDefaultIdentifiers
:
772 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
773 sizeof( CFMutableArrayRef
), NULL
, &array
);
774 // not implemented yet
775 // GetToolbarDefaultItems( array );
780 case kEventToolbarGetAllowedIdentifiers
:
782 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
783 sizeof( CFMutableArrayRef
), NULL
, &array
);
784 // not implemented yet
785 // GetToolbarAllowedItems( array );
789 case kEventToolbarCreateItemWithIdentifier
:
791 HIToolbarItemRef item
= NULL
;
792 CFTypeRef data
= NULL
;
793 CFStringRef identifier
= NULL
;
795 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
796 sizeof( CFStringRef
), NULL
, &identifier
);
798 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
799 sizeof( CFTypeRef
), NULL
, &data
);
801 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
803 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
806 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
807 sizeof( HIToolbarItemRef
), &item
);
817 #endif // wxMAC_USE_NATIVE_TOOLBAR
819 // also for the toolbar we have the dual implementation:
820 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
822 bool wxToolBar::Create(
828 const wxString
& name
)
830 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
835 OSStatus err
= noErr
;
837 #if wxMAC_USE_NATIVE_TOOLBAR
838 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
840 wxString labelStr
= wxString::Format( wxT("%p"), this );
841 err
= HIToolbarCreate(
842 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
843 (HIToolbarRef
*) &m_macHIToolbarRef
);
845 if (m_macHIToolbarRef
!= NULL
)
847 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macHIToolbarRef
), ToolbarDelegateHandler
,
848 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
850 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
851 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
853 if ( style
& wxTB_NOICONS
)
854 mode
= kHIToolbarDisplayModeLabelOnly
;
855 else if ( style
& wxTB_TEXT
)
856 mode
= kHIToolbarDisplayModeIconAndLabel
;
858 mode
= kHIToolbarDisplayModeIconOnly
;
860 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
861 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
864 #endif // wxMAC_USE_NATIVE_TOOLBAR
866 return (err
== noErr
);
869 wxToolBar::~wxToolBar()
871 #if wxMAC_USE_NATIVE_TOOLBAR
872 if (m_macHIToolbarRef
!= NULL
)
874 // if this is the installed toolbar, then deinstall it
875 if (m_macUsesNativeToolbar
)
876 MacInstallNativeToolbar( false );
878 CFIndex count
= CFGetRetainCount( m_macHIToolbarRef
) ;
879 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
880 if ( UMAGetSystemVersion() < 0x1050 )
882 wxASSERT_MSG( count
== 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
884 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
885 m_macHIToolbarRef
= NULL
;
890 bool wxToolBar::Show( bool show
)
892 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
893 bool bResult
= (tlw
!= NULL
);
897 #if wxMAC_USE_NATIVE_TOOLBAR
898 bool ownToolbarInstalled
= false;
899 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
900 if (ownToolbarInstalled
)
902 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
904 ShowHideWindowToolbar( tlw
, show
, false );
907 bResult
= wxToolBarBase::Show( show
);
910 bResult
= wxToolBarBase::Show( show
);
917 bool wxToolBar::IsShown() const
921 #if wxMAC_USE_NATIVE_TOOLBAR
922 bool ownToolbarInstalled
;
924 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
925 if (ownToolbarInstalled
)
927 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
928 bResult
= IsWindowToolbarVisible( tlw
);
931 bResult
= wxToolBarBase::IsShown();
934 bResult
= wxToolBarBase::IsShown();
940 void wxToolBar::DoGetSize( int *width
, int *height
) const
942 #if wxMAC_USE_NATIVE_TOOLBAR
944 bool ownToolbarInstalled
;
946 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
947 if ( ownToolbarInstalled
)
949 // TODO: is this really a control ?
950 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
952 *width
= boundsR
.right
- boundsR
.left
;
953 if ( height
!= NULL
)
954 *height
= boundsR
.bottom
- boundsR
.top
;
957 wxToolBarBase::DoGetSize( width
, height
);
960 wxToolBarBase::DoGetSize( width
, height
);
964 wxSize
wxToolBar::DoGetBestSize() const
968 DoGetSize( &width
, &height
);
970 return wxSize( width
, height
);
973 void wxToolBar::SetWindowStyleFlag( long style
)
975 wxToolBarBase::SetWindowStyleFlag( style
);
977 #if wxMAC_USE_NATIVE_TOOLBAR
978 if (m_macHIToolbarRef
!= NULL
)
980 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
982 if ( style
& wxTB_NOICONS
)
983 mode
= kHIToolbarDisplayModeLabelOnly
;
984 else if ( style
& wxTB_TEXT
)
985 mode
= kHIToolbarDisplayModeIconAndLabel
;
987 mode
= kHIToolbarDisplayModeIconOnly
;
989 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
994 #if wxMAC_USE_NATIVE_TOOLBAR
995 bool wxToolBar::MacWantsNativeToolbar()
997 return m_macUsesNativeToolbar
;
1000 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
1002 bool bResultV
= false;
1004 if (ownToolbarInstalled
!= NULL
)
1005 *ownToolbarInstalled
= false;
1007 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1010 HIToolbarRef curToolbarRef
= NULL
;
1011 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1012 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
1013 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
1014 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
1020 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
1022 bool bResult
= false;
1024 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
1027 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
1030 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
1034 // check the existing toolbar
1035 HIToolbarRef curToolbarRef
= NULL
;
1036 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
1038 curToolbarRef
= NULL
;
1040 m_macUsesNativeToolbar
= usesNative
;
1042 if (m_macUsesNativeToolbar
)
1044 // only install toolbar if there isn't one installed already
1045 if (curToolbarRef
== NULL
)
1049 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
1050 ShowHideWindowToolbar( tlw
, true, false );
1051 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1052 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1054 Rect r
= { 0, 0, 0, 0 };
1055 m_peer
->SetRect( &r
);
1056 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1057 m_peer
->SetVisibility( false, true );
1058 wxToolBarBase::Show( false );
1063 // only deinstall toolbar if this is the installed one
1064 if (m_macHIToolbarRef
== curToolbarRef
)
1068 ShowHideWindowToolbar( tlw
, false, false );
1069 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1070 SetWindowToolbar( tlw
, NULL
);
1072 m_peer
->SetVisibility( true, true );
1077 InvalidateBestSize();
1079 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1084 bool wxToolBar::Realize()
1086 if (m_tools
.GetCount() == 0)
1092 int maxToolWidth
= 0;
1093 int maxToolHeight
= 0;
1095 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1096 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1099 GetSize( &tw
, &th
);
1101 // find the maximum tool width and height
1102 wxToolBarTool
*tool
;
1103 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1106 tool
= (wxToolBarTool
*) node
->GetData();
1109 wxSize sz
= tool
->GetSize();
1111 if ( sz
.x
> maxToolWidth
)
1112 maxToolWidth
= sz
.x
;
1113 if ( sz
.y
> maxToolHeight
)
1114 maxToolHeight
= sz
.y
;
1117 node
= node
->GetNext();
1120 bool lastIsRadio
= false;
1121 bool curIsRadio
= false;
1123 #if wxMAC_USE_NATIVE_TOOLBAR
1124 CFIndex currentPosition
= 0;
1125 bool insertAll
= false;
1127 HIToolbarRef refTB
= (HIToolbarRef
)m_macHIToolbarRef
;
1130 node
= m_tools
.GetFirst();
1133 tool
= (wxToolBarTool
*) node
->GetData();
1136 node
= node
->GetNext();
1140 // set tool position:
1141 // for the moment just perform a single row/column alignment
1142 wxSize cursize
= tool
->GetSize();
1143 if ( x
+ cursize
.x
> maxWidth
)
1144 maxWidth
= x
+ cursize
.x
;
1145 if ( y
+ cursize
.y
> maxHeight
)
1146 maxHeight
= y
+ cursize
.y
;
1148 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1150 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1151 tool
->SetPosition( wxPoint(x1
, y
) );
1155 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1156 tool
->SetPosition( wxPoint(x
, y1
) );
1159 // update the item positioning state
1160 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1161 y
+= cursize
.y
+ kwxMacToolSpacing
;
1163 x
+= cursize
.x
+ kwxMacToolSpacing
;
1165 #if wxMAC_USE_NATIVE_TOOLBAR
1166 // install in native HIToolbar
1169 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1170 if ( hiItemRef
!= NULL
)
1172 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1174 OSStatus err
= noErr
;
1179 // if this is the first tool that gets newly inserted or repositioned
1180 // first remove all 'old' tools from here to the right, because of this
1181 // all following tools will have to be reinserted (insertAll).
1182 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1184 node2
= node2
->GetPrevious() )
1186 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1188 const long idx
= tool2
->GetIndex();
1191 if ( tool2
->IsControl() )
1193 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1194 wxASSERT_MSG( count
== 3 || count
== 2 , wxT("Reference Count of native tool was illegal before removal") );
1195 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1197 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1200 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1203 if ( tool2
->IsControl() )
1205 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1206 wxASSERT_MSG( count
== 2 , wxT("Reference Count of native tool was not 2 after removal") );
1207 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1210 tool2
->SetIndex(-1);
1215 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1218 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1221 tool
->SetIndex( currentPosition
);
1222 if ( tool
->IsControl() )
1224 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1225 wxASSERT_MSG( count
== 3 || count
== 2, wxT("Reference Count of native tool was illegal after insertion") );
1226 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1235 // update radio button (and group) state
1236 lastIsRadio
= curIsRadio
;
1237 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1241 if ( tool
->IsToggled() )
1242 DoToggleTool( tool
, true );
1248 if ( tool
->Toggle( true ) )
1250 DoToggleTool( tool
, true );
1253 else if ( tool
->IsToggled() )
1255 if ( tool
->IsToggled() )
1256 DoToggleTool( tool
, true );
1258 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1261 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1262 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1265 if ( toggleTool
->Toggle( false ) )
1266 DoToggleTool( toggleTool
, false );
1268 nodePrev
= nodePrev
->GetPrevious();
1273 node
= node
->GetNext();
1276 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1278 // if not set yet, only one row
1279 if ( m_maxRows
<= 0 )
1282 m_minWidth
= maxWidth
;
1284 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1285 m_minHeight
= m_maxHeight
= maxHeight
;
1289 // if not set yet, have one column
1290 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1291 SetRows( GetToolsCount() );
1293 m_minHeight
= maxHeight
;
1295 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1296 m_minWidth
= m_maxWidth
= maxWidth
;
1300 // FIXME: should this be OSX-only?
1302 bool wantNativeToolbar
, ownToolbarInstalled
;
1304 // attempt to install the native toolbar
1305 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1306 MacInstallNativeToolbar( wantNativeToolbar
);
1307 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1308 if (!ownToolbarInstalled
)
1310 SetSize( maxWidth
, maxHeight
);
1311 InvalidateBestSize();
1315 SetSize( maxWidth
, maxHeight
);
1316 InvalidateBestSize();
1324 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1326 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1327 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1329 #if wxMAC_USE_NATIVE_TOOLBAR
1330 if (m_macHIToolbarRef
!= NULL
)
1332 int maxs
= wxMax( size
.x
, size
.y
);
1333 HIToolbarDisplaySize sizeSpec
;
1335 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1336 else if ( maxs
> 24 )
1337 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1339 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1341 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1346 // The button size is bigger than the bitmap size
1347 wxSize
wxToolBar::GetToolSize() const
1349 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1352 void wxToolBar::SetRows(int nRows
)
1354 // avoid resizing the frame uselessly
1355 if ( nRows
!= m_maxRows
)
1359 void wxToolBar::MacSuperChangedPosition()
1361 wxWindow::MacSuperChangedPosition();
1363 #if wxMAC_USE_NATIVE_TOOLBAR
1364 if (! m_macUsesNativeToolbar
)
1372 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1374 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1377 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1379 tool
->SetNormalBitmap(bitmap
);
1381 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1382 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1386 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1388 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1391 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1393 tool
->SetDisabledBitmap(bitmap
);
1395 // TODO: what to do for this one?
1399 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1401 wxToolBarTool
*tool
;
1402 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1405 tool
= (wxToolBarTool
*)node
->GetData();
1408 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1409 if ( r
.Contains( wxPoint( x
, y
) ) )
1413 node
= node
->GetNext();
1416 return (wxToolBarToolBase
*)NULL
;
1419 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1421 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1423 return tool
->GetShortHelp();
1425 return wxEmptyString
;
1428 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1431 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1434 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1436 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1437 if ( ( tool
!= NULL
) && tool
->IsButton() )
1438 tool
->UpdateToggleImage( toggle
);
1441 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1443 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1447 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1448 wxSize toolSize
= GetToolSize();
1449 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1450 ControlRef controlHandle
= NULL
;
1452 tool
->Attach( this );
1454 #if wxMAC_USE_NATIVE_TOOLBAR
1455 wxString label
= tool
->GetLabel();
1456 if (m_macHIToolbarRef
&& !label
.empty() )
1458 // strip mnemonics from the label for compatibility
1459 // with the usual labels in wxStaticText sense
1460 label
= wxStripMenuCodes(label
);
1462 #endif // wxMAC_USE_NATIVE_TOOLBAR
1464 switch (tool
->GetStyle())
1466 case wxTOOL_STYLE_SEPARATOR
:
1468 wxASSERT( tool
->GetControlHandle() == NULL
);
1471 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1472 toolrect
.bottom
= toolSize
.y
;
1474 toolrect
.right
= toolSize
.x
;
1476 // in flat style we need a visual separator
1477 #if wxMAC_USE_NATIVE_TOOLBAR
1478 if (m_macHIToolbarRef
!= NULL
)
1480 HIToolbarItemRef item
;
1481 err
= HIToolbarItemCreate(
1482 kHIToolbarSeparatorIdentifier
,
1483 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1486 tool
->SetToolbarItemRef( item
);
1490 #endif // wxMAC_USE_NATIVE_TOOLBAR
1492 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1493 tool
->SetControlHandle( controlHandle
);
1497 case wxTOOL_STYLE_BUTTON
:
1499 wxASSERT( tool
->GetControlHandle() == NULL
);
1500 ControlButtonContentInfo info
;
1501 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1503 if ( UMAGetSystemVersion() >= 0x1000)
1505 // contrary to the docs this control only works with iconrefs
1506 ControlButtonContentInfo info
;
1507 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1508 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1509 wxMacReleaseBitmapButton( &info
);
1513 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1514 if ( tool
->CanBeToggled() )
1515 behaviour
|= kControlBehaviorToggles
;
1516 err
= CreateBevelButtonControl( window
,
1517 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1518 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1521 #if wxMAC_USE_NATIVE_TOOLBAR
1522 if (m_macHIToolbarRef
!= NULL
)
1524 HIToolbarItemRef item
;
1525 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1526 err
= HIToolbarItemCreate(
1527 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1528 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1531 ControlButtonContentInfo info2
;
1532 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1534 InstallEventHandler(
1535 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1536 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1537 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(label
, m_font
.GetEncoding()) );
1538 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1539 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1540 tool
->SetToolbarItemRef( item
);
1542 wxMacReleaseBitmapButton( &info2
);
1547 #endif // wxMAC_USE_NATIVE_TOOLBAR
1549 wxMacReleaseBitmapButton( &info
);
1552 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1553 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1556 InstallControlEventHandler(
1557 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1558 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1560 tool
->SetControlHandle( controlHandle
);
1564 case wxTOOL_STYLE_CONTROL
:
1566 #if wxMAC_USE_NATIVE_TOOLBAR
1567 if (m_macHIToolbarRef
!= NULL
)
1569 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1570 HIToolbarItemRef item
;
1571 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1572 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1573 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macHIToolbarRef
,kControlToolbarItemClassID
,
1578 tool
->SetToolbarItemRef( item
);
1588 // right now there's nothing to do here
1598 if ( controlHandle
)
1600 ControlRef container
= (ControlRef
) GetHandle();
1601 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1603 UMAShowControl( controlHandle
);
1604 ::EmbedControl( controlHandle
, container
);
1607 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1608 tool
->UpdateToggleImage( true );
1610 // nothing special to do here - we relayout in Realize() later
1611 InvalidateBestSize();
1615 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1618 return (err
== noErr
);
1621 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1623 wxFAIL_MSG( wxT("not implemented") );
1626 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1628 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1629 wxToolBarToolsList::compatibility_iterator node
;
1630 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1632 wxToolBarToolBase
*tool2
= node
->GetData();
1633 if ( tool2
== tool
)
1635 // let node point to the next node in the list
1636 node
= node
->GetNext();
1642 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1646 #if wxMAC_USE_NATIVE_TOOLBAR
1647 CFIndex removeIndex
= tool
->GetIndex();
1650 #if wxMAC_USE_NATIVE_TOOLBAR
1651 if (m_macHIToolbarRef
!= NULL
)
1653 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1655 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1656 tool
->SetIndex( -1 );
1660 switch ( tool
->GetStyle() )
1662 case wxTOOL_STYLE_CONTROL
:
1663 if ( tool
->GetControl() )
1664 tool
->GetControl()->Destroy();
1667 case wxTOOL_STYLE_BUTTON
:
1668 case wxTOOL_STYLE_SEPARATOR
:
1675 tool
->ClearControl();
1677 // and finally reposition all the controls after this one
1679 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1681 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1682 wxPoint pt
= tool2
->GetPosition();
1684 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1689 tool2
->SetPosition( pt
);
1691 #if wxMAC_USE_NATIVE_TOOLBAR
1692 if (m_macHIToolbarRef
!= NULL
)
1694 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1695 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1700 InvalidateBestSize();
1705 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1707 #if wxMAC_USE_NATIVE_TOOLBAR
1708 if ( m_macUsesNativeToolbar
)
1720 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1722 #if wxMAC_USE_CORE_GRAPHICS
1723 if ( !drawMetalTheme
)
1725 HIThemePlacardDrawInfo info
;
1726 memset( &info
, 0, sizeof(info
) );
1728 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1730 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1731 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1732 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1736 // leave the background as it is (striped or metal)
1741 const bool drawBorder
= true;
1745 wxMacPortSetter
helper( &dc
);
1747 if ( !drawMetalTheme
)
1749 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1750 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1753 if ( toolbarrect
.left
< 0 )
1754 toolbarrect
.left
= 0;
1755 if ( toolbarrect
.top
< 0 )
1756 toolbarrect
.top
= 0;
1759 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1763 #if TARGET_API_MAC_OSX
1764 HIRect hiToolbarrect
= CGRectMake(
1765 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1766 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1767 CGContextRef cgContext
;
1770 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1771 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1773 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1774 CGContextScaleCTM( cgContext
, 1, -1 );
1776 HIThemeBackgroundDrawInfo drawInfo
;
1777 drawInfo
.version
= 0;
1778 drawInfo
.state
= kThemeStateActive
;
1779 drawInfo
.kind
= kThemeBackgroundMetal
;
1780 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1783 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1793 #endif // wxUSE_TOOLBAR