+}
+
+#pragma mark -
+#pragma mark Toolbar Implementation
+
+wxToolBarToolBase *wxToolBar::CreateTool(
+ int id,
+ const wxString& label,
+ const wxBitmap& bmpNormal,
+ const wxBitmap& bmpDisabled,
+ wxItemKind kind,
+ wxObject *clientData,
+ const wxString& shortHelp,
+ const wxString& longHelp )
+{
+ return new wxToolBarTool(
+ this, id, label, bmpNormal, bmpDisabled, kind,
+ clientData, shortHelp, longHelp );
+}
+
+wxToolBarToolBase *
+wxToolBar::CreateTool(wxControl *control, const wxString& label)
+{
+ return new wxToolBarTool(this, control, label);
+}
+
+void wxToolBar::Init()
+{
+ m_maxWidth = -1;
+ m_maxHeight = -1;
+ m_defaultWidth = kwxMacToolBarToolDefaultWidth;
+ m_defaultHeight = kwxMacToolBarToolDefaultHeight;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ m_macHIToolbarRef = NULL;
+ m_macUsesNativeToolbar = false;
+#endif
+}
+
+#define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
+
+const EventTypeSpec kEvents[] =
+{
+ { kEventClassHIObject, kEventHIObjectConstruct },
+ { kEventClassHIObject, kEventHIObjectInitialize },
+ { kEventClassHIObject, kEventHIObjectDestruct },
+
+ { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
+};
+
+const EventTypeSpec kViewEvents[] =
+{
+ { kEventClassControl, kEventControlGetSizeConstraints }
+};
+
+struct ControlToolbarItem
+{
+ HIToolbarItemRef toolbarItem;
+ HIViewRef viewRef;
+ wxSize lastValidSize ;
+};
+
+static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
+{
+ OSStatus result = eventNotHandledErr;
+ ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
+
+ switch ( GetEventClass( inEvent ) )
+ {
+ case kEventClassHIObject:
+ switch ( GetEventKind( inEvent ) )
+ {
+ case kEventHIObjectConstruct:
+ {
+ HIObjectRef toolbarItem;
+ ControlToolbarItem* item;
+
+ GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
+ sizeof( HIObjectRef ), NULL, &toolbarItem );
+
+ item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
+ item->toolbarItem = toolbarItem ;
+ item->lastValidSize = wxSize(-1,-1);
+ item->viewRef = NULL ;
+
+ SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
+
+ result = noErr ;
+ }
+ break;
+
+ case kEventHIObjectInitialize:
+ result = CallNextEventHandler( inCallRef, inEvent );
+ if ( result == noErr )
+ {
+ CFDataRef data;
+ GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
+ sizeof( CFTypeRef ), NULL, &data );
+
+ HIViewRef viewRef ;
+
+ wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
+ memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
+
+ object->viewRef = (HIViewRef) viewRef ;
+ // make sure we keep that control during our lifetime
+ CFRetain( object->viewRef ) ;
+
+ verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
+ GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL ));
+ result = noErr ;
+ }
+ break;
+
+ case kEventHIObjectDestruct:
+ {
+ HIViewRef viewRef = object->viewRef ;
+ if( viewRef && IsValidControlHandle( viewRef) )
+ {
+ // depending whether the wxControl corresponding to this HIView has already been destroyed or
+ // not, ref counts differ, so we cannot assert a special value
+ CFIndex count = CFGetRetainCount( viewRef ) ;
+ wxASSERT_MSG( count >=1 , wxT("Reference Count of native tool was illegal before removal") );
+ if ( count >= 1 )
+ CFRelease( viewRef ) ;
+ }
+ free( object ) ;
+ result = noErr;
+ }
+ break;
+ }
+ break;
+
+ case kEventClassToolbarItem:
+ switch ( GetEventKind( inEvent ) )
+ {
+ case kEventToolbarItemCreateCustomView:
+ {
+ HIViewRef viewRef = object->viewRef ;
+ HIViewRemoveFromSuperview( viewRef ) ;
+ HIViewSetVisible(viewRef, true) ;
+ CFRetain( viewRef ) ;
+ result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
+ }
+ break;
+ }
+ break;
+
+ case kEventClassControl:
+ switch ( GetEventKind( inEvent ) )
+ {
+ case kEventControlGetSizeConstraints:
+ {
+ wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ;
+ if ( wxwindow )
+ {
+ // during toolbar layout the native window sometimes gets negative sizes,
+ // sometimes it just gets shrunk behind our back, so in order to avoid
+ // ever shrinking more, once a valid size is captured, we keep it
+
+ wxSize sz = object->lastValidSize;
+ if ( sz.x <= 0 || sz.y <= 0 )
+ {
+ sz = wxwindow->GetSize() ;
+ sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
+ sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
+ if ( sz.x > 0 && sz.y > 0 )
+ object->lastValidSize = sz ;
+ else
+ sz = wxSize(0,0) ;
+ }
+
+ // Extra width to avoid edge of combobox being cut off
+ sz.x += 3;
+
+ HISize min, max;
+ min.width = max.width = sz.x ;
+ min.height = max.height = sz.y ;
+
+ result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
+ sizeof( HISize ), &min );
+
+ result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
+ sizeof( HISize ), &max );
+ result = noErr ;
+ }
+ }
+ break;
+ }
+ break;
+ }
+
+ return result;
+}
+
+void RegisterControlToolbarItemClass()
+{
+ static bool sRegistered;
+
+ if ( !sRegistered )
+ {
+ HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
+ ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
+
+ sRegistered = true;
+ }
+}
+
+HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
+{
+ RegisterControlToolbarItemClass();
+
+ OSStatus err;
+ EventRef event;
+ UInt32 options = kHIToolbarItemAllowDuplicates;
+ HIToolbarItemRef result = NULL;
+
+ err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
+ require_noerr( err, CantCreateEvent );
+
+ SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
+ SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
+
+ if ( inConfigData )
+ SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
+
+ err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
+ check_noerr( err );
+
+ ReleaseEvent( event );
+CantCreateEvent :
+ return result ;
+}
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+static const EventTypeSpec kToolbarEvents[] =
+{
+ { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
+ { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
+ { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
+};
+
+static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
+{
+ OSStatus result = eventNotHandledErr;
+ // Not yet needed
+ // wxToolBar* toolbar = (wxToolBar*) inUserData ;
+ CFMutableArrayRef array;
+
+ switch ( GetEventKind( inEvent ) )
+ {
+ case kEventToolbarGetDefaultIdentifiers:
+ {
+ GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
+ sizeof( CFMutableArrayRef ), NULL, &array );
+ // not implemented yet
+ // GetToolbarDefaultItems( array );
+ result = noErr;
+ }
+ break;
+
+ case kEventToolbarGetAllowedIdentifiers:
+ {
+ GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
+ sizeof( CFMutableArrayRef ), NULL, &array );
+ // not implemented yet
+ // GetToolbarAllowedItems( array );
+ result = noErr;
+ }
+ break;
+ case kEventToolbarCreateItemWithIdentifier:
+ {
+ HIToolbarItemRef item = NULL;
+ CFTypeRef data = NULL;
+ CFStringRef identifier = NULL ;
+
+ GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
+ sizeof( CFStringRef ), NULL, &identifier );
+
+ GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
+ sizeof( CFTypeRef ), NULL, &data );
+
+ if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
+ {
+ item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
+ if ( item )
+ {
+ SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
+ sizeof( HIToolbarItemRef ), &item );
+ result = noErr;
+ }
+ }
+
+ }
+ break;
+ }
+ return result ;
+}
+#endif // wxMAC_USE_NATIVE_TOOLBAR
+
+// also for the toolbar we have the dual implementation:
+// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
+
+bool wxToolBar::Create(
+ wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name )
+{
+ if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
+ return false;
+
+ FixupStyle();
+
+ OSStatus err = noErr;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
+ {
+ wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
+ err = HIToolbarCreate(
+ wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
+ (HIToolbarRef*) &m_macHIToolbarRef );
+
+ if (m_macHIToolbarRef != NULL)
+ {
+ InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
+ GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
+
+ HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
+ HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
+
+ if ( style & wxTB_NOICONS )
+ mode = kHIToolbarDisplayModeLabelOnly;
+ else if ( style & wxTB_TEXT )
+ mode = kHIToolbarDisplayModeIconAndLabel;
+ else
+ mode = kHIToolbarDisplayModeIconOnly;
+
+ HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
+ HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
+ }
+ }
+#endif // wxMAC_USE_NATIVE_TOOLBAR
+
+ return (err == noErr);
+}
+
+wxToolBar::~wxToolBar()
+{
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (m_macHIToolbarRef != NULL)
+ {
+ // if this is the installed toolbar, then deinstall it
+ if (m_macUsesNativeToolbar)
+ MacInstallNativeToolbar( false );
+
+ CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ;
+ wxASSERT_MSG( count == 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
+
+ CFRelease( (HIToolbarRef)m_macHIToolbarRef );
+ m_macHIToolbarRef = NULL;
+ }
+#endif
+}
+
+bool wxToolBar::Show( bool show )
+{
+ WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
+ bool bResult = (tlw != NULL);
+
+ if (bResult)
+ {
+#if wxMAC_USE_NATIVE_TOOLBAR
+ bool ownToolbarInstalled = false;
+ MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
+ if (ownToolbarInstalled)
+ {
+ bResult = (IsWindowToolbarVisible( tlw ) != show);
+ if ( bResult )
+ ShowHideWindowToolbar( tlw, show, false );
+ }
+ else
+ bResult = wxToolBarBase::Show( show );
+#else
+
+ bResult = wxToolBarBase::Show( show );
+#endif
+ }
+
+ return bResult;
+}
+
+bool wxToolBar::IsShown() const
+{
+ bool bResult;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ bool ownToolbarInstalled;
+
+ MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
+ if (ownToolbarInstalled)
+ {
+ WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
+ bResult = IsWindowToolbarVisible( tlw );