View Full Version : Overriding NSApplication-Run method.

Apr 15, 2008, 12:30 AM
I'm working on the Mac port of Unicon 2D Graphics (http://caseycode.com/development/macportunicon/abstract.html). Basically I am trying to make Cocoa 2d graphics code for Unicon Programming Language (unicon.org/). I spent much time on finding how to draw windows and lines without xCode. However, I cannot find how to override NSApplication-Run method. In order to implement the event handling for Unicon language, I need to override the Run method. Otherwise I need to use complicated multiple threads.
I found an old version of NSApplication-run method here (http://svn.gna.org/viewcvs/gnustep/libs/gui/trunk/Source/NSApplication.m?rev=26307&view=markup).

The reference article could be GUI Programming without XCode (http://forums.macrumors.com/showthread.php?t=93013&highlight=NSApplication+Run) in this forum.

Thanks for reading this article. :)

Apr 15, 2008, 07:52 AM
I'd say the best way to do this is to setup your main() like this (from Apple's documentation):

void NSApplicationMain(int argc, char *argv[]) {
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"myMain" owner:NSApp];
[NSApp run];

But instead of calling NSApplication, call your subclass. So it might look like this:

void NSApplicationMain(int argc, char *argv[]) {
MyApp *app = [MyApp sharedApplication];
[NSBundle loadNibNamed:@"myMain" owner:app];
[app run];

I haven't tried this though, so you'd have to experiment.

Edit: here's what Apple says on subclassing it:

To use a custom subclass of NSApplication, simply send sharedApplication to your subclass rather than directly to NSApplication. If you create your application in Xcode, you can accomplish this by setting your custom application class to be the principal class. In Xcode, double-click the application target in the Groups and Files list to open the Info window for the target. Then display the Properties pane of the window and replace “NSApplication” in the Principal Class field with the name of your custom class. The NSApplicationMain function sends sharedApplication to the principal class to obtain the global application instance (NSApp)—which in this case will be an instance of your custom subclass of NSApplication.

Apr 15, 2008, 12:26 PM
Hi kainjow, Thanks for replying this topic.
I know how to use the subclass of NSApplication class, but the problem is, "what kind of code should I put in the custom run method."
So, I guess I need to know the actual code of NSApplication run method or at least the structure of run method.

For example, I this is the example of overriding sendEvent method. However, run method is more complicated than sendEvent method.
- (void)sendEvent:(NSEvent *)theEvent
/* my own procedures */
[super sendEvent:theEvent];

Apr 15, 2008, 05:25 PM
Ah I see. What exactly are you trying to do that requires overriding the run method? What kind of events are you trying to handle?

Apr 16, 2008, 11:09 AM
Well, actually the reason that I want to override NSApp run() is not for event handling. I need to connect two methods: NSApp in Cocoa and VM interp() in Unicon Language. There are two infinite loops: VM interp() and NSApp run. I can make either the NSApp run() call VM interp(), or the VM interp() call NSApp often. However, it is very complicated to change VM interp() code to call NSApp. So, what I want to do is to use the original run method and add some code to call VM interp() function. I really want to get the source code of NSApp run().
Otherwise I have to develop my own code, using two threads. Does it make sense?

Apr 16, 2008, 11:14 PM
You'll want to investigate NSRunLoop rather than NSApplication, most likely.