How to implement NSApplicationDelegate protocol in glfw application

2k Views Asked by At

I am currently programming an application using glfw in MacOS X. My only problem is that the application doesn't use an AppDelegate, but does all the initialization in the main.cpp file like a command line utility. I specifically want to implement the function

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

and I already registered the file extensions in the plist file and when I try to open them I get an error message saying "The document "Doc.xxx" could not be opened. MyApp cannot open files in the "xxx" format.

I attempted to create a singleton that sets itself as the delegate but it just doesn't call the function. Can anyone comment on the possibility of creating a class and implementing the NSApplicationDelegate functions?

1

There are 1 best solutions below

3
On BEST ANSWER

Basically there is nothing in your code to receive events from the OS and to pump the events received.

The solution, according to another stack overflow Q/A, appears to be to invoke NSApplication manually. You still need to decide where to hook your OpenGL render loop into as [NSApp run] doesn't return until the app finishes. Another solution could be to make a custom NSApplication replacement. http://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html

Copied pretty much verbatim from this answer taking into account the modifications for ARC: https://stackoverflow.com/a/2154666/1874323

@autoreleasepool { 
    AppDelegate * delegate = [[AppDelegate alloc] init];

    NSApplication * application = [NSApplication sharedApplication];
    [application setDelegate:delegate];
    [NSApp run]; // Note this never returns until the app finishes, so you need to decide where to hook your code into
}

As always you can find documentation from the Apple website: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/Reference/Reference.html