| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | /* Note, elements of GraphicsWindowX11 have used Prodcer/RenderSurface_X11.cpp as both |
|---|
| 15 | * a guide to use of X11/GLX and copiying directly in the case of setBorder(). |
|---|
| 16 | * These elements are license under OSGPL as above, with Copyright (C) 2001-2004 Don Burns. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef OSGVIEWER_GRAPHICSWINDOWCOCOA |
|---|
| 20 | #define OSGVIEWER_GRAPHICSWINDOWCOCOA 1 |
|---|
| 21 | |
|---|
| 22 | #ifdef __APPLE__ |
|---|
| 23 | |
|---|
| 24 | #ifdef __OBJC__ |
|---|
| 25 | @class GraphicsWindowCocoaWindow; |
|---|
| 26 | @class GraphicsWindowCocoaGLView; |
|---|
| 27 | @class NSOpenGLContext; |
|---|
| 28 | @class NSWindow; |
|---|
| 29 | @class NSView; |
|---|
| 30 | #else |
|---|
| 31 | class GraphicsWindowCocoaGLView; |
|---|
| 32 | class GraphicsWindowCocoaWindow; |
|---|
| 33 | class NSOpenGLContext; |
|---|
| 34 | class NSWindow; |
|---|
| 35 | class NSView; |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | #include <osgViewer/GraphicsWindow> |
|---|
| 39 | |
|---|
| 40 | // we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors |
|---|
| 41 | |
|---|
| 42 | namespace osgViewer |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | class GraphicsWindowCocoa : public osgViewer::GraphicsWindow |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | class Implementation; |
|---|
| 49 | |
|---|
| 50 | GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits): |
|---|
| 51 | _valid(false), |
|---|
| 52 | _initialized(false), |
|---|
| 53 | _realized(false), |
|---|
| 54 | _closeRequested(false), |
|---|
| 55 | _checkForEvents(true), |
|---|
| 56 | _ownsWindow(true), |
|---|
| 57 | _currentCursor(RightArrowCursor), |
|---|
| 58 | _window(NULL), |
|---|
| 59 | _context(NULL) |
|---|
| 60 | { |
|---|
| 61 | _traits = traits; |
|---|
| 62 | |
|---|
| 63 | init(); |
|---|
| 64 | |
|---|
| 65 | if (valid()) |
|---|
| 66 | { |
|---|
| 67 | setState( new osg::State ); |
|---|
| 68 | getState()->setGraphicsContext(this); |
|---|
| 69 | |
|---|
| 70 | if (_traits.valid() && _traits->sharedContext) |
|---|
| 71 | { |
|---|
| 72 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 73 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 74 | } |
|---|
| 75 | else |
|---|
| 76 | { |
|---|
| 77 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowCocoa*>(object)!=0; } |
|---|
| 83 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 84 | virtual const char* className() const { return "GraphicsWindowCarbon"; } |
|---|
| 85 | |
|---|
| 86 | virtual bool valid() const { return _valid; } |
|---|
| 87 | |
|---|
| 88 | /** Realise the GraphicsContext.*/ |
|---|
| 89 | virtual bool realizeImplementation(); |
|---|
| 90 | |
|---|
| 91 | /** Return true if the graphics context has been realised and is ready to use.*/ |
|---|
| 92 | virtual bool isRealizedImplementation() const { return _realized; } |
|---|
| 93 | |
|---|
| 94 | /** Close the graphics context.*/ |
|---|
| 95 | virtual void closeImplementation(); |
|---|
| 96 | |
|---|
| 97 | /** Make this graphics context current.*/ |
|---|
| 98 | virtual bool makeCurrentImplementation(); |
|---|
| 99 | |
|---|
| 100 | /** Release the graphics context.*/ |
|---|
| 101 | virtual bool releaseContextImplementation(); |
|---|
| 102 | |
|---|
| 103 | /** Swap the front and back buffers.*/ |
|---|
| 104 | virtual void swapBuffersImplementation(); |
|---|
| 105 | |
|---|
| 106 | /** Check to see if any events have been generated.*/ |
|---|
| 107 | virtual void checkEvents(); |
|---|
| 108 | |
|---|
| 109 | /** Set Window decoration.*/ |
|---|
| 110 | virtual bool setWindowDecorationImplementation(bool flag); |
|---|
| 111 | |
|---|
| 112 | /** Get focus.*/ |
|---|
| 113 | virtual void grabFocus(); |
|---|
| 114 | |
|---|
| 115 | /** Get focus on if the pointer is in this window.*/ |
|---|
| 116 | virtual void grabFocusIfPointerInWindow(); |
|---|
| 117 | |
|---|
| 118 | bool requestClose() { bool b = _closeRequested; _closeRequested = true; return b; } |
|---|
| 119 | |
|---|
| 120 | virtual void resizedImplementation(int x, int y, int width, int height); |
|---|
| 121 | |
|---|
| 122 | virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); |
|---|
| 123 | |
|---|
| 124 | virtual void setWindowName (const std::string & name); |
|---|
| 125 | virtual void useCursor(bool cursorOn); |
|---|
| 126 | virtual void setCursor(MouseCursor mouseCursor); |
|---|
| 127 | |
|---|
| 128 | /** WindowData is used to pass in the Cocoa window handle attached the GraphicsContext::Traits structure. */ |
|---|
| 129 | class WindowData : public osg::Referenced |
|---|
| 130 | { |
|---|
| 131 | public: |
|---|
| 132 | enum Options { CreateOnlyView = 1, CheckForEvents = 2, PoseAsStandaloneApp = 4}; |
|---|
| 133 | WindowData(unsigned int options) |
|---|
| 134 | : _createOnlyView(options & CreateOnlyView), |
|---|
| 135 | _checkForEvents(options & CheckForEvents), |
|---|
| 136 | _poseAsStandaloneApp(options & PoseAsStandaloneApp), |
|---|
| 137 | _view(NULL) |
|---|
| 138 | { |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | inline NSView* getCreatedNSView() { return _view; } |
|---|
| 142 | bool createOnlyView() const { return _createOnlyView; } |
|---|
| 143 | bool checkForEvents() const { return _checkForEvents; } |
|---|
| 144 | bool poseAsStandaloneApp() const { return _poseAsStandaloneApp; } |
|---|
| 145 | |
|---|
| 146 | protected: |
|---|
| 147 | inline void setCreatedNSView(NSView* view) { _view = view; } |
|---|
| 148 | |
|---|
| 149 | private: |
|---|
| 150 | bool _createOnlyView, _checkForEvents, _poseAsStandaloneApp; |
|---|
| 151 | NSView* _view; |
|---|
| 152 | |
|---|
| 153 | friend class GraphicsWindowCocoa; |
|---|
| 154 | |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | NSOpenGLContext* getContext() { return _context; } |
|---|
| 158 | GraphicsWindowCocoaWindow* getWindow() { return _window; } |
|---|
| 159 | |
|---|
| 160 | void setVSync(bool f); |
|---|
| 161 | |
|---|
| 162 | /** adapts a resize / move of the window, coords in global screen space */ |
|---|
| 163 | void adaptResize(int x, int y, int w, int h); |
|---|
| 164 | |
|---|
| 165 | protected: |
|---|
| 166 | |
|---|
| 167 | void init(); |
|---|
| 168 | |
|---|
| 169 | void transformMouseXY(float& x, float& y); |
|---|
| 170 | void setupNSWindow(NSWindow* win); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | virtual ~GraphicsWindowCocoa(); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | bool _valid; |
|---|
| 177 | bool _initialized; |
|---|
| 178 | bool _realized; |
|---|
| 179 | bool _useWindowDecoration; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | private: |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | bool _closeRequested, _checkForEvents,_ownsWindow; |
|---|
| 187 | MouseCursor _currentCursor; |
|---|
| 188 | GraphicsWindowCocoaWindow* _window; |
|---|
| 189 | GraphicsWindowCocoaGLView* _view; |
|---|
| 190 | NSOpenGLContext* _context; |
|---|
| 191 | bool _updateContext; |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | #endif |
|---|
| 197 | #endif |
|---|