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 // also for the toolbar we have the dual implementation:
549 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
551 bool wxToolBar::Create(
557 const wxString
& name
)
559 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
562 OSStatus err
= noErr
;
564 #if wxMAC_USE_NATIVE_TOOLBAR
565 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
566 err
= HIToolbarCreate(
567 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
568 (HIToolbarRef
*) &m_macHIToolbarRef
);
570 if (m_macHIToolbarRef
!= NULL
)
572 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
573 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
575 if ( style
& wxTB_NOICONS
)
576 mode
= kHIToolbarDisplayModeLabelOnly
;
577 else if ( style
& wxTB_TEXT
)
578 mode
= kHIToolbarDisplayModeIconAndLabel
;
580 mode
= kHIToolbarDisplayModeIconOnly
;
582 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
583 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
587 return (err
== noErr
);
590 wxToolBar::~wxToolBar()
592 #if wxMAC_USE_NATIVE_TOOLBAR
593 if (m_macHIToolbarRef
!= NULL
)
595 // if this is the installed toolbar, then deinstall it
596 if (m_macUsesNativeToolbar
)
597 MacInstallNativeToolbar( false );
599 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
600 m_macHIToolbarRef
= NULL
;
605 bool wxToolBar::Show( bool show
)
607 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
608 bool bResult
= (tlw
!= NULL
);
612 #if wxMAC_USE_NATIVE_TOOLBAR
613 bool ownToolbarInstalled
= false;
614 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
615 if (ownToolbarInstalled
)
617 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
619 ShowHideWindowToolbar( tlw
, show
, false );
622 bResult
= wxToolBarBase::Show( show
);
625 bResult
= wxToolBarBase::Show( show
);
632 bool wxToolBar::IsShown() const
636 #if wxMAC_USE_NATIVE_TOOLBAR
637 bool ownToolbarInstalled
;
639 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
640 if (ownToolbarInstalled
)
642 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
643 bResult
= IsWindowToolbarVisible( tlw
);
646 bResult
= wxToolBarBase::IsShown();
649 bResult
= wxToolBarBase::IsShown();
655 void wxToolBar::DoGetSize( int *width
, int *height
) const
657 #if wxMAC_USE_NATIVE_TOOLBAR
659 bool ownToolbarInstalled
;
661 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
662 if ( ownToolbarInstalled
)
664 // TODO: is this really a control ?
665 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
667 *width
= boundsR
.right
- boundsR
.left
;
668 if ( height
!= NULL
)
669 *height
= boundsR
.bottom
- boundsR
.top
;
672 wxToolBarBase::DoGetSize( width
, height
);
675 wxToolBarBase::DoGetSize( width
, height
);
679 wxSize
wxToolBar::DoGetBestSize() const
683 DoGetSize( &width
, &height
);
685 return wxSize( width
, height
);
688 void wxToolBar::SetWindowStyleFlag( long style
)
690 wxToolBarBase::SetWindowStyleFlag( style
);
692 #if wxMAC_USE_NATIVE_TOOLBAR
693 if (m_macHIToolbarRef
!= NULL
)
695 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
697 if ( style
& wxTB_NOICONS
)
698 mode
= kHIToolbarDisplayModeLabelOnly
;
699 else if ( style
& wxTB_TEXT
)
700 mode
= kHIToolbarDisplayModeIconAndLabel
;
702 mode
= kHIToolbarDisplayModeIconOnly
;
704 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
709 #if wxMAC_USE_NATIVE_TOOLBAR
710 bool wxToolBar::MacWantsNativeToolbar()
712 return m_macUsesNativeToolbar
;
715 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
717 bool bResultV
= false;
719 if (ownToolbarInstalled
!= NULL
)
720 *ownToolbarInstalled
= false;
722 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
725 HIToolbarRef curToolbarRef
= NULL
;
726 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
727 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
728 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
729 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
735 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
737 bool bResult
= false;
739 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
742 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
745 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
749 // check the existing toolbar
750 HIToolbarRef curToolbarRef
= NULL
;
751 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
753 curToolbarRef
= NULL
;
755 m_macUsesNativeToolbar
= usesNative
;
757 if (m_macUsesNativeToolbar
)
759 // only install toolbar if there isn't one installed already
760 if (curToolbarRef
== NULL
)
764 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
765 ShowHideWindowToolbar( tlw
, true, false );
766 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
767 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
769 Rect r
= { 0, 0, 0, 0 };
770 m_peer
->SetRect( &r
);
771 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
772 m_peer
->SetVisibility( false, true );
773 wxToolBarBase::Show( false );
778 // only deinstall toolbar if this is the installed one
779 if (m_macHIToolbarRef
== curToolbarRef
)
783 ShowHideWindowToolbar( tlw
, false, false );
784 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
785 SetWindowToolbar( tlw
, NULL
);
787 m_peer
->SetVisibility( true, true );
792 InvalidateBestSize();
794 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
799 bool wxToolBar::Realize()
801 if (m_tools
.GetCount() == 0)
807 int maxToolWidth
= 0;
808 int maxToolHeight
= 0;
810 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
811 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
816 // find the maximum tool width and height
818 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
819 while ( node
!= NULL
)
821 tool
= (wxToolBarTool
*) node
->GetData();
824 wxSize sz
= tool
->GetSize();
826 if ( sz
.x
> maxToolWidth
)
828 if ( sz
.y
> maxToolHeight
)
829 maxToolHeight
= sz
.y
;
832 node
= node
->GetNext();
835 bool lastIsRadio
= false;
836 bool curIsRadio
= false;
837 bool setChoiceInGroup
= false;
839 #if wxMAC_USE_NATIVE_TOOLBAR
840 CFIndex currentPosition
= 0;
841 bool insertAll
= false;
844 node
= m_tools
.GetFirst();
845 while ( node
!= NULL
)
847 tool
= (wxToolBarTool
*) node
->GetData();
850 node
= node
->GetNext();
854 // set tool position:
855 // for the moment just perform a single row/column alignment
856 wxSize cursize
= tool
->GetSize();
857 if ( x
+ cursize
.x
> maxWidth
)
858 maxWidth
= x
+ cursize
.x
;
859 if ( y
+ cursize
.y
> maxHeight
)
860 maxHeight
= y
+ cursize
.y
;
862 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
864 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
865 tool
->SetPosition( wxPoint(x1
, y
) );
869 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
870 tool
->SetPosition( wxPoint(x
, y1
) );
873 // update the item positioning state
874 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
875 y
+= cursize
.y
+ kwxMacToolSpacing
;
877 x
+= cursize
.x
+ kwxMacToolSpacing
;
879 #if wxMAC_USE_NATIVE_TOOLBAR
880 // install in native HIToolbar
881 if ( m_macHIToolbarRef
!= NULL
)
883 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
884 if ( hiItemRef
!= NULL
)
886 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
888 OSStatus err
= noErr
;
893 // if this is the first tool that gets newly inserted or repositioned
894 // first remove all 'old' tools from here to the right, because of this
895 // all following tools will have to be reinserted (insertAll). i = 100 because there's
896 // no way to determine how many there are in a toolbar, so just a high number :-(
897 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
899 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
904 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
905 wxFAIL_MSG( errMsg
.c_str() );
909 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
912 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
913 wxFAIL_MSG( errMsg
.c_str() );
916 tool
->SetIndex( currentPosition
);
924 // update radio button (and group) state
925 lastIsRadio
= curIsRadio
;
926 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
930 if ( tool
->IsToggled() )
931 DoToggleTool( tool
, true );
933 setChoiceInGroup
= false;
939 if ( tool
->Toggle( true ) )
941 DoToggleTool( tool
, true );
942 setChoiceInGroup
= true;
945 else if ( tool
->IsToggled() )
947 if ( tool
->IsToggled() )
948 DoToggleTool( tool
, true );
950 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
951 while ( nodePrev
!= NULL
)
953 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
954 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
957 if ( toggleTool
->Toggle( false ) )
958 DoToggleTool( toggleTool
, false );
960 nodePrev
= nodePrev
->GetPrevious();
965 node
= node
->GetNext();
968 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
970 // if not set yet, only one row
971 if ( m_maxRows
<= 0 )
974 m_minWidth
= maxWidth
;
976 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
977 m_minHeight
= m_maxHeight
= maxHeight
;
981 // if not set yet, have one column
982 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
983 SetRows( GetToolsCount() );
985 m_minHeight
= maxHeight
;
987 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
988 m_minWidth
= m_maxWidth
= maxWidth
;
992 // FIXME: should this be OSX-only?
994 bool wantNativeToolbar
, ownToolbarInstalled
;
996 // attempt to install the native toolbar
997 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
998 MacInstallNativeToolbar( wantNativeToolbar
);
999 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1000 if (!ownToolbarInstalled
)
1002 SetSize( maxWidth
, maxHeight
);
1003 InvalidateBestSize();
1007 SetSize( maxWidth
, maxHeight
);
1008 InvalidateBestSize();
1011 SetBestFittingSize();
1016 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1018 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1019 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1021 #if wxMAC_USE_NATIVE_TOOLBAR
1022 if (m_macHIToolbarRef
!= NULL
)
1024 int maxs
= wxMax( size
.x
, size
.y
);
1025 HIToolbarDisplaySize sizeSpec
;
1027 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1028 else if ( maxs
> 24 )
1029 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1031 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1033 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1038 // The button size is bigger than the bitmap size
1039 wxSize
wxToolBar::GetToolSize() const
1041 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1044 void wxToolBar::SetRows(int nRows
)
1046 // avoid resizing the frame uselessly
1047 if ( nRows
!= m_maxRows
)
1051 void wxToolBar::MacSuperChangedPosition()
1053 wxWindow::MacSuperChangedPosition();
1055 #if wxMAC_USE_NATIVE_TOOLBAR
1056 if (! m_macUsesNativeToolbar
)
1064 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1066 wxToolBarTool
*tool
;
1067 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1068 while ( node
!= NULL
)
1070 tool
= (wxToolBarTool
*)node
->GetData();
1073 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1074 if ( r
.Contains( wxPoint( x
, y
) ) )
1078 node
= node
->GetNext();
1081 return (wxToolBarToolBase
*)NULL
;
1084 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1086 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1088 return tool
->GetShortHelp();
1090 return wxEmptyString
;
1093 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1096 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1099 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1101 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1102 if ( ( tool
!= NULL
) && tool
->IsButton() )
1103 tool
->UpdateToggleImage( toggle
);
1106 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1108 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1112 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1113 wxSize toolSize
= GetToolSize();
1114 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1115 ControlRef controlHandle
= NULL
;
1118 switch (tool
->GetStyle())
1120 case wxTOOL_STYLE_SEPARATOR
:
1122 wxASSERT( tool
->GetControlHandle() == NULL
);
1125 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1126 toolrect
.bottom
= toolSize
.y
;
1128 toolrect
.right
= toolSize
.x
;
1130 #ifdef __WXMAC_OSX__
1131 // in flat style we need a visual separator
1132 #if wxMAC_USE_NATIVE_TOOLBAR
1133 HIToolbarItemRef item
;
1134 err
= HIToolbarItemCreate(
1135 kHIToolbarSeparatorIdentifier
,
1136 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1139 tool
->SetToolbarItemRef( item
);
1142 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1143 tool
->SetControlHandle( controlHandle
);
1148 case wxTOOL_STYLE_BUTTON
:
1150 wxASSERT( tool
->GetControlHandle() == NULL
);
1151 ControlButtonContentInfo info
;
1152 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1154 if ( UMAGetSystemVersion() >= 0x1000)
1156 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1160 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1161 if ( tool
->CanBeToggled() )
1162 behaviour
|= kControlBehaviorToggles
;
1163 err
= CreateBevelButtonControl( window
,
1164 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1165 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1168 #if wxMAC_USE_NATIVE_TOOLBAR
1169 HIToolbarItemRef item
;
1170 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1171 err
= HIToolbarItemCreate(
1172 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1173 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1176 InstallEventHandler(
1177 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1178 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1179 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1180 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1181 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1182 tool
->SetToolbarItemRef( item
);
1186 wxMacReleaseBitmapButton( &info
);
1189 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1190 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1193 InstallControlEventHandler(
1194 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1195 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1197 tool
->SetControlHandle( controlHandle
);
1201 case wxTOOL_STYLE_CONTROL
:
1202 wxASSERT( tool
->GetControl() != NULL
);
1204 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1205 // FIXME: doesn't work yet...
1207 HIToolbarItemRef item
;
1208 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1209 result
= HIToolbarItemCreate(
1210 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1211 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1213 if ( result
== noErr
)
1215 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1216 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1217 tool
->SetToolbarItemRef( item
);
1219 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1220 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1222 // FIXME: is this necessary ??
1223 ::GetControlBounds( controlHandle
, &toolrect
);
1224 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1226 // FIXME: is this necessary ??
1227 InstallControlEventHandler(
1228 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1229 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1234 // FIXME: right now there's nothing to do here
1244 if ( controlHandle
)
1246 ControlRef container
= (ControlRef
) GetHandle();
1247 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1249 UMAShowControl( controlHandle
);
1250 ::EmbedControl( controlHandle
, container
);
1253 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1254 tool
->UpdateToggleImage( true );
1256 // nothing special to do here - we relayout in Realize() later
1257 tool
->Attach( this );
1258 InvalidateBestSize();
1262 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1263 wxFAIL_MSG( errMsg
.c_str() );
1266 return (err
== noErr
);
1269 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1271 wxFAIL_MSG( wxT("not implemented") );
1274 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1276 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1277 wxToolBarToolsList::compatibility_iterator node
;
1278 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1280 wxToolBarToolBase
*tool2
= node
->GetData();
1281 if ( tool2
== tool
)
1283 // let node point to the next node in the list
1284 node
= node
->GetNext();
1290 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1294 #if wxMAC_USE_NATIVE_TOOLBAR
1295 CFIndex removeIndex
= tool
->GetIndex();
1298 switch ( tool
->GetStyle() )
1300 case wxTOOL_STYLE_CONTROL
:
1302 tool
->GetControl()->Destroy();
1303 tool
->ClearControl();
1307 case wxTOOL_STYLE_BUTTON
:
1308 case wxTOOL_STYLE_SEPARATOR
:
1309 if ( tool
->GetControlHandle() )
1311 #if wxMAC_USE_NATIVE_TOOLBAR
1312 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1314 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1315 tool
->SetIndex( -1 );
1319 tool
->ClearControl();
1327 // and finally reposition all the controls after this one
1329 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1331 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1332 wxPoint pt
= tool2
->GetPosition();
1334 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1339 tool2
->SetPosition( pt
);
1341 #if wxMAC_USE_NATIVE_TOOLBAR
1342 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1343 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1347 InvalidateBestSize();
1352 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1354 #if wxMAC_USE_NATIVE_TOOLBAR
1355 if ( m_macUsesNativeToolbar
)
1367 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1368 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1370 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1371 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1373 HIThemePlacardDrawInfo info
;
1374 memset( &info
, 0, sizeof(info
) );
1376 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1378 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1379 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1380 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1384 // leave the background as it is (striped or metal)
1389 const bool drawBorder
= true;
1393 wxMacPortSetter
helper( &dc
);
1395 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1397 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1398 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1401 if ( toolbarrect
.left
< 0 )
1402 toolbarrect
.left
= 0;
1403 if ( toolbarrect
.top
< 0 )
1404 toolbarrect
.top
= 0;
1407 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1411 #if TARGET_API_MAC_OSX
1412 HIRect hiToolbarrect
= CGRectMake(
1413 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1414 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1415 CGContextRef cgContext
;
1418 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1419 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1421 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1422 CGContextScaleCTM( cgContext
, 1, -1 );
1424 HIThemeBackgroundDrawInfo drawInfo
;
1425 drawInfo
.version
= 0;
1426 drawInfo
.state
= kThemeStateActive
;
1427 drawInfo
.kind
= kThemeBackgroundMetal
;
1428 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1430 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1439 #endif // wxUSE_TOOLBAR