# How to get the NSView from glfwGetCocoaWindow()? (without obj-c++)

**URL:** <https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265>\
**Category:** support\
**Created:** [December 22, 2022, 3:41pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265 "2022-12-22T15:41:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sh93](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sh93](https://discourse.glfw.org/u/sh93)\
**Post date:** [December 22, 2022, 3:41pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265/1 "2022-12-22T15:41:31Z")

</div>

so i need to get NSView as i’m making a window to pipe through some graphics from a plugin (using the vst3sdk).

i’ve looked  
[here:](https://www.glfw.org/docs/latest/group__native.html#gac3ed9d495d0c2bb9652de5a50c648715)  
but unfortunately i can only get the NSWindow ID from it? and theres not much else there regarding NSView

if i look at the cocoa\_window.m file at line 327 [here](https://github.com/glfw/glfw/blob/d299d9f78857e921b66bdab42c7ea27fe2e31810/src/cocoa_window.m#L327)

it seems like its there? i just don’t know how to get it from my c++ code and currently have to compile the file as obj-c++ and use:

```auto
  NSWindow* nsWindow = glfwGetCocoaWindow(editorWindow);
  NSView* nsView = nsWindow.contentView;
  view->attached(nsView, Steinberg::kPlatformTypeNSView);

```

(which is something i’d rather avoid if i can!)

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [December 22, 2022, 4:23pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265/2 "2022-12-22T16:23:45Z")

</div>

I don’t think there is a way to do this without Objective-C/C++. The GLFW Cocoa source code is itself Objective-C. A simple approach might be to make a very small library which does the work you need in Objective-C and thus you can stay in C/C++ for the rest of your code.

---

<div class="post-metadata">

**Author:** ![sh93](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sh93](https://discourse.glfw.org/u/sh93)\
**Post date:** [December 22, 2022, 4:35pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265/4 "2022-12-22T16:35:30Z")

</div>

ah i was afraid of that! unfortunately i’ve never looked at obj-c or obj-c++ before today… is there a reason the NSWindow can be exposed via glfwGetCocoaWindow() but there isn’t something like a glfwGetCocoaView() which can expose the NSView in your C++ code?

with that aside i have started to write a getNSView obj-c++ file…

```auto
#import <Cocoa/Cocoa.h>

NSView *getNSView(GLFWwindow *glfwWindow)
{
  NSWindow* nsWindow = glfwGetCocoaWindow(glfwWindow);
  NSView* nsView = nsWindow.contentView;

  return nsView;
}

```

where in my main c++ code i originally had this?

```auto
  editorWindow = glfwCreateWindow(
    dimensions.getWidth(), dimensions.getHeight(),
    "VST 3 SDK", nullptr, nullptr);

  glfwSetWindowAttrib(editorWindow, GLFW_RESIZABLE, false);

  NSWindow* nsWindow = glfwGetCocoaWindow(editorWindow);
  NSView* nsView = nsWindow.contentView;
  view->attached(nsView, Steinberg::kPlatformTypeNSView);

```

---

<div class="post-metadata">

**Author:** ![sh93](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sh93](https://discourse.glfw.org/u/sh93)\
**Post date:** [December 22, 2022, 4:41pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265/5 "2022-12-22T16:41:06Z")

</div>

[here](https://github.com/glfw/glfw/blob/d299d9f78857e921b66bdab42c7ea27fe2e31810/src/cocoa_window.m#L2037) theres the definition for glfwGetCocoaWindow()? could it be possible to simply add my own glfwGetCocoaView() through a patch?

for example?

```auto
GLFWAPI id glfwGetCocoaView(GLFWwindow* handle)
{
    _GLFWwindow* window = (_GLFWwindow*) handle;
    _GLFW_REQUIRE_INIT_OR_RETURN(nil);

    if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA)
    {
        _glfwInputError(GLFW_PLATFORM_UNAVAILABLE,
                        "Cocoa: Platform not initialized");
        return NULL;
    }

    return window->ns.view;
}

```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [December 22, 2022, 6:57pm UTC](https://discourse.glfw.org/t/how-to-get-the-nsview-from-glfwgetcocoawindow-without-obj-c/2265/6 "2022-12-22T18:57:10Z")

</div>

You can definitely add that to your own fork of GLFW, and potentially submit a PR. I don’t know if it will be accepted as I think the reason this wasn’t included was that the most users of the API would need to write in Objective-C to access the native functionality, and GLFW tries to be lightweight by minimizing API surface area.
