1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of the examples of the Open CASCADE Technology software library.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
22 #include "GlfwOcctWindow.h"
24 #if defined (__APPLE__)
25 #undef Handle // avoid name collisions in macOS headers
26 #define GLFW_EXPOSE_NATIVE_COCOA
27 #define GLFW_EXPOSE_NATIVE_NSGL
28 #elif defined (_WIN32)
29 #define GLFW_EXPOSE_NATIVE_WIN32
30 #define GLFW_EXPOSE_NATIVE_WGL
32 #define GLFW_EXPOSE_NATIVE_X11
33 #define GLFW_EXPOSE_NATIVE_GLX
35 #include <GLFW/glfw3.h>
36 #include <GLFW/glfw3native.h>
38 // ================================================================
39 // Function : GlfwOcctWindow
41 // ================================================================
42 GlfwOcctWindow::GlfwOcctWindow (int theWidth, int theHeight, const TCollection_AsciiString& theTitle)
43 : myGlfwWindow (glfwCreateWindow (theWidth, theHeight, theTitle.ToCString(), NULL, NULL)),
49 if (myGlfwWindow != nullptr)
51 int aWidth = 0, aHeight = 0;
52 glfwGetWindowPos (myGlfwWindow, &myXLeft, &myYTop);
53 glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight);
54 myXRight = myXLeft + aWidth;
55 myYBottom = myYTop + aHeight;
57 #if !defined(_WIN32) && !defined(__APPLE__)
58 myDisplay = new Aspect_DisplayConnection (glfwGetX11Display());
63 // ================================================================
66 // ================================================================
67 void GlfwOcctWindow::Close()
69 if (myGlfwWindow != nullptr)
71 glfwDestroyWindow (myGlfwWindow);
72 myGlfwWindow = nullptr;
76 // ================================================================
77 // Function : NativeHandle
79 // ================================================================
80 Aspect_Drawable GlfwOcctWindow::NativeHandle() const
82 #if defined (__APPLE__)
83 return (Aspect_Drawable)glfwGetCocoaWindow (myGlfwWindow);
84 #elif defined (_WIN32)
85 return (Aspect_Drawable)glfwGetWin32Window (myGlfwWindow);
87 return (Aspect_Drawable)glfwGetX11Window (myGlfwWindow);
91 // ================================================================
92 // Function : NativeGlContext
94 // ================================================================
95 Aspect_RenderingContext GlfwOcctWindow::NativeGlContext() const
97 #if defined (__APPLE__)
98 return (NSOpenGLContext*)glfwGetNSGLContext (myGlfwWindow);
99 #elif defined (_WIN32)
100 return glfwGetWGLContext (myGlfwWindow);
102 return glfwGetGLXContext (myGlfwWindow);
106 // ================================================================
107 // Function : IsMapped
109 // ================================================================
110 Standard_Boolean GlfwOcctWindow::IsMapped() const
112 return glfwGetWindowAttrib (myGlfwWindow, GLFW_VISIBLE) != 0;
115 // ================================================================
118 // ================================================================
119 void GlfwOcctWindow::Map() const
121 glfwShowWindow (myGlfwWindow);
124 // ================================================================
127 // ================================================================
128 void GlfwOcctWindow::Unmap() const
130 glfwHideWindow (myGlfwWindow);
133 // ================================================================
134 // Function : DoResize
136 // ================================================================
137 Aspect_TypeOfResize GlfwOcctWindow::DoResize()
139 if (glfwGetWindowAttrib (myGlfwWindow, GLFW_VISIBLE) == 1)
141 int anXPos = 0, anYPos = 0, aWidth = 0, aHeight = 0;
142 glfwGetWindowPos (myGlfwWindow, &anXPos, &anYPos);
143 glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight);
145 myXRight = anXPos + aWidth;
147 myYBottom = anYPos + aHeight;
149 return Aspect_TOR_UNKNOWN;
152 // ================================================================
153 // Function : CursorPosition
155 // ================================================================
156 Graphic3d_Vec2i GlfwOcctWindow::CursorPosition() const
158 Graphic3d_Vec2d aPos;
159 glfwGetCursorPos (myGlfwWindow, &aPos.x(), &aPos.y());
160 return Graphic3d_Vec2i ((int )aPos.x(), (int )aPos.y());