Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField over NSOpenGLView

I have made a window with an NSOpenGLView that I am rendering openGL content into.

I want to add some buttons and text fields to the view: I can add NSTextFields and NSButtons using interface builder (or code) but they do not appear.

NSOpenGLView is documented as not being able to have sub views, so I then made my own CustomGLView by deriving directly from NSView and implementing the code to create and use a NSOpenGLContext in it. But the subviews are still not appearing :- the OpenGL context paints over them.

On Windows this problem does not exist:- Windows used to host OpenGL MUST have the WS_CLIPCHILDREN and WS_CHIPSIBLINGS styles set ensuring that any peer, or sub children (views) will not be obscured by the OpenGL surface.

How do I get subviews to display over a NSView thats drawing using OpenGL ?

like image 605
Chris Becke Avatar asked Feb 08 '10 12:02

Chris Becke


3 Answers

You have 2 choices:

  1. Create a window just for the text field. Add as a child window of the one hosting the OpenGL view. Major downside is you have to manage positioning it correctly if the Open GL view is moved.

  2. Set up your view hierarchy like so:

    • Layer-backed view
      • Layer-hosting view whose layer contains an OpenGL layer
      • Text field
like image 184
Mike Abdullah Avatar answered Nov 11 '22 02:11

Mike Abdullah


Simply call -setWantsLayer:YES on the subviews of the NSOpenGLView.

like image 6
Frederik Slijkerman Avatar answered Nov 11 '22 00:11

Frederik Slijkerman


NSOpenGLView cannot have subviews according to the documentation. Even if you subclass the NSOpenGLView, that will change nothing.

What you can do is to create a NSView that will hold both the NSOpenGLView and the NSTextField. You then overlap them in the right order to make one draw atop the other.

like image 1
Laurent Etiemble Avatar answered Nov 11 '22 02:11

Laurent Etiemble