title: Set Focus to a Cocoa Window in Carbon App
body: <p>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).</p>
  
  <p>Here's a code snippit:</p>
  
  <pre><code>WindowRef winref = static_cast&lt;eq::AGLWindow*&gt;(getOSWindow())-&gt;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!
  </code></pre>
  
  <p>And I try to show it with one of the following lines:</p>
  
  <pre><code>// 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];
  </code></pre>
  
  <p>The window shows, but the focus is never given.  I can't edit any of the controls, and everything is grayed out.</p>
  
  <p>help!?</p>
  
  <p>I tried</p>
  
  <pre><code>HIViewRef viewRef;
  HICocoaViewCreate([vc view], 0, &amp;viewRef);
  WindowRef attachedRef = (WindowRef)[attachedWindow windowRef];
  SetKeyboardFocus(attachedRef, viewRef, kControlNoPart);
  </code></pre>
  
  <p>Thinking it might have been a Carbon/Cocoa thing, but to no avail.</p>