Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nauten

macrumors newbie
Original poster
Mar 3, 2008
3
0
Moscow, ID
I'm working on the Mac port of Unicon 2D Graphics. Basically I am trying to make Cocoa 2d graphics code for Unicon Programming Language. 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.

The reference article could be GUI Programming without XCode in this forum.

Thanks for reading this article. :)
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I'd say the best way to do this is to setup your main() like this (from Apple's documentation):

Code:
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:

Code:
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.
 

nauten

macrumors newbie
Original poster
Mar 3, 2008
3
0
Moscow, ID
respond to kainjow's message

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.
Code:
- (void)sendEvent:(NSEvent *)theEvent
{
    /* my own procedures */
    [super sendEvent:theEvent];
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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?
 

nauten

macrumors newbie
Original poster
Mar 3, 2008
3
0
Moscow, ID
reason why I need to override NSApp run()

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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.