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"
22 #include "wx/mac/uma.h"
23 #include "wx/geometry.h"
27 const short kwxMacToolBarToolDefaultWidth
= 16;
28 const short kwxMacToolBarToolDefaultHeight
= 16;
29 const short kwxMacToolBarTopMargin
= 4;
30 const short kwxMacToolBarLeftMargin
= 4;
31 const short kwxMacToolBorder
= 0;
32 const short kwxMacToolSpacing
= 6;
34 const short kwxMacToolBarToolDefaultWidth
= 24;
35 const short kwxMacToolBarToolDefaultHeight
= 22;
36 const short kwxMacToolBarTopMargin
= 2;
37 const short kwxMacToolBarLeftMargin
= 2;
38 const short kwxMacToolBorder
= 4;
39 const short kwxMacToolSpacing
= 0;
43 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
45 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
46 EVT_PAINT( wxToolBar::OnPaint
)
51 #pragma mark Tool Implementation
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
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
)
75 : wxToolBarToolBase(tbar
, control
)
79 SetControlHandle( (ControlRef
) control
->GetHandle() );
86 #if wxMAC_USE_NATIVE_TOOLBAR
87 if ( m_toolbarItemRef
)
88 CFRelease( m_toolbarItemRef
);
92 WXWidget
GetControlHandle()
94 return (WXWidget
) m_controlHandle
;
97 void SetControlHandle( ControlRef handle
)
99 m_controlHandle
= handle
;
102 void SetPosition( const wxPoint
& position
);
107 if ( m_controlHandle
)
109 DisposeControl( m_controlHandle
);
110 m_controlHandle
= NULL
;
113 #if wxMAC_USE_NATIVE_TOOLBAR
114 m_toolbarItemRef
= NULL
;
118 wxSize
GetSize() const
124 curSize
= GetControl()->GetSize();
126 else if ( IsButton() )
128 curSize
= GetToolBar()->GetToolSize();
133 curSize
= GetToolBar()->GetToolSize();
134 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
143 wxPoint
GetPosition() const
145 return wxPoint( m_x
, m_y
);
148 bool DoEnable( bool enable
);
150 void UpdateToggleImage( bool toggle
);
152 #if wxMAC_USE_NATIVE_TOOLBAR
153 void SetToolbarItemRef( HIToolbarItemRef ref
)
155 if ( m_controlHandle
)
156 HideControl( m_controlHandle
);
157 if ( m_toolbarItemRef
)
158 CFRelease( m_toolbarItemRef
);
160 m_toolbarItemRef
= ref
;
161 if ( m_toolbarItemRef
)
163 HIToolbarItemSetHelpText(
165 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
166 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
170 HIToolbarItemRef
GetToolbarItemRef() const
172 return m_toolbarItemRef
;
175 void SetIndex( CFIndex idx
)
180 CFIndex
GetIndex() const
189 m_controlHandle
= NULL
;
191 #if wxMAC_USE_NATIVE_TOOLBAR
192 m_toolbarItemRef
= NULL
;
197 ControlRef m_controlHandle
;
201 #if wxMAC_USE_NATIVE_TOOLBAR
202 HIToolbarItemRef m_toolbarItemRef
;
203 // position in its toolbar, -1 means not inserted
208 static const EventTypeSpec eventList
[] =
210 { kEventClassControl
, kEventControlHit
},
212 { kEventClassControl
, kEventControlHitTest
},
216 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
218 OSStatus result
= eventNotHandledErr
;
219 ControlRef controlRef
;
220 wxMacCarbonEvent
cEvent( event
);
222 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
224 switch ( GetEventKind( event
) )
226 case kEventControlHit
:
228 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
229 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
230 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
235 shouldToggle
= !tbartool
->IsToggled();
237 shouldToggle
= (GetControl32BitValue( (ControlRef
)(tbartool
->GetControlHandle()) ) != 0);
240 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
243 if (tbartool
!= NULL
)
244 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
250 case kEventControlHitTest
:
252 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
254 HIViewGetBounds( controlRef
, &rect
);
256 ControlPartCode pc
= kControlNoPart
;
257 if ( CGRectContainsPoint( rect
, pt
) )
258 pc
= kControlIconPart
;
259 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
272 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
274 OSStatus result
= eventNotHandledErr
;
276 switch ( GetEventClass( event
) )
278 case kEventClassControl
:
279 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
289 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
291 #if wxMAC_USE_NATIVE_TOOLBAR
293 static const EventTypeSpec toolBarEventList
[] =
295 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
298 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
300 OSStatus result
= eventNotHandledErr
;
302 switch ( GetEventKind( event
) )
304 case kEventToolbarItemPerformAction
:
306 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
307 if ( tbartool
!= NULL
)
309 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
310 int toolID
= tbartool
->GetId();
312 if ( tbartool
->CanBeToggled() )
315 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
319 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
332 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
334 OSStatus result
= eventNotHandledErr
;
336 switch ( GetEventClass( event
) )
338 case kEventClassToolbarItem
:
339 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
349 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
353 bool wxToolBarTool::DoEnable( bool enable
)
357 GetControl()->Enable( enable
);
359 else if ( IsButton() )
361 #if wxMAC_USE_NATIVE_TOOLBAR
362 if ( m_toolbarItemRef
!= NULL
)
363 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
366 if ( m_controlHandle
!= NULL
)
368 #if TARGET_API_MAC_OSX
370 EnableControl( m_controlHandle
);
372 DisableControl( m_controlHandle
);
375 ActivateControl( m_controlHandle
);
377 DeactivateControl( m_controlHandle
);
385 void wxToolBarTool::SetPosition( const wxPoint
& position
)
392 int mac_x
= position
.x
;
393 int mac_y
= position
.y
;
398 GetControlBounds( m_controlHandle
, &contrlRect
);
399 int former_mac_x
= contrlRect
.left
;
400 int former_mac_y
= contrlRect
.top
;
401 GetToolBar()->GetToolSize();
403 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
405 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
408 else if ( IsControl() )
410 GetControl()->Move( position
);
417 GetControlBounds( m_controlHandle
, &contrlRect
);
418 int former_mac_x
= contrlRect
.left
;
419 int former_mac_y
= contrlRect
.top
;
421 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
422 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
427 void wxToolBarTool::UpdateToggleImage( bool toggle
)
429 #if wxMAC_USE_NATIVE_TOOLBAR
431 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
432 #define kHIToolbarItemSelected (1 << 7)
435 // FIXME: this should be a OSX v10.4 runtime check
436 if (m_toolbarItemRef
!= NULL
)
438 OptionBits addAttrs
, removeAttrs
;
443 addAttrs
= kHIToolbarItemSelected
;
444 removeAttrs
= kHIToolbarItemNoAttributes
;
448 addAttrs
= kHIToolbarItemNoAttributes
;
449 removeAttrs
= kHIToolbarItemSelected
;
452 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
459 int w
= m_bmpNormal
.GetWidth();
460 int h
= m_bmpNormal
.GetHeight();
461 wxBitmap
bmp( w
, h
);
464 dc
.SelectObject( bmp
);
465 dc
.SetPen( wxNullPen
);
466 dc
.SetBackground( *wxWHITE
);
467 dc
.DrawRectangle( 0, 0, w
, h
);
468 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
469 dc
.SelectObject( wxNullBitmap
);
470 ControlButtonContentInfo info
;
471 wxMacCreateBitmapButton( &info
, bmp
);
472 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
473 wxMacReleaseBitmapButton( &info
);
477 ControlButtonContentInfo info
;
478 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
479 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
480 wxMacReleaseBitmapButton( &info
);
483 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
485 m_controlHandle
, 0, kControlIconTransformTag
,
486 sizeof(transform
), (Ptr
)&transform
);
487 HIViewSetNeedsDisplay( m_controlHandle
, true );
490 ::SetControl32BitValue( m_controlHandle
, toggle
);
494 wxToolBarTool::wxToolBarTool(
497 const wxString
& label
,
498 const wxBitmap
& bmpNormal
,
499 const wxBitmap
& bmpDisabled
,
501 wxObject
*clientData
,
502 const wxString
& shortHelp
,
503 const wxString
& longHelp
)
506 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
507 clientData
, shortHelp
, longHelp
)
513 #pragma mark Toolbar Implementation
515 wxToolBarToolBase
*wxToolBar::CreateTool(
517 const wxString
& label
,
518 const wxBitmap
& bmpNormal
,
519 const wxBitmap
& bmpDisabled
,
521 wxObject
*clientData
,
522 const wxString
& shortHelp
,
523 const wxString
& longHelp
)
525 return new wxToolBarTool(
526 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
527 clientData
, shortHelp
, longHelp
);
530 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
532 return new wxToolBarTool( this, control
);
535 void wxToolBar::Init()
539 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
540 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
542 #if wxMAC_USE_NATIVE_TOOLBAR
543 m_macHIToolbarRef
= NULL
;
544 m_macUsesNativeToolbar
= false;
548 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
550 const EventTypeSpec kEvents
[] =
552 { kEventClassHIObject
, kEventHIObjectConstruct
},
553 { kEventClassHIObject
, kEventHIObjectInitialize
},
554 { kEventClassHIObject
, kEventHIObjectDestruct
},
556 { kEventClassToolbarItem
, kEventToolbarItemCreateCustomView
}
559 const EventTypeSpec kViewEvents
[] =
561 { kEventClassControl
, kEventControlGetSizeConstraints
}
564 struct ControlToolbarItem
566 HIToolbarItemRef toolbarItem
;
568 wxSize lastValidSize
;
571 static pascal OSStatus
ControlToolbarItemHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
573 OSStatus result
= eventNotHandledErr
;
574 ControlToolbarItem
* object
= (ControlToolbarItem
*)inUserData
;
576 switch ( GetEventClass( inEvent
) )
578 case kEventClassHIObject
:
579 switch ( GetEventKind( inEvent
) )
581 case kEventHIObjectConstruct
:
583 HIObjectRef toolbarItem
;
584 ControlToolbarItem
* item
;
586 GetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeHIObjectRef
, NULL
,
587 sizeof( HIObjectRef
), NULL
, &toolbarItem
);
589 item
= (ControlToolbarItem
*) malloc(sizeof(ControlToolbarItem
)) ;
590 item
->toolbarItem
= toolbarItem
;
591 item
->viewRef
= NULL
;
593 SetEventParameter( inEvent
, kEventParamHIObjectInstance
, typeVoidPtr
, sizeof( void * ), &item
);
599 case kEventHIObjectInitialize
:
600 result
= CallNextEventHandler( inCallRef
, inEvent
);
601 if ( result
== noErr
)
604 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
605 sizeof( CFTypeRef
), NULL
, &data
);
609 wxASSERT_MSG( CFDataGetLength( data
) == sizeof( viewRef
) , wxT("Illegal Data passed") ) ;
610 memcpy( &viewRef
, CFDataGetBytePtr( data
) , sizeof( viewRef
) ) ;
612 object
->viewRef
= (HIViewRef
) viewRef
;
618 case kEventHIObjectDestruct
:
625 case kEventClassToolbarItem
:
626 switch ( GetEventKind( inEvent
) )
628 case kEventToolbarItemCreateCustomView
:
630 HIViewRef viewRef
= object
->viewRef
;
632 HIViewRemoveFromSuperview( viewRef
) ;
633 HIViewSetVisible(viewRef
, true) ;
634 InstallEventHandler( GetControlEventTarget( viewRef
), ControlToolbarItemHandler
,
635 GetEventTypeCount( kViewEvents
), kViewEvents
, object
, NULL
);
637 result
= SetEventParameter( inEvent
, kEventParamControlRef
, typeControlRef
, sizeof( HIViewRef
), &viewRef
);
643 case kEventClassControl
:
644 switch ( GetEventKind( inEvent
) )
646 case kEventControlGetSizeConstraints
:
648 wxWindow
* wxwindow
= wxFindControlFromMacControl(object
->viewRef
) ;
651 wxSize sz
= wxwindow
->GetSize() ;
652 // during toolbar layout the native window sometimes gets negative sizes
653 // so we always keep the last valid size here, to make sure we survive the
655 if ( sz
.x
> 0 && sz
.y
> 0 )
656 object
->lastValidSize
= sz
;
658 sz
= object
->lastValidSize
;
661 min
.width
= max
.width
= sz
.x
;
662 min
.height
= max
.height
= sz
.y
;
664 result
= SetEventParameter( inEvent
, kEventParamMinimumSize
, typeHISize
,
665 sizeof( HISize
), &min
);
667 result
= SetEventParameter( inEvent
, kEventParamMaximumSize
, typeHISize
,
668 sizeof( HISize
), &max
);
680 void RegisterControlToolbarItemClass()
682 static bool sRegistered
;
686 HIObjectRegisterSubclass( kControlToolbarItemClassID
, kHIToolbarItemClassID
, 0,
687 ControlToolbarItemHandler
, GetEventTypeCount( kEvents
), kEvents
, 0, NULL
);
693 HIToolbarItemRef
CreateControlToolbarItem(CFStringRef inIdentifier
, CFTypeRef inConfigData
)
695 RegisterControlToolbarItemClass();
699 UInt32 options
= kHIToolbarItemAllowDuplicates
;
700 HIToolbarItemRef result
= NULL
;
702 err
= CreateEvent( NULL
, kEventClassHIObject
, kEventHIObjectInitialize
, GetCurrentEventTime(), 0, &event
);
703 require_noerr( err
, CantCreateEvent
);
705 SetEventParameter( event
, kEventParamAttributes
, typeUInt32
, sizeof( UInt32
), &options
);
706 SetEventParameter( event
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, sizeof( CFStringRef
), &inIdentifier
);
709 SetEventParameter( event
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, sizeof( CFTypeRef
), &inConfigData
);
711 err
= HIObjectCreate( kControlToolbarItemClassID
, event
, (HIObjectRef
*)&result
);
714 ReleaseEvent( event
);
719 static const EventTypeSpec kToolbarEvents
[] =
721 { kEventClassToolbar
, kEventToolbarGetDefaultIdentifiers
},
722 { kEventClassToolbar
, kEventToolbarGetAllowedIdentifiers
},
723 { kEventClassToolbar
, kEventToolbarCreateItemWithIdentifier
},
726 static OSStatus
ToolbarDelegateHandler( EventHandlerCallRef inCallRef
, EventRef inEvent
, void* inUserData
)
728 OSStatus result
= eventNotHandledErr
;
729 wxToolBar
* toolbar
= (wxToolBar
*) inUserData
;
730 CFMutableArrayRef array
;
732 switch ( GetEventKind( inEvent
) )
734 case kEventToolbarGetDefaultIdentifiers
:
736 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
737 sizeof( CFMutableArrayRef
), NULL
, &array
);
738 // not implemented yet
739 // GetToolbarDefaultItems( array );
744 case kEventToolbarGetAllowedIdentifiers
:
746 GetEventParameter( inEvent
, kEventParamMutableArray
, typeCFMutableArrayRef
, NULL
,
747 sizeof( CFMutableArrayRef
), NULL
, &array
);
748 // not implemented yet
749 // GetToolbarAllowedItems( array );
753 case kEventToolbarCreateItemWithIdentifier
:
755 HIToolbarItemRef item
= NULL
;
756 CFTypeRef data
= NULL
;
757 CFStringRef identifier
= NULL
;
759 GetEventParameter( inEvent
, kEventParamToolbarItemIdentifier
, typeCFStringRef
, NULL
,
760 sizeof( CFStringRef
), NULL
, &identifier
);
762 GetEventParameter( inEvent
, kEventParamToolbarItemConfigData
, typeCFTypeRef
, NULL
,
763 sizeof( CFTypeRef
), NULL
, &data
);
765 if ( CFStringCompare( kControlToolbarItemClassID
, identifier
, kCFCompareBackwards
) == kCFCompareEqualTo
)
767 item
= CreateControlToolbarItem( kControlToolbarItemClassID
, data
);
770 SetEventParameter( inEvent
, kEventParamToolbarItem
, typeHIToolbarItemRef
,
771 sizeof( HIToolbarItemRef
), &item
);
782 // also for the toolbar we have the dual implementation:
783 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
785 bool wxToolBar::Create(
791 const wxString
& name
)
793 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
796 OSStatus err
= noErr
;
798 #if wxMAC_USE_NATIVE_TOOLBAR
799 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
800 err
= HIToolbarCreate(
801 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
802 (HIToolbarRef
*) &m_macHIToolbarRef
);
804 if (m_macHIToolbarRef
!= NULL
)
806 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef
)m_macHIToolbarRef
), ToolbarDelegateHandler
,
807 GetEventTypeCount( kToolbarEvents
), kToolbarEvents
, this, NULL
);
809 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
810 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
812 if ( style
& wxTB_NOICONS
)
813 mode
= kHIToolbarDisplayModeLabelOnly
;
814 else if ( style
& wxTB_TEXT
)
815 mode
= kHIToolbarDisplayModeIconAndLabel
;
817 mode
= kHIToolbarDisplayModeIconOnly
;
819 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
820 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
824 return (err
== noErr
);
827 wxToolBar::~wxToolBar()
829 #if wxMAC_USE_NATIVE_TOOLBAR
830 if (m_macHIToolbarRef
!= NULL
)
832 // if this is the installed toolbar, then deinstall it
833 if (m_macUsesNativeToolbar
)
834 MacInstallNativeToolbar( false );
836 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
837 m_macHIToolbarRef
= NULL
;
842 bool wxToolBar::Show( bool show
)
844 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
845 bool bResult
= (tlw
!= NULL
);
849 #if wxMAC_USE_NATIVE_TOOLBAR
850 bool ownToolbarInstalled
= false;
851 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
852 if (ownToolbarInstalled
)
854 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
856 ShowHideWindowToolbar( tlw
, show
, false );
859 bResult
= wxToolBarBase::Show( show
);
862 bResult
= wxToolBarBase::Show( show
);
869 bool wxToolBar::IsShown() const
873 #if wxMAC_USE_NATIVE_TOOLBAR
874 bool ownToolbarInstalled
;
876 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
877 if (ownToolbarInstalled
)
879 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
880 bResult
= IsWindowToolbarVisible( tlw
);
883 bResult
= wxToolBarBase::IsShown();
886 bResult
= wxToolBarBase::IsShown();
892 void wxToolBar::DoGetSize( int *width
, int *height
) const
894 #if wxMAC_USE_NATIVE_TOOLBAR
896 bool ownToolbarInstalled
;
898 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
899 if ( ownToolbarInstalled
)
901 // TODO: is this really a control ?
902 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
904 *width
= boundsR
.right
- boundsR
.left
;
905 if ( height
!= NULL
)
906 *height
= boundsR
.bottom
- boundsR
.top
;
909 wxToolBarBase::DoGetSize( width
, height
);
912 wxToolBarBase::DoGetSize( width
, height
);
916 wxSize
wxToolBar::DoGetBestSize() const
920 DoGetSize( &width
, &height
);
922 return wxSize( width
, height
);
925 void wxToolBar::SetWindowStyleFlag( long style
)
927 wxToolBarBase::SetWindowStyleFlag( style
);
929 #if wxMAC_USE_NATIVE_TOOLBAR
930 if (m_macHIToolbarRef
!= NULL
)
932 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
934 if ( style
& wxTB_NOICONS
)
935 mode
= kHIToolbarDisplayModeLabelOnly
;
936 else if ( style
& wxTB_TEXT
)
937 mode
= kHIToolbarDisplayModeIconAndLabel
;
939 mode
= kHIToolbarDisplayModeIconOnly
;
941 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
946 #if wxMAC_USE_NATIVE_TOOLBAR
947 bool wxToolBar::MacWantsNativeToolbar()
949 return m_macUsesNativeToolbar
;
952 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
954 bool bResultV
= false;
956 if (ownToolbarInstalled
!= NULL
)
957 *ownToolbarInstalled
= false;
959 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
962 HIToolbarRef curToolbarRef
= NULL
;
963 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
964 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
965 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
966 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
972 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
974 bool bResult
= false;
976 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
979 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
982 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
986 // check the existing toolbar
987 HIToolbarRef curToolbarRef
= NULL
;
988 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
990 curToolbarRef
= NULL
;
992 m_macUsesNativeToolbar
= usesNative
;
994 if (m_macUsesNativeToolbar
)
996 // only install toolbar if there isn't one installed already
997 if (curToolbarRef
== NULL
)
1001 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
1002 ShowHideWindowToolbar( tlw
, true, false );
1003 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
1004 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
1006 Rect r
= { 0, 0, 0, 0 };
1007 m_peer
->SetRect( &r
);
1008 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
1009 m_peer
->SetVisibility( false, true );
1010 wxToolBarBase::Show( false );
1015 // only deinstall toolbar if this is the installed one
1016 if (m_macHIToolbarRef
== curToolbarRef
)
1020 ShowHideWindowToolbar( tlw
, false, false );
1021 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
1022 SetWindowToolbar( tlw
, NULL
);
1024 m_peer
->SetVisibility( true, true );
1029 InvalidateBestSize();
1031 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1036 bool wxToolBar::Realize()
1038 if (m_tools
.GetCount() == 0)
1044 int maxToolWidth
= 0;
1045 int maxToolHeight
= 0;
1047 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
1048 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
1051 GetSize( &tw
, &th
);
1053 // find the maximum tool width and height
1054 wxToolBarTool
*tool
;
1055 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1056 while ( node
!= NULL
)
1058 tool
= (wxToolBarTool
*) node
->GetData();
1061 wxSize sz
= tool
->GetSize();
1063 if ( sz
.x
> maxToolWidth
)
1064 maxToolWidth
= sz
.x
;
1065 if ( sz
.y
> maxToolHeight
)
1066 maxToolHeight
= sz
.y
;
1069 node
= node
->GetNext();
1072 bool lastIsRadio
= false;
1073 bool curIsRadio
= false;
1074 bool setChoiceInGroup
= false;
1076 #if wxMAC_USE_NATIVE_TOOLBAR
1077 CFIndex currentPosition
= 0;
1078 bool insertAll
= false;
1081 node
= m_tools
.GetFirst();
1082 while ( node
!= NULL
)
1084 tool
= (wxToolBarTool
*) node
->GetData();
1087 node
= node
->GetNext();
1091 // set tool position:
1092 // for the moment just perform a single row/column alignment
1093 wxSize cursize
= tool
->GetSize();
1094 if ( x
+ cursize
.x
> maxWidth
)
1095 maxWidth
= x
+ cursize
.x
;
1096 if ( y
+ cursize
.y
> maxHeight
)
1097 maxHeight
= y
+ cursize
.y
;
1099 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1101 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
1102 tool
->SetPosition( wxPoint(x1
, y
) );
1106 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
1107 tool
->SetPosition( wxPoint(x
, y1
) );
1110 // update the item positioning state
1111 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1112 y
+= cursize
.y
+ kwxMacToolSpacing
;
1114 x
+= cursize
.x
+ kwxMacToolSpacing
;
1116 #if wxMAC_USE_NATIVE_TOOLBAR
1117 // install in native HIToolbar
1118 if ( m_macHIToolbarRef
!= NULL
)
1120 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
1121 if ( hiItemRef
!= NULL
)
1123 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
1125 OSStatus err
= noErr
;
1130 // if this is the first tool that gets newly inserted or repositioned
1131 // first remove all 'old' tools from here to the right, because of this
1132 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1133 // no way to determine how many there are in a toolbar, so just a high number :-(
1134 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
1136 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
1141 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
1142 wxFAIL_MSG( errMsg
.c_str() );
1146 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
1149 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
1150 wxFAIL_MSG( errMsg
.c_str() );
1153 tool
->SetIndex( currentPosition
);
1161 // update radio button (and group) state
1162 lastIsRadio
= curIsRadio
;
1163 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
1167 if ( tool
->IsToggled() )
1168 DoToggleTool( tool
, true );
1170 setChoiceInGroup
= false;
1176 if ( tool
->Toggle( true ) )
1178 DoToggleTool( tool
, true );
1179 setChoiceInGroup
= true;
1182 else if ( tool
->IsToggled() )
1184 if ( tool
->IsToggled() )
1185 DoToggleTool( tool
, true );
1187 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1188 while ( nodePrev
!= NULL
)
1190 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
1191 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
1194 if ( toggleTool
->Toggle( false ) )
1195 DoToggleTool( toggleTool
, false );
1197 nodePrev
= nodePrev
->GetPrevious();
1202 node
= node
->GetNext();
1205 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
1207 // if not set yet, only one row
1208 if ( m_maxRows
<= 0 )
1211 m_minWidth
= maxWidth
;
1213 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
1214 m_minHeight
= m_maxHeight
= maxHeight
;
1218 // if not set yet, have one column
1219 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
1220 SetRows( GetToolsCount() );
1222 m_minHeight
= maxHeight
;
1224 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
1225 m_minWidth
= m_maxWidth
= maxWidth
;
1229 // FIXME: should this be OSX-only?
1231 bool wantNativeToolbar
, ownToolbarInstalled
;
1233 // attempt to install the native toolbar
1234 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1235 MacInstallNativeToolbar( wantNativeToolbar
);
1236 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1237 if (!ownToolbarInstalled
)
1239 SetSize( maxWidth
, maxHeight
);
1240 InvalidateBestSize();
1244 SetSize( maxWidth
, maxHeight
);
1245 InvalidateBestSize();
1248 SetBestFittingSize();
1253 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1255 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1256 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1258 #if wxMAC_USE_NATIVE_TOOLBAR
1259 if (m_macHIToolbarRef
!= NULL
)
1261 int maxs
= wxMax( size
.x
, size
.y
);
1262 HIToolbarDisplaySize sizeSpec
;
1264 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1265 else if ( maxs
> 24 )
1266 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1268 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1270 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1275 // The button size is bigger than the bitmap size
1276 wxSize
wxToolBar::GetToolSize() const
1278 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1281 void wxToolBar::SetRows(int nRows
)
1283 // avoid resizing the frame uselessly
1284 if ( nRows
!= m_maxRows
)
1288 void wxToolBar::MacSuperChangedPosition()
1290 wxWindow::MacSuperChangedPosition();
1292 #if wxMAC_USE_NATIVE_TOOLBAR
1293 if (! m_macUsesNativeToolbar
)
1301 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1303 wxToolBarTool
*tool
;
1304 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1305 while ( node
!= NULL
)
1307 tool
= (wxToolBarTool
*)node
->GetData();
1310 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1311 if ( r
.Contains( wxPoint( x
, y
) ) )
1315 node
= node
->GetNext();
1318 return (wxToolBarToolBase
*)NULL
;
1321 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1323 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1325 return tool
->GetShortHelp();
1327 return wxEmptyString
;
1330 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1333 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1336 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1338 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1339 if ( ( tool
!= NULL
) && tool
->IsButton() )
1340 tool
->UpdateToggleImage( toggle
);
1343 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1345 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1349 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1350 wxSize toolSize
= GetToolSize();
1351 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1352 ControlRef controlHandle
= NULL
;
1355 switch (tool
->GetStyle())
1357 case wxTOOL_STYLE_SEPARATOR
:
1359 wxASSERT( tool
->GetControlHandle() == NULL
);
1362 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1363 toolrect
.bottom
= toolSize
.y
;
1365 toolrect
.right
= toolSize
.x
;
1367 #ifdef __WXMAC_OSX__
1368 // in flat style we need a visual separator
1369 #if wxMAC_USE_NATIVE_TOOLBAR
1370 HIToolbarItemRef item
;
1371 err
= HIToolbarItemCreate(
1372 kHIToolbarSeparatorIdentifier
,
1373 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1376 tool
->SetToolbarItemRef( item
);
1379 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1380 tool
->SetControlHandle( controlHandle
);
1385 case wxTOOL_STYLE_BUTTON
:
1387 wxASSERT( tool
->GetControlHandle() == NULL
);
1388 ControlButtonContentInfo info
;
1389 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1391 if ( UMAGetSystemVersion() >= 0x1000)
1393 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1397 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1398 if ( tool
->CanBeToggled() )
1399 behaviour
|= kControlBehaviorToggles
;
1400 err
= CreateBevelButtonControl( window
,
1401 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1402 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1405 #if wxMAC_USE_NATIVE_TOOLBAR
1406 HIToolbarItemRef item
;
1407 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1408 err
= HIToolbarItemCreate(
1409 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1410 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1413 InstallEventHandler(
1414 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1415 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1416 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1417 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1418 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1419 tool
->SetToolbarItemRef( item
);
1423 wxMacReleaseBitmapButton( &info
);
1426 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1427 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1430 InstallControlEventHandler(
1431 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1432 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1434 tool
->SetControlHandle( controlHandle
);
1438 case wxTOOL_STYLE_CONTROL
:
1440 #if wxMAC_USE_NATIVE_TOOLBAR
1442 wxASSERT( tool
->GetControl() != NULL
);
1443 HIToolbarItemRef item
;
1444 HIViewRef viewRef
= (HIViewRef
) tool
->GetControl()->GetHandle() ;
1445 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*) &viewRef
, sizeof(viewRef
) ) ;
1446 err
= HIToolbarCreateItemWithIdentifier((HIToolbarRef
) m_macHIToolbarRef
,kControlToolbarItemClassID
,
1451 tool
->SetToolbarItemRef( item
);
1457 // right now there's nothing to do here
1467 if ( controlHandle
)
1469 ControlRef container
= (ControlRef
) GetHandle();
1470 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1472 UMAShowControl( controlHandle
);
1473 ::EmbedControl( controlHandle
, container
);
1476 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1477 tool
->UpdateToggleImage( true );
1479 // nothing special to do here - we relayout in Realize() later
1480 tool
->Attach( this );
1481 InvalidateBestSize();
1485 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1486 wxFAIL_MSG( errMsg
.c_str() );
1489 return (err
== noErr
);
1492 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1494 wxFAIL_MSG( wxT("not implemented") );
1497 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1499 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1500 wxToolBarToolsList::compatibility_iterator node
;
1501 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1503 wxToolBarToolBase
*tool2
= node
->GetData();
1504 if ( tool2
== tool
)
1506 // let node point to the next node in the list
1507 node
= node
->GetNext();
1513 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1517 #if wxMAC_USE_NATIVE_TOOLBAR
1518 CFIndex removeIndex
= tool
->GetIndex();
1521 switch ( tool
->GetStyle() )
1523 case wxTOOL_STYLE_CONTROL
:
1525 tool
->GetControl()->Destroy();
1526 tool
->ClearControl();
1530 case wxTOOL_STYLE_BUTTON
:
1531 case wxTOOL_STYLE_SEPARATOR
:
1532 if ( tool
->GetControlHandle() )
1534 #if wxMAC_USE_NATIVE_TOOLBAR
1535 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1537 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1538 tool
->SetIndex( -1 );
1542 tool
->ClearControl();
1550 // and finally reposition all the controls after this one
1552 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1554 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1555 wxPoint pt
= tool2
->GetPosition();
1557 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1562 tool2
->SetPosition( pt
);
1564 #if wxMAC_USE_NATIVE_TOOLBAR
1565 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1566 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1570 InvalidateBestSize();
1575 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1577 #if wxMAC_USE_NATIVE_TOOLBAR
1578 if ( m_macUsesNativeToolbar
)
1590 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1591 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1593 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1594 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1596 HIThemePlacardDrawInfo info
;
1597 memset( &info
, 0, sizeof(info
) );
1599 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1601 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1602 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1603 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1607 // leave the background as it is (striped or metal)
1612 const bool drawBorder
= true;
1616 wxMacPortSetter
helper( &dc
);
1618 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1620 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1621 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1624 if ( toolbarrect
.left
< 0 )
1625 toolbarrect
.left
= 0;
1626 if ( toolbarrect
.top
< 0 )
1627 toolbarrect
.top
= 0;
1630 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1634 #if TARGET_API_MAC_OSX
1635 HIRect hiToolbarrect
= CGRectMake(
1636 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1637 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1638 CGContextRef cgContext
;
1641 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1642 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1644 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1645 CGContextScaleCTM( cgContext
, 1, -1 );
1647 HIThemeBackgroundDrawInfo drawInfo
;
1648 drawInfo
.version
= 0;
1649 drawInfo
.state
= kThemeStateActive
;
1650 drawInfo
.kind
= kThemeBackgroundMetal
;
1651 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1653 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1662 #endif // wxUSE_TOOLBAR