Tuesday, January 9, 2007

IsItCarbonWindow or IsItCocoaWindow?!

I've found the following implementation:


@class NSCarbonWindow;

bool IsItACocoaWindow (WindowRef window)
{
NSWindow* cocoaWindow = [[NSWindow alloc] initWithWindowRef:window];
BOOL carbon = [[cocoaWindow class]isSubclassOfClass:[NSCarbonWindow class]];
[cocoaWindow release];
return !carbon;
}


bool IsItACarbonWindow (WindowRef window)
{
/* This one is a pure carbon function but it's a bit fragile, as for example in Leopard it actually returns: com.apple.hitoolbox.window.viewadapter */
CFStringRef classid = HIObjectCopyClassID ((HIObjectRef) window);
bool cocoa = (CFStringCompare (CFSTR ("com.apple.hitoolbox.window.external"), classid, 0) == kCFCompareEqualTo) ||
(CFStringCompare (CFSTR ("com.apple.hitoolbox.window. viewadapter"), classid, 0) == kCFCompareEqualTo);
CFRelease (classid);
return !cocoa;
}



It's also interesting to note that this can work because in case of the CocoaWindow the initWithWindowRef method will return the original window object for Cocoa objects. That is:


window == [[NSWindow alloc] initWithWindowRef: [window windowRef]];