views:

70

answers:

1

Hello,

I'm trying to create a Cocoa Window within an otherwise Carbon Application (it's an OpenGL API that uses AGL. Can't change it so don't comment on that).

Here's a code snippit:

WindowRef winref = static_cast<eq::AGLWindow*>(getOSWindow())->getCarbonWindow();
vc = [[SFAttachedViewController alloc] initWithConfig:config]; //loads from view nib
NSPoint buttonPoint = NSMakePoint(event.pointerButtonPress.x + [cocoaWrap frame].origin.x, [cocoaWrap frame].size.height - event.pointerButtonPress.y + [cocoaWrap frame].origin.y);
MAAttachedWindow *attachedWindow = [[MAAttachedWindow alloc] initWithView:[vc view] attachedToPoint:buttonPoint onSide:side atDistance:0.0f]; // some Matt Gemmell goodness!

And I try to show it with one of the following lines:

// A)
[NSApp runModalForWindow:[attachedWindow retain]]; // makes a white box
// B)
NSWindow *cocoaWrap = [[NSWindow alloc] initWithWindowRef:winref];
[cocoaWrap addChildWindow:attachedWindow ordered:NSWindowAbove];
// C)
[attachedWindow makeKeyAndOrderFront:NSApp];

The window shows, but the focus is never given. I can't edit any of the controls, and everything is grayed out.

help!?

I tried

HIViewRef viewRef;
HICocoaViewCreate([vc view], 0, &viewRef);
WindowRef attachedRef = (WindowRef)[attachedWindow windowRef];
SetKeyboardFocus(attachedRef, viewRef, kControlNoPart);

Thinking it might have been a Carbon/Cocoa thing, but to no avail.

+2  A: 

Did you call NSApplicationLoad() before calling Cocoa methods?

Yuji
Yes. Does it need to be called in each thread?
Stephen Furlani
!? I can't believe that was it. I moved the call to the main thread instead of my rendering thread and voila! working! DERP *facepalm*
Stephen Furlani
Good that it worked. In general, you shouldn't call any Cocoa UI methods from non-main threads. It's unfortunate but Cocoa uses a lot of main-thread-only constructs. e.g. You shouldn't call `makeKeyAndOrderFront:` from the secondary threads, etc. etc.
Yuji