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() );
84 #if wxMAC_USE_NATIVE_TOOLBAR
85 if ( m_toolbarItemRef
)
86 CFRelease( m_toolbarItemRef
);
90 WXWidget
GetControlHandle()
92 return (WXWidget
) m_controlHandle
;
95 void SetControlHandle( ControlRef handle
)
97 m_controlHandle
= handle
;
100 void SetPosition( const wxPoint
& position
);
105 if ( m_controlHandle
)
107 DisposeControl( m_controlHandle
);
108 m_controlHandle
= NULL
;
111 #if wxMAC_USE_NATIVE_TOOLBAR
112 m_toolbarItemRef
= NULL
;
116 wxSize
GetSize() const
122 curSize
= GetControl()->GetSize();
124 else if ( IsButton() )
126 curSize
= GetToolBar()->GetToolSize();
131 curSize
= GetToolBar()->GetToolSize();
132 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
141 wxPoint
GetPosition() const
143 return wxPoint( m_x
, m_y
);
146 bool DoEnable( bool enable
);
148 void UpdateToggleImage( bool toggle
);
150 #if wxMAC_USE_NATIVE_TOOLBAR
151 void SetToolbarItemRef( HIToolbarItemRef ref
)
153 if ( m_controlHandle
)
154 HideControl( m_controlHandle
);
155 if ( m_toolbarItemRef
)
156 CFRelease( m_toolbarItemRef
);
158 m_toolbarItemRef
= ref
;
159 if ( m_toolbarItemRef
)
161 HIToolbarItemSetHelpText(
163 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
164 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
168 HIToolbarItemRef
GetToolbarItemRef() const
170 return m_toolbarItemRef
;
173 void SetIndex( CFIndex idx
)
178 CFIndex
GetIndex() const
187 m_controlHandle
= NULL
;
189 #if wxMAC_USE_NATIVE_TOOLBAR
190 m_toolbarItemRef
= NULL
;
195 ControlRef m_controlHandle
;
199 #if wxMAC_USE_NATIVE_TOOLBAR
200 HIToolbarItemRef m_toolbarItemRef
;
201 // position in its toolbar, -1 means not inserted
206 static const EventTypeSpec eventList
[] =
208 { kEventClassControl
, kEventControlHit
},
210 { kEventClassControl
, kEventControlHitTest
},
214 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
216 OSStatus result
= eventNotHandledErr
;
217 ControlRef controlRef
;
218 wxMacCarbonEvent
cEvent( event
);
220 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
222 switch ( GetEventKind( event
) )
224 case kEventControlHit
:
226 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
227 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
228 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
233 shouldToggle
= !tbartool
->IsToggled();
235 shouldToggle
= (GetControl32BitValue( (ControlRef
)(tbartool
->GetControlHandle()) ) != 0);
238 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
241 if (tbartool
!= NULL
)
242 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
248 case kEventControlHitTest
:
250 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
252 HIViewGetBounds( controlRef
, &rect
);
254 ControlPartCode pc
= kControlNoPart
;
255 if ( CGRectContainsPoint( rect
, pt
) )
256 pc
= kControlIconPart
;
257 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
270 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
272 OSStatus result
= eventNotHandledErr
;
274 switch ( GetEventClass( event
) )
276 case kEventClassControl
:
277 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
287 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
289 #if wxMAC_USE_NATIVE_TOOLBAR
291 static const EventTypeSpec toolBarEventList
[] =
293 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
296 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
298 OSStatus result
= eventNotHandledErr
;
300 switch ( GetEventKind( event
) )
302 case kEventToolbarItemPerformAction
:
304 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
305 if ( tbartool
!= NULL
)
307 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
308 int toolID
= tbartool
->GetId();
310 if ( tbartool
->CanBeToggled() )
313 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
317 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
330 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
332 OSStatus result
= eventNotHandledErr
;
334 switch ( GetEventClass( event
) )
336 case kEventClassToolbarItem
:
337 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
347 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
351 bool wxToolBarTool::DoEnable( bool enable
)
355 GetControl()->Enable( enable
);
357 else if ( IsButton() )
359 #if wxMAC_USE_NATIVE_TOOLBAR
360 if ( m_toolbarItemRef
!= NULL
)
361 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
364 if ( m_controlHandle
!= NULL
)
366 #if TARGET_API_MAC_OSX
368 EnableControl( m_controlHandle
);
370 DisableControl( m_controlHandle
);
373 ActivateControl( m_controlHandle
);
375 DeactivateControl( m_controlHandle
);
383 void wxToolBarTool::SetPosition( const wxPoint
& position
)
390 int mac_x
= position
.x
;
391 int mac_y
= position
.y
;
393 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
395 GetToolBar()->MacWindowToRootWindow( &x
, &y
);
403 GetControlBounds( m_controlHandle
, &contrlRect
);
404 int former_mac_x
= contrlRect
.left
;
405 int former_mac_y
= contrlRect
.top
;
406 GetToolBar()->GetToolSize();
408 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
410 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
413 else if ( IsControl() )
415 GetControl()->Move( position
);
422 GetControlBounds( m_controlHandle
, &contrlRect
);
423 int former_mac_x
= contrlRect
.left
;
424 int former_mac_y
= contrlRect
.top
;
426 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
427 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
432 void wxToolBarTool::UpdateToggleImage( bool toggle
)
434 #if wxMAC_USE_NATIVE_TOOLBAR
436 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
437 #define kHIToolbarItemSelected (1 << 7)
440 // FIXME: this should be a OSX v10.4 runtime check
441 if (m_toolbarItemRef
!= NULL
)
443 OptionBits addAttrs
, removeAttrs
;
448 addAttrs
= kHIToolbarItemSelected
;
449 removeAttrs
= kHIToolbarItemNoAttributes
;
453 addAttrs
= kHIToolbarItemNoAttributes
;
454 removeAttrs
= kHIToolbarItemSelected
;
457 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
464 int w
= m_bmpNormal
.GetWidth();
465 int h
= m_bmpNormal
.GetHeight();
466 wxBitmap
bmp( w
, h
);
469 dc
.SelectObject( bmp
);
470 dc
.SetPen( wxNullPen
);
471 dc
.SetBackground( *wxWHITE
);
472 dc
.DrawRectangle( 0, 0, w
, h
);
473 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
474 dc
.SelectObject( wxNullBitmap
);
475 ControlButtonContentInfo info
;
476 wxMacCreateBitmapButton( &info
, bmp
);
477 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
478 wxMacReleaseBitmapButton( &info
);
482 ControlButtonContentInfo info
;
483 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
484 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
485 wxMacReleaseBitmapButton( &info
);
488 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
490 m_controlHandle
, 0, kControlIconTransformTag
,
491 sizeof(transform
), (Ptr
)&transform
);
492 HIViewSetNeedsDisplay( m_controlHandle
, true );
495 ::SetControl32BitValue( m_controlHandle
, toggle
);
499 wxToolBarTool::wxToolBarTool(
502 const wxString
& label
,
503 const wxBitmap
& bmpNormal
,
504 const wxBitmap
& bmpDisabled
,
506 wxObject
*clientData
,
507 const wxString
& shortHelp
,
508 const wxString
& longHelp
)
511 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
512 clientData
, shortHelp
, longHelp
)
518 #pragma mark Toolbar Implementation
520 wxToolBarToolBase
*wxToolBar::CreateTool(
522 const wxString
& label
,
523 const wxBitmap
& bmpNormal
,
524 const wxBitmap
& bmpDisabled
,
526 wxObject
*clientData
,
527 const wxString
& shortHelp
,
528 const wxString
& longHelp
)
530 return new wxToolBarTool(
531 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
532 clientData
, shortHelp
, longHelp
);
535 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
537 return new wxToolBarTool( this, control
);
540 void wxToolBar::Init()
544 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
545 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
547 #if wxMAC_USE_NATIVE_TOOLBAR
548 m_macHIToolbarRef
= NULL
;
549 m_macUsesNativeToolbar
= false;
553 // also for the toolbar we have the dual implementation:
554 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
556 bool wxToolBar::Create(
562 const wxString
& name
)
564 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
567 OSStatus err
= noErr
;
569 #if wxMAC_USE_NATIVE_TOOLBAR
570 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
571 err
= HIToolbarCreate(
572 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
573 (HIToolbarRef
*) &m_macHIToolbarRef
);
575 if (m_macHIToolbarRef
!= NULL
)
577 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
578 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
580 if ( style
& wxTB_NOICONS
)
581 mode
= kHIToolbarDisplayModeLabelOnly
;
582 else if ( style
& wxTB_TEXT
)
583 mode
= kHIToolbarDisplayModeIconAndLabel
;
585 mode
= kHIToolbarDisplayModeIconOnly
;
587 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
588 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
592 return (err
== noErr
);
595 wxToolBar::~wxToolBar()
597 #if wxMAC_USE_NATIVE_TOOLBAR
598 if (m_macHIToolbarRef
!= NULL
)
600 // if this is the installed toolbar, then deinstall it
601 if (m_macUsesNativeToolbar
)
602 MacInstallNativeToolbar( false );
604 CFRelease( (HIToolbarRef
)m_macHIToolbarRef
);
605 m_macHIToolbarRef
= NULL
;
610 bool wxToolBar::Show( bool show
)
612 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
613 bool bResult
= (tlw
!= NULL
);
617 #if wxMAC_USE_NATIVE_TOOLBAR
618 bool ownToolbarInstalled
= false;
619 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
620 if (ownToolbarInstalled
)
622 bResult
= (IsWindowToolbarVisible( tlw
) != show
);
624 ShowHideWindowToolbar( tlw
, show
, false );
627 bResult
= wxToolBarBase::Show( show
);
630 bResult
= wxToolBarBase::Show( show
);
637 bool wxToolBar::IsShown() const
641 #if wxMAC_USE_NATIVE_TOOLBAR
642 bool ownToolbarInstalled
;
644 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
645 if (ownToolbarInstalled
)
647 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
648 bResult
= IsWindowToolbarVisible( tlw
);
651 bResult
= wxToolBarBase::IsShown();
654 bResult
= wxToolBarBase::IsShown();
660 void wxToolBar::DoGetSize( int *width
, int *height
) const
662 #if wxMAC_USE_NATIVE_TOOLBAR
664 bool ownToolbarInstalled
;
666 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
667 if ( ownToolbarInstalled
)
669 // TODO: is this really a control ?
670 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
672 *width
= boundsR
.right
- boundsR
.left
;
673 if ( height
!= NULL
)
674 *height
= boundsR
.bottom
- boundsR
.top
;
677 wxToolBarBase::DoGetSize( width
, height
);
680 wxToolBarBase::DoGetSize( width
, height
);
684 wxSize
wxToolBar::DoGetBestSize() const
688 DoGetSize( &width
, &height
);
690 return wxSize( width
, height
);
693 void wxToolBar::SetWindowStyleFlag( long style
)
695 wxToolBarBase::SetWindowStyleFlag( style
);
697 #if wxMAC_USE_NATIVE_TOOLBAR
698 if (m_macHIToolbarRef
!= NULL
)
700 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
702 if ( style
& wxTB_NOICONS
)
703 mode
= kHIToolbarDisplayModeLabelOnly
;
704 else if ( style
& wxTB_TEXT
)
705 mode
= kHIToolbarDisplayModeIconAndLabel
;
707 mode
= kHIToolbarDisplayModeIconOnly
;
709 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
714 #if wxMAC_USE_NATIVE_TOOLBAR
715 bool wxToolBar::MacWantsNativeToolbar()
717 return m_macUsesNativeToolbar
;
720 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
722 bool bResultV
= false;
724 if (ownToolbarInstalled
!= NULL
)
725 *ownToolbarInstalled
= false;
727 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
730 HIToolbarRef curToolbarRef
= NULL
;
731 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
732 bResultV
= ((err
== noErr
) && (curToolbarRef
!= NULL
));
733 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
734 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
740 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
742 bool bResult
= false;
744 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
747 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
750 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
754 // check the existing toolbar
755 HIToolbarRef curToolbarRef
= NULL
;
756 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
758 curToolbarRef
= NULL
;
760 m_macUsesNativeToolbar
= usesNative
;
762 if (m_macUsesNativeToolbar
)
764 // only install toolbar if there isn't one installed already
765 if (curToolbarRef
== NULL
)
769 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
770 ShowHideWindowToolbar( tlw
, true, false );
771 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
772 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
774 Rect r
= { 0, 0, 0, 0 };
775 m_peer
->SetRect( &r
);
776 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
777 m_peer
->SetVisibility( false, true );
778 wxToolBarBase::Show( false );
783 // only deinstall toolbar if this is the installed one
784 if (m_macHIToolbarRef
== curToolbarRef
)
788 ShowHideWindowToolbar( tlw
, false, false );
789 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
790 SetWindowToolbar( tlw
, NULL
);
792 m_peer
->SetVisibility( true, true );
797 InvalidateBestSize();
799 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
804 bool wxToolBar::Realize()
806 if (m_tools
.GetCount() == 0)
812 int maxToolWidth
= 0;
813 int maxToolHeight
= 0;
815 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
816 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
821 // find the maximum tool width and height
823 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
824 while ( node
!= NULL
)
826 tool
= (wxToolBarTool
*) node
->GetData();
829 wxSize sz
= tool
->GetSize();
831 if ( sz
.x
> maxToolWidth
)
833 if ( sz
.y
> maxToolHeight
)
834 maxToolHeight
= sz
.y
;
837 node
= node
->GetNext();
840 bool lastIsRadio
= false;
841 bool curIsRadio
= false;
842 bool setChoiceInGroup
= false;
844 #if wxMAC_USE_NATIVE_TOOLBAR
845 CFIndex currentPosition
= 0;
846 bool insertAll
= false;
849 node
= m_tools
.GetFirst();
850 while ( node
!= NULL
)
852 tool
= (wxToolBarTool
*) node
->GetData();
855 node
= node
->GetNext();
859 // set tool position:
860 // for the moment just perform a single row/column alignment
861 wxSize cursize
= tool
->GetSize();
862 if ( x
+ cursize
.x
> maxWidth
)
863 maxWidth
= x
+ cursize
.x
;
864 if ( y
+ cursize
.y
> maxHeight
)
865 maxHeight
= y
+ cursize
.y
;
867 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
869 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
870 tool
->SetPosition( wxPoint(x1
, y
) );
874 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
875 tool
->SetPosition( wxPoint(x
, y1
) );
878 // update the item positioning state
879 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
880 y
+= cursize
.y
+ kwxMacToolSpacing
;
882 x
+= cursize
.x
+ kwxMacToolSpacing
;
884 #if wxMAC_USE_NATIVE_TOOLBAR
885 // install in native HIToolbar
886 if ( m_macHIToolbarRef
!= NULL
)
888 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
889 if ( hiItemRef
!= NULL
)
891 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
893 OSStatus err
= noErr
;
898 // if this is the first tool that gets newly inserted or repositioned
899 // first remove all 'old' tools from here to the right, because of this
900 // all following tools will have to be reinserted (insertAll). i = 100 because there's
901 // no way to determine how many there are in a toolbar, so just a high number :-(
902 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
904 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
909 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
910 wxASSERT_MSG( 0, errMsg
.c_str() );
914 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
917 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
918 wxASSERT_MSG( 0, errMsg
.c_str() );
921 tool
->SetIndex( currentPosition
);
929 // update radio button (and group) state
930 lastIsRadio
= curIsRadio
;
931 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
935 if ( tool
->IsToggled() )
936 DoToggleTool( tool
, true );
938 setChoiceInGroup
= false;
944 if ( tool
->Toggle( true ) )
946 DoToggleTool( tool
, true );
947 setChoiceInGroup
= true;
950 else if ( tool
->IsToggled() )
952 if ( tool
->IsToggled() )
953 DoToggleTool( tool
, true );
955 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
956 while ( nodePrev
!= NULL
)
958 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
959 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
962 if ( toggleTool
->Toggle( false ) )
963 DoToggleTool( toggleTool
, false );
965 nodePrev
= nodePrev
->GetPrevious();
970 node
= node
->GetNext();
973 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
975 // if not set yet, only one row
976 if ( m_maxRows
<= 0 )
979 m_minWidth
= maxWidth
;
981 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
982 m_minHeight
= m_maxHeight
= maxHeight
;
986 // if not set yet, have one column
987 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
988 SetRows( GetToolsCount() );
990 m_minHeight
= maxHeight
;
992 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
993 m_minWidth
= m_maxWidth
= maxWidth
;
997 // FIXME: should this be OSX-only?
999 bool wantNativeToolbar
, ownToolbarInstalled
;
1001 // attempt to install the native toolbar
1002 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1003 MacInstallNativeToolbar( wantNativeToolbar
);
1004 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1005 if (!ownToolbarInstalled
)
1007 SetSize( maxWidth
, maxHeight
);
1008 InvalidateBestSize();
1012 SetSize( maxWidth
, maxHeight
);
1013 InvalidateBestSize();
1016 SetBestFittingSize();
1021 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1023 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1024 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1026 #if wxMAC_USE_NATIVE_TOOLBAR
1027 if (m_macHIToolbarRef
!= NULL
)
1029 int maxs
= wxMax( size
.x
, size
.y
);
1030 HIToolbarDisplaySize sizeSpec
;
1032 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1033 else if ( maxs
> 24 )
1034 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1036 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1038 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1043 // The button size is bigger than the bitmap size
1044 wxSize
wxToolBar::GetToolSize() const
1046 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1049 void wxToolBar::SetRows(int nRows
)
1051 // avoid resizing the frame uselessly
1052 if ( nRows
!= m_maxRows
)
1056 void wxToolBar::MacSuperChangedPosition()
1058 wxWindow::MacSuperChangedPosition();
1060 #if wxMAC_USE_NATIVE_TOOLBAR
1061 if (! m_macUsesNativeToolbar
)
1069 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1071 wxToolBarTool
*tool
;
1072 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1073 while ( node
!= NULL
)
1075 tool
= (wxToolBarTool
*)node
->GetData();
1078 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1079 if ( r
.Contains( wxPoint( x
, y
) ) )
1083 node
= node
->GetNext();
1086 return (wxToolBarToolBase
*)NULL
;
1089 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1091 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1093 return tool
->GetShortHelp();
1095 return wxEmptyString
;
1098 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1101 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1104 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1106 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1107 if ( ( tool
!= NULL
) && tool
->IsButton() )
1108 tool
->UpdateToggleImage( toggle
);
1111 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1113 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1117 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1118 wxSize toolSize
= GetToolSize();
1119 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1120 ControlRef controlHandle
= NULL
;
1123 switch (tool
->GetStyle())
1125 case wxTOOL_STYLE_SEPARATOR
:
1127 wxASSERT( tool
->GetControlHandle() == NULL
);
1130 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1131 toolrect
.bottom
= toolSize
.y
;
1133 toolrect
.right
= toolSize
.x
;
1135 #ifdef __WXMAC_OSX__
1136 // in flat style we need a visual separator
1137 #if wxMAC_USE_NATIVE_TOOLBAR
1138 HIToolbarItemRef item
;
1139 err
= HIToolbarItemCreate(
1140 kHIToolbarSeparatorIdentifier
,
1141 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1144 tool
->SetToolbarItemRef( item
);
1147 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1148 tool
->SetControlHandle( controlHandle
);
1153 case wxTOOL_STYLE_BUTTON
:
1155 wxASSERT( tool
->GetControlHandle() == NULL
);
1156 ControlButtonContentInfo info
;
1157 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1159 if ( UMAGetSystemVersion() >= 0x1000)
1161 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1165 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1166 if ( tool
->CanBeToggled() )
1167 behaviour
|= kControlBehaviorToggles
;
1168 err
= CreateBevelButtonControl( window
,
1169 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1170 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1173 #if wxMAC_USE_NATIVE_TOOLBAR
1174 HIToolbarItemRef item
;
1175 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1176 err
= HIToolbarItemCreate(
1177 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1178 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1181 InstallEventHandler(
1182 HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1183 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1184 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1185 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1186 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1187 tool
->SetToolbarItemRef( item
);
1191 wxMacReleaseBitmapButton( &info
);
1194 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1195 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1198 InstallControlEventHandler(
1199 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1200 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1202 tool
->SetControlHandle( controlHandle
);
1206 case wxTOOL_STYLE_CONTROL
:
1207 wxASSERT( tool
->GetControl() != NULL
);
1209 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1210 // FIXME: doesn't work yet...
1212 HIToolbarItemRef item
;
1213 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1214 result
= HIToolbarItemCreate(
1215 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1216 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1218 if ( result
== noErr
)
1220 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1221 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1222 tool
->SetToolbarItemRef( item
);
1224 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1225 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1227 // FIXME: is this necessary ??
1228 ::GetControlBounds( controlHandle
, &toolrect
);
1229 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1231 // FIXME: is this necessary ??
1232 InstallControlEventHandler(
1233 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1234 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1239 // FIXME: right now there's nothing to do here
1249 if ( controlHandle
)
1251 ControlRef container
= (ControlRef
) GetHandle();
1252 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1254 UMAShowControl( controlHandle
);
1255 ::EmbedControl( controlHandle
, container
);
1258 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1259 tool
->UpdateToggleImage( true );
1261 // nothing special to do here - we relayout in Realize() later
1262 tool
->Attach( this );
1263 InvalidateBestSize();
1267 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1268 wxASSERT_MSG( false, errMsg
.c_str() );
1271 return (err
== noErr
);
1274 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1276 wxFAIL_MSG( wxT("not implemented") );
1279 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1281 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1282 wxToolBarToolsList::compatibility_iterator node
;
1283 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1285 wxToolBarToolBase
*tool2
= node
->GetData();
1286 if ( tool2
== tool
)
1288 // let node point to the next node in the list
1289 node
= node
->GetNext();
1295 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1299 #if wxMAC_USE_NATIVE_TOOLBAR
1300 CFIndex removeIndex
= tool
->GetIndex();
1303 switch ( tool
->GetStyle() )
1305 case wxTOOL_STYLE_CONTROL
:
1307 tool
->GetControl()->Destroy();
1308 tool
->ClearControl();
1312 case wxTOOL_STYLE_BUTTON
:
1313 case wxTOOL_STYLE_SEPARATOR
:
1314 if ( tool
->GetControlHandle() )
1316 #if wxMAC_USE_NATIVE_TOOLBAR
1317 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1319 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1320 tool
->SetIndex( -1 );
1324 tool
->ClearControl();
1332 // and finally reposition all the controls after this one
1334 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1336 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1337 wxPoint pt
= tool2
->GetPosition();
1339 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1344 tool2
->SetPosition( pt
);
1346 #if wxMAC_USE_NATIVE_TOOLBAR
1347 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1348 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1352 InvalidateBestSize();
1357 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1359 #if wxMAC_USE_NATIVE_TOOLBAR
1360 if ( m_macUsesNativeToolbar
)
1372 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1373 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1375 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1376 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1378 HIThemePlacardDrawInfo info
;
1379 memset( &info
, 0, sizeof(info
) );
1381 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1383 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1384 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1385 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1389 // leave the background as it is (striped or metal)
1394 const bool drawBorder
= true;
1398 wxMacPortSetter
helper( &dc
);
1400 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1402 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1403 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1406 if ( toolbarrect
.left
< 0 )
1407 toolbarrect
.left
= 0;
1408 if ( toolbarrect
.top
< 0 )
1409 toolbarrect
.top
= 0;
1412 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1416 #if TARGET_API_MAC_OSX
1417 HIRect hiToolbarrect
= CGRectMake(
1418 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1419 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1420 CGContextRef cgContext
;
1423 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1424 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1426 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1427 CGContextScaleCTM( cgContext
, 1, -1 );
1429 HIThemeBackgroundDrawInfo drawInfo
;
1430 drawInfo
.version
= 0;
1431 drawInfo
.state
= kThemeStateActive
;
1432 drawInfo
.kind
= kThemeBackgroundMetal
;
1433 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1435 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1444 #endif // wxUSE_TOOLBAR