Browse Source

Minor refactoring of obj-C internals.

tags/2021-05-28
jules 13 years ago
parent
commit
dab9295611
7 changed files with 129 additions and 156 deletions
  1. +5
    -4
      modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm
  2. +2
    -2
      modules/juce_core/native/juce_osx_ObjCHelpers.h
  3. +110
    -118
      modules/juce_events/native/juce_mac_MessageManager.mm
  4. +4
    -7
      modules/juce_gui_basics/native/juce_mac_FileChooser.mm
  5. +4
    -7
      modules/juce_gui_basics/native/juce_mac_MainMenu.mm
  6. +2
    -9
      modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm
  7. +2
    -9
      modules/juce_video/native/juce_mac_CameraDevice.mm

+ 5
- 4
modules/juce_audio_plugin_client/AU/juce_AU_Wrapper.mm View File

@@ -988,10 +988,11 @@ public:
addIvar<JuceAU*> ("au");
addIvar<EditorCompHolder*> ("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();
}


+ 2
- 2
modules/juce_core/native/juce_osx_ObjCHelpers.h View File

@@ -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)
{


+ 110
- 118
modules/juce_events/native/juce_mac_MessageManager.mm View File

@@ -30,41 +30,19 @@ typedef bool (*CheckEventBlockedByModalComps) (NSEvent*);
CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
//==============================================================================
struct AppDelegateClass : public ObjCClass <NSObject>
struct AppDelegate
{
AppDelegateClass() : ObjCClass ("JUCEAppDelegate_")
{
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)
public:
AppDelegate()
{
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)
{
sendSuperclassMessage (self, @selector (dealloc));
}
static void unregisterObservers (id self, SEL)
~AppDelegate()
{
[[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();
return juceStringToNS ("juce_" + String::toHexString (File::getSpecialLocation (File::currentExecutableFile).hashCode64()));
}
if (! MessageManager::getInstance()->hasStopMessageBeenSent())
return NSTerminateCancel;
}
MessageQueue messageQueue;
return NSTerminateNow;
}
private:
id delegate;
CFRunLoopRef runLoop;
CFRunLoopSourceRef runLoopSource;
static void applicationWillTerminate (id /*self*/, SEL, NSNotification*)
//==============================================================================
struct AppDelegateClass : public ObjCClass <NSObject>
{
JUCEApplicationBase::appWillTerminateByForce();
}
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@:");
registerClass();
}
static BOOL application_openFile (id /*self*/, SEL, NSApplication*, NSString* filename)
{
if (JUCEApplicationBase::getInstance() != nullptr)
private:
static NSApplicationTerminateReply applicationShouldTerminate (id /*self*/, SEL, NSApplication*)
{
JUCEApplicationBase::getInstance()->anotherInstanceStarted (quotedIfContainsSpaces (filename));
return YES;
JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
if (app != nullptr)
{
app->systemRequestedQuit();
if (! MessageManager::getInstance()->hasStopMessageBeenSent())
return NSTerminateCancel;
}
return NSTerminateNow;
}
return NO;
}
static void applicationWillTerminate (id /*self*/, SEL, NSNotification*)
{
JUCEApplicationBase::appWillTerminateByForce();
}
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]));
static BOOL application_openFile (id /*self*/, SEL, NSApplication*, NSString* filename)
{
JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
if (files.size() > 0 && JUCEApplicationBase::getInstance() != nullptr)
JUCEApplicationBase::getInstance()->anotherInstanceStarted (files.joinIntoString (" "));
}
if (app != nullptr)
{
app->anotherInstanceStarted (quotedIfContainsSpaces (filename));
return YES;
}
static void applicationDidBecomeActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
static void applicationDidResignActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
static void applicationWillUnhide (id /*self*/, SEL, NSNotification*) { focusChanged(); }
return NO;
}
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 application_openFiles (id /*self*/, SEL, NSApplication*, NSArray* filenames)
{
JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
static void dummyMethod (id /*self*/, SEL) {} // (used as a way of running a dummy thread)
if (app != nullptr)
{
StringArray files;
for (unsigned int i = 0; i < [filenames count]; ++i)
files.add (quotedIfContainsSpaces ((NSString*) [filenames objectAtIndex: i]));
private:
static void focusChanged()
{
if (appFocusChangeCallback != nullptr)
(*appFocusChangeCallback)();
}
if (files.size() > 0)
app->anotherInstanceStarted (files.joinIntoString (" "));
}
}
static String quotedIfContainsSpaces (NSString* file)
{
String s (nsStringToJuce (file));
if (s.containsChar (' '))
s = s.quoted ('"');
static void applicationDidBecomeActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
static void applicationDidResignActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
static void applicationWillUnhide (id /*self*/, SEL, NSNotification*) { focusChanged(); }
return s;
}
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];
}

+ 4
- 7
modules/juce_gui_basics/native/juce_mac_FileChooser.mm View File

@@ -31,7 +31,6 @@ struct FileChooserDelegateClass : public ObjCClass <NSObject>
{
addIvar<StringArray*> ("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 <NSObject>
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<StringArray*> (self, "filters");
@@ -157,8 +154,8 @@ void FileChooser::showPlatformDialog (Array<File>& 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];


+ 4
- 7
modules/juce_gui_basics/native/juce_mac_MainMenu.mm View File

@@ -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<JuceMainMenuHandler*> ("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<JuceMainMenuHandler*> (self, "owner");


+ 2
- 9
modules/juce_gui_extra/native/juce_mac_WebBrowserComponent.mm View File

@@ -38,17 +38,10 @@ struct DownloadClickDetectorClass : public ObjCClass <NSObject>
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<WebBrowserComponent*> (self, "owner"); }
private:
static WebBrowserComponent* getOwner (id self)
{
return getIvar<WebBrowserComponent*> (self, "owner");
}
static void decidePolicyForNavigationAction (id self, SEL, WebView*, NSDictionary* actionInformation,
NSURLRequest*, WebFrame*, id <WebPolicyDecisionListener> listener)
{


+ 2
- 9
modules/juce_video/native/juce_mac_CameraDevice.mm View File

@@ -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<QTCameraDeviceInternal*> (self, "owner"); }
private:
static QTCameraDeviceInternal* getOwner (id self)
{
return getIvar<QTCameraDeviceInternal*> (self, "owner");
}
static void didOutputVideoFrame (id self, SEL, QTCaptureOutput* captureOutput,
CVImageBufferRef videoFrame, QTSampleBuffer* sampleBuffer,
QTCaptureConnection* connection)


Loading…
Cancel
Save