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"
29 const short kwxMacToolBarToolDefaultWidth
= 16;
30 const short kwxMacToolBarToolDefaultHeight
= 16;
31 const short kwxMacToolBarTopMargin
= 4;
32 const short kwxMacToolBarLeftMargin
= 4;
33 const short kwxMacToolBorder
= 0;
34 const short kwxMacToolSpacing
= 6;
36 const short kwxMacToolBarToolDefaultWidth
= 24;
37 const short kwxMacToolBarToolDefaultHeight
= 22;
38 const short kwxMacToolBarTopMargin
= 2;
39 const short kwxMacToolBarLeftMargin
= 2;
40 const short kwxMacToolBorder
= 4;
41 const short kwxMacToolSpacing
= 0;
45 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
47 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
48 EVT_PAINT( wxToolBar::OnPaint
)
53 #pragma mark Tool Implementation
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
62 // when embedding native controls in the native toolbar we must make sure the
63 // control does not get deleted behind our backs, so the retain count gets increased
64 // (after creation it is 1), first be the creation of the custom HIToolbarItem wrapper
65 // object, and second by the code 'creating' the custom HIView (which is the same as the
66 // already existing native control, therefore we just increase the ref count)
67 // when this view is removed from the native toolbar its count gets decremented again
68 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
69 // so in the end the control lives with a refcount of one and can be disposed of by the
70 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
71 // so we can only test against a range, not a specific value of the refcount.
73 class wxToolBarTool
: public wxToolBarToolBase
79 const wxString
& label
,
80 const wxBitmap
& bmpNormal
,
81 const wxBitmap
& bmpDisabled
,
84 const wxString
& shortHelp
,
85 const wxString
& longHelp
);
87 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
88 : wxToolBarToolBase(tbar
, control
, label
)
92 SetControlHandle( (ControlRef
) control
->GetHandle() );
95 virtual ~wxToolBarTool()
100 WXWidget
GetControlHandle()
102 return (WXWidget
) m_controlHandle
;
105 void SetControlHandle( ControlRef handle
)
107 m_controlHandle
= handle
;
110 void SetPosition( const wxPoint
& position
);
114 if ( m_controlHandle
)
117 DisposeControl( m_controlHandle
);
120 // the embedded control is not under the responsibility of the tool, it will be disposed of in the
121 // proper wxControl destructor
122 wxASSERT( IsValidControlHandle(GetControl()->GetPeer()->GetControlRef() )) ;
124 m_controlHandle
= NULL
;
128 #if wxMAC_USE_NATIVE_TOOLBAR
129 if ( m_toolbarItemRef
)
131 CFIndex count
= CFGetRetainCount( m_toolbarItemRef
) ;
132 // different behaviour under Leopard
133 if ( UMAGetSystemVersion() < 0x1050 )
135 wxASSERT_MSG( count
== 1 , wxT("Reference Count of native tool was not 1 in wxToolBarTool destructor") );
137 wxTheApp
->MacAddToAutorelease(m_toolbarItemRef
);
138 CFRelease(m_toolbarItemRef
);
139 m_toolbarItemRef
= NULL
;
144 wxSize
GetSize() const
150 curSize
= GetControl()->GetSize();
152 else if ( IsButton() )
154 curSize
= GetToolBar()->GetToolSize();
159 curSize
= GetToolBar()->GetToolSize();
160 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
169 wxPoint
GetPosition() const
171 return wxPoint( m_x
, m_y
);
174 bool DoEnable( bool enable
);
176 void UpdateToggleImage( bool toggle
);
178 #if wxMAC_USE_NATIVE_TOOLBAR
179 void SetToolbarItemRef( HIToolbarItemRef ref
)
181 if ( m_controlHandle
)
182 HideControl( m_controlHandle
);
183 if ( m_toolbarItemRef
)
184 CFRelease( m_toolbarItemRef
);
186 m_toolbarItemRef
= ref
;
187 if ( m_toolbarItemRef
)
192 f
= GetToolBar()->GetFont();
194 enc
= f
.GetEncoding();
196 enc
= wxFont::GetDefaultEncoding();
198 HIToolbarItemSetHelpText(
200 wxMacCFStringHolder( GetShortHelp(), enc
),
201 wxMacCFStringHolder( GetLongHelp(), enc
) );
205 HIToolbarItemRef
GetToolbarItemRef() const
207 return m_toolbarItemRef
;
210 void SetIndex( CFIndex idx
)
215 CFIndex
GetIndex() const
224 m_controlHandle
= NULL
;
226 #if wxMAC_USE_NATIVE_TOOLBAR
227 m_toolbarItemRef
= NULL
;
232 ControlRef m_controlHandle
;
236 #if wxMAC_USE_NATIVE_TOOLBAR
237 HIToolbarItemRef m_toolbarItemRef
;
238 // position in its toolbar, -1 means not inserted
243 static const EventTypeSpec eventList
[] =
245 { kEventClassControl
, kEventControlHit
},
247 { kEventClassControl
, kEventControlHitTest
},
251 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
253 OSStatus result
= eventNotHandledErr
;
254 ControlRef controlRef
;
255 wxMacCarbonEvent
cEvent( event
);
257 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
259 switch ( GetEventKind( event
) )
261 case kEventControlHit
:
263 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
264 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
265 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
270 shouldToggle
= !tbartool
->IsToggled();
272 shouldToggle
= (GetControl32BitValue( (ControlRef
)(tbartool
->GetControlHandle()) ) != 0);
275 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
278 if (tbartool
!= NULL
)
279 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
);
307 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
309 OSStatus result
= eventNotHandledErr
;
311 switch ( GetEventClass( event
) )
313 case kEventClassControl
:
314 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
324 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
326 #if wxMAC_USE_NATIVE_TOOLBAR
328 static const EventTypeSpec toolBarEventList
[] =
330 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
333 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
), EventRef event
, void *data
)
335 OSStatus result
= eventNotHandledErr
;
337 switch ( GetEventKind( event
) )
339 case kEventToolbarItemPerformAction
:
341 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
342 if ( tbartool
!= NULL
)
344 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
345 int toolID
= tbartool
->GetId();
347 if ( tbartool
->CanBeToggled() )
350 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
354 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
367 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
369 OSStatus result
= eventNotHandledErr
;
371 switch ( GetEventClass( event
) )
373 case kEventClassToolbarItem
:
374 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
384 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
388 bool wxToolBarTool::DoEnable( bool enable
)
392 GetControl()->Enable( enable
);
394 else if ( IsButton() )
396 #if wxMAC_USE_NATIVE_TOOLBAR
397 if ( m_toolbarItemRef
!= NULL
)
398 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
401 if ( m_controlHandle
!= NULL
)
403 #if TARGET_API_MAC_OSX
405 EnableControl( m_controlHandle
);
407 DisableControl( m_controlHandle
);
410 ActivateControl( m_controlHandle
);
412 DeactivateControl( 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 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
441 else if ( IsControl() )
443 // embedded native controls are moved by the OS
444 #if wxMAC_USE_NATIVE_TOOLBAR
445 if ( ((wxToolBar
*)GetToolBar())->MacWantsNativeToolbar() == false )
448 GetControl()->Move( position
);
456 GetControlBounds( m_controlHandle
, &contrlRect
);
457 int former_mac_x
= contrlRect
.left
;
458 int former_mac_y
= contrlRect
.top
;
460 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
461 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
466 void wxToolBarTool::UpdateToggleImage( bool toggle
)
471 int w
= m_bmpNormal
.GetWidth();
472 int h
= m_bmpNormal
.GetHeight();
473 wxBitmap
bmp( w
, h
);
476 dc
.SelectObject( bmp
);
477 dc
.SetPen( wxPen(*wxBLACK
) );
478 dc
.SetBrush( wxBrush( *wxLIGHT_GREY
));
479 dc
.DrawRectangle( 0, 0, w
, h
);
480 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
481 dc
.SelectObject( wxNullBitmap
);
482 ControlButtonContentInfo info
;
483 wxMacCreateBitmapButton( &info
, bmp
);
484 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
485 #if wxMAC_USE_NATIVE_TOOLBAR
486 if (m_toolbarItemRef
!= NULL
)
488 ControlButtonContentInfo info2
;
489 wxMacCreateBitmapButton( &info2
, bmp
, kControlContentCGImageRef
);
490 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
491 wxMacReleaseBitmapButton( &info2
);
494 wxMacReleaseBitmapButton( &info
);
498 ControlButtonContentInfo info
;
499 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
500 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
501 #if wxMAC_USE_NATIVE_TOOLBAR
502 if (m_toolbarItemRef
!= NULL
)
504 ControlButtonContentInfo info2
;
505 wxMacCreateBitmapButton( &info2
, m_bmpNormal
, kControlContentCGImageRef
);
506 HIToolbarItemSetImage( m_toolbarItemRef
, info2
.u
.imageRef
);
507 wxMacReleaseBitmapButton( &info2
);
510 wxMacReleaseBitmapButton( &info
);
513 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
515 m_controlHandle
, 0, kControlIconTransformTag
,
516 sizeof(transform
), (Ptr
)&transform
);
517 HIViewSetNeedsDisplay( m_controlHandle
, true );
520 ::SetControl32BitValue( m_controlHandle
, toggle
);
524 wxToolBarTool::wxToolBarTool(
527 const wxString
& label
,
528 const wxBitmap
& bmpNormal
,
529 const wxBitmap
& bmpDisabled
,
531 wxObject
*clientData
,
532 const wxString
& shortHelp
,
533 const wxString
& longHelp
)
536 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
537 clientData
, shortHelp
, longHelp
)
543 #pragma mark Toolbar Implementation
545 wxToolBarToolBase
*wxToolBar::CreateTool(
547 const wxString
& label
,
548 const wxBitmap
& bmpNormal
,
549 const wxBitmap
& bmpDisabled
,
551 wxObject
*clientData
,
552 const wxString
& shortHelp
,
553 const wxString
& longHelp
)
555 return new wxToolBarTool(
556 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
557 clientData
, shortHelp
, longHelp
);
561 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
563 return new wxToolBarTool(this, control
, label
);
566 void wxToolBar::Init()
570 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
571 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
573 #if wxMAC_USE_NATIVE_TOOLBAR
574 m_macHIToolbarRef
= NULL
;
575 m_macUsesNativeToolbar
= false;
579 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
581 const EventTypeSpec kEvents
[] =
583 { kEventClassHIObject
, kEventHIObjectConstruct
},
584 { kEventClassHIObject
, kEventHIObjectInitialize
},
585 { kEventClassHIObject
, kEventHIObjectDestruct
},
587 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
590 const EventTypeSpec kViewEvents
[] =
592 { kEventClassControl
, kEventControlGetSizeConstraints
}
595 struct ControlToolbarItem
597 HIToolbarItemRef toolbarItem
;
599 wxSize lastValidSize
;
602 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
604 OSStatus result
= eventNotHandledErr
;
605 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
607 switch ( GetEventClass( inEvent
) )
609 case kEventClassHIObject
:
610 switch ( GetEventKind( inEvent
) )
612 case kEventHIObjectConstruct
:
614 HIObjectRef toolbarItem
;
615 ControlToolbarItem
* item
;
617 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
618 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
620 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
621 item
->toolbarItem
= toolbarItem
;
622 item
->lastValidSize
= wxSize(-1,-1);
623 item
->viewRef
= NULL
;
625 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
631 case kEventHIObjectInitialize
:
632 result
= CallNextEventHandler( inCallRef
, inEvent
);
633 if ( result
== noErr
)
636 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
637 sizeof( CFTypeRef
), NULL
, &data
);
641 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
642 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
644 object
->viewRef
= (HIViewRef
) viewRef
;
645 // make sure we keep that control during our lifetime
646 CFRetain( object
->viewRef
) ;
648 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
649 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
));
654 case kEventHIObjectDestruct
:
656 HIViewRef viewRef
= object
->viewRef
;
657 if( viewRef
&& IsValidControlHandle( viewRef
) )
659 // depending whether the wxControl corresponding to this HIView has already been destroyed or
660 // not, ref counts differ, so we cannot assert a special value
661 CFIndex count
= CFGetRetainCount( viewRef
) ;
662 wxASSERT_MSG( count
>=1 , wxT("Reference Count of native tool was illegal before removal") );
664 CFRelease( viewRef
) ;
673 case kEventClassToolbarItem
:
674 switch ( GetEventKind( inEvent
) )
676 case kEventToolbarItemCreateCustomView
:
678 HIViewRef viewRef
= object
->viewRef
;
679 HIViewRemoveFromSuperview( viewRef
) ;
680 HIViewSetVisible(viewRef
, true) ;
681 CFRetain( viewRef
) ;
682 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
688 case kEventClassControl
:
689 switch ( GetEventKind( inEvent
) )
691 case kEventControlGetSizeConstraints
:
693 wxWindow
* wxwindow
= wxFindControlFromMacControl(object
->viewRef
) ;
696 // during toolbar layout the native window sometimes gets negative sizes,
697 // sometimes it just gets shrunk behind our back, so in order to avoid
698 // ever shrinking more, once a valid size is captured, we keep it
700 wxSize sz
= object
->lastValidSize
;
701 if ( sz
.x
<= 0 || sz
.y
<= 0 )
703 sz
= wxwindow
->GetSize() ;
704 sz
.x
-= wxwindow
->MacGetLeftBorderSize() + wxwindow
->MacGetRightBorderSize();
705 sz
.y
-= wxwindow
->MacGetTopBorderSize() + wxwindow
->MacGetBottomBorderSize();
706 if ( sz
.x
> 0 && sz
.y
> 0 )
707 object
->lastValidSize
= sz
;
712 // Extra width to avoid edge of combobox being cut off
716 min
.width
= max
.width
= sz
.x
;
717 min
.height
= max
.height
= sz
.y
;
719 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
720 sizeof( HISize
), &min
);
722 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
723 sizeof( HISize
), &max
);
735 void RegisterControlToolbarItemClass()
737 static bool sRegistered
;
741 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
742 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
748 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
750 RegisterControlToolbarItemClass();
754 UInt32 options
= kHIToolbarItemAllowDuplicates
;
755 HIToolbarItemRef result
= NULL
;
757 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
758 require_noerr( err
, CantCreateEvent
);
760 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
761 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
764 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
766 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
769 ReleaseEvent( event
);
774 #if wxMAC_USE_NATIVE_TOOLBAR
775 static const EventTypeSpec kToolbarEvents
[] =
777 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
778 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
779 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
782 static OSStatus
ToolbarDelegateHandler(EventHandlerCallRef
WXUNUSED(inCallRef
),
784 void* WXUNUSED(inUserData
))
786 OSStatus result
= eventNotHandledErr
;
788 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
789 CFMutableArrayRef array
;
791 switch ( GetEventKind( inEvent
) )
793 case kEventToolbarGetDefaultIdentifiers
:
795 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
796 sizeof( CFMutableArrayRef
), NULL
, &array
);
797 // not implemented yet
798 // GetToolbarDefaultItems( array );
803 case kEventToolbarGetAllowedIdentifiers
:
805 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
806 sizeof( CFMutableArrayRef
), NULL
, &array
);
807 // not implemented yet
808 // GetToolbarAllowedItems( array );
812 case kEventToolbarCreateItemWithIdentifier
:
814 HIToolbarItemRef item
= NULL
;
815 CFTypeRef data
= NULL
;
816 CFStringRef identifier
= NULL
;
818 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
819 sizeof( CFStringRef
), NULL
, &identifier
);
821 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
822 sizeof( CFTypeRef
), NULL
, &data
);
824 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
826 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
829 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
830 sizeof( HIToolbarItemRef
), &item
);
840 #endif // wxMAC_USE_NATIVE_TOOLBAR
842 // also for the toolbar we have the dual implementation:
843 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
845 bool wxToolBar::Create(
851 const wxString
& name
)
853 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
858 OSStatus err
= noErr
;
860 #if wxMAC_USE_NATIVE_TOOLBAR
861 if (parent
->IsKindOf(CLASSINFO(wxFrame
)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
863 wxString labelStr
= wxString::Format( wxT("%p"), this );
864 err
= HIToolbarCreate(
865 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
866 (HIToolbarRef
*) &m_macHIToolbarRef
);
868 if (m_macHIToolbarRef
!= NULL
)
870 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macHIToolbarRef
), ToolbarDelegateHandler
,
871 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
873 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
874 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
876 if ( style
& wxTB_NOICONS
)
877 mode
= kHIToolbarDisplayModeLabelOnly
;
878 else if ( style
& wxTB_TEXT
)
879 mode
= kHIToolbarDisplayModeIconAndLabel
;
881 mode
= kHIToolbarDisplayModeIconOnly
;
883 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
884 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
887 #endif // wxMAC_USE_NATIVE_TOOLBAR
889 return (err
== noErr
);
892 wxToolBar::~wxToolBar()
894 #if wxMAC_USE_NATIVE_TOOLBAR
895 if (m_macHIToolbarRef
!= NULL
)
897 // if this is the installed toolbar, then deinstall it
898 if (m_macUsesNativeToolbar
)
899 MacInstallNativeToolbar( false );
901 CFIndex count
= CFGetRetainCount( m_macHIToolbarRef
) ;
902 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
903 if ( UMAGetSystemVersion() < 0x1050 )
905 wxASSERT_MSG( count
== 1 , wxT("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 wxMAC_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 wxMAC_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 wxMAC_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 wxMAC_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 wxMAC_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 Rect r
= { 0, 0, 0, 0 };
1078 m_peer
->SetRect( &r
);
1079 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1080 m_peer
->SetVisibility( false, true );
1081 wxToolBarBase::Show( false );
1086 // only deinstall toolbar if this is the installed one
1087 if (m_macHIToolbarRef
== curToolbarRef
)
1091 ShowHideWindowToolbar( tlw
, false, false );
1092 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1093 SetWindowToolbar( tlw
, NULL
);
1095 m_peer
->SetVisibility( true, true );
1100 InvalidateBestSize();
1102 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1107 bool wxToolBar::Realize()
1109 if (m_tools
.GetCount() == 0)
1115 int maxToolWidth
= 0;
1116 int maxToolHeight
= 0;
1118 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1119 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1122 GetSize( &tw
, &th
);
1124 // find the maximum tool width and height
1125 wxToolBarTool
*tool
;
1126 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1129 tool
= (wxToolBarTool
*) node
->GetData();
1132 wxSize sz
= tool
->GetSize();
1134 if ( sz
.x
> maxToolWidth
)
1135 maxToolWidth
= sz
.x
;
1136 if ( sz
.y
> maxToolHeight
)
1137 maxToolHeight
= sz
.y
;
1140 node
= node
->GetNext();
1143 bool lastIsRadio
= false;
1144 bool curIsRadio
= false;
1146 #if wxMAC_USE_NATIVE_TOOLBAR
1147 CFIndex currentPosition
= 0;
1148 bool insertAll
= false;
1150 HIToolbarRef refTB
= (HIToolbarRef
)m_macHIToolbarRef
;
1153 node
= m_tools
.GetFirst();
1156 tool
= (wxToolBarTool
*) node
->GetData();
1159 node
= node
->GetNext();
1163 // set tool position:
1164 // for the moment just perform a single row/column alignment
1165 wxSize cursize
= tool
->GetSize();
1166 if ( x
+ cursize
.x
> maxWidth
)
1167 maxWidth
= x
+ cursize
.x
;
1168 if ( y
+ cursize
.y
> maxHeight
)
1169 maxHeight
= y
+ cursize
.y
;
1171 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1173 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1174 tool
->SetPosition( wxPoint(x1
, y
) );
1178 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1179 tool
->SetPosition( wxPoint(x
, y1
) );
1182 // update the item positioning state
1183 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1184 y
+= cursize
.y
+ kwxMacToolSpacing
;
1186 x
+= cursize
.x
+ kwxMacToolSpacing
;
1188 #if wxMAC_USE_NATIVE_TOOLBAR
1189 // install in native HIToolbar
1192 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1193 if ( hiItemRef
!= NULL
)
1195 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1197 OSStatus err
= noErr
;
1202 // if this is the first tool that gets newly inserted or repositioned
1203 // first remove all 'old' tools from here to the right, because of this
1204 // all following tools will have to be reinserted (insertAll).
1205 for ( wxToolBarToolsList::compatibility_iterator node2
= m_tools
.GetLast();
1207 node2
= node2
->GetPrevious() )
1209 wxToolBarTool
*tool2
= (wxToolBarTool
*) node2
->GetData();
1211 const long idx
= tool2
->GetIndex();
1214 if ( tool2
->IsControl() )
1216 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1217 wxASSERT_MSG( count
== 3 || count
== 2 , wxT("Reference Count of native tool was illegal before removal") );
1218 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1220 err
= HIToolbarRemoveItemAtIndex(refTB
, idx
);
1223 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1226 if ( tool2
->IsControl() )
1228 CFIndex count
= CFGetRetainCount( tool2
->GetControl()->GetPeer()->GetControlRef() ) ;
1229 wxASSERT_MSG( count
== 2 , wxT("Reference Count of native tool was not 2 after removal") );
1230 wxASSERT( IsValidControlHandle(tool2
->GetControl()->GetPeer()->GetControlRef() )) ;
1233 tool2
->SetIndex(-1);
1238 err
= HIToolbarInsertItemAtIndex( refTB
, hiItemRef
, currentPosition
);
1241 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1244 tool
->SetIndex( currentPosition
);
1245 if ( tool
->IsControl() )
1247 CFIndex count
= CFGetRetainCount( tool
->GetControl()->GetPeer()->GetControlRef() ) ;
1248 wxASSERT_MSG( count
== 3 || count
== 2, wxT("Reference Count of native tool was illegal after insertion") );
1249 wxASSERT( IsValidControlHandle(tool
->GetControl()->GetPeer()->GetControlRef() )) ;
1258 // update radio button (and group) state
1259 lastIsRadio
= curIsRadio
;
1260 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1264 if ( tool
->IsToggled() )
1265 DoToggleTool( tool
, true );
1271 if ( tool
->Toggle( true ) )
1273 DoToggleTool( tool
, true );
1276 else if ( tool
->IsToggled() )
1278 if ( tool
->IsToggled() )
1279 DoToggleTool( tool
, true );
1281 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1284 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1285 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1288 if ( toggleTool
->Toggle( false ) )
1289 DoToggleTool( toggleTool
, false );
1291 nodePrev
= nodePrev
->GetPrevious();
1296 node
= node
->GetNext();
1299 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1301 // if not set yet, only one row
1302 if ( m_maxRows
<= 0 )
1305 m_minWidth
= maxWidth
;
1307 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1308 m_minHeight
= m_maxHeight
= maxHeight
;
1312 // if not set yet, have one column
1313 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1314 SetRows( GetToolsCount() );
1316 m_minHeight
= maxHeight
;
1318 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1319 m_minWidth
= m_maxWidth
= maxWidth
;
1323 // FIXME: should this be OSX-only?
1325 bool wantNativeToolbar
, ownToolbarInstalled
;
1327 // attempt to install the native toolbar
1328 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1329 MacInstallNativeToolbar( wantNativeToolbar
);
1330 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1331 if (!ownToolbarInstalled
)
1333 SetSize( maxWidth
, maxHeight
);
1334 InvalidateBestSize();
1338 SetSize( maxWidth
, maxHeight
);
1339 InvalidateBestSize();
1347 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1349 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1350 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1352 #if wxMAC_USE_NATIVE_TOOLBAR
1353 if (m_macHIToolbarRef
!= NULL
)
1355 int maxs
= wxMax( size
.x
, size
.y
);
1356 HIToolbarDisplaySize sizeSpec
;
1358 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1359 else if ( maxs
> 24 )
1360 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1362 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1364 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1369 // The button size is bigger than the bitmap size
1370 wxSize
wxToolBar::GetToolSize() const
1372 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1375 void wxToolBar::SetRows(int nRows
)
1377 // avoid resizing the frame uselessly
1378 if ( nRows
!= m_maxRows
)
1382 void wxToolBar::MacSuperChangedPosition()
1384 wxWindow::MacSuperChangedPosition();
1386 #if wxMAC_USE_NATIVE_TOOLBAR
1387 if (! m_macUsesNativeToolbar
)
1395 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1397 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1400 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1402 tool
->SetNormalBitmap(bitmap
);
1404 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1405 tool
->UpdateToggleImage( tool
->CanBeToggled() && tool
->IsToggled() );
1409 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1411 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1414 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1416 tool
->SetDisabledBitmap(bitmap
);
1418 // TODO: what to do for this one?
1422 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1424 wxToolBarTool
*tool
;
1425 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1428 tool
= (wxToolBarTool
*)node
->GetData();
1431 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1432 if ( r
.Contains( wxPoint( x
, y
) ) )
1436 node
= node
->GetNext();
1439 return (wxToolBarToolBase
*)NULL
;
1442 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1444 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1446 return tool
->GetShortHelp();
1448 return wxEmptyString
;
1451 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1454 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1457 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1459 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1460 if ( ( tool
!= NULL
) && tool
->IsButton() )
1461 tool
->UpdateToggleImage( toggle
);
1464 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1466 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1470 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1471 wxSize toolSize
= GetToolSize();
1472 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1473 ControlRef controlHandle
= NULL
;
1475 tool
->Attach( this );
1477 #if wxMAC_USE_NATIVE_TOOLBAR
1478 wxString label
= tool
->GetLabel();
1479 if (m_macHIToolbarRef
&& !label
.empty() )
1481 // strip mnemonics from the label for compatibility
1482 // with the usual labels in wxStaticText sense
1483 label
= wxStripMenuCodes(label
);
1485 #endif // wxMAC_USE_NATIVE_TOOLBAR
1487 switch (tool
->GetStyle())
1489 case wxTOOL_STYLE_SEPARATOR
:
1491 wxASSERT( tool
->GetControlHandle() == NULL
);
1494 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1495 toolrect
.bottom
= toolSize
.y
;
1497 toolrect
.right
= toolSize
.x
;
1499 // in flat style we need a visual separator
1500 #if wxMAC_USE_NATIVE_TOOLBAR
1501 if (m_macHIToolbarRef
!= NULL
)
1503 err
= HIToolbarItemCreate(
1504 kHIToolbarSeparatorIdentifier
,
1505 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1508 tool
->SetToolbarItemRef( item
);
1512 #endif // wxMAC_USE_NATIVE_TOOLBAR
1514 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1515 tool
->SetControlHandle( controlHandle
);
1519 case wxTOOL_STYLE_BUTTON
:
1521 wxASSERT( tool
->GetControlHandle() == NULL
);
1522 ControlButtonContentInfo info
;
1523 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() );
1525 if ( UMAGetSystemVersion() >= 0x1000)
1527 // contrary to the docs this control only works with iconrefs
1528 ControlButtonContentInfo info
;
1529 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1530 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1531 wxMacReleaseBitmapButton( &info
);
1535 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1536 if ( tool
->CanBeToggled() )
1537 behaviour
|= kControlBehaviorToggles
;
1538 err
= CreateBevelButtonControl( window
,
1539 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1540 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1543 #if wxMAC_USE_NATIVE_TOOLBAR
1544 if (m_macHIToolbarRef
!= NULL
)
1546 HIToolbarItemRef item
;
1547 wxString labelStr
= wxString::Format(wxT("%p"), tool
);
1548 err
= HIToolbarItemCreate(
1549 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1550 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1553 ControlButtonContentInfo info2
;
1554 wxMacCreateBitmapButton( &info2
, tool
->GetNormalBitmap(), kControlContentCGImageRef
);
1556 InstallEventHandler(
1557 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1558 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1559 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(label
, m_font
.GetEncoding()) );
1560 HIToolbarItemSetImage( item
, info2
.u
.imageRef
);
1561 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1562 tool
->SetToolbarItemRef( item
);
1564 wxMacReleaseBitmapButton( &info2
);
1569 #endif // wxMAC_USE_NATIVE_TOOLBAR
1571 wxMacReleaseBitmapButton( &info
);
1574 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1575 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1578 InstallControlEventHandler(
1579 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1580 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1582 tool
->SetControlHandle( controlHandle
);
1586 case wxTOOL_STYLE_CONTROL
:
1588 #if wxMAC_USE_NATIVE_TOOLBAR
1589 if (m_macHIToolbarRef
!= NULL
)
1591 wxCHECK_MSG( tool
->GetControl(), false, _T("control must be non-NULL") );
1592 HIToolbarItemRef item
;
1593 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1594 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1595 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macHIToolbarRef
,kControlToolbarItemClassID
,
1600 tool
->SetToolbarItemRef( item
);
1610 // right now there's nothing to do here
1620 if ( controlHandle
)
1622 ControlRef container
= (ControlRef
) GetHandle();
1623 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1625 UMAShowControl( controlHandle
);
1626 ::EmbedControl( controlHandle
, container
);
1629 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1630 tool
->UpdateToggleImage( true );
1632 // nothing special to do here - we relayout in Realize() later
1633 InvalidateBestSize();
1637 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
) );
1640 return (err
== noErr
);
1643 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1645 wxFAIL_MSG( wxT("not implemented") );
1648 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1650 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1651 wxToolBarToolsList::compatibility_iterator node
;
1652 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1654 wxToolBarToolBase
*tool2
= node
->GetData();
1655 if ( tool2
== tool
)
1657 // let node point to the next node in the list
1658 node
= node
->GetNext();
1664 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1668 #if wxMAC_USE_NATIVE_TOOLBAR
1669 CFIndex removeIndex
= tool
->GetIndex();
1672 #if wxMAC_USE_NATIVE_TOOLBAR
1673 if (m_macHIToolbarRef
!= NULL
)
1675 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1677 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1678 tool
->SetIndex( -1 );
1682 switch ( tool
->GetStyle() )
1684 case wxTOOL_STYLE_CONTROL
:
1685 if ( tool
->GetControl() )
1686 tool
->GetControl()->Destroy();
1689 case wxTOOL_STYLE_BUTTON
:
1690 case wxTOOL_STYLE_SEPARATOR
:
1697 tool
->ClearControl();
1699 // and finally reposition all the controls after this one
1701 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1703 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1704 wxPoint pt
= tool2
->GetPosition();
1706 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1711 tool2
->SetPosition( pt
);
1713 #if wxMAC_USE_NATIVE_TOOLBAR
1714 if (m_macHIToolbarRef
!= NULL
)
1716 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1717 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1722 InvalidateBestSize();
1727 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1729 #if wxMAC_USE_NATIVE_TOOLBAR
1730 if ( m_macUsesNativeToolbar
)
1742 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1743 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1745 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1746 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1748 HIThemePlacardDrawInfo info
;
1749 memset( &info
, 0, sizeof(info
) );
1751 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1753 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1754 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1755 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1759 // leave the background as it is (striped or metal)
1764 const bool drawBorder
= true;
1768 wxMacPortSetter
helper( &dc
);
1770 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1772 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1773 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1776 if ( toolbarrect
.left
< 0 )
1777 toolbarrect
.left
= 0;
1778 if ( toolbarrect
.top
< 0 )
1779 toolbarrect
.top
= 0;
1782 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1786 #if TARGET_API_MAC_OSX
1787 HIRect hiToolbarrect
= CGRectMake(
1788 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1789 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1790 CGContextRef cgContext
;
1793 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1794 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1796 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1797 CGContextScaleCTM( cgContext
, 1, -1 );
1799 HIThemeBackgroundDrawInfo drawInfo
;
1800 drawInfo
.version
= 0;
1801 drawInfo
.state
= kThemeStateActive
;
1802 drawInfo
.kind
= kThemeBackgroundMetal
;
1803 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1806 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1816 #endif // wxUSE_TOOLBAR