Mixing Carbon events & Cocoa
Mixing Carbon events & Cocoa
- Subject: Mixing Carbon events & Cocoa
- From: Benoit Widemann <email@hidden>
- Date: Mon, 24 Jun 2002 14:37:20 +0200
I have been asking around about how to get Carbon events from a Cocoa
application, with no luck.
However, with a big help from Christine Buttin, we have found a
simple solution that may be of interest for other developers, so here
it is:
In a Cocoa app, main() is reduced to a single line:
int main(int argc, const char *argv[])
{
return NSApplicationMain(argc, argv);
}
NSApplicationMain() is equivalent to this:
int NSApplicationMain(int argc, char *argv[]) {
[NSApplication sharedApplication]; // create app instance
[NSBundle loadNibNamed:@"main" owner:app]; // load main nib
[NSApp run]; // run main event loop
}
It seems impossible (yet) to grab Carbon events in a Cocoa app. So we
reversed the problem. We started from a *Carbon* nib-based app, and
we replaced the main.c with a main.m, where Cocoa is initialized the
same way as above. Error checking removed for readability.
int main(int argc, char* argv[])
{
[NSApplication sharedApplication]; // create app instance
LoadMainNibTheCarbonWay(); // load main nib
SetupCarbonThings(); // event handlers, etc.
aFoo = [foo createFooWithOption: bar]; // create a Cocoa object
RunApplicationEventLoop(); // run main event loop (Carbon)
}
void LoadMainNibTheCarbonWay()
{
IBNibRef nibRef;
WindowRef window;
CreateNibReference(CFSTR("main"), &nibRef);
SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
DisposeNibReference(nibRef);
ShowWindow(window);
}
The biggest problem is that Carbon-style nib is rudimentary, compared
with Cocoa usual IB tricks. Still, this has been a saver for us
because there are things that are not available here but only there
and that we couldn't do without. We can now use both Carbon events
and Cocoa objects.
It's easy to verify (using timers, for example) that callbacks work
in both worlds, which opens architectural fun that would be
impossible with an external bundle.
One question remains: should we allocate an autorelease pool
manually, or is the call to [NSApplication sharedApplication] enough?
We are still in the dark here and we'd rather play it safe...
Benoit Widemann
---------------------------------------------
Home page <
http://www.widemann.net>
---------------------------------------------
#
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.