+ { kEventClassControl, kEventControlHit },
+ { kEventClassControl, kEventControlHitTest },
+};
+
+static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
+{
+ OSStatus result = eventNotHandledErr;
+ ControlRef controlRef;
+ wxMacCarbonEvent cEvent( event );
+
+ cEvent.GetParameter( kEventParamDirectObject, &controlRef );
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventControlHit:
+ {
+ wxToolBarTool *tbartool = (wxToolBarTool*)data;
+ wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL;
+ if ((tbartool != NULL) && tbartool->CanBeToggled())
+ {
+ bool shouldToggle;
+
+ shouldToggle = !tbartool->IsToggled();
+
+ tbar->ToggleTool( tbartool->GetId(), shouldToggle );
+ }
+
+ if (tbartool != NULL)
+ tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
+ result = noErr;
+ }
+ break;
+
+ case kEventControlHitTest:
+ {
+ HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation);
+ HIRect rect;
+ HIViewGetBounds( controlRef, &rect );
+
+ ControlPartCode pc = kControlNoPart;
+ if ( CGRectContainsPoint( rect, pt ) )
+ pc = kControlIconPart;
+ cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc );
+ result = noErr;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return result;
+}
+
+static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
+{
+ OSStatus result = eventNotHandledErr;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassControl:
+ result = wxMacToolBarToolControlEventHandler( handler, event, data );
+ break;
+
+ default:
+ break;
+ }
+
+ return result;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+
+static const EventTypeSpec toolBarEventList[] =
+{
+ { kEventClassToolbarItem, kEventToolbarItemPerformAction },
+};
+
+static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
+{
+ OSStatus result = eventNotHandledErr;
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventToolbarItemPerformAction:
+ {
+ wxToolBarTool* tbartool = (wxToolBarTool*) data;
+ if ( tbartool != NULL )
+ {
+ wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar());
+ int toolID = tbartool->GetId();
+
+ if ( tbartool->CanBeToggled() )
+ {
+ if ( tbar != NULL )
+ tbar->ToggleTool(toolID, !tbartool->IsToggled() );
+ }
+
+ if ( tbar != NULL )
+ tbar->OnLeftClick( toolID, tbartool->IsToggled() );
+ result = noErr;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return result;
+}
+
+static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
+{
+ OSStatus result = eventNotHandledErr;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassToolbarItem:
+ result = wxMacToolBarCommandEventHandler( handler, event, data );
+ break;
+
+ default:
+ break;
+ }
+
+ return result;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
+
+#endif
+
+bool wxToolBarTool::DoEnable( bool enable )
+{
+ if ( IsControl() )
+ {
+ GetControl()->Enable( enable );
+ }
+ else if ( IsButton() )
+ {
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if ( m_toolbarItemRef != NULL )
+ HIToolbarItemSetEnabled( m_toolbarItemRef, enable );
+#endif
+
+ if ( m_controlHandle != NULL )
+ {
+ if ( enable )
+ EnableControl( m_controlHandle );
+ else
+ DisableControl( m_controlHandle );
+ }
+ }
+
+ return true;
+}
+
+void wxToolBarTool::SetPosition( const wxPoint& position )
+{
+ m_x = position.x;
+ m_y = position.y;
+
+ int mac_x = position.x;
+ int mac_y = position.y;
+
+ if ( IsButton() )
+ {
+ Rect contrlRect;
+ GetControlBounds( m_controlHandle, &contrlRect );
+ int former_mac_x = contrlRect.left;
+ int former_mac_y = contrlRect.top;
+ GetToolBar()->GetToolSize();
+
+ if ( mac_x != former_mac_x || mac_y != former_mac_y )
+ {
+ ::MoveControl( m_controlHandle, mac_x, mac_y );
+ }
+ }
+ else if ( IsControl() )
+ {
+ // embedded native controls are moved by the OS
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
+#endif
+ {
+ GetControl()->Move( position );
+ }
+ }
+ else
+ {
+ // separator
+ Rect contrlRect;
+ GetControlBounds( m_controlHandle, &contrlRect );
+ int former_mac_x = contrlRect.left;
+ int former_mac_y = contrlRect.top;
+
+ if ( mac_x != former_mac_x || mac_y != former_mac_y )
+ ::MoveControl( m_controlHandle, mac_x, mac_y );
+ }
+}
+
+void wxToolBarTool::UpdateToggleImage( bool toggle )
+{
+ if ( toggle )
+ {
+ int w = m_bmpNormal.GetWidth();
+ int h = m_bmpNormal.GetHeight();
+ wxBitmap bmp( w, h );
+ wxMemoryDC dc;
+
+ dc.SelectObject( bmp );
+ dc.SetPen( wxPen(*wxBLACK) );
+ dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
+ dc.DrawRectangle( 0, 0, w, h );
+ dc.DrawBitmap( m_bmpNormal, 0, 0, true );
+ dc.SelectObject( wxNullBitmap );
+ ControlButtonContentInfo info;
+ wxMacCreateBitmapButton( &info, bmp );
+ SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (m_toolbarItemRef != NULL)
+ {
+ ControlButtonContentInfo info2;
+ wxMacCreateBitmapButton( &info2, bmp, kControlContentCGImageRef);
+ HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
+ wxMacReleaseBitmapButton( &info2 );
+ }
+#endif
+ wxMacReleaseBitmapButton( &info );
+ }
+ else
+ {
+ ControlButtonContentInfo info;
+ wxMacCreateBitmapButton( &info, m_bmpNormal );
+ SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (m_toolbarItemRef != NULL)
+ {
+ ControlButtonContentInfo info2;
+ wxMacCreateBitmapButton( &info2, m_bmpNormal, kControlContentCGImageRef);
+ HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
+ wxMacReleaseBitmapButton( &info2 );
+ }
+#endif
+ wxMacReleaseBitmapButton( &info );
+ }
+
+ IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
+ SetControlData(
+ m_controlHandle, 0, kControlIconTransformTag,
+ sizeof(transform), (Ptr)&transform );
+ HIViewSetNeedsDisplay( m_controlHandle, true );
+
+}