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
;
395 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
397 GetToolBar()->MacWindowToRootWindow( &x
, &y
);
405 GetControlBounds( m_controlHandle
, &contrlRect
);
406 int former_mac_x
= contrlRect
.left
;
407 int former_mac_y
= contrlRect
.top
;
408 GetToolBar()->GetToolSize();
410 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
412 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
415 else if ( IsControl() )
417 GetControl()->Move( position
);
424 GetControlBounds( m_controlHandle
, &contrlRect
);
425 int former_mac_x
= contrlRect
.left
;
426 int former_mac_y
= contrlRect
.top
;
428 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
429 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
434 void wxToolBarTool::UpdateToggleImage( bool toggle
)
436 #if wxMAC_USE_NATIVE_TOOLBAR
438 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
439 #define kHIToolbarItemSelected (1 << 7)
442 // FIXME: this should be a OSX v10.4 runtime check
443 if (m_toolbarItemRef
!= NULL
)
445 OptionBits addAttrs
, removeAttrs
;
450 addAttrs
= kHIToolbarItemSelected
;
451 removeAttrs
= kHIToolbarItemNoAttributes
;
455 addAttrs
= kHIToolbarItemNoAttributes
;
456 removeAttrs
= kHIToolbarItemSelected
;
459 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
466 int w
= m_bmpNormal
.GetWidth();
467 int h
= m_bmpNormal
.GetHeight();
468 wxBitmap
bmp( w
, h
);
471 dc
.SelectObject( bmp
);
472 dc
.SetPen( wxNullPen
);
473 dc
.SetBackground( *wxWHITE
);
474 dc
.DrawRectangle( 0, 0, w
, h
);
475 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
476 dc
.SelectObject( wxNullBitmap
);
477 ControlButtonContentInfo info
;
478 wxMacCreateBitmapButton( &info
, bmp
);
479 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
480 wxMacReleaseBitmapButton( &info
);
484 ControlButtonContentInfo info
;
485 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
486 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
487 wxMacReleaseBitmapButton( &info
);
490 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
492 m_controlHandle
, 0, kControlIconTransformTag
,
493 sizeof(transform
), (Ptr
)&transform
);
494 HIViewSetNeedsDisplay( m_controlHandle
, true );
497 ::SetControl32BitValue( m_controlHandle
, toggle
);
501 wxToolBarTool::wxToolBarTool(
504 const wxString
& label
,
505 const wxBitmap
& bmpNormal
,
506 const wxBitmap
& bmpDisabled
,
508 wxObject
*clientData
,
509 const wxString
& shortHelp
,
510 const wxString
& longHelp
)
513 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
514 clientData
, shortHelp
, longHelp
)
520 #pragma mark Toolbar Implementation
522 wxToolBarToolBase
*wxToolBar::CreateTool(
524 const wxString
& label
,
525 const wxBitmap
& bmpNormal
,
526 const wxBitmap
& bmpDisabled
,
528 wxObject
*clientData
,
529 const wxString
& shortHelp
,
530 const wxString
& longHelp
)
532 return new wxToolBarTool(
533 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
534 clientData
, shortHelp
, longHelp
);
537 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
539 return new wxToolBarTool( this, control
);
542 void wxToolBar::Init()
546 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
547 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
549 #if wxMAC_USE_NATIVE_TOOLBAR
550 m_macHIToolbarRef
= NULL
;
551 m_macUsesNativeToolbar
= false;
555 // also for the toolbar we have the dual implementation:
556 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
558 bool wxToolBar::Create(
564 const wxString
& name
)
566 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
569 OSStatus err
= noErr
;
571 #if wxMAC_USE_NATIVE_TOOLBAR
572 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
573 err
= HIToolbarCreate(
574 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
575 (HIToolbarRef
*) &m_macHIToolbarRef
);
577 if (m_macHIToolbarRef
!= NULL
)
579 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
580 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
582 if ( style
& wxTB_NOICONS
)
583 mode
= kHIToolbarDisplayModeLabelOnly
;
584 else if ( style
& wxTB_TEXT
)
585 mode
= kHIToolbarDisplayModeIconAndLabel
;
587 mode
= kHIToolbarDisplayModeIconOnly
;
589 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
590 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
594 return (err
== noErr
);
597 wxToolBar::~wxToolBar()
599 #if wxMAC_USE_NATIVE_TOOLBAR
600 if (m_macHIToolbarRef
!= NULL
)
602 // if this is the installed toolbar, then deinstall it
603 if (m_macUsesNativeToolbar
)
604 MacInstallNativeToolbar( false );
606 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
607 m_macHIToolbarRef
= NULL
;
612 bool wxToolBar::Show( bool show
)
614 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
615 bool bResult
= (tlw
!= NULL
);
619 #if wxMAC_USE_NATIVE_TOOLBAR
620 bool ownToolbarInstalled
= false;
621 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
622 if (ownToolbarInstalled
)
624 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
626 ShowHideWindowToolbar( tlw
, show
, false );
629 bResult
= wxToolBarBase::Show( show
);
632 bResult
= wxToolBarBase::Show( show
);
639 bool wxToolBar::IsShown() const
643 #if wxMAC_USE_NATIVE_TOOLBAR
644 bool ownToolbarInstalled
;
646 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
647 if (ownToolbarInstalled
)
649 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
650 bResult
= IsWindowToolbarVisible( tlw
);
653 bResult
= wxToolBarBase::IsShown();
656 bResult
= wxToolBarBase::IsShown();
662 void wxToolBar::DoGetSize( int *width
, int *height
) const
664 #if wxMAC_USE_NATIVE_TOOLBAR
666 bool ownToolbarInstalled
;
668 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
669 if ( ownToolbarInstalled
)
671 // TODO: is this really a control ?
672 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
674 *width
= boundsR
.right
- boundsR
.left
;
675 if ( height
!= NULL
)
676 *height
= boundsR
.bottom
- boundsR
.top
;
679 wxToolBarBase::DoGetSize( width
, height
);
682 wxToolBarBase::DoGetSize( width
, height
);
686 wxSize
wxToolBar::DoGetBestSize() const
690 DoGetSize( &width
, &height
);
692 return wxSize( width
, height
);
695 void wxToolBar::SetWindowStyleFlag( long style
)
697 wxToolBarBase::SetWindowStyleFlag( style
);
699 #if wxMAC_USE_NATIVE_TOOLBAR
700 if (m_macHIToolbarRef
!= NULL
)
702 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
704 if ( style
& wxTB_NOICONS
)
705 mode
= kHIToolbarDisplayModeLabelOnly
;
706 else if ( style
& wxTB_TEXT
)
707 mode
= kHIToolbarDisplayModeIconAndLabel
;
709 mode
= kHIToolbarDisplayModeIconOnly
;
711 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
716 #if wxMAC_USE_NATIVE_TOOLBAR
717 bool wxToolBar::MacWantsNativeToolbar()
719 return m_macUsesNativeToolbar
;
722 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
724 bool bResultV
= false;
726 if (ownToolbarInstalled
!= NULL
)
727 *ownToolbarInstalled
= false;
729 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
732 HIToolbarRef curToolbarRef
= NULL
;
733 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
734 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
735 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
736 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
742 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
744 bool bResult
= false;
746 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
749 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
752 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
756 // check the existing toolbar
757 HIToolbarRef curToolbarRef
= NULL
;
758 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
760 curToolbarRef
= NULL
;
762 m_macUsesNativeToolbar
= usesNative
;
764 if (m_macUsesNativeToolbar
)
766 // only install toolbar if there isn't one installed already
767 if (curToolbarRef
== NULL
)
771 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
772 ShowHideWindowToolbar( tlw
, true, false );
773 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
774 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
776 Rect r
= { 0, 0, 0, 0 };
777 m_peer
->SetRect( &r
);
778 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
779 m_peer
->SetVisibility( false, true );
780 wxToolBarBase::Show( false );
785 // only deinstall toolbar if this is the installed one
786 if (m_macHIToolbarRef
== curToolbarRef
)
790 ShowHideWindowToolbar( tlw
, false, false );
791 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
792 SetWindowToolbar( tlw
, NULL
);
794 m_peer
->SetVisibility( true, true );
799 InvalidateBestSize();
801 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
806 bool wxToolBar::Realize()
808 if (m_tools
.GetCount() == 0)
814 int maxToolWidth
= 0;
815 int maxToolHeight
= 0;
817 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
818 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
823 // find the maximum tool width and height
825 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
826 while ( node
!= NULL
)
828 tool
= (wxToolBarTool
*) node
->GetData();
831 wxSize sz
= tool
->GetSize();
833 if ( sz
.x
> maxToolWidth
)
835 if ( sz
.y
> maxToolHeight
)
836 maxToolHeight
= sz
.y
;
839 node
= node
->GetNext();
842 bool lastIsRadio
= false;
843 bool curIsRadio
= false;
844 bool setChoiceInGroup
= false;
846 #if wxMAC_USE_NATIVE_TOOLBAR
847 CFIndex currentPosition
= 0;
848 bool insertAll
= false;
851 node
= m_tools
.GetFirst();
852 while ( node
!= NULL
)
854 tool
= (wxToolBarTool
*) node
->GetData();
857 node
= node
->GetNext();
861 // set tool position:
862 // for the moment just perform a single row/column alignment
863 wxSize cursize
= tool
->GetSize();
864 if ( x
+ cursize
.x
> maxWidth
)
865 maxWidth
= x
+ cursize
.x
;
866 if ( y
+ cursize
.y
> maxHeight
)
867 maxHeight
= y
+ cursize
.y
;
869 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
871 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
872 tool
->SetPosition( wxPoint(x1
, y
) );
876 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
877 tool
->SetPosition( wxPoint(x
, y1
) );
880 // update the item positioning state
881 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
882 y
+= cursize
.y
+ kwxMacToolSpacing
;
884 x
+= cursize
.x
+ kwxMacToolSpacing
;
886 #if wxMAC_USE_NATIVE_TOOLBAR
887 // install in native HIToolbar
888 if ( m_macHIToolbarRef
!= NULL
)
890 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
891 if ( hiItemRef
!= NULL
)
893 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
895 OSStatus err
= noErr
;
900 // if this is the first tool that gets newly inserted or repositioned
901 // first remove all 'old' tools from here to the right, because of this
902 // all following tools will have to be reinserted (insertAll). i = 100 because there's
903 // no way to determine how many there are in a toolbar, so just a high number :-(
904 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
906 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
911 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
912 wxFAIL_MSG( errMsg
.c_str() );
916 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
919 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
920 wxFAIL_MSG( errMsg
.c_str() );
923 tool
->SetIndex( currentPosition
);
931 // update radio button (and group) state
932 lastIsRadio
= curIsRadio
;
933 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
937 if ( tool
->IsToggled() )
938 DoToggleTool( tool
, true );
940 setChoiceInGroup
= false;
946 if ( tool
->Toggle( true ) )
948 DoToggleTool( tool
, true );
949 setChoiceInGroup
= true;
952 else if ( tool
->IsToggled() )
954 if ( tool
->IsToggled() )
955 DoToggleTool( tool
, true );
957 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
958 while ( nodePrev
!= NULL
)
960 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
961 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
964 if ( toggleTool
->Toggle( false ) )
965 DoToggleTool( toggleTool
, false );
967 nodePrev
= nodePrev
->GetPrevious();
972 node
= node
->GetNext();
975 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
977 // if not set yet, only one row
978 if ( m_maxRows
<= 0 )
981 m_minWidth
= maxWidth
;
983 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
984 m_minHeight
= m_maxHeight
= maxHeight
;
988 // if not set yet, have one column
989 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
990 SetRows( GetToolsCount() );
992 m_minHeight
= maxHeight
;
994 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
995 m_minWidth
= m_maxWidth
= maxWidth
;
999 // FIXME: should this be OSX-only?
1001 bool wantNativeToolbar
, ownToolbarInstalled
;
1003 // attempt to install the native toolbar
1004 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1005 MacInstallNativeToolbar( wantNativeToolbar
);
1006 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1007 if (!ownToolbarInstalled
)
1009 SetSize( maxWidth
, maxHeight
);
1010 InvalidateBestSize();
1014 SetSize( maxWidth
, maxHeight
);
1015 InvalidateBestSize();
1018 SetBestFittingSize();
1023 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1025 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1026 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1028 #if wxMAC_USE_NATIVE_TOOLBAR
1029 if (m_macHIToolbarRef
!= NULL
)
1031 int maxs
= wxMax( size
.x
, size
.y
);
1032 HIToolbarDisplaySize sizeSpec
;
1034 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1035 else if ( maxs
> 24 )
1036 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1038 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1040 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1045 // The button size is bigger than the bitmap size
1046 wxSize
wxToolBar::GetToolSize() const
1048 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1051 void wxToolBar::SetRows(int nRows
)
1053 // avoid resizing the frame uselessly
1054 if ( nRows
!= m_maxRows
)
1058 void wxToolBar::MacSuperChangedPosition()
1060 wxWindow::MacSuperChangedPosition();
1062 #if wxMAC_USE_NATIVE_TOOLBAR
1063 if (! m_macUsesNativeToolbar
)
1071 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1073 wxToolBarTool
*tool
;
1074 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1075 while ( node
!= NULL
)
1077 tool
= (wxToolBarTool
*)node
->GetData();
1080 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1081 if ( r
.Contains( wxPoint( x
, y
) ) )
1085 node
= node
->GetNext();
1088 return (wxToolBarToolBase
*)NULL
;
1091 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1093 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1095 return tool
->GetShortHelp();
1097 return wxEmptyString
;
1100 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1103 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1106 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1108 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1109 if ( ( tool
!= NULL
) && tool
->IsButton() )
1110 tool
->UpdateToggleImage( toggle
);
1113 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1115 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1119 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1120 wxSize toolSize
= GetToolSize();
1121 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1122 ControlRef controlHandle
= NULL
;
1125 switch (tool
->GetStyle())
1127 case wxTOOL_STYLE_SEPARATOR
:
1129 wxASSERT( tool
->GetControlHandle() == NULL
);
1132 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1133 toolrect
.bottom
= toolSize
.y
;
1135 toolrect
.right
= toolSize
.x
;
1137 #ifdef __WXMAC_OSX__
1138 // in flat style we need a visual separator
1139 #if wxMAC_USE_NATIVE_TOOLBAR
1140 HIToolbarItemRef item
;
1141 err
= HIToolbarItemCreate(
1142 kHIToolbarSeparatorIdentifier
,
1143 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1146 tool
->SetToolbarItemRef( item
);
1149 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1150 tool
->SetControlHandle( controlHandle
);
1155 case wxTOOL_STYLE_BUTTON
:
1157 wxASSERT( tool
->GetControlHandle() == NULL
);
1158 ControlButtonContentInfo info
;
1159 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1161 if ( UMAGetSystemVersion() >= 0x1000)
1163 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1167 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1168 if ( tool
->CanBeToggled() )
1169 behaviour
|= kControlBehaviorToggles
;
1170 err
= CreateBevelButtonControl( window
,
1171 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1172 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1175 #if wxMAC_USE_NATIVE_TOOLBAR
1176 HIToolbarItemRef item
;
1177 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1178 err
= HIToolbarItemCreate(
1179 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1180 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1183 InstallEventHandler(
1184 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1185 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1186 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1187 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1188 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1189 tool
->SetToolbarItemRef( item
);
1193 wxMacReleaseBitmapButton( &info
);
1196 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1197 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1200 InstallControlEventHandler(
1201 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1202 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1204 tool
->SetControlHandle( controlHandle
);
1208 case wxTOOL_STYLE_CONTROL
:
1209 wxASSERT( tool
->GetControl() != NULL
);
1211 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1212 // FIXME: doesn't work yet...
1214 HIToolbarItemRef item
;
1215 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1216 result
= HIToolbarItemCreate(
1217 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1218 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1220 if ( result
== noErr
)
1222 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1223 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1224 tool
->SetToolbarItemRef( item
);
1226 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1227 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1229 // FIXME: is this necessary ??
1230 ::GetControlBounds( controlHandle
, &toolrect
);
1231 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1233 // FIXME: is this necessary ??
1234 InstallControlEventHandler(
1235 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1236 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1241 // FIXME: right now there's nothing to do here
1251 if ( controlHandle
)
1253 ControlRef container
= (ControlRef
) GetHandle();
1254 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1256 UMAShowControl( controlHandle
);
1257 ::EmbedControl( controlHandle
, container
);
1260 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1261 tool
->UpdateToggleImage( true );
1263 // nothing special to do here - we relayout in Realize() later
1264 tool
->Attach( this );
1265 InvalidateBestSize();
1269 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1270 wxFAIL_MSG( errMsg
.c_str() );
1273 return (err
== noErr
);
1276 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1278 wxFAIL_MSG( wxT("not implemented") );
1281 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1283 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1284 wxToolBarToolsList::compatibility_iterator node
;
1285 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1287 wxToolBarToolBase
*tool2
= node
->GetData();
1288 if ( tool2
== tool
)
1290 // let node point to the next node in the list
1291 node
= node
->GetNext();
1297 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1301 #if wxMAC_USE_NATIVE_TOOLBAR
1302 CFIndex removeIndex
= tool
->GetIndex();
1305 switch ( tool
->GetStyle() )
1307 case wxTOOL_STYLE_CONTROL
:
1309 tool
->GetControl()->Destroy();
1310 tool
->ClearControl();
1314 case wxTOOL_STYLE_BUTTON
:
1315 case wxTOOL_STYLE_SEPARATOR
:
1316 if ( tool
->GetControlHandle() )
1318 #if wxMAC_USE_NATIVE_TOOLBAR
1319 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1321 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1322 tool
->SetIndex( -1 );
1326 tool
->ClearControl();
1334 // and finally reposition all the controls after this one
1336 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1338 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1339 wxPoint pt
= tool2
->GetPosition();
1341 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1346 tool2
->SetPosition( pt
);
1348 #if wxMAC_USE_NATIVE_TOOLBAR
1349 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1350 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1354 InvalidateBestSize();
1359 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1361 #if wxMAC_USE_NATIVE_TOOLBAR
1362 if ( m_macUsesNativeToolbar
)
1374 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1375 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1377 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1378 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1380 HIThemePlacardDrawInfo info
;
1381 memset( &info
, 0, sizeof(info
) );
1383 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1385 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1386 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1387 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1391 // leave the background as it is (striped or metal)
1396 const bool drawBorder
= true;
1400 wxMacPortSetter
helper( &dc
);
1402 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1404 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1405 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1408 if ( toolbarrect
.left
< 0 )
1409 toolbarrect
.left
= 0;
1410 if ( toolbarrect
.top
< 0 )
1411 toolbarrect
.top
= 0;
1414 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1418 #if TARGET_API_MAC_OSX
1419 HIRect hiToolbarrect
= CGRectMake(
1420 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1421 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1422 CGContextRef cgContext
;
1425 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1426 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1428 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1429 CGContextScaleCTM( cgContext
, 1, -1 );
1431 HIThemeBackgroundDrawInfo drawInfo
;
1432 drawInfo
.version
= 0;
1433 drawInfo
.state
= kThemeStateActive
;
1434 drawInfo
.kind
= kThemeBackgroundMetal
;
1435 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1437 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1446 #endif // wxUSE_TOOLBAR