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/bitmap.h"
24 #include "wx/mac/uma.h"
25 #include "wx/geometry.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 class wxToolBarTool
: public wxToolBarToolBase
68 const wxString
& label
,
69 const wxBitmap
& bmpNormal
,
70 const wxBitmap
& bmpDisabled
,
73 const wxString
& shortHelp
,
74 const wxString
& longHelp
);
76 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
77 : wxToolBarToolBase(tbar
, control
)
81 SetControlHandle( (ControlRef
) control
->GetHandle() );
88 #if wxMAC_USE_NATIVE_TOOLBAR
89 if ( m_toolbarItemRef
)
90 CFRelease( m_toolbarItemRef
);
94 WXWidget
GetControlHandle()
96 return (WXWidget
) m_controlHandle
;
99 void SetControlHandle( ControlRef handle
)
101 m_controlHandle
= handle
;
104 void SetPosition( const wxPoint
& position
);
109 if ( m_controlHandle
)
111 DisposeControl( m_controlHandle
);
112 m_controlHandle
= NULL
;
115 #if wxMAC_USE_NATIVE_TOOLBAR
116 m_toolbarItemRef
= NULL
;
120 wxSize
GetSize() const
126 curSize
= GetControl()->GetSize();
128 else if ( IsButton() )
130 curSize
= GetToolBar()->GetToolSize();
135 curSize
= GetToolBar()->GetToolSize();
136 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
145 wxPoint
GetPosition() const
147 return wxPoint( m_x
, m_y
);
150 bool DoEnable( bool enable
);
152 void UpdateToggleImage( bool toggle
);
154 #if wxMAC_USE_NATIVE_TOOLBAR
155 void SetToolbarItemRef( HIToolbarItemRef ref
)
157 if ( m_controlHandle
)
158 HideControl( m_controlHandle
);
159 if ( m_toolbarItemRef
)
160 CFRelease( m_toolbarItemRef
);
162 m_toolbarItemRef
= ref
;
163 if ( m_toolbarItemRef
)
165 HIToolbarItemSetHelpText(
167 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
168 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
172 HIToolbarItemRef
GetToolbarItemRef() const
174 return m_toolbarItemRef
;
177 void SetIndex( CFIndex idx
)
182 CFIndex
GetIndex() const
191 m_controlHandle
= NULL
;
193 #if wxMAC_USE_NATIVE_TOOLBAR
194 m_toolbarItemRef
= NULL
;
199 ControlRef m_controlHandle
;
203 #if wxMAC_USE_NATIVE_TOOLBAR
204 HIToolbarItemRef m_toolbarItemRef
;
205 // position in its toolbar, -1 means not inserted
210 static const EventTypeSpec eventList
[] =
212 { kEventClassControl
, kEventControlHit
},
214 { kEventClassControl
, kEventControlHitTest
},
218 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
220 OSStatus result
= eventNotHandledErr
;
221 ControlRef controlRef
;
222 wxMacCarbonEvent
cEvent( event
);
224 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
226 switch ( GetEventKind( event
) )
228 case kEventControlHit
:
230 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
231 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
232 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
237 shouldToggle
= !tbartool
->IsToggled();
239 shouldToggle
= (GetControl32BitValue( (ControlRef
)(tbartool
->GetControlHandle()) ) != 0);
242 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
245 if (tbartool
!= NULL
)
246 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
252 case kEventControlHitTest
:
254 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
256 HIViewGetBounds( controlRef
, &rect
);
258 ControlPartCode pc
= kControlNoPart
;
259 if ( CGRectContainsPoint( rect
, pt
) )
260 pc
= kControlIconPart
;
261 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
274 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
276 OSStatus result
= eventNotHandledErr
;
278 switch ( GetEventClass( event
) )
280 case kEventClassControl
:
281 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
291 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
293 #if wxMAC_USE_NATIVE_TOOLBAR
295 static const EventTypeSpec toolBarEventList
[] =
297 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
300 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
302 OSStatus result
= eventNotHandledErr
;
304 switch ( GetEventKind( event
) )
306 case kEventToolbarItemPerformAction
:
308 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
309 if ( tbartool
!= NULL
)
311 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
312 int toolID
= tbartool
->GetId();
314 if ( tbartool
->CanBeToggled() )
317 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
321 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
334 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
336 OSStatus result
= eventNotHandledErr
;
338 switch ( GetEventClass( event
) )
340 case kEventClassToolbarItem
:
341 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
351 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
355 bool wxToolBarTool::DoEnable( bool enable
)
359 GetControl()->Enable( enable
);
361 else if ( IsButton() )
363 #if wxMAC_USE_NATIVE_TOOLBAR
364 if ( m_toolbarItemRef
!= NULL
)
365 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
368 if ( m_controlHandle
!= NULL
)
370 #if TARGET_API_MAC_OSX
372 EnableControl( m_controlHandle
);
374 DisableControl( m_controlHandle
);
377 ActivateControl( m_controlHandle
);
379 DeactivateControl( m_controlHandle
);
387 void wxToolBarTool::SetPosition( const wxPoint
& position
)
394 int mac_x
= position
.x
;
395 int mac_y
= position
.y
;
397 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
399 GetToolBar()->MacWindowToRootWindow( &x
, &y
);
407 GetControlBounds( m_controlHandle
, &contrlRect
);
408 int former_mac_x
= contrlRect
.left
;
409 int former_mac_y
= contrlRect
.top
;
410 GetToolBar()->GetToolSize();
412 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
414 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
417 else if ( IsControl() )
419 GetControl()->Move( position
);
426 GetControlBounds( m_controlHandle
, &contrlRect
);
427 int former_mac_x
= contrlRect
.left
;
428 int former_mac_y
= contrlRect
.top
;
430 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
431 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
436 void wxToolBarTool::UpdateToggleImage( bool toggle
)
438 #if wxMAC_USE_NATIVE_TOOLBAR
440 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
441 #define kHIToolbarItemSelected (1 << 7)
444 // FIXME: this should be a OSX v10.4 runtime check
445 if (m_toolbarItemRef
!= NULL
)
447 OptionBits addAttrs
, removeAttrs
;
452 addAttrs
= kHIToolbarItemSelected
;
453 removeAttrs
= kHIToolbarItemNoAttributes
;
457 addAttrs
= kHIToolbarItemNoAttributes
;
458 removeAttrs
= kHIToolbarItemSelected
;
461 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
468 int w
= m_bmpNormal
.GetWidth();
469 int h
= m_bmpNormal
.GetHeight();
470 wxBitmap
bmp( w
, h
);
473 dc
.SelectObject( bmp
);
474 dc
.SetPen( wxNullPen
);
475 dc
.SetBackground( *wxWHITE
);
476 dc
.DrawRectangle( 0, 0, w
, h
);
477 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
478 dc
.SelectObject( wxNullBitmap
);
479 ControlButtonContentInfo info
;
480 wxMacCreateBitmapButton( &info
, bmp
);
481 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
482 wxMacReleaseBitmapButton( &info
);
486 ControlButtonContentInfo info
;
487 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
488 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
489 wxMacReleaseBitmapButton( &info
);
492 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
494 m_controlHandle
, 0, kControlIconTransformTag
,
495 sizeof(transform
), (Ptr
)&transform
);
496 HIViewSetNeedsDisplay( m_controlHandle
, true );
499 ::SetControl32BitValue( m_controlHandle
, toggle
);
503 wxToolBarTool::wxToolBarTool(
506 const wxString
& label
,
507 const wxBitmap
& bmpNormal
,
508 const wxBitmap
& bmpDisabled
,
510 wxObject
*clientData
,
511 const wxString
& shortHelp
,
512 const wxString
& longHelp
)
515 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
516 clientData
, shortHelp
, longHelp
)
522 #pragma mark Toolbar Implementation
524 wxToolBarToolBase
*wxToolBar::CreateTool(
526 const wxString
& label
,
527 const wxBitmap
& bmpNormal
,
528 const wxBitmap
& bmpDisabled
,
530 wxObject
*clientData
,
531 const wxString
& shortHelp
,
532 const wxString
& longHelp
)
534 return new wxToolBarTool(
535 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
536 clientData
, shortHelp
, longHelp
);
539 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
541 return new wxToolBarTool( this, control
);
544 void wxToolBar::Init()
548 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
549 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
551 #if wxMAC_USE_NATIVE_TOOLBAR
552 m_macHIToolbarRef
= NULL
;
553 m_macUsesNativeToolbar
= false;
557 // also for the toolbar we have the dual implementation:
558 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
560 bool wxToolBar::Create(
566 const wxString
& name
)
568 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
571 OSStatus err
= noErr
;
573 #if wxMAC_USE_NATIVE_TOOLBAR
574 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
575 err
= HIToolbarCreate(
576 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
577 (HIToolbarRef
*) &m_macHIToolbarRef
);
579 if (m_macHIToolbarRef
!= NULL
)
581 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
582 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
584 if ( style
& wxTB_NOICONS
)
585 mode
= kHIToolbarDisplayModeLabelOnly
;
586 else if ( style
& wxTB_TEXT
)
587 mode
= kHIToolbarDisplayModeIconAndLabel
;
589 mode
= kHIToolbarDisplayModeIconOnly
;
591 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
592 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
596 return (err
== noErr
);
599 wxToolBar::~wxToolBar()
601 #if wxMAC_USE_NATIVE_TOOLBAR
602 if (m_macHIToolbarRef
!= NULL
)
604 // if this is the installed toolbar, then deinstall it
605 if (m_macUsesNativeToolbar
)
606 MacInstallNativeToolbar( false );
608 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
609 m_macHIToolbarRef
= NULL
;
614 bool wxToolBar::Show( bool show
)
616 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
617 bool bResult
= (tlw
!= NULL
);
621 #if wxMAC_USE_NATIVE_TOOLBAR
622 bool ownToolbarInstalled
= false;
623 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
624 if (ownToolbarInstalled
)
626 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
628 ShowHideWindowToolbar( tlw
, show
, false );
631 bResult
= wxToolBarBase::Show( show
);
634 bResult
= wxToolBarBase::Show( show
);
641 bool wxToolBar::IsShown() const
645 #if wxMAC_USE_NATIVE_TOOLBAR
646 bool ownToolbarInstalled
;
648 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
649 if (ownToolbarInstalled
)
651 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
652 bResult
= IsWindowToolbarVisible( tlw
);
655 bResult
= wxToolBarBase::IsShown();
658 bResult
= wxToolBarBase::IsShown();
664 void wxToolBar::DoGetSize( int *width
, int *height
) const
666 #if wxMAC_USE_NATIVE_TOOLBAR
668 bool ownToolbarInstalled
;
670 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
671 if ( ownToolbarInstalled
)
673 // TODO: is this really a control ?
674 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
676 *width
= boundsR
.right
- boundsR
.left
;
677 if ( height
!= NULL
)
678 *height
= boundsR
.bottom
- boundsR
.top
;
681 wxToolBarBase::DoGetSize( width
, height
);
684 wxToolBarBase::DoGetSize( width
, height
);
688 wxSize
wxToolBar::DoGetBestSize() const
692 DoGetSize( &width
, &height
);
694 return wxSize( width
, height
);
697 void wxToolBar::SetWindowStyleFlag( long style
)
699 wxToolBarBase::SetWindowStyleFlag( style
);
701 #if wxMAC_USE_NATIVE_TOOLBAR
702 if (m_macHIToolbarRef
!= NULL
)
704 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
706 if ( style
& wxTB_NOICONS
)
707 mode
= kHIToolbarDisplayModeLabelOnly
;
708 else if ( style
& wxTB_TEXT
)
709 mode
= kHIToolbarDisplayModeIconAndLabel
;
711 mode
= kHIToolbarDisplayModeIconOnly
;
713 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
718 #if wxMAC_USE_NATIVE_TOOLBAR
719 bool wxToolBar::MacWantsNativeToolbar()
721 return m_macUsesNativeToolbar
;
724 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
726 bool bResultV
= false;
728 if (ownToolbarInstalled
!= NULL
)
729 *ownToolbarInstalled
= false;
731 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
734 HIToolbarRef curToolbarRef
= NULL
;
735 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
736 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
737 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
738 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
744 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
746 bool bResult
= false;
748 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
751 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
754 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
758 // check the existing toolbar
759 HIToolbarRef curToolbarRef
= NULL
;
760 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
762 curToolbarRef
= NULL
;
764 m_macUsesNativeToolbar
= usesNative
;
766 if (m_macUsesNativeToolbar
)
768 // only install toolbar if there isn't one installed already
769 if (curToolbarRef
== NULL
)
773 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
774 ShowHideWindowToolbar( tlw
, true, false );
775 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
776 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
778 Rect r
= { 0, 0, 0, 0 };
779 m_peer
->SetRect( &r
);
780 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
781 m_peer
->SetVisibility( false, true );
782 wxToolBarBase::Show( false );
787 // only deinstall toolbar if this is the installed one
788 if (m_macHIToolbarRef
== curToolbarRef
)
792 ShowHideWindowToolbar( tlw
, false, false );
793 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
794 SetWindowToolbar( tlw
, NULL
);
796 m_peer
->SetVisibility( true, true );
801 InvalidateBestSize();
803 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
808 bool wxToolBar::Realize()
810 if (m_tools
.GetCount() == 0)
816 int maxToolWidth
= 0;
817 int maxToolHeight
= 0;
819 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
820 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
825 // find the maximum tool width and height
827 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
828 while ( node
!= NULL
)
830 tool
= (wxToolBarTool
*) node
->GetData();
833 wxSize sz
= tool
->GetSize();
835 if ( sz
.x
> maxToolWidth
)
837 if ( sz
.y
> maxToolHeight
)
838 maxToolHeight
= sz
.y
;
841 node
= node
->GetNext();
844 bool lastIsRadio
= false;
845 bool curIsRadio
= false;
846 bool setChoiceInGroup
= false;
848 #if wxMAC_USE_NATIVE_TOOLBAR
849 CFIndex currentPosition
= 0;
850 bool insertAll
= false;
853 node
= m_tools
.GetFirst();
854 while ( node
!= NULL
)
856 tool
= (wxToolBarTool
*) node
->GetData();
859 node
= node
->GetNext();
863 // set tool position:
864 // for the moment just perform a single row/column alignment
865 wxSize cursize
= tool
->GetSize();
866 if ( x
+ cursize
.x
> maxWidth
)
867 maxWidth
= x
+ cursize
.x
;
868 if ( y
+ cursize
.y
> maxHeight
)
869 maxHeight
= y
+ cursize
.y
;
871 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
873 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
874 tool
->SetPosition( wxPoint(x1
, y
) );
878 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
879 tool
->SetPosition( wxPoint(x
, y1
) );
882 // update the item positioning state
883 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
884 y
+= cursize
.y
+ kwxMacToolSpacing
;
886 x
+= cursize
.x
+ kwxMacToolSpacing
;
888 #if wxMAC_USE_NATIVE_TOOLBAR
889 // install in native HIToolbar
890 if ( m_macHIToolbarRef
!= NULL
)
892 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
893 if ( hiItemRef
!= NULL
)
895 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
897 OSStatus err
= noErr
;
902 // if this is the first tool that gets newly inserted or repositioned
903 // first remove all 'old' tools from here to the right, because of this
904 // all following tools will have to be reinserted (insertAll). i = 100 because there's
905 // no way to determine how many there are in a toolbar, so just a high number :-(
906 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
908 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
913 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
914 wxFAIL_MSG( errMsg
.c_str() );
918 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
921 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
922 wxFAIL_MSG( errMsg
.c_str() );
925 tool
->SetIndex( currentPosition
);
933 // update radio button (and group) state
934 lastIsRadio
= curIsRadio
;
935 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
939 if ( tool
->IsToggled() )
940 DoToggleTool( tool
, true );
942 setChoiceInGroup
= false;
948 if ( tool
->Toggle( true ) )
950 DoToggleTool( tool
, true );
951 setChoiceInGroup
= true;
954 else if ( tool
->IsToggled() )
956 if ( tool
->IsToggled() )
957 DoToggleTool( tool
, true );
959 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
960 while ( nodePrev
!= NULL
)
962 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
963 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
966 if ( toggleTool
->Toggle( false ) )
967 DoToggleTool( toggleTool
, false );
969 nodePrev
= nodePrev
->GetPrevious();
974 node
= node
->GetNext();
977 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
979 // if not set yet, only one row
980 if ( m_maxRows
<= 0 )
983 m_minWidth
= maxWidth
;
985 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
986 m_minHeight
= m_maxHeight
= maxHeight
;
990 // if not set yet, have one column
991 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
992 SetRows( GetToolsCount() );
994 m_minHeight
= maxHeight
;
996 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
997 m_minWidth
= m_maxWidth
= maxWidth
;
1001 // FIXME: should this be OSX-only?
1003 bool wantNativeToolbar
, ownToolbarInstalled
;
1005 // attempt to install the native toolbar
1006 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1007 MacInstallNativeToolbar( wantNativeToolbar
);
1008 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1009 if (!ownToolbarInstalled
)
1011 SetSize( maxWidth
, maxHeight
);
1012 InvalidateBestSize();
1016 SetSize( maxWidth
, maxHeight
);
1017 InvalidateBestSize();
1020 SetBestFittingSize();
1025 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1027 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1028 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1030 #if wxMAC_USE_NATIVE_TOOLBAR
1031 if (m_macHIToolbarRef
!= NULL
)
1033 int maxs
= wxMax( size
.x
, size
.y
);
1034 HIToolbarDisplaySize sizeSpec
;
1036 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1037 else if ( maxs
> 24 )
1038 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1040 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1042 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1047 // The button size is bigger than the bitmap size
1048 wxSize
wxToolBar::GetToolSize() const
1050 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1053 void wxToolBar::SetRows(int nRows
)
1055 // avoid resizing the frame uselessly
1056 if ( nRows
!= m_maxRows
)
1060 void wxToolBar::MacSuperChangedPosition()
1062 wxWindow::MacSuperChangedPosition();
1064 #if wxMAC_USE_NATIVE_TOOLBAR
1065 if (! m_macUsesNativeToolbar
)
1073 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1075 wxToolBarTool
*tool
;
1076 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1077 while ( node
!= NULL
)
1079 tool
= (wxToolBarTool
*)node
->GetData();
1082 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1083 if ( r
.Contains( wxPoint( x
, y
) ) )
1087 node
= node
->GetNext();
1090 return (wxToolBarToolBase
*)NULL
;
1093 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1095 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1097 return tool
->GetShortHelp();
1099 return wxEmptyString
;
1102 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1105 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1108 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1110 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1111 if ( ( tool
!= NULL
) && tool
->IsButton() )
1112 tool
->UpdateToggleImage( toggle
);
1115 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1117 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1121 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1122 wxSize toolSize
= GetToolSize();
1123 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1124 ControlRef controlHandle
= NULL
;
1127 switch (tool
->GetStyle())
1129 case wxTOOL_STYLE_SEPARATOR
:
1131 wxASSERT( tool
->GetControlHandle() == NULL
);
1134 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1135 toolrect
.bottom
= toolSize
.y
;
1137 toolrect
.right
= toolSize
.x
;
1139 #ifdef __WXMAC_OSX__
1140 // in flat style we need a visual separator
1141 #if wxMAC_USE_NATIVE_TOOLBAR
1142 HIToolbarItemRef item
;
1143 err
= HIToolbarItemCreate(
1144 kHIToolbarSeparatorIdentifier
,
1145 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1148 tool
->SetToolbarItemRef( item
);
1151 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1152 tool
->SetControlHandle( controlHandle
);
1157 case wxTOOL_STYLE_BUTTON
:
1159 wxASSERT( tool
->GetControlHandle() == NULL
);
1160 ControlButtonContentInfo info
;
1161 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1163 if ( UMAGetSystemVersion() >= 0x1000)
1165 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1169 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1170 if ( tool
->CanBeToggled() )
1171 behaviour
|= kControlBehaviorToggles
;
1172 err
= CreateBevelButtonControl( window
,
1173 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1174 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1177 #if wxMAC_USE_NATIVE_TOOLBAR
1178 HIToolbarItemRef item
;
1179 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1180 err
= HIToolbarItemCreate(
1181 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1182 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1185 InstallEventHandler(
1186 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1187 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1188 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1189 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1190 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1191 tool
->SetToolbarItemRef( item
);
1195 wxMacReleaseBitmapButton( &info
);
1198 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1199 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1202 InstallControlEventHandler(
1203 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1204 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1206 tool
->SetControlHandle( controlHandle
);
1210 case wxTOOL_STYLE_CONTROL
:
1211 wxASSERT( tool
->GetControl() != NULL
);
1213 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1214 // FIXME: doesn't work yet...
1216 HIToolbarItemRef item
;
1217 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1218 result
= HIToolbarItemCreate(
1219 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1220 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1222 if ( result
== noErr
)
1224 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1225 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1226 tool
->SetToolbarItemRef( item
);
1228 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1229 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1231 // FIXME: is this necessary ??
1232 ::GetControlBounds( controlHandle
, &toolrect
);
1233 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1235 // FIXME: is this necessary ??
1236 InstallControlEventHandler(
1237 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1238 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1243 // FIXME: right now there's nothing to do here
1253 if ( controlHandle
)
1255 ControlRef container
= (ControlRef
) GetHandle();
1256 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1258 UMAShowControl( controlHandle
);
1259 ::EmbedControl( controlHandle
, container
);
1262 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1263 tool
->UpdateToggleImage( true );
1265 // nothing special to do here - we relayout in Realize() later
1266 tool
->Attach( this );
1267 InvalidateBestSize();
1271 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1272 wxFAIL_MSG( errMsg
.c_str() );
1275 return (err
== noErr
);
1278 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1280 wxFAIL_MSG( wxT("not implemented") );
1283 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1285 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1286 wxToolBarToolsList::compatibility_iterator node
;
1287 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1289 wxToolBarToolBase
*tool2
= node
->GetData();
1290 if ( tool2
== tool
)
1292 // let node point to the next node in the list
1293 node
= node
->GetNext();
1299 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1303 #if wxMAC_USE_NATIVE_TOOLBAR
1304 CFIndex removeIndex
= tool
->GetIndex();
1307 switch ( tool
->GetStyle() )
1309 case wxTOOL_STYLE_CONTROL
:
1311 tool
->GetControl()->Destroy();
1312 tool
->ClearControl();
1316 case wxTOOL_STYLE_BUTTON
:
1317 case wxTOOL_STYLE_SEPARATOR
:
1318 if ( tool
->GetControlHandle() )
1320 #if wxMAC_USE_NATIVE_TOOLBAR
1321 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1323 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1324 tool
->SetIndex( -1 );
1328 tool
->ClearControl();
1336 // and finally reposition all the controls after this one
1338 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1340 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1341 wxPoint pt
= tool2
->GetPosition();
1343 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1348 tool2
->SetPosition( pt
);
1350 #if wxMAC_USE_NATIVE_TOOLBAR
1351 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1352 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1356 InvalidateBestSize();
1361 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1363 #if wxMAC_USE_NATIVE_TOOLBAR
1364 if ( m_macUsesNativeToolbar
)
1376 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1377 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1379 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1380 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1382 HIThemePlacardDrawInfo info
;
1383 memset( &info
, 0, sizeof(info
) );
1385 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1387 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1388 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1389 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1393 // leave the background as it is (striped or metal)
1398 const bool drawBorder
= true;
1402 wxMacPortSetter
helper( &dc
);
1404 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1406 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1407 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1410 if ( toolbarrect
.left
< 0 )
1411 toolbarrect
.left
= 0;
1412 if ( toolbarrect
.top
< 0 )
1413 toolbarrect
.top
= 0;
1416 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1420 #if TARGET_API_MAC_OSX
1421 HIRect hiToolbarrect
= CGRectMake(
1422 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1423 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1424 CGContextRef cgContext
;
1427 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1428 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1430 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1431 CGContextScaleCTM( cgContext
, 1, -1 );
1433 HIThemeBackgroundDrawInfo drawInfo
;
1434 drawInfo
.version
= 0;
1435 drawInfo
.state
= kThemeStateActive
;
1436 drawInfo
.kind
= kThemeBackgroundMetal
;
1437 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1439 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1448 #endif // wxUSE_TOOLBAR