8cd9f6fe4bc05107939d5daa6d8d2c9cc0b73186
[occt.git] / samples / glfw / GlfwOcctWindow.cpp
1 // Copyright (c) 2019 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
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
21
22 #include "GlfwOcctWindow.h"
23
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
31 #else
32   #define GLFW_EXPOSE_NATIVE_X11
33   #define GLFW_EXPOSE_NATIVE_GLX
34 #endif
35 #include <GLFW/glfw3.h>
36 #include <GLFW/glfw3native.h>
37
38 // ================================================================
39 // Function : GlfwOcctWindow
40 // Purpose  :
41 // ================================================================
42 GlfwOcctWindow::GlfwOcctWindow (int theWidth, int theHeight, const TCollection_AsciiString& theTitle)
43 : myGlfwWindow (glfwCreateWindow (theWidth, theHeight, theTitle.ToCString(), NULL, NULL)),
44   myXLeft  (0),
45   myYTop   (0),
46   myXRight (0),
47   myYBottom(0)
48 {
49   if (myGlfwWindow != nullptr)
50   {
51     int aWidth = 0, aHeight = 0;
52     glfwGetWindowPos (myGlfwWindow, &myXLeft, &myYTop);
53     glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight);
54     myXRight  = myXLeft + aWidth;
55     myYBottom = myYTop + aHeight;
56
57   #if !defined(_WIN32) && !defined(__APPLE__)
58     myDisplay = new Aspect_DisplayConnection (glfwGetX11Display());
59   #endif
60   }
61 }
62
63 // ================================================================
64 // Function : Close
65 // Purpose  :
66 // ================================================================
67 void GlfwOcctWindow::Close()
68 {
69   if (myGlfwWindow != nullptr)
70   {
71     glfwDestroyWindow (myGlfwWindow);
72     myGlfwWindow = nullptr;
73   }
74 }
75
76 // ================================================================
77 // Function : NativeHandle
78 // Purpose  :
79 // ================================================================
80 Aspect_Drawable GlfwOcctWindow::NativeHandle() const
81 {
82 #if defined (__APPLE__)
83   return (Aspect_Drawable)glfwGetCocoaWindow (myGlfwWindow);
84 #elif defined (_WIN32)
85   return (Aspect_Drawable)glfwGetWin32Window (myGlfwWindow);
86 #else
87   return (Aspect_Drawable)glfwGetX11Window (myGlfwWindow);
88 #endif
89 }
90
91 // ================================================================
92 // Function : NativeGlContext
93 // Purpose  :
94 // ================================================================
95 Aspect_RenderingContext GlfwOcctWindow::NativeGlContext() const
96 {
97 #if defined (__APPLE__)
98   return (NSOpenGLContext*)glfwGetNSGLContext (myGlfwWindow);
99 #elif defined (_WIN32)
100   return glfwGetWGLContext (myGlfwWindow);
101 #else
102   return glfwGetGLXContext (myGlfwWindow);
103 #endif
104 }
105
106 // ================================================================
107 // Function : IsMapped
108 // Purpose  :
109 // ================================================================
110 Standard_Boolean GlfwOcctWindow::IsMapped() const
111 {
112   return glfwGetWindowAttrib (myGlfwWindow, GLFW_VISIBLE) != 0;
113 }
114
115 // ================================================================
116 // Function : Map
117 // Purpose  :
118 // ================================================================
119 void GlfwOcctWindow::Map() const
120 {
121   glfwShowWindow (myGlfwWindow);
122 }
123
124 // ================================================================
125 // Function : Unmap
126 // Purpose  :
127 // ================================================================
128 void GlfwOcctWindow::Unmap() const
129 {
130   glfwHideWindow (myGlfwWindow);
131 }
132
133 // ================================================================
134 // Function : DoResize
135 // Purpose  :
136 // ================================================================
137 Aspect_TypeOfResize GlfwOcctWindow::DoResize()
138 {
139   if (glfwGetWindowAttrib (myGlfwWindow, GLFW_VISIBLE) == 1)
140   {
141     int anXPos = 0, anYPos = 0, aWidth = 0, aHeight = 0;
142     glfwGetWindowPos (myGlfwWindow, &anXPos, &anYPos);
143     glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight);
144     myXLeft   = anXPos;
145     myXRight  = anXPos + aWidth;
146     myYTop    = anYPos;
147     myYBottom = anYPos + aHeight;
148   }
149   return Aspect_TOR_UNKNOWN;
150 }
151
152 // ================================================================
153 // Function : CursorPosition
154 // Purpose  :
155 // ================================================================
156 Graphic3d_Vec2i GlfwOcctWindow::CursorPosition() const
157 {
158   Graphic3d_Vec2d aPos;
159   glfwGetCursorPos (myGlfwWindow, &aPos.x(), &aPos.y());
160   return Graphic3d_Vec2i ((int )aPos.x(), (int )aPos.y());
161 }