Overriding NSApplication-Run method.

Discussion in 'Mac Programming' started by nauten, Apr 14, 2008.

  1. macrumors newbie

    nauten

    Joined:
    Mar 3, 2008
    Location:
    Moscow, ID
    #1
    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. :)
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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:

     
  3. thread starter macrumors newbie

    nauten

    Joined:
    Mar 3, 2008
    Location:
    Moscow, ID
    #3
    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];
    }
    
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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?
     
  5. thread starter macrumors newbie

    nauten

    Joined:
    Mar 3, 2008
    Location:
    Moscow, ID
    #5
    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?
     
  6. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
    You'll want to investigate NSRunLoop rather than NSApplication, most likely.
     

Share This Page