1 /////////////////////////////////////////////////////////////////////////////
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
;
212 wxMacCarbonEvent
cEvent( event
) ;
214 ControlRef controlRef
;
216 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
218 switch ( GetEventKind( event
) )
220 case kEventControlHit
:
222 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
223 wxToolBar
*tbar
= tbartool
!= NULL
? (wxToolBar
*) (tbartool
->GetToolBar()) : NULL
;
224 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled())
229 shouldToggle
= !tbartool
->IsToggled();
231 shouldToggle
= ( GetControl32BitValue((ControlRef
) tbartool
->GetControlHandle()) != 0 );
234 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
237 if (tbartool
!= NULL
)
238 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
244 case kEventControlHitTest
:
246 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
248 HIViewGetBounds( controlRef
, &rect
) ;
250 ControlPartCode pc
= kControlNoPart
;
251 if ( CGRectContainsPoint( rect
, pt
) )
252 pc
= kControlIconPart
;
253 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
266 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
268 OSStatus result
= eventNotHandledErr
;
270 switch ( GetEventClass( event
) )
272 case kEventClassControl
:
273 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
283 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
285 #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 // ============================================================================
353 // ============================================================================
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 bool wxToolBarTool::DoEnable(bool enable
)
363 GetControl()->Enable( enable
) ;
365 else if ( IsButton() )
367 #if wxMAC_USE_NATIVE_TOOLBAR
368 if ( m_toolbarItemRef
)
369 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
) ;
372 if ( m_controlHandle
)
374 #if TARGET_API_MAC_OSX
376 EnableControl( m_controlHandle
) ;
378 DisableControl( m_controlHandle
) ;
381 ActivateControl( m_controlHandle
) ;
383 DeactivateControl( m_controlHandle
) ;
391 void wxToolBarTool::SetPosition(const wxPoint
& position
)
398 int mac_x
= position
.x
;
399 int mac_y
= position
.y
;
401 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
403 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
411 GetControlBounds( m_controlHandle
, &contrlRect
) ;
412 int former_mac_x
= contrlRect
.left
;
413 int former_mac_y
= contrlRect
.top
;
414 GetToolBar()->GetToolSize() ;
416 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
418 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
421 else if ( IsControl() )
423 GetControl()->Move( position
) ;
430 GetControlBounds( m_controlHandle
, &contrlRect
) ;
431 int former_mac_x
= contrlRect
.left
;
432 int former_mac_y
= contrlRect
.top
;
434 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
435 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
440 void wxToolBarTool::UpdateToggleImage( bool toggle
)
442 #if wxMAC_USE_NATIVE_TOOLBAR
444 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
445 #define kHIToolbarItemSelected (1 << 7)
448 // FIXME: this should be a OSX v10.4 runtime check
449 if (m_toolbarItemRef
!= NULL
)
451 OptionBits addAttrs
, removeAttrs
;
456 addAttrs
= kHIToolbarItemSelected
;
457 removeAttrs
= kHIToolbarItemNoAttributes
;
461 addAttrs
= kHIToolbarItemNoAttributes
;
462 removeAttrs
= kHIToolbarItemSelected
;
465 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
472 int w
= m_bmpNormal
.GetWidth() ;
473 int h
= m_bmpNormal
.GetHeight() ;
474 wxBitmap
bmp( w
, h
) ;
477 dc
.SelectObject( bmp
) ;
478 dc
.SetPen( wxNullPen
) ;
479 dc
.SetBackground( *wxWHITE
) ;
480 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
481 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true ) ;
482 dc
.SelectObject( wxNullBitmap
) ;
483 ControlButtonContentInfo info
;
484 wxMacCreateBitmapButton( &info
, bmp
) ;
485 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof(info
), (Ptr
)&info
);
486 wxMacReleaseBitmapButton( &info
) ;
490 ControlButtonContentInfo info
;
491 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
492 SetControlData( m_controlHandle
, 0, kControlIconContentTag
,
493 sizeof( info
), (Ptr
)&info
);
494 wxMacReleaseBitmapButton( &info
) ;
497 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
498 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
,
499 sizeof( transform
), (Ptr
)&transform
);
500 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
503 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
507 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
509 const wxString
& label
,
510 const wxBitmap
& bmpNormal
,
511 const wxBitmap
& bmpDisabled
,
513 wxObject
*clientData
,
514 const wxString
& shortHelp
,
515 const wxString
& longHelp
)
516 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
517 clientData
, shortHelp
, longHelp
)
523 #pragma mark Toolbar Implementation
525 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
526 const wxString
& label
,
527 const wxBitmap
& bmpNormal
,
528 const wxBitmap
& bmpDisabled
,
530 wxObject
*clientData
,
531 const wxString
& shortHelp
,
532 const wxString
& longHelp
)
534 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
535 clientData
, shortHelp
, longHelp
);
538 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
540 return new wxToolBarTool(this, control
);
543 void wxToolBar::Init()
547 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
548 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
550 #if wxMAC_USE_NATIVE_TOOLBAR
551 m_macHIToolbarRef
= NULL
;
552 m_macUsesNativeToolbar
= false ;
556 // also for the toolbar we have the dual implementation:
557 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
559 bool wxToolBar::Create(wxWindow
*parent
,
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( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding() ) , 0 ,
574 (HIToolbarRef
*) &m_macHIToolbarRef
);
576 if (m_macHIToolbarRef
!= NULL
)
578 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
579 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
581 if ( style
& wxTB_NOICONS
)
582 mode
= kHIToolbarDisplayModeLabelOnly
;
583 else if ( style
& wxTB_TEXT
)
584 mode
= kHIToolbarDisplayModeIconAndLabel
;
586 mode
= kHIToolbarDisplayModeIconOnly
;
588 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
) ;
589 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
) ;
593 return (err
== noErr
);
596 wxToolBar::~wxToolBar()
598 #if wxMAC_USE_NATIVE_TOOLBAR
599 if ( m_macHIToolbarRef
)
601 // if this is the installed toolbar, then deinstall it
602 if (m_macUsesNativeToolbar
)
603 MacInstallNativeToolbar( false );
605 CFRelease( (HIToolbarRef
) m_macHIToolbarRef
);
606 m_macHIToolbarRef
= NULL
;
611 bool wxToolBar::Show( bool show
)
614 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
616 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 );
631 bResult
= wxToolBarBase::Show( show
);
637 bool wxToolBar::IsShown() const
641 #if wxMAC_USE_NATIVE_TOOLBAR
642 bool ownToolbarInstalled
;
643 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
644 if (ownToolbarInstalled
)
646 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
647 bResult
= IsWindowToolbarVisible(tlw
) ;
651 bResult
= wxToolBarBase::IsShown();
656 void wxToolBar::DoGetSize( int *width
, int *height
) const
658 #if wxMAC_USE_NATIVE_TOOLBAR
660 bool ownToolbarInstalled
;
662 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
663 if ( ownToolbarInstalled
)
665 // TODO: is this really a control ?
666 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
668 *width
= boundsR
.right
- boundsR
.left
;
669 if ( height
!= NULL
)
670 *height
= boundsR
.bottom
- boundsR
.top
;
674 wxToolBarBase::DoGetSize( width
, height
);
677 wxSize
wxToolBar::DoGetBestSize() const
681 DoGetSize( &width
, &height
) ;
683 return wxSize( width
, height
) ;
686 void wxToolBar::SetWindowStyleFlag( long style
)
688 wxToolBarBase::SetWindowStyleFlag( style
);
690 #if wxMAC_USE_NATIVE_TOOLBAR
691 if (m_macHIToolbarRef
!= NULL
)
693 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
695 if ( style
& wxTB_NOICONS
)
696 mode
= kHIToolbarDisplayModeLabelOnly
;
697 else if ( style
& wxTB_TEXT
)
698 mode
= kHIToolbarDisplayModeIconAndLabel
;
700 mode
= kHIToolbarDisplayModeIconOnly
;
702 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
707 #if wxMAC_USE_NATIVE_TOOLBAR
708 bool wxToolBar::MacWantsNativeToolbar()
710 return m_macUsesNativeToolbar
;
713 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
715 bool bResultV
= false;
717 if (ownToolbarInstalled
!= NULL
)
718 *ownToolbarInstalled
= false;
720 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
723 HIToolbarRef curToolbarRef
= NULL
;
724 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
725 bResultV
= ((err
== 0) && (curToolbarRef
!= NULL
));
726 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
727 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
733 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
735 bool bResult
= false;
737 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
741 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
744 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
747 // check the existing toolbar
748 HIToolbarRef curToolbarRef
= NULL
;
749 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
751 curToolbarRef
= NULL
;
753 m_macUsesNativeToolbar
= usesNative
;
755 if (m_macUsesNativeToolbar
)
757 // only install toolbar if there isn't one installed already
758 if (curToolbarRef
== NULL
)
762 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
763 ShowHideWindowToolbar( tlw
, true, false );
764 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
765 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
767 Rect r
= { 0 , 0 , 0 , 0 };
768 m_peer
->SetRect( &r
);
769 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
770 m_peer
->SetVisibility( false, true );
771 wxToolBarBase::Show( false );
776 // only deinstall toolbar if this is the installed one
777 if (m_macHIToolbarRef
== curToolbarRef
)
781 ShowHideWindowToolbar( tlw
, false, false );
782 ChangeWindowAttributes( tlw
, 0 , kWindowToolbarButtonAttribute
);
783 SetWindowToolbar( tlw
, NULL
);
785 m_peer
->SetVisibility( true, true );
790 InvalidateBestSize();
792 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
797 bool wxToolBar::Realize()
799 if (m_tools
.GetCount() == 0)
805 int maxToolWidth
= 0;
806 int maxToolHeight
= 0;
808 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
809 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
814 // find the maximum tool width and height
815 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
816 while ( node
!= NULL
)
818 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
822 wxSize sz
= tool
->GetSize();
824 if ( sz
.x
> maxToolWidth
)
826 if ( sz
.y
> maxToolHeight
)
827 maxToolHeight
= sz
.y
;
830 node
= node
->GetNext();
833 bool lastIsRadio
= false;
834 bool curIsRadio
= false;
835 bool setChoiceInGroup
= false;
837 #if wxMAC_USE_NATIVE_TOOLBAR
838 CFIndex currentPosition
= 0 ;
839 bool insertAll
= false ;
842 node
= m_tools
.GetFirst();
843 while ( node
!= NULL
)
845 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
849 node
= node
->GetNext();
853 // set tool position:
854 // for the moment just perform a single row/column alignment
855 wxSize cursize
= tool
->GetSize();
856 if ( x
+ cursize
.x
> maxWidth
)
857 maxWidth
= x
+ cursize
.x
;
858 if ( y
+ cursize
.y
> maxHeight
)
859 maxHeight
= y
+ cursize
.y
;
861 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
863 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
864 tool
->SetPosition( wxPoint(x1
, y
) );
868 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
869 tool
->SetPosition( wxPoint(x
, y1
) );
872 // update the item positioning state
873 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
874 y
+= cursize
.y
+ kwxMacToolSpacing
;
876 x
+= cursize
.x
+ kwxMacToolSpacing
;
878 #if wxMAC_USE_NATIVE_TOOLBAR
879 // install in native HIToolbar
880 if ( m_macHIToolbarRef
!= NULL
)
882 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
883 if ( hiItemRef
!= NULL
)
885 if ( insertAll
|| (tool
->GetIndex() != currentPosition
) )
887 OSStatus err
= noErr
;
890 // if this is the first tool that gets newly inserted or repositioned
891 // first remove all 'old' tools from here to the right, because of this
892 // all following tools will have to be reinserted (insertAll). i = 100 because there's
893 // no way to determine how many there are in a toolbar, so just a high number :-(
894 for ( CFIndex i
= 100 ; i
>= currentPosition
; --i
)
896 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
) ;
898 wxASSERT_MSG( err
== noErr
, _T("HIToolbarRemoveItemAtIndex failed") );
902 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
) ;
903 wxASSERT_MSG( err
== noErr
, _T("HIToolbarInsertItemAtIndex failed") );
905 tool
->SetIndex( currentPosition
) ;
911 #endif // wxMAC_USE_NATIVE_TOOLBAR
913 // update radio button (and group) state
914 lastIsRadio
= curIsRadio
;
915 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
919 if ( tool
->IsToggled() )
920 DoToggleTool( tool
, true );
922 setChoiceInGroup
= false;
928 if ( tool
->Toggle(true) )
930 DoToggleTool( tool
, true );
931 setChoiceInGroup
= true;
934 else if ( tool
->IsToggled() )
936 if ( tool
->IsToggled() )
937 DoToggleTool( tool
, true );
939 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
940 while ( nodePrev
!= NULL
)
942 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
943 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
946 if ( toggleTool
->Toggle(false) )
947 DoToggleTool( toggleTool
, false );
949 nodePrev
= nodePrev
->GetPrevious();
954 node
= node
->GetNext();
957 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
959 // if not set yet, only one row
960 if ( m_maxRows
<= 0 )
963 m_minWidth
= maxWidth
;
965 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
966 m_minHeight
= m_maxHeight
= maxHeight
;
970 // if not set yet, have one column
971 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
972 SetRows( GetToolsCount() );
974 m_minHeight
= maxHeight
;
976 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
977 m_minWidth
= m_maxWidth
= maxWidth
;
981 // FIXME: should this be OSX-only?
983 bool wantNativeToolbar
, ownToolbarInstalled
;
985 // attempt to install the native toolbar
986 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
987 MacInstallNativeToolbar( wantNativeToolbar
);
988 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
989 if (!ownToolbarInstalled
)
991 SetSize( maxWidth
, maxHeight
);
992 InvalidateBestSize();
996 SetSize( maxWidth
, maxHeight
);
997 InvalidateBestSize();
1000 SetBestFittingSize();
1005 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1007 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
1008 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
1010 #if wxMAC_USE_NATIVE_TOOLBAR
1011 if (m_macHIToolbarRef
!= NULL
)
1013 int maxs
= wxMax( size
.x
, size
.y
);
1014 HIToolbarDisplaySize sizeSpec
;
1016 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1017 else if ( maxs
> 24 )
1018 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1020 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1022 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1027 // The button size is bigger than the bitmap size
1028 wxSize
wxToolBar::GetToolSize() const
1030 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1033 void wxToolBar::SetRows(int nRows
)
1035 // avoid resizing the frame uselessly
1036 if ( nRows
!= m_maxRows
)
1040 void wxToolBar::MacSuperChangedPosition()
1042 wxWindow::MacSuperChangedPosition();
1044 #if wxMAC_USE_NATIVE_TOOLBAR
1045 if (! m_macUsesNativeToolbar
)
1052 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1054 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1055 while ( node
!= NULL
)
1057 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
1061 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1062 if ( r
.Contains( wxPoint( x
, y
) ) )
1066 node
= node
->GetNext();
1069 return (wxToolBarToolBase
*)NULL
;
1072 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1074 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
1076 return tool
->GetShortHelp() ;
1078 return wxEmptyString
;
1081 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1084 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
1087 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1089 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1090 if ( ( tool
!= NULL
) && tool
->IsButton() )
1091 tool
->UpdateToggleImage( toggle
);
1094 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
1095 wxToolBarToolBase
*toolBase
)
1097 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
);
1101 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1102 wxSize toolSize
= GetToolSize();
1103 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
};
1104 ControlRef controlHandle
= NULL
;
1107 switch (tool
->GetStyle())
1109 case wxTOOL_STYLE_SEPARATOR
:
1111 wxASSERT( tool
->GetControlHandle() == NULL
);
1114 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1115 toolrect
.bottom
= toolSize
.y
;
1117 toolrect
.right
= toolSize
.x
;
1119 #ifdef __WXMAC_OSX__
1120 // in flat style we need a visual separator
1121 #if wxMAC_USE_NATIVE_TOOLBAR
1122 HIToolbarItemRef item
;
1123 err
= HIToolbarItemCreate(
1124 kHIToolbarSeparatorIdentifier
,
1125 kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
,
1128 tool
->SetToolbarItemRef( item
);
1131 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1132 tool
->SetControlHandle( controlHandle
);
1137 case wxTOOL_STYLE_BUTTON
:
1139 wxASSERT( tool
->GetControlHandle() == NULL
) ;
1140 ControlButtonContentInfo info
;
1141 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
1143 if ( UMAGetSystemVersion() >= 0x1000)
1144 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
1147 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1148 if ( tool
->CanBeToggled() )
1149 behaviour
|= kControlBehaviorToggles
;
1150 CreateBevelButtonControl( window
,
1151 &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
,
1152 behaviour
, &info
, 0 , 0 , 0 , &controlHandle
) ;
1155 #if wxMAC_USE_NATIVE_TOOLBAR
1156 HIToolbarItemRef item
;
1157 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1158 err
= HIToolbarItemCreate(
1159 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1160 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1163 InstallEventHandler( HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1164 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1165 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1166 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1167 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1168 tool
->SetToolbarItemRef( item
);
1172 wxMacReleaseBitmapButton( &info
) ;
1174 SetBevelButtonTextPlacement( m_controlHandle
, kControlBevelButtonPlaceBelowGraphic
) ;
1175 UMASetControlTitle( m_controlHandle
, label
, wxFont::GetDefaultEncoding() ) ;
1178 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1179 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1181 tool
->SetControlHandle( controlHandle
);
1185 case wxTOOL_STYLE_CONTROL
:
1186 wxASSERT( tool
->GetControl() != NULL
);
1187 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1188 // FIXME: doesn't work yet...
1190 HIToolbarItemRef item
;
1191 wxString labelStr
= wxString::Format( wxT("%xd"), (int) tool
);
1192 result
= HIToolbarItemCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1193 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1197 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1198 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1199 tool
->SetToolbarItemRef( item
);
1201 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1202 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1204 // FIXME: is this necessary ??
1205 ::GetControlBounds( controlHandle
, &toolrect
);
1206 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1208 // FIXME: is this necessary ??
1209 InstallControlEventHandler( controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1210 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1215 // FIXME: right now there's nothing to do here
1225 if ( controlHandle
)
1227 ControlRef container
= (ControlRef
) GetHandle();
1228 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") );
1230 UMAShowControl( controlHandle
);
1231 ::EmbedControl( controlHandle
, container
);
1234 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1235 tool
->UpdateToggleImage( true );
1237 // nothing special to do here - we relayout in Realize() later
1239 InvalidateBestSize();
1243 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err
);
1244 wxASSERT_MSG( false, errMsg
.c_str() );
1247 return (err
== noErr
);
1250 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1252 wxFAIL_MSG( _T("not implemented") );
1255 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1257 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
1258 wxToolBarToolsList::compatibility_iterator node
;
1259 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1261 wxToolBarToolBase
*tool2
= node
->GetData();
1262 if ( tool2
== tool
)
1264 // let node point to the next node in the list
1265 node
= node
->GetNext();
1271 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
1275 #if wxMAC_USE_NATIVE_TOOLBAR
1276 CFIndex removeIndex
= tool
->GetIndex();
1279 switch ( tool
->GetStyle() )
1281 case wxTOOL_STYLE_CONTROL
:
1283 tool
->GetControl()->Destroy();
1284 tool
->ClearControl() ;
1288 case wxTOOL_STYLE_BUTTON
:
1289 case wxTOOL_STYLE_SEPARATOR
:
1290 if ( tool
->GetControlHandle() )
1292 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
1294 #if wxMAC_USE_NATIVE_TOOLBAR
1295 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1297 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
) ;
1298 tool
->SetIndex( -1 ) ;
1302 tool
->ClearControl() ;
1310 // and finally reposition all the controls after this one
1312 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
1314 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1315 wxPoint pt
= tool2
->GetPosition() ;
1317 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1322 tool2
->SetPosition( pt
) ;
1324 #if wxMAC_USE_NATIVE_TOOLBAR
1325 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1326 tool2
->SetIndex( tool2
->GetIndex() - 1 ) ;
1330 InvalidateBestSize();
1335 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1337 #if wxMAC_USE_NATIVE_TOOLBAR
1338 if ( m_macUsesNativeToolbar
)
1345 wxPaintDC
dc(this) ;
1348 GetSize( &w
, &h
) ;
1350 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1351 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1353 if ( UMAGetSystemVersion() >= 0x1030 )
1355 HIThemePlacardDrawInfo info
;
1356 memset( &info
, 0, sizeof(info
) ) ;
1358 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1360 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
1361 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
1362 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
1367 // leave the background as it is (striped or metal)
1371 wxMacPortSetter
helper(&dc
) ;
1373 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1374 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
1376 if ( toolbarrect.left < 0 )
1377 toolbarrect.left = 0 ;
1378 if ( toolbarrect.top < 0 )
1379 toolbarrect.top = 0 ;
1382 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1384 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1388 #if TARGET_API_MAC_OSX
1389 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1390 if ( UMAGetSystemVersion() >= 0x1030 )
1392 HIRect hiToolbarrect
= CGRectMake(
1393 dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1394 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
1395 CGContextRef cgContext
;
1397 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
1398 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1399 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
1400 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
1403 HIThemeBackgroundDrawInfo drawInfo
;
1404 drawInfo
.version
= 0 ;
1405 drawInfo
.state
= kThemeStateActive
;
1406 drawInfo
.kind
= kThemeBackgroundMetal
;
1407 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
, kHIThemeOrientationNormal
) ;
1410 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1415 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1424 #endif // wxUSE_TOOLBAR