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"
17 #include "wx/bitmap.h"
18 #include "wx/toolbar.h"
20 #include "wx/mac/uma.h"
21 #include "wx/geometry.h"
25 const short kwxMacToolBarToolDefaultWidth
= 16;
26 const short kwxMacToolBarToolDefaultHeight
= 16;
27 const short kwxMacToolBarTopMargin
= 4;
28 const short kwxMacToolBarLeftMargin
= 4;
29 const short kwxMacToolBorder
= 0;
30 const short kwxMacToolSpacing
= 6;
32 const short kwxMacToolBarToolDefaultWidth
= 24;
33 const short kwxMacToolBarToolDefaultHeight
= 22;
34 const short kwxMacToolBarTopMargin
= 2;
35 const short kwxMacToolBarLeftMargin
= 2;
36 const short kwxMacToolBorder
= 4;
37 const short kwxMacToolSpacing
= 0;
41 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
43 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
44 EVT_PAINT( wxToolBar::OnPaint
)
49 #pragma mark Tool Implementation
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
58 class wxToolBarTool
: public wxToolBarToolBase
64 const wxString
& label
,
65 const wxBitmap
& bmpNormal
,
66 const wxBitmap
& bmpDisabled
,
69 const wxString
& shortHelp
,
70 const wxString
& longHelp
);
72 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
73 : wxToolBarToolBase(tbar
, control
)
77 SetControlHandle( (ControlRef
) control
->GetHandle() );
83 if ( m_controlHandle
)
84 DisposeControl( m_controlHandle
);
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
);
108 #if wxMAC_USE_NATIVE_TOOLBAR
109 m_toolbarItemRef
= NULL
;
113 wxSize
GetSize() const
119 curSize
= GetControl()->GetSize();
121 else if ( IsButton() )
123 curSize
= GetToolBar()->GetToolSize();
128 curSize
= GetToolBar()->GetToolSize();
129 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
138 wxPoint
GetPosition() const
140 return wxPoint( m_x
, m_y
);
143 bool DoEnable( bool enable
);
145 void UpdateToggleImage( bool toggle
);
147 #if wxMAC_USE_NATIVE_TOOLBAR
148 void SetToolbarItemRef( HIToolbarItemRef ref
)
150 if ( m_controlHandle
)
151 HideControl( m_controlHandle
);
152 if ( m_toolbarItemRef
)
153 CFRelease( m_toolbarItemRef
);
155 m_toolbarItemRef
= ref
;
156 if ( m_toolbarItemRef
)
158 HIToolbarItemSetHelpText(
160 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
161 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
165 HIToolbarItemRef
GetToolbarItemRef() const
167 return m_toolbarItemRef
;
170 void SetIndex( CFIndex idx
)
175 CFIndex
GetIndex() const
184 m_controlHandle
= NULL
;
186 #if wxMAC_USE_NATIVE_TOOLBAR
187 m_toolbarItemRef
= NULL
;
192 ControlRef m_controlHandle
;
196 #if wxMAC_USE_NATIVE_TOOLBAR
197 HIToolbarItemRef m_toolbarItemRef
;
198 // position in its toolbar, -1 means not inserted
203 static const EventTypeSpec eventList
[] =
205 { kEventClassControl
, kEventControlHit
},
207 { kEventClassControl
, kEventControlHitTest
},
211 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
213 OSStatus result
= eventNotHandledErr
;
214 ControlRef controlRef
;
215 wxMacCarbonEvent
cEvent( event
);
217 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
219 switch ( GetEventKind( event
) )
221 case kEventControlHit
:
223 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
224 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
225 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
230 shouldToggle
= !tbartool
->IsToggled();
232 shouldToggle
= (GetControl32BitValue( (ControlRef
)(tbartool
->GetControlHandle()) ) != 0);
235 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
238 if (tbartool
!= NULL
)
239 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
245 case kEventControlHitTest
:
247 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
249 HIViewGetBounds( controlRef
, &rect
);
251 ControlPartCode pc
= kControlNoPart
;
252 if ( CGRectContainsPoint( rect
, pt
) )
253 pc
= kControlIconPart
;
254 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
267 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
269 OSStatus result
= eventNotHandledErr
;
271 switch ( GetEventClass( event
) )
273 case kEventClassControl
:
274 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
284 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
286 #if wxMAC_USE_NATIVE_TOOLBAR
288 static const EventTypeSpec toolBarEventList
[] =
290 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
293 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
295 OSStatus result
= eventNotHandledErr
;
297 switch ( GetEventKind( event
) )
299 case kEventToolbarItemPerformAction
:
301 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
302 if ( tbartool
!= NULL
)
304 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
305 int toolID
= tbartool
->GetId();
307 if ( tbartool
->CanBeToggled() )
310 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
314 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
327 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
329 OSStatus result
= eventNotHandledErr
;
331 switch ( GetEventClass( event
) )
333 case kEventClassToolbarItem
:
334 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
344 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
348 bool wxToolBarTool::DoEnable( bool enable
)
352 GetControl()->Enable( enable
);
354 else if ( IsButton() )
356 #if wxMAC_USE_NATIVE_TOOLBAR
357 if ( m_toolbarItemRef
!= NULL
)
358 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
361 if ( m_controlHandle
!= NULL
)
363 #if TARGET_API_MAC_OSX
365 EnableControl( m_controlHandle
);
367 DisableControl( m_controlHandle
);
370 ActivateControl( m_controlHandle
);
372 DeactivateControl( m_controlHandle
);
380 void wxToolBarTool::SetPosition( const wxPoint
& position
)
387 int mac_x
= position
.x
;
388 int mac_y
= position
.y
;
390 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
392 GetToolBar()->MacWindowToRootWindow( &x
, &y
);
400 GetControlBounds( m_controlHandle
, &contrlRect
);
401 int former_mac_x
= contrlRect
.left
;
402 int former_mac_y
= contrlRect
.top
;
403 GetToolBar()->GetToolSize();
405 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
407 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
410 else if ( IsControl() )
412 GetControl()->Move( position
);
419 GetControlBounds( m_controlHandle
, &contrlRect
);
420 int former_mac_x
= contrlRect
.left
;
421 int former_mac_y
= contrlRect
.top
;
423 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
424 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
429 void wxToolBarTool::UpdateToggleImage( bool toggle
)
431 #if wxMAC_USE_NATIVE_TOOLBAR
433 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
434 #define kHIToolbarItemSelected (1 << 7)
437 // FIXME: this should be a OSX v10.4 runtime check
438 if (m_toolbarItemRef
!= NULL
)
440 OptionBits addAttrs
, removeAttrs
;
445 addAttrs
= kHIToolbarItemSelected
;
446 removeAttrs
= kHIToolbarItemNoAttributes
;
450 addAttrs
= kHIToolbarItemNoAttributes
;
451 removeAttrs
= kHIToolbarItemSelected
;
454 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
461 int w
= m_bmpNormal
.GetWidth();
462 int h
= m_bmpNormal
.GetHeight();
463 wxBitmap
bmp( w
, h
);
466 dc
.SelectObject( bmp
);
467 dc
.SetPen( wxNullPen
);
468 dc
.SetBackground( *wxWHITE
);
469 dc
.DrawRectangle( 0, 0, w
, h
);
470 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
471 dc
.SelectObject( wxNullBitmap
);
472 ControlButtonContentInfo info
;
473 wxMacCreateBitmapButton( &info
, bmp
);
474 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
475 wxMacReleaseBitmapButton( &info
);
479 ControlButtonContentInfo info
;
480 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
481 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
482 wxMacReleaseBitmapButton( &info
);
485 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
487 m_controlHandle
, 0, kControlIconTransformTag
,
488 sizeof(transform
), (Ptr
)&transform
);
489 HIViewSetNeedsDisplay( m_controlHandle
, true );
492 ::SetControl32BitValue( m_controlHandle
, toggle
);
496 wxToolBarTool::wxToolBarTool(
499 const wxString
& label
,
500 const wxBitmap
& bmpNormal
,
501 const wxBitmap
& bmpDisabled
,
503 wxObject
*clientData
,
504 const wxString
& shortHelp
,
505 const wxString
& longHelp
)
508 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
509 clientData
, shortHelp
, longHelp
)
515 #pragma mark Toolbar Implementation
517 wxToolBarToolBase
*wxToolBar::CreateTool(
519 const wxString
& label
,
520 const wxBitmap
& bmpNormal
,
521 const wxBitmap
& bmpDisabled
,
523 wxObject
*clientData
,
524 const wxString
& shortHelp
,
525 const wxString
& longHelp
)
527 return new wxToolBarTool(
528 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
529 clientData
, shortHelp
, longHelp
);
532 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
534 return new wxToolBarTool( this, control
);
537 void wxToolBar::Init()
541 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
542 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
544 #if wxMAC_USE_NATIVE_TOOLBAR
545 m_macHIToolbarRef
= NULL
;
546 m_macUsesNativeToolbar
= false;
550 // also for the toolbar we have the dual implementation:
551 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
553 bool wxToolBar::Create(
559 const wxString
& name
)
561 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
564 OSStatus err
= noErr
;
566 #if wxMAC_USE_NATIVE_TOOLBAR
567 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
568 err
= HIToolbarCreate(
569 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
570 (HIToolbarRef
*) &m_macHIToolbarRef
);
572 if (m_macHIToolbarRef
!= NULL
)
574 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
575 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
577 if ( style
& wxTB_NOICONS
)
578 mode
= kHIToolbarDisplayModeLabelOnly
;
579 else if ( style
& wxTB_TEXT
)
580 mode
= kHIToolbarDisplayModeIconAndLabel
;
582 mode
= kHIToolbarDisplayModeIconOnly
;
584 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
585 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
589 return (err
== noErr
);
592 wxToolBar::~wxToolBar()
594 #if wxMAC_USE_NATIVE_TOOLBAR
595 if (m_macHIToolbarRef
!= NULL
)
597 // if this is the installed toolbar, then deinstall it
598 if (m_macUsesNativeToolbar
)
599 MacInstallNativeToolbar( false );
601 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
602 m_macHIToolbarRef
= NULL
;
607 bool wxToolBar::Show( bool show
)
609 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
610 bool bResult
= (tlw
!= NULL
);
614 #if wxMAC_USE_NATIVE_TOOLBAR
615 bool ownToolbarInstalled
= false;
616 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
617 if (ownToolbarInstalled
)
619 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
621 ShowHideWindowToolbar( tlw
, show
, false );
624 bResult
= wxToolBarBase::Show( show
);
627 bResult
= wxToolBarBase::Show( show
);
634 bool wxToolBar::IsShown() const
638 #if wxMAC_USE_NATIVE_TOOLBAR
639 bool ownToolbarInstalled
;
641 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
642 if (ownToolbarInstalled
)
644 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
645 bResult
= IsWindowToolbarVisible( tlw
);
648 bResult
= wxToolBarBase::IsShown();
651 bResult
= wxToolBarBase::IsShown();
657 void wxToolBar::DoGetSize( int *width
, int *height
) const
659 #if wxMAC_USE_NATIVE_TOOLBAR
661 bool ownToolbarInstalled
;
663 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
664 if ( ownToolbarInstalled
)
666 // TODO: is this really a control ?
667 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
669 *width
= boundsR
.right
- boundsR
.left
;
670 if ( height
!= NULL
)
671 *height
= boundsR
.bottom
- boundsR
.top
;
674 wxToolBarBase::DoGetSize( width
, height
);
677 wxToolBarBase::DoGetSize( width
, height
);
681 wxSize
wxToolBar::DoGetBestSize() const
685 DoGetSize( &width
, &height
);
687 return wxSize( width
, height
);
690 void wxToolBar::SetWindowStyleFlag( long style
)
692 wxToolBarBase::SetWindowStyleFlag( style
);
694 #if wxMAC_USE_NATIVE_TOOLBAR
695 if (m_macHIToolbarRef
!= NULL
)
697 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
699 if ( style
& wxTB_NOICONS
)
700 mode
= kHIToolbarDisplayModeLabelOnly
;
701 else if ( style
& wxTB_TEXT
)
702 mode
= kHIToolbarDisplayModeIconAndLabel
;
704 mode
= kHIToolbarDisplayModeIconOnly
;
706 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
711 #if wxMAC_USE_NATIVE_TOOLBAR
712 bool wxToolBar::MacWantsNativeToolbar()
714 return m_macUsesNativeToolbar
;
717 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
719 bool bResultV
= false;
721 if (ownToolbarInstalled
!= NULL
)
722 *ownToolbarInstalled
= false;
724 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
727 HIToolbarRef curToolbarRef
= NULL
;
728 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
729 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
730 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
731 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
737 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
739 bool bResult
= false;
741 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
744 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
747 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
751 // check the existing toolbar
752 HIToolbarRef curToolbarRef
= NULL
;
753 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
755 curToolbarRef
= NULL
;
757 m_macUsesNativeToolbar
= usesNative
;
759 if (m_macUsesNativeToolbar
)
761 // only install toolbar if there isn't one installed already
762 if (curToolbarRef
== NULL
)
766 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
767 ShowHideWindowToolbar( tlw
, true, false );
768 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
769 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
771 Rect r
= { 0, 0, 0, 0 };
772 m_peer
->SetRect( &r
);
773 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
774 m_peer
->SetVisibility( false, true );
775 wxToolBarBase::Show( false );
780 // only deinstall toolbar if this is the installed one
781 if (m_macHIToolbarRef
== curToolbarRef
)
785 ShowHideWindowToolbar( tlw
, false, false );
786 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
787 SetWindowToolbar( tlw
, NULL
);
789 m_peer
->SetVisibility( true, true );
794 InvalidateBestSize();
796 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
801 bool wxToolBar::Realize()
803 if (m_tools
.GetCount() == 0)
809 int maxToolWidth
= 0;
810 int maxToolHeight
= 0;
812 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
813 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
818 // find the maximum tool width and height
820 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
821 while ( node
!= NULL
)
823 tool
= (wxToolBarTool
*) node
->GetData();
826 wxSize sz
= tool
->GetSize();
828 if ( sz
.x
> maxToolWidth
)
830 if ( sz
.y
> maxToolHeight
)
831 maxToolHeight
= sz
.y
;
834 node
= node
->GetNext();
837 bool lastIsRadio
= false;
838 bool curIsRadio
= false;
839 bool setChoiceInGroup
= false;
841 #if wxMAC_USE_NATIVE_TOOLBAR
842 CFIndex currentPosition
= 0;
843 bool insertAll
= false;
846 node
= m_tools
.GetFirst();
847 while ( node
!= NULL
)
849 tool
= (wxToolBarTool
*) node
->GetData();
852 node
= node
->GetNext();
856 // set tool position:
857 // for the moment just perform a single row/column alignment
858 wxSize cursize
= tool
->GetSize();
859 if ( x
+ cursize
.x
> maxWidth
)
860 maxWidth
= x
+ cursize
.x
;
861 if ( y
+ cursize
.y
> maxHeight
)
862 maxHeight
= y
+ cursize
.y
;
864 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
866 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
867 tool
->SetPosition( wxPoint(x1
, y
) );
871 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
872 tool
->SetPosition( wxPoint(x
, y1
) );
875 // update the item positioning state
876 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
877 y
+= cursize
.y
+ kwxMacToolSpacing
;
879 x
+= cursize
.x
+ kwxMacToolSpacing
;
881 #if wxMAC_USE_NATIVE_TOOLBAR
882 // install in native HIToolbar
883 if ( m_macHIToolbarRef
!= NULL
)
885 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
886 if ( hiItemRef
!= NULL
)
888 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
890 OSStatus err
= noErr
;
895 // if this is the first tool that gets newly inserted or repositioned
896 // first remove all 'old' tools from here to the right, because of this
897 // all following tools will have to be reinserted (insertAll). i = 100 because there's
898 // no way to determine how many there are in a toolbar, so just a high number :-(
899 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
901 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
906 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
907 wxASSERT_MSG( 0, errMsg
.c_str() );
911 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
914 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
915 wxASSERT_MSG( 0, errMsg
.c_str() );
918 tool
->SetIndex( currentPosition
);
926 // update radio button (and group) state
927 lastIsRadio
= curIsRadio
;
928 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
932 if ( tool
->IsToggled() )
933 DoToggleTool( tool
, true );
935 setChoiceInGroup
= false;
941 if ( tool
->Toggle( true ) )
943 DoToggleTool( tool
, true );
944 setChoiceInGroup
= true;
947 else if ( tool
->IsToggled() )
949 if ( tool
->IsToggled() )
950 DoToggleTool( tool
, true );
952 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
953 while ( nodePrev
!= NULL
)
955 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
956 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
959 if ( toggleTool
->Toggle( false ) )
960 DoToggleTool( toggleTool
, false );
962 nodePrev
= nodePrev
->GetPrevious();
967 node
= node
->GetNext();
970 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
972 // if not set yet, only one row
973 if ( m_maxRows
<= 0 )
976 m_minWidth
= maxWidth
;
978 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
979 m_minHeight
= m_maxHeight
= maxHeight
;
983 // if not set yet, have one column
984 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
985 SetRows( GetToolsCount() );
987 m_minHeight
= maxHeight
;
989 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
990 m_minWidth
= m_maxWidth
= maxWidth
;
994 // FIXME: should this be OSX-only?
996 bool wantNativeToolbar
, ownToolbarInstalled
;
998 // attempt to install the native toolbar
999 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1000 MacInstallNativeToolbar( wantNativeToolbar
);
1001 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1002 if (!ownToolbarInstalled
)
1004 SetSize( maxWidth
, maxHeight
);
1005 InvalidateBestSize();
1009 SetSize( maxWidth
, maxHeight
);
1010 InvalidateBestSize();
1013 SetBestFittingSize();
1018 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1020 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1021 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1023 #if wxMAC_USE_NATIVE_TOOLBAR
1024 if (m_macHIToolbarRef
!= NULL
)
1026 int maxs
= wxMax( size
.x
, size
.y
);
1027 HIToolbarDisplaySize sizeSpec
;
1029 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1030 else if ( maxs
> 24 )
1031 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1033 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1035 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1040 // The button size is bigger than the bitmap size
1041 wxSize
wxToolBar::GetToolSize() const
1043 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1046 void wxToolBar::SetRows(int nRows
)
1048 // avoid resizing the frame uselessly
1049 if ( nRows
!= m_maxRows
)
1053 void wxToolBar::MacSuperChangedPosition()
1055 wxWindow::MacSuperChangedPosition();
1057 #if wxMAC_USE_NATIVE_TOOLBAR
1058 if (! m_macUsesNativeToolbar
)
1066 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1068 wxToolBarTool
*tool
;
1069 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1070 while ( node
!= NULL
)
1072 tool
= (wxToolBarTool
*)node
->GetData();
1075 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1076 if ( r
.Contains( wxPoint( x
, y
) ) )
1080 node
= node
->GetNext();
1083 return (wxToolBarToolBase
*)NULL
;
1086 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1088 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1090 return tool
->GetShortHelp();
1092 return wxEmptyString
;
1095 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1098 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1101 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1103 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1104 if ( ( tool
!= NULL
) && tool
->IsButton() )
1105 tool
->UpdateToggleImage( toggle
);
1108 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1110 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1114 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1115 wxSize toolSize
= GetToolSize();
1116 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1117 ControlRef controlHandle
= NULL
;
1120 switch (tool
->GetStyle())
1122 case wxTOOL_STYLE_SEPARATOR
:
1124 wxASSERT( tool
->GetControlHandle() == NULL
);
1127 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1128 toolrect
.bottom
= toolSize
.y
;
1130 toolrect
.right
= toolSize
.x
;
1132 #ifdef __WXMAC_OSX__
1133 // in flat style we need a visual separator
1134 #if wxMAC_USE_NATIVE_TOOLBAR
1135 HIToolbarItemRef item
;
1136 err
= HIToolbarItemCreate(
1137 kHIToolbarSeparatorIdentifier
,
1138 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1141 tool
->SetToolbarItemRef( item
);
1144 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1145 tool
->SetControlHandle( controlHandle
);
1150 case wxTOOL_STYLE_BUTTON
:
1152 wxASSERT( tool
->GetControlHandle() == NULL
);
1153 ControlButtonContentInfo info
;
1154 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1156 if ( UMAGetSystemVersion() >= 0x1000)
1158 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1162 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1163 if ( tool
->CanBeToggled() )
1164 behaviour
|= kControlBehaviorToggles
;
1165 err
= CreateBevelButtonControl( window
,
1166 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1167 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1170 #if wxMAC_USE_NATIVE_TOOLBAR
1171 HIToolbarItemRef item
;
1172 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1173 err
= HIToolbarItemCreate(
1174 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1175 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1178 InstallEventHandler(
1179 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1180 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1181 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1182 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1183 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1184 tool
->SetToolbarItemRef( item
);
1188 wxMacReleaseBitmapButton( &info
);
1191 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1192 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1195 InstallControlEventHandler(
1196 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1197 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1199 tool
->SetControlHandle( controlHandle
);
1203 case wxTOOL_STYLE_CONTROL
:
1204 wxASSERT( tool
->GetControl() != NULL
);
1206 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1207 // FIXME: doesn't work yet...
1209 HIToolbarItemRef item
;
1210 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1211 result
= HIToolbarItemCreate(
1212 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1213 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1215 if ( result
== noErr
)
1217 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1218 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1219 tool
->SetToolbarItemRef( item
);
1221 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1222 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1224 // FIXME: is this necessary ??
1225 ::GetControlBounds( controlHandle
, &toolrect
);
1226 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1228 // FIXME: is this necessary ??
1229 InstallControlEventHandler(
1230 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1231 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1236 // FIXME: right now there's nothing to do here
1246 if ( controlHandle
)
1248 ControlRef container
= (ControlRef
) GetHandle();
1249 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1251 UMAShowControl( controlHandle
);
1252 ::EmbedControl( controlHandle
, container
);
1255 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1256 tool
->UpdateToggleImage( true );
1258 // nothing special to do here - we relayout in Realize() later
1259 tool
->Attach( this );
1260 InvalidateBestSize();
1264 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1265 wxASSERT_MSG( false, errMsg
.c_str() );
1268 return (err
== noErr
);
1271 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1273 wxFAIL_MSG( wxT("not implemented") );
1276 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1278 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1279 wxToolBarToolsList::compatibility_iterator node
;
1280 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1282 wxToolBarToolBase
*tool2
= node
->GetData();
1283 if ( tool2
== tool
)
1285 // let node point to the next node in the list
1286 node
= node
->GetNext();
1292 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1296 #if wxMAC_USE_NATIVE_TOOLBAR
1297 CFIndex removeIndex
= tool
->GetIndex();
1300 switch ( tool
->GetStyle() )
1302 case wxTOOL_STYLE_CONTROL
:
1304 tool
->GetControl()->Destroy();
1305 tool
->ClearControl();
1309 case wxTOOL_STYLE_BUTTON
:
1310 case wxTOOL_STYLE_SEPARATOR
:
1311 if ( tool
->GetControlHandle() )
1313 DisposeControl( (ControlRef
) tool
->GetControlHandle() );
1315 #if wxMAC_USE_NATIVE_TOOLBAR
1316 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1318 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1319 tool
->SetIndex( -1 );
1323 tool
->ClearControl();
1331 // and finally reposition all the controls after this one
1333 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1335 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1336 wxPoint pt
= tool2
->GetPosition();
1338 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1343 tool2
->SetPosition( pt
);
1345 #if wxMAC_USE_NATIVE_TOOLBAR
1346 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1347 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1351 InvalidateBestSize();
1356 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1358 #if wxMAC_USE_NATIVE_TOOLBAR
1359 if ( m_macUsesNativeToolbar
)
1371 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1372 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1374 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1375 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1377 HIThemePlacardDrawInfo info
;
1378 memset( &info
, 0, sizeof(info
) );
1380 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1382 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1383 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1384 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1388 // leave the background as it is (striped or metal)
1393 const bool drawBorder
= true;
1397 wxMacPortSetter
helper( &dc
);
1399 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1401 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1402 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1405 if ( toolbarrect
.left
< 0 )
1406 toolbarrect
.left
= 0;
1407 if ( toolbarrect
.top
< 0 )
1408 toolbarrect
.top
= 0;
1411 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1415 #if TARGET_API_MAC_OSX
1416 HIRect hiToolbarrect
= CGRectMake(
1417 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1418 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1419 CGContextRef cgContext
;
1422 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1423 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1425 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1426 CGContextScaleCTM( cgContext
, 1, -1 );
1428 HIThemeBackgroundDrawInfo drawInfo
;
1429 drawInfo
.version
= 0;
1430 drawInfo
.state
= kThemeStateActive
;
1431 drawInfo
.kind
= kThemeBackgroundMetal
;
1432 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1434 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1443 #endif // wxUSE_TOOLBAR