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
61 wxToolBarTool( wxToolBar
*tbar
,
63 const wxString
& label
,
64 const wxBitmap
& bmpNormal
,
65 const wxBitmap
& bmpDisabled
,
68 const wxString
& shortHelp
,
69 const wxString
& longHelp
);
71 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
72 : wxToolBarToolBase(tbar
, control
)
76 SetControlHandle( (ControlRef
) control
->GetHandle() );
82 if ( m_controlHandle
)
83 DisposeControl( m_controlHandle
);
85 #if wxMAC_USE_NATIVE_TOOLBAR
86 if ( m_toolbarItemRef
)
87 CFRelease( m_toolbarItemRef
);
91 WXWidget
GetControlHandle()
93 return (WXWidget
) m_controlHandle
;
96 void SetControlHandle( ControlRef handle
)
98 m_controlHandle
= handle
;
101 void SetPosition( const wxPoint
& position
);
107 #if wxMAC_USE_NATIVE_TOOLBAR
108 m_toolbarItemRef
= NULL
;
112 wxSize
GetSize() const
116 return GetControl()->GetSize();
118 else if ( IsButton() )
120 return GetToolBar()->GetToolSize();
125 wxSize sz
= GetToolBar()->GetToolSize();
126 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
135 wxPoint
GetPosition() const
137 return wxPoint( m_x
, m_y
);
140 bool DoEnable( bool enable
);
142 void UpdateToggleImage( bool toggle
);
144 #if wxMAC_USE_NATIVE_TOOLBAR
145 void SetToolbarItemRef( HIToolbarItemRef ref
)
147 if ( m_controlHandle
)
148 HideControl( m_controlHandle
);
149 if ( m_toolbarItemRef
)
150 CFRelease( m_toolbarItemRef
);
152 m_toolbarItemRef
= ref
;
153 if ( m_toolbarItemRef
)
155 HIToolbarItemSetHelpText(
157 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
158 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
162 HIToolbarItemRef
GetToolbarItemRef() const
164 return m_toolbarItemRef
;
167 void SetIndex( CFIndex idx
)
172 CFIndex
GetIndex() const
181 m_controlHandle
= NULL
;
183 #if wxMAC_USE_NATIVE_TOOLBAR
184 m_toolbarItemRef
= NULL
;
189 ControlRef m_controlHandle
;
193 #if wxMAC_USE_NATIVE_TOOLBAR
194 HIToolbarItemRef m_toolbarItemRef
;
195 // position in its toolbar, -1 means not inserted
200 static const EventTypeSpec eventList
[] =
202 { kEventClassControl
, kEventControlHit
},
204 { kEventClassControl
, kEventControlHitTest
},
208 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
210 OSStatus result
= eventNotHandledErr
;
211 ControlRef controlRef
;
212 wxMacCarbonEvent
cEvent( event
);
214 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
);
216 switch ( GetEventKind( event
) )
218 case kEventControlHit
:
220 wxToolBarTool
*tbartool
= (wxToolBarTool
*)data
;
221 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
222 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
227 shouldToggle
= !tbartool
->IsToggled();
229 shouldToggle
= ( GetControl32BitValue((ControlRef
) tbartool
->GetControlHandle()) != 0 );
232 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
235 if (tbartool
!= NULL
)
236 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
242 case kEventControlHitTest
:
244 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
);
246 HIViewGetBounds( controlRef
, &rect
);
248 ControlPartCode pc
= kControlNoPart
;
249 if ( CGRectContainsPoint( rect
, pt
) )
250 pc
= kControlIconPart
;
251 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
);
264 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
266 OSStatus result
= eventNotHandledErr
;
268 switch ( GetEventClass( event
) )
270 case kEventClassControl
:
271 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
);
281 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
283 #if wxMAC_USE_NATIVE_TOOLBAR
289 static const EventTypeSpec toolBarEventList
[] =
291 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
},
294 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
296 OSStatus result
= eventNotHandledErr
;
298 switch ( GetEventKind( event
) )
300 case kEventToolbarItemPerformAction
:
302 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
303 if ( tbartool
!= NULL
)
305 wxToolBar
*tbar
= (wxToolBar
*)(tbartool
->GetToolBar());
306 int toolID
= tbartool
->GetId();
308 if ( tbartool
->CanBeToggled() )
311 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
315 tbar
->OnLeftClick( toolID
, tbartool
->IsToggled() );
328 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
330 OSStatus result
= eventNotHandledErr
;
332 switch ( GetEventClass( event
) )
334 case kEventClassToolbarItem
:
335 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
);
345 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
349 // ============================================================================
351 // ============================================================================
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 bool wxToolBarTool::DoEnable( bool enable
)
361 GetControl()->Enable( enable
);
363 else if ( IsButton() )
365 #if wxMAC_USE_NATIVE_TOOLBAR
366 if ( m_toolbarItemRef
)
367 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
);
370 if ( m_controlHandle
)
372 #if TARGET_API_MAC_OSX
374 EnableControl( m_controlHandle
);
376 DisableControl( m_controlHandle
);
379 ActivateControl( m_controlHandle
);
381 DeactivateControl( m_controlHandle
);
389 void wxToolBarTool::SetPosition( const wxPoint
& position
)
396 int mac_x
= position
.x
;
397 int mac_y
= position
.y
;
399 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
401 GetToolBar()->MacWindowToRootWindow( &x
, &y
);
409 GetControlBounds( m_controlHandle
, &contrlRect
);
410 int former_mac_x
= contrlRect
.left
;
411 int former_mac_y
= contrlRect
.top
;
412 GetToolBar()->GetToolSize();
414 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
416 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
419 else if ( IsControl() )
421 GetControl()->Move( position
);
428 GetControlBounds( m_controlHandle
, &contrlRect
);
429 int former_mac_x
= contrlRect
.left
;
430 int former_mac_y
= contrlRect
.top
;
432 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
433 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
);
438 void wxToolBarTool::UpdateToggleImage( bool toggle
)
440 #if wxMAC_USE_NATIVE_TOOLBAR
442 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
443 #define kHIToolbarItemSelected (1 << 7)
446 // FIXME: this should be a OSX v10.4 runtime check
447 if (m_toolbarItemRef
!= NULL
)
449 OptionBits addAttrs
, removeAttrs
;
454 addAttrs
= kHIToolbarItemSelected
;
455 removeAttrs
= kHIToolbarItemNoAttributes
;
459 addAttrs
= kHIToolbarItemNoAttributes
;
460 removeAttrs
= kHIToolbarItemSelected
;
463 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
470 int w
= m_bmpNormal
.GetWidth();
471 int h
= m_bmpNormal
.GetHeight();
472 wxBitmap
bmp( w
, h
);
475 dc
.SelectObject( bmp
);
476 dc
.SetPen( wxNullPen
);
477 dc
.SetBackground( *wxWHITE
);
478 dc
.DrawRectangle( 0, 0, w
, h
);
479 dc
.DrawBitmap( m_bmpNormal
, 0, 0, true );
480 dc
.SelectObject( wxNullBitmap
);
481 ControlButtonContentInfo info
;
482 wxMacCreateBitmapButton( &info
, bmp
);
483 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
484 wxMacReleaseBitmapButton( &info
);
488 ControlButtonContentInfo info
;
489 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
490 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
), (Ptr
)&info
);
491 wxMacReleaseBitmapButton( &info
);
494 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
496 m_controlHandle
, 0, kControlIconTransformTag
,
497 sizeof( transform
), (Ptr
)&transform
);
498 HIViewSetNeedsDisplay( m_controlHandle
, true );
501 ::SetControl32BitValue( m_controlHandle
, toggle
);
505 wxToolBarTool::wxToolBarTool(
508 const wxString
& label
,
509 const wxBitmap
& bmpNormal
,
510 const wxBitmap
& bmpDisabled
,
512 wxObject
*clientData
,
513 const wxString
& shortHelp
,
514 const wxString
& longHelp
)
517 tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
518 clientData
, shortHelp
, longHelp
)
524 #pragma mark Toolbar Implementation
526 wxToolBarToolBase
*wxToolBar::CreateTool(
528 const wxString
& label
,
529 const wxBitmap
& bmpNormal
,
530 const wxBitmap
& bmpDisabled
,
532 wxObject
*clientData
,
533 const wxString
& shortHelp
,
534 const wxString
& longHelp
)
536 return new wxToolBarTool(
537 this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
538 clientData
, shortHelp
, longHelp
);
541 wxToolBarToolBase
* wxToolBar::CreateTool( wxControl
*control
)
543 return new wxToolBarTool(this, control
);
546 void wxToolBar::Init()
550 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
551 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
553 #if wxMAC_USE_NATIVE_TOOLBAR
554 m_macHIToolbarRef
= NULL
;
555 m_macUsesNativeToolbar
= false;
559 // also for the toolbar we have the dual implementation:
560 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
562 bool wxToolBar::Create(
568 const wxString
& name
)
570 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
573 OSStatus err
= noErr
;
575 #if wxMAC_USE_NATIVE_TOOLBAR
576 wxString labelStr
= wxString::Format( wxT("%xd"), (int)this );
577 err
= HIToolbarCreate(
578 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ), 0,
579 (HIToolbarRef
*) &m_macHIToolbarRef
);
581 if (m_macHIToolbarRef
!= NULL
)
583 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
584 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
586 if ( style
& wxTB_NOICONS
)
587 mode
= kHIToolbarDisplayModeLabelOnly
;
588 else if ( style
& wxTB_TEXT
)
589 mode
= kHIToolbarDisplayModeIconAndLabel
;
591 mode
= kHIToolbarDisplayModeIconOnly
;
593 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
594 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
);
598 return (err
== noErr
);
601 wxToolBar::~wxToolBar()
603 #if wxMAC_USE_NATIVE_TOOLBAR
604 if ( m_macHIToolbarRef
)
606 // if this is the installed toolbar, then deinstall it
607 if (m_macUsesNativeToolbar
)
608 MacInstallNativeToolbar( false );
610 CFRelease( (HIToolbarRef
) m_macHIToolbarRef
);
611 m_macHIToolbarRef
= NULL
;
616 bool wxToolBar::Show( bool show
)
619 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
621 bResult
= (tlw
!= NULL
);
624 #if wxMAC_USE_NATIVE_TOOLBAR
625 bool ownToolbarInstalled
= false;
626 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
627 if (ownToolbarInstalled
)
629 bResult
= ( IsWindowToolbarVisible( tlw
) != show
);
631 ShowHideWindowToolbar( tlw
, show
, false );
636 bResult
= wxToolBarBase::Show( show
);
642 bool wxToolBar::IsShown() const
646 #if wxMAC_USE_NATIVE_TOOLBAR
647 bool ownToolbarInstalled
;
648 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
649 if (ownToolbarInstalled
)
651 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
652 bResult
= IsWindowToolbarVisible( tlw
);
656 bResult
= wxToolBarBase::IsShown();
661 void wxToolBar::DoGetSize( int *width
, int *height
) const
663 #if wxMAC_USE_NATIVE_TOOLBAR
665 bool ownToolbarInstalled
;
667 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
668 if ( ownToolbarInstalled
)
670 // TODO: is this really a control ?
671 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
673 *width
= boundsR
.right
- boundsR
.left
;
674 if ( height
!= NULL
)
675 *height
= boundsR
.bottom
- boundsR
.top
;
679 wxToolBarBase::DoGetSize( width
, height
);
682 wxSize
wxToolBar::DoGetBestSize() const
686 DoGetSize( &width
, &height
);
688 return wxSize( width
, height
);
691 void wxToolBar::SetWindowStyleFlag( long style
)
693 wxToolBarBase::SetWindowStyleFlag( style
);
695 #if wxMAC_USE_NATIVE_TOOLBAR
696 if (m_macHIToolbarRef
!= NULL
)
698 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
700 if ( style
& wxTB_NOICONS
)
701 mode
= kHIToolbarDisplayModeLabelOnly
;
702 else if ( style
& wxTB_TEXT
)
703 mode
= kHIToolbarDisplayModeIconAndLabel
;
705 mode
= kHIToolbarDisplayModeIconOnly
;
707 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
712 #if wxMAC_USE_NATIVE_TOOLBAR
713 bool wxToolBar::MacWantsNativeToolbar()
715 return m_macUsesNativeToolbar
;
718 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
720 bool bResultV
= false;
722 if (ownToolbarInstalled
!= NULL
)
723 *ownToolbarInstalled
= false;
725 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
728 HIToolbarRef curToolbarRef
= NULL
;
729 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
730 bResultV
= ((err
== 0) && (curToolbarRef
!= NULL
));
731 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
732 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
738 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
740 bool bResult
= false;
742 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
746 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
749 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
752 // check the existing toolbar
753 HIToolbarRef curToolbarRef
= NULL
;
754 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
756 curToolbarRef
= NULL
;
758 m_macUsesNativeToolbar
= usesNative
;
760 if (m_macUsesNativeToolbar
)
762 // only install toolbar if there isn't one installed already
763 if (curToolbarRef
== NULL
)
767 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
768 ShowHideWindowToolbar( tlw
, true, false );
769 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
770 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
772 Rect r
= { 0, 0, 0, 0 };
773 m_peer
->SetRect( &r
);
774 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
775 m_peer
->SetVisibility( false, true );
776 wxToolBarBase::Show( false );
781 // only deinstall toolbar if this is the installed one
782 if (m_macHIToolbarRef
== curToolbarRef
)
786 ShowHideWindowToolbar( tlw
, false, false );
787 ChangeWindowAttributes( tlw
, 0, kWindowToolbarButtonAttribute
);
788 SetWindowToolbar( tlw
, NULL
);
790 m_peer
->SetVisibility( true, true );
795 InvalidateBestSize();
797 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
802 bool wxToolBar::Realize()
804 if (m_tools
.GetCount() == 0)
810 int maxToolWidth
= 0;
811 int maxToolHeight
= 0;
813 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
814 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
819 // find the maximum tool width and height
820 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
821 while ( node
!= NULL
)
823 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
827 wxSize sz
= tool
->GetSize();
829 if ( sz
.x
> maxToolWidth
)
831 if ( sz
.y
> maxToolHeight
)
832 maxToolHeight
= sz
.y
;
835 node
= node
->GetNext();
838 bool lastIsRadio
= false;
839 bool curIsRadio
= false;
840 bool setChoiceInGroup
= false;
842 #if wxMAC_USE_NATIVE_TOOLBAR
843 CFIndex currentPosition
= 0;
844 bool insertAll
= false;
847 node
= m_tools
.GetFirst();
848 while ( node
!= NULL
)
850 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
854 node
= node
->GetNext();
858 // set tool position:
859 // for the moment just perform a single row/column alignment
860 wxSize cursize
= tool
->GetSize();
861 if ( x
+ cursize
.x
> maxWidth
)
862 maxWidth
= x
+ cursize
.x
;
863 if ( y
+ cursize
.y
> maxHeight
)
864 maxHeight
= y
+ cursize
.y
;
866 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
868 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
869 tool
->SetPosition( wxPoint(x1
, y
) );
873 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
874 tool
->SetPosition( wxPoint(x
, y1
) );
877 // update the item positioning state
878 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
879 y
+= cursize
.y
+ kwxMacToolSpacing
;
881 x
+= cursize
.x
+ kwxMacToolSpacing
;
883 #if wxMAC_USE_NATIVE_TOOLBAR
884 // install in native HIToolbar
885 if ( m_macHIToolbarRef
!= NULL
)
887 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
888 if ( hiItemRef
!= NULL
)
890 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
892 OSStatus err
= noErr
;
897 // if this is the first tool that gets newly inserted or repositioned
898 // first remove all 'old' tools from here to the right, because of this
899 // all following tools will have to be reinserted (insertAll). i = 100 because there's
900 // no way to determine how many there are in a toolbar, so just a high number :-(
901 for ( CFIndex i
= 100; i
>= currentPosition
; --i
)
903 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
);
908 wxString errMsg
= wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err
);
909 wxASSERT_MSG( 0, errMsg
.c_str() );
913 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
);
916 wxString errMsg
= wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err
);
917 wxASSERT_MSG( 0, errMsg
.c_str() );
920 tool
->SetIndex( currentPosition
);
928 // update radio button (and group) state
929 lastIsRadio
= curIsRadio
;
930 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
934 if ( tool
->IsToggled() )
935 DoToggleTool( tool
, true );
937 setChoiceInGroup
= false;
943 if ( tool
->Toggle( true ) )
945 DoToggleTool( tool
, true );
946 setChoiceInGroup
= true;
949 else if ( tool
->IsToggled() )
951 if ( tool
->IsToggled() )
952 DoToggleTool( tool
, true );
954 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
955 while ( nodePrev
!= NULL
)
957 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
958 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
961 if ( toggleTool
->Toggle( false ) )
962 DoToggleTool( toggleTool
, false );
964 nodePrev
= nodePrev
->GetPrevious();
969 node
= node
->GetNext();
972 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
974 // if not set yet, only one row
975 if ( m_maxRows
<= 0 )
978 m_minWidth
= maxWidth
;
980 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
981 m_minHeight
= m_maxHeight
= maxHeight
;
985 // if not set yet, have one column
986 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
987 SetRows( GetToolsCount() );
989 m_minHeight
= maxHeight
;
991 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
992 m_minWidth
= m_maxWidth
= maxWidth
;
996 // FIXME: should this be OSX-only?
998 bool wantNativeToolbar
, ownToolbarInstalled
;
1000 // attempt to install the native toolbar
1001 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
1002 MacInstallNativeToolbar( wantNativeToolbar
);
1003 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
1004 if (!ownToolbarInstalled
)
1006 SetSize( maxWidth
, maxHeight
);
1007 InvalidateBestSize();
1011 SetSize( maxWidth
, maxHeight
);
1012 InvalidateBestSize();
1015 SetBestFittingSize();
1020 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1022 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1023 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1025 #if wxMAC_USE_NATIVE_TOOLBAR
1026 if (m_macHIToolbarRef
!= NULL
)
1028 int maxs
= wxMax( size
.x
, size
.y
);
1029 HIToolbarDisplaySize sizeSpec
;
1031 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1032 else if ( maxs
> 24 )
1033 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1035 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1037 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1042 // The button size is bigger than the bitmap size
1043 wxSize
wxToolBar::GetToolSize() const
1045 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1048 void wxToolBar::SetRows(int nRows
)
1050 // avoid resizing the frame uselessly
1051 if ( nRows
!= m_maxRows
)
1055 void wxToolBar::MacSuperChangedPosition()
1057 wxWindow::MacSuperChangedPosition();
1059 #if wxMAC_USE_NATIVE_TOOLBAR
1060 if (! m_macUsesNativeToolbar
)
1067 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1069 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1070 while ( node
!= NULL
)
1072 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1076 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1077 if ( r
.Contains( wxPoint( x
, y
) ) )
1081 node
= node
->GetNext();
1084 return (wxToolBarToolBase
*)NULL
;
1087 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1089 wxToolBarToolBase
*tool
= FindToolForPosition( pt
.x
, pt
.y
);
1091 return tool
->GetShortHelp();
1093 return wxEmptyString
;
1096 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1099 ((wxToolBarTool
*)t
)->DoEnable( enable
);
1102 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1104 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1105 if ( ( tool
!= NULL
) && tool
->IsButton() )
1106 tool
->UpdateToggleImage( toggle
);
1109 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
1111 wxToolBarTool
*tool
= wx_static_cast( wxToolBarTool
*, toolBase
);
1115 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1116 wxSize toolSize
= GetToolSize();
1117 Rect toolrect
= { 0, 0, toolSize
.y
, toolSize
.x
};
1118 ControlRef controlHandle
= NULL
;
1121 switch (tool
->GetStyle())
1123 case wxTOOL_STYLE_SEPARATOR
:
1125 wxASSERT( tool
->GetControlHandle() == NULL
);
1128 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1129 toolrect
.bottom
= toolSize
.y
;
1131 toolrect
.right
= toolSize
.x
;
1133 #ifdef __WXMAC_OSX__
1134 // in flat style we need a visual separator
1135 #if wxMAC_USE_NATIVE_TOOLBAR
1136 HIToolbarItemRef item
;
1137 err
= HIToolbarItemCreate(
1138 kHIToolbarSeparatorIdentifier
,
1139 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1142 tool
->SetToolbarItemRef( item
);
1145 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1146 tool
->SetControlHandle( controlHandle
);
1151 case wxTOOL_STYLE_BUTTON
:
1153 wxASSERT( tool
->GetControlHandle() == NULL
);
1154 ControlButtonContentInfo info
;
1155 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap(), kControlContentIconRef
);
1157 if ( UMAGetSystemVersion() >= 0x1000)
1159 CreateIconControl( window
, &toolrect
, &info
, false, &controlHandle
);
1163 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1164 if ( tool
->CanBeToggled() )
1165 behaviour
|= kControlBehaviorToggles
;
1166 err
= CreateBevelButtonControl( window
,
1167 &toolrect
, CFSTR(""), kControlBevelButtonNormalBevel
,
1168 behaviour
, &info
, 0, 0, 0, &controlHandle
);
1171 #if wxMAC_USE_NATIVE_TOOLBAR
1172 HIToolbarItemRef item
;
1173 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1174 err
= HIToolbarItemCreate(
1175 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1176 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1179 InstallEventHandler( 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
);
1190 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
);
1191 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() );
1194 InstallControlEventHandler(
1195 (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1196 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1198 tool
->SetControlHandle( controlHandle
);
1202 case wxTOOL_STYLE_CONTROL
:
1203 wxASSERT( tool
->GetControl() != NULL
);
1205 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1206 // FIXME: doesn't work yet...
1208 HIToolbarItemRef item
;
1209 wxString labelStr
= wxString::Format( wxT("%xd"), (int)tool
);
1210 result
= HIToolbarItemCreate(
1211 wxMacCFStringHolder( labelStr
, wxFont::GetDefaultEncoding() ),
1212 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1214 if ( result
== noErr
)
1216 HIToolbarItemSetLabel( item
, wxMacCFStringHolder( tool
->GetLabel(), m_font
.GetEncoding() ) );
1217 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1218 tool
->SetToolbarItemRef( item
);
1220 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1221 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1223 // FIXME: is this necessary ??
1224 ::GetControlBounds( controlHandle
, &toolrect
);
1225 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1227 // FIXME: is this necessary ??
1228 InstallControlEventHandler(
1229 controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1230 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1235 // FIXME: right now there's nothing to do here
1245 if ( controlHandle
)
1247 ControlRef container
= (ControlRef
) GetHandle();
1248 wxASSERT_MSG( container
!= NULL
, wxT("No valid Mac container control") );
1250 UMAShowControl( controlHandle
);
1251 ::EmbedControl( controlHandle
, container
);
1254 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1255 tool
->UpdateToggleImage( true );
1257 // nothing special to do here - we relayout in Realize() later
1258 tool
->Attach( this );
1259 InvalidateBestSize();
1263 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err
);
1264 wxASSERT_MSG( false, errMsg
.c_str() );
1267 return (err
== noErr
);
1270 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1272 wxFAIL_MSG( wxT("not implemented") );
1275 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1277 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
*, toolbase
);
1278 wxToolBarToolsList::compatibility_iterator node
;
1279 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1281 wxToolBarToolBase
*tool2
= node
->GetData();
1282 if ( tool2
== tool
)
1284 // let node point to the next node in the list
1285 node
= node
->GetNext();
1291 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize();
1295 #if wxMAC_USE_NATIVE_TOOLBAR
1296 CFIndex removeIndex
= tool
->GetIndex();
1299 switch ( tool
->GetStyle() )
1301 case wxTOOL_STYLE_CONTROL
:
1303 tool
->GetControl()->Destroy();
1304 tool
->ClearControl();
1308 case wxTOOL_STYLE_BUTTON
:
1309 case wxTOOL_STYLE_SEPARATOR
:
1310 if ( tool
->GetControlHandle() )
1312 DisposeControl( (ControlRef
) tool
->GetControlHandle() );
1314 #if wxMAC_USE_NATIVE_TOOLBAR
1315 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1317 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
);
1318 tool
->SetIndex( -1 );
1322 tool
->ClearControl();
1330 // and finally reposition all the controls after this one
1332 for ( /* node -> first after deleted */; node
; node
= node
->GetNext() )
1334 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1335 wxPoint pt
= tool2
->GetPosition();
1337 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1342 tool2
->SetPosition( pt
);
1344 #if wxMAC_USE_NATIVE_TOOLBAR
1345 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1346 tool2
->SetIndex( tool2
->GetIndex() - 1 );
1350 InvalidateBestSize();
1355 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1357 #if wxMAC_USE_NATIVE_TOOLBAR
1358 if ( m_macUsesNativeToolbar
)
1370 bool drawMetalTheme
= MacGetTopLevelWindow()->MacGetMetalAppearance();
1371 bool minimumUmaAvailable
= (UMAGetSystemVersion() >= 0x1030);
1373 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1374 if ( !drawMetalTheme
&& minimumUmaAvailable
)
1376 HIThemePlacardDrawInfo info
;
1377 memset( &info
, 0, sizeof(info
) );
1379 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1381 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef();
1382 HIRect rect
= CGRectMake( 0, 0, w
, h
);
1383 HIThemeDrawPlacard( &rect
, &info
, cgContext
, kHIThemeOrientationNormal
);
1387 // leave the background as it is (striped or metal)
1392 const bool drawBorder
= true;
1396 wxMacPortSetter
helper( &dc
);
1398 if ( !drawMetalTheme
|| !minimumUmaAvailable
)
1400 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1401 dc
.YLOG2DEVMAC(h
), dc
.XLOG2DEVMAC(w
) };
1404 if ( toolbarrect
.left
< 0 )
1405 toolbarrect
.left
= 0;
1406 if ( toolbarrect
.top
< 0 )
1407 toolbarrect
.top
= 0;
1410 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
);
1414 #if TARGET_API_MAC_OSX
1415 HIRect hiToolbarrect
= CGRectMake(
1416 dc
.YLOG2DEVMAC(0), dc
.XLOG2DEVMAC(0),
1417 dc
.YLOG2DEVREL(h
), dc
.XLOG2DEVREL(w
) );
1418 CGContextRef cgContext
;
1421 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
);
1422 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1424 CGContextTranslateCTM( cgContext
, 0, bounds
.bottom
- bounds
.top
);
1425 CGContextScaleCTM( cgContext
, 1, -1 );
1427 HIThemeBackgroundDrawInfo drawInfo
;
1428 drawInfo
.version
= 0;
1429 drawInfo
.state
= kThemeStateActive
;
1430 drawInfo
.kind
= kThemeBackgroundMetal
;
1431 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
);
1433 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
);
1442 #endif // wxUSE_TOOLBAR