diff --git a/modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm b/modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm index 9c51557415..cb85fb4b3e 100644 --- a/modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm +++ b/modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm @@ -988,10 +988,11 @@ public: addIvar ("au"); addIvar ("editor"); - addMethod (@selector (dealloc), dealloc, "v@:"); - addMethod (@selector (applicationWillTerminate:), applicationWillTerminate, "v@:@"); - addMethod (@selector (viewDidMoveToWindow), viewDidMoveToWindow, "v@:"); - addMethod (@selector (mouseDownCanMoveWindow), mouseDownCanMoveWindow, "c@:"); + addMethod (@selector (dealloc), dealloc, "v@:"); + addMethod (@selector (applicationWillTerminate:), applicationWillTerminate, "v@:@"); + addMethod (@selector (viewDidMoveToWindow), viewDidMoveToWindow, "v@:"); + addMethod (@selector (mouseDownCanMoveWindow), mouseDownCanMoveWindow, "c@:"); + registerClass(); } diff --git a/modules/juce_core/native/juce_osx_ObjCHelpers.h b/modules/juce_core/native/juce_osx_ObjCHelpers.h index e77f5a2ce9..449517e08f 100644 --- a/modules/juce_core/native/juce_osx_ObjCHelpers.h +++ b/modules/juce_core/native/juce_osx_ObjCHelpers.h @@ -116,8 +116,6 @@ struct ObjCClass jassert (b); (void) b; } - Class cls; - static id sendSuperclassMessage (id self, SEL selector) { objc_super s = { self, [SuperclassType class] }; @@ -132,6 +130,8 @@ struct ObjCClass return v; } + Class cls; + private: static String getRandomisedName (const char* root) { diff --git a/modules/juce_events/native/juce_mac_MessageManager.mm b/modules/juce_events/native/juce_mac_MessageManager.mm index 3cf25273cb..3b307d7bbc 100644 --- a/modules/juce_events/native/juce_mac_MessageManager.mm +++ b/modules/juce_events/native/juce_mac_MessageManager.mm @@ -30,41 +30,19 @@ typedef bool (*CheckEventBlockedByModalComps) (NSEvent*); CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr; //============================================================================== -struct AppDelegateClass : public ObjCClass +struct AppDelegate { - AppDelegateClass() : ObjCClass ("JUCEAppDelegate_") +public: + AppDelegate() { - addMethod (@selector (init), init, "@@:"); - addMethod (@selector (dealloc), dealloc, "v@:"); - addMethod (@selector (unregisterObservers), unregisterObservers, "v@:"); - addMethod (@selector (applicationShouldTerminate:), applicationShouldTerminate, "I@:@"); - addMethod (@selector (applicationWillTerminate:), applicationWillTerminate, "v@:@"); - addMethod (@selector (application:openFile:), application_openFile, "c@:@@"); - addMethod (@selector (application:openFiles:), application_openFiles, "v@:@@"); - addMethod (@selector (applicationDidBecomeActive:), applicationDidBecomeActive, "v@:@"); - addMethod (@selector (applicationDidResignActive:), applicationDidResignActive, "v@:@"); - addMethod (@selector (applicationWillUnhide:), applicationWillUnhide, "v@:@"); - addMethod (@selector (broadcastMessageCallback:), broadcastMessageCallback, "v@:@"); - addMethod (@selector (dummyMethod), dummyMethod, "v@:"); - - registerClass(); - } - - static NSString* getBroacastEventName() - { - return juceStringToNS ("juce_" + String::toHexString (File::getSpecialLocation (File::currentExecutableFile).hashCode64())); - } - -private: - static id init (id self, SEL) - { - self = sendSuperclassMessage (self, @selector (init)); + static AppDelegateClass cls; + delegate = [cls.createInstance() init]; if (JUCEApplicationBase::isStandaloneApp()) { - [NSApp setDelegate: self]; + [NSApp setDelegate: delegate]; - [[NSDistributedNotificationCenter defaultCenter] addObserver: self + [[NSDistributedNotificationCenter defaultCenter] addObserver: delegate selector: @selector (broadcastMessageCallback:) name: getBroacastEventName() object: nil]; @@ -73,106 +51,142 @@ private: { NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; - [center addObserver: self selector: @selector (applicationDidResignActive:) + [center addObserver: delegate selector: @selector (applicationDidResignActive:) name: NSApplicationDidResignActiveNotification object: NSApp]; - [center addObserver: self selector: @selector (applicationDidBecomeActive:) + [center addObserver: delegate selector: @selector (applicationDidBecomeActive:) name: NSApplicationDidBecomeActiveNotification object: NSApp]; - [center addObserver: self selector: @selector (applicationWillUnhide:) + [center addObserver: delegate selector: @selector (applicationWillUnhide:) name: NSApplicationWillUnhideNotification object: NSApp]; } - - return self; } - static void dealloc (id self, SEL) + ~AppDelegate() { - sendSuperclassMessage (self, @selector (dealloc)); - } - - static void unregisterObservers (id self, SEL) - { - [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: self]; - [[NSNotificationCenter defaultCenter] removeObserver: self]; + [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: delegate]; + [[NSNotificationCenter defaultCenter] removeObserver: delegate]; if (JUCEApplicationBase::isStandaloneApp()) { [NSApp setDelegate: nil]; - [[NSDistributedNotificationCenter defaultCenter] removeObserver: self + [[NSDistributedNotificationCenter defaultCenter] removeObserver: delegate name: getBroacastEventName() object: nil]; } + + [delegate release]; } - static NSApplicationTerminateReply applicationShouldTerminate (id /*self*/, SEL, NSApplication*) + static NSString* getBroacastEventName() { - if (JUCEApplicationBase::getInstance() != nullptr) - { - JUCEApplicationBase::getInstance()->systemRequestedQuit(); - - if (! MessageManager::getInstance()->hasStopMessageBeenSent()) - return NSTerminateCancel; - } - - return NSTerminateNow; + return juceStringToNS ("juce_" + String::toHexString (File::getSpecialLocation (File::currentExecutableFile).hashCode64())); } - static void applicationWillTerminate (id /*self*/, SEL, NSNotification*) - { - JUCEApplicationBase::appWillTerminateByForce(); - } - - static BOOL application_openFile (id /*self*/, SEL, NSApplication*, NSString* filename) - { - if (JUCEApplicationBase::getInstance() != nullptr) - { - JUCEApplicationBase::getInstance()->anotherInstanceStarted (quotedIfContainsSpaces (filename)); - return YES; - } - - return NO; - } - - static void application_openFiles (id /*self*/, SEL, NSApplication*, NSArray* filenames) - { - StringArray files; - for (unsigned int i = 0; i < [filenames count]; ++i) - files.add (quotedIfContainsSpaces ((NSString*) [filenames objectAtIndex: i])); - - if (files.size() > 0 && JUCEApplicationBase::getInstance() != nullptr) - JUCEApplicationBase::getInstance()->anotherInstanceStarted (files.joinIntoString (" ")); - } - - static void applicationDidBecomeActive (id /*self*/, SEL, NSNotification*) { focusChanged(); } - static void applicationDidResignActive (id /*self*/, SEL, NSNotification*) { focusChanged(); } - static void applicationWillUnhide (id /*self*/, SEL, NSNotification*) { focusChanged(); } - - static void broadcastMessageCallback (id /*self*/, SEL, NSNotification* n) - { - NSDictionary* dict = (NSDictionary*) [n userInfo]; - const String messageString (nsStringToJuce ((NSString*) [dict valueForKey: nsStringLiteral ("message")])); - MessageManager::getInstance()->deliverBroadcastMessage (messageString); - } - - static void dummyMethod (id /*self*/, SEL) {} // (used as a way of running a dummy thread) + MessageQueue messageQueue; private: - static void focusChanged() - { - if (appFocusChangeCallback != nullptr) - (*appFocusChangeCallback)(); - } + id delegate; + CFRunLoopRef runLoop; + CFRunLoopSourceRef runLoopSource; - static String quotedIfContainsSpaces (NSString* file) + //============================================================================== + struct AppDelegateClass : public ObjCClass { - String s (nsStringToJuce (file)); - if (s.containsChar (' ')) - s = s.quoted ('"'); + AppDelegateClass() : ObjCClass ("JUCEAppDelegate_") + { + addMethod (@selector (applicationShouldTerminate:), applicationShouldTerminate, "I@:@"); + addMethod (@selector (applicationWillTerminate:), applicationWillTerminate, "v@:@"); + addMethod (@selector (application:openFile:), application_openFile, "c@:@@"); + addMethod (@selector (application:openFiles:), application_openFiles, "v@:@@"); + addMethod (@selector (applicationDidBecomeActive:), applicationDidBecomeActive, "v@:@"); + addMethod (@selector (applicationDidResignActive:), applicationDidResignActive, "v@:@"); + addMethod (@selector (applicationWillUnhide:), applicationWillUnhide, "v@:@"); + addMethod (@selector (broadcastMessageCallback:), broadcastMessageCallback, "v@:@"); + addMethod (@selector (dummyMethod), dummyMethod, "v@:"); - return s; - } + registerClass(); + } + + private: + static NSApplicationTerminateReply applicationShouldTerminate (id /*self*/, SEL, NSApplication*) + { + JUCEApplicationBase* const app = JUCEApplicationBase::getInstance(); + + if (app != nullptr) + { + app->systemRequestedQuit(); + + if (! MessageManager::getInstance()->hasStopMessageBeenSent()) + return NSTerminateCancel; + } + + return NSTerminateNow; + } + + static void applicationWillTerminate (id /*self*/, SEL, NSNotification*) + { + JUCEApplicationBase::appWillTerminateByForce(); + } + + static BOOL application_openFile (id /*self*/, SEL, NSApplication*, NSString* filename) + { + JUCEApplicationBase* const app = JUCEApplicationBase::getInstance(); + + if (app != nullptr) + { + app->anotherInstanceStarted (quotedIfContainsSpaces (filename)); + return YES; + } + + return NO; + } + + static void application_openFiles (id /*self*/, SEL, NSApplication*, NSArray* filenames) + { + JUCEApplicationBase* const app = JUCEApplicationBase::getInstance(); + + if (app != nullptr) + { + StringArray files; + for (unsigned int i = 0; i < [filenames count]; ++i) + files.add (quotedIfContainsSpaces ((NSString*) [filenames objectAtIndex: i])); + + if (files.size() > 0) + app->anotherInstanceStarted (files.joinIntoString (" ")); + } + } + + static void applicationDidBecomeActive (id /*self*/, SEL, NSNotification*) { focusChanged(); } + static void applicationDidResignActive (id /*self*/, SEL, NSNotification*) { focusChanged(); } + static void applicationWillUnhide (id /*self*/, SEL, NSNotification*) { focusChanged(); } + + static void broadcastMessageCallback (id /*self*/, SEL, NSNotification* n) + { + NSDictionary* dict = (NSDictionary*) [n userInfo]; + const String messageString (nsStringToJuce ((NSString*) [dict valueForKey: nsStringLiteral ("message")])); + MessageManager::getInstance()->deliverBroadcastMessage (messageString); + } + + static void dummyMethod (id /*self*/, SEL) {} // (used as a way of running a dummy thread) + + private: + static void focusChanged() + { + if (appFocusChangeCallback != nullptr) + (*appFocusChangeCallback)(); + } + + static String quotedIfContainsSpaces (NSString* file) + { + String s (nsStringToJuce (file)); + if (s.containsChar (' ')) + s = s.quoted ('"'); + + return s; + } + }; }; //============================================================================== @@ -251,34 +265,12 @@ void initialiseNSApplication() [NSApplication sharedApplication]; } -//============================================================================== -struct AppDelegateHolder -{ -public: - AppDelegateHolder() - { - static AppDelegateClass cls; - delegate = [cls.createInstance() init]; - } - - ~AppDelegateHolder() - { - [delegate performSelector: @selector (unregisterObservers)]; - [delegate release]; - } - - id delegate; - CFRunLoopRef runLoop; - CFRunLoopSourceRef runLoopSource; - MessageQueue messageQueue; -}; - -static AppDelegateHolder* appDelegate = nullptr; +static AppDelegate* appDelegate = nullptr; void MessageManager::doPlatformSpecificInitialisation() { if (appDelegate == nil) - appDelegate = new AppDelegateHolder(); + appDelegate = new AppDelegate(); #if ! (defined (MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) // This launches a dummy thread, which forces Cocoa to initialise NSThreads correctly (needed prior to 10.5) @@ -307,7 +299,7 @@ void MessageManager::broadcastMessage (const String& message) NSDictionary* info = [NSDictionary dictionaryWithObject: juceStringToNS (message) forKey: nsStringLiteral ("message")]; - [[NSDistributedNotificationCenter defaultCenter] postNotificationName: AppDelegateClass::getBroacastEventName() + [[NSDistributedNotificationCenter defaultCenter] postNotificationName: AppDelegate::getBroacastEventName() object: nil userInfo: info]; } diff --git a/modules/juce_gui_basics/native/juce_mac_FileChooser.mm b/modules/juce_gui_basics/native/juce_mac_FileChooser.mm index d7bae0982b..a2bac84a27 100644 --- a/modules/juce_gui_basics/native/juce_mac_FileChooser.mm +++ b/modules/juce_gui_basics/native/juce_mac_FileChooser.mm @@ -31,7 +31,6 @@ struct FileChooserDelegateClass : public ObjCClass { addIvar ("filters"); - addMethod (@selector (initWithFilters:), initWithFilters, "@@:^v"); addMethod (@selector (dealloc), dealloc, "v@:"); addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@"); @@ -42,14 +41,12 @@ struct FileChooserDelegateClass : public ObjCClass registerClass(); } -private: - static id initWithFilters (id self, SEL, StringArray* filters) + static void setFilters (id self, StringArray* filters) { - self = sendSuperclassMessage (self, @selector (init)); object_setInstanceVariable (self, "filters", filters); - return self; } +private: static void dealloc (id self, SEL) { delete getIvar (self, "filters"); @@ -157,8 +154,8 @@ void FileChooser::showPlatformDialog (Array& results, #endif static FileChooserDelegateClass cls; - DelegateType* delegate = [[cls.createInstance() performSelector: @selector (initWithFilters:) - withObject: (id) filters] autorelease]; + DelegateType* delegate = (DelegateType*) [[cls.createInstance() init] autorelease]; + FileChooserDelegateClass::setFilters (delegate, filters); NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel] : [NSOpenPanel openPanel]; diff --git a/modules/juce_gui_basics/native/juce_mac_MainMenu.mm b/modules/juce_gui_basics/native/juce_mac_MainMenu.mm index cc8403a170..8824d817cf 100644 --- a/modules/juce_gui_basics/native/juce_mac_MainMenu.mm +++ b/modules/juce_gui_basics/native/juce_mac_MainMenu.mm @@ -32,8 +32,8 @@ public: lastUpdateTime (0) { static JuceMenuCallbackClass cls; - callback = [cls.createInstance() performSelector: @selector (initWithOwner:) - withObject: (id) this]; + callback = [cls.createInstance() init]; + JuceMenuCallbackClass::setOwner (callback, this); } ~JuceMainMenuHandler() @@ -399,7 +399,6 @@ private: { addIvar ("owner"); - addMethod (@selector (initWithOwner:), initWithOwner, "@@:^v"); addMethod (@selector (menuItemInvoked:), menuItemInvoked, "v@:@"); addMethod (@selector (menuNeedsUpdate:), menuNeedsUpdate, "v@:@"); @@ -410,14 +409,12 @@ private: registerClass(); } - private: - static id initWithOwner (id self, SEL, JuceMainMenuHandler* owner) + static void setOwner (id self, JuceMainMenuHandler* owner) { - self = sendSuperclassMessage (self, @selector (init)); object_setInstanceVariable (self, "owner", owner); - return self; } + private: static void menuItemInvoked (id self, SEL, id menu) { JuceMainMenuHandler* const owner = getIvar (self, "owner"); diff --git a/modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm b/modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm index a6f38681b7..98ee1ae327 100644 --- a/modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm +++ b/modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm @@ -38,17 +38,10 @@ struct DownloadClickDetectorClass : public ObjCClass registerClass(); } - static void setOwner (id self, WebBrowserComponent* owner) - { - object_setInstanceVariable (self, "owner", owner); - } + static void setOwner (id self, WebBrowserComponent* owner) { object_setInstanceVariable (self, "owner", owner); } + static WebBrowserComponent* getOwner (id self) { return getIvar (self, "owner"); } private: - static WebBrowserComponent* getOwner (id self) - { - return getIvar (self, "owner"); - } - static void decidePolicyForNavigationAction (id self, SEL, WebView*, NSDictionary* actionInformation, NSURLRequest*, WebFrame*, id listener) { diff --git a/modules/juce_video/native/juce_mac_CameraDevice.mm b/modules/juce_video/native/juce_mac_CameraDevice.mm index e061f5e98e..4f60f8c7ff 100644 --- a/modules/juce_video/native/juce_mac_CameraDevice.mm +++ b/modules/juce_video/native/juce_mac_CameraDevice.mm @@ -224,17 +224,10 @@ private: registerClass(); } - static void setOwner (id self, QTCameraDeviceInternal* owner) - { - object_setInstanceVariable (self, "owner", owner); - } + static void setOwner (id self, QTCameraDeviceInternal* owner) { object_setInstanceVariable (self, "owner", owner); } + static QTCameraDeviceInternal* getOwner (id self) { return getIvar (self, "owner"); } private: - static QTCameraDeviceInternal* getOwner (id self) - { - return getIvar (self, "owner"); - } - static void didOutputVideoFrame (id self, SEL, QTCaptureOutput* captureOutput, CVImageBufferRef videoFrame, QTSampleBuffer* sampleBuffer, QTCaptureConnection* connection)