| [9879] | 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 NSOpenGLContext; |
|---|
| 27 | @class NSWindow; |
|---|
| 28 | #else |
|---|
| 29 | class GraphicsWindowCocoaWindow; |
|---|
| 30 | class NSOpenGLContext; |
|---|
| 31 | class NSWindow; |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #include <osgViewer/GraphicsWindow> |
|---|
| 35 | |
|---|
| 36 | // we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors |
|---|
| 37 | |
|---|
| 38 | namespace osgViewer |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | class GraphicsWindowCocoa : public osgViewer::GraphicsWindow |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | class Implementation; |
|---|
| 45 | |
|---|
| 46 | GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits): |
|---|
| 47 | _valid(false), |
|---|
| 48 | _initialized(false), |
|---|
| 49 | _realized(false), |
|---|
| 50 | _ownsWindow(true), |
|---|
| 51 | _currentCursor(RightArrowCursor) |
|---|
| 52 | { |
|---|
| 53 | _traits = traits; |
|---|
| 54 | |
|---|
| 55 | init(); |
|---|
| 56 | |
|---|
| 57 | if (valid()) |
|---|
| 58 | { |
|---|
| 59 | setState( new osg::State ); |
|---|
| 60 | getState()->setGraphicsContext(this); |
|---|
| 61 | |
|---|
| 62 | if (_traits.valid() && _traits->sharedContext) |
|---|
| 63 | { |
|---|
| 64 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 65 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 66 | } |
|---|
| 67 | else |
|---|
| 68 | { |
|---|
| 69 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowCocoa*>(object)!=0; } |
|---|
| 75 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 76 | virtual const char* className() const { return "GraphicsWindowCarbon"; } |
|---|
| 77 | |
|---|
| 78 | virtual bool valid() const { return _valid; } |
|---|
| 79 | |
|---|
| 80 | /** Realise the GraphicsContext.*/ |
|---|
| 81 | virtual bool realizeImplementation(); |
|---|
| 82 | |
|---|
| 83 | /** Return true if the graphics context has been realised and is ready to use.*/ |
|---|
| 84 | virtual bool isRealizedImplementation() const { return _realized; } |
|---|
| 85 | |
|---|
| 86 | /** Close the graphics context.*/ |
|---|
| 87 | virtual void closeImplementation(); |
|---|
| 88 | |
|---|
| 89 | /** Make this graphics context current.*/ |
|---|
| 90 | virtual bool makeCurrentImplementation(); |
|---|
| 91 | |
|---|
| 92 | /** Release the graphics context.*/ |
|---|
| 93 | virtual bool releaseContextImplementation(); |
|---|
| 94 | |
|---|
| 95 | /** Swap the front and back buffers.*/ |
|---|
| 96 | virtual void swapBuffersImplementation(); |
|---|
| 97 | |
|---|
| 98 | /** Check to see if any events have been generated.*/ |
|---|
| 99 | virtual void checkEvents(); |
|---|
| 100 | |
|---|
| 101 | /** Set Window decoration.*/ |
|---|
| 102 | virtual bool setWindowDecorationImplementation(bool flag); |
|---|
| 103 | |
|---|
| 104 | /** Get focus.*/ |
|---|
| 105 | virtual void grabFocus(); |
|---|
| 106 | |
|---|
| 107 | /** Get focus on if the pointer is in this window.*/ |
|---|
| 108 | virtual void grabFocusIfPointerInWindow(); |
|---|
| 109 | |
|---|
| 110 | bool requestClose() { bool b = _closeRequested; _closeRequested = true; return b; } |
|---|
| 111 | |
|---|
| 112 | virtual void resizedImplementation(int x, int y, int width, int height); |
|---|
| 113 | |
|---|
| 114 | virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); |
|---|
| 115 | |
|---|
| 116 | virtual void setWindowName (const std::string & name); |
|---|
| 117 | virtual void useCursor(bool cursorOn); |
|---|
| 118 | virtual void setCursor(MouseCursor mouseCursor); |
|---|
| 119 | |
|---|
| 120 | /** WindowData is used to pass in the Cocoa window handle attached the GraphicsContext::Traits structure. */ |
|---|
| 121 | class WindowData : public osg::Referenced |
|---|
| 122 | { |
|---|
| 123 | public: |
|---|
| 124 | WindowData(NSWindow* window) |
|---|
| 125 | : _window(window) |
|---|
| 126 | { |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | inline NSWindow* getNativeWindowRef() { return _window; } |
|---|
| 130 | |
|---|
| 131 | private: |
|---|
| 132 | NSWindow* _window; |
|---|
| 133 | bool _installEventHandler; |
|---|
| 134 | |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | NSOpenGLContext* getContext() { return _context; } |
|---|
| 138 | GraphicsWindowCocoaWindow* getWindow() { return _window; } |
|---|
| 139 | |
|---|
| 140 | void setVSync(bool f); |
|---|
| 141 | |
|---|
| 142 | /** adapts a resize / move of the window, coords in global screen space */ |
|---|
| 143 | void adaptResize(int x, int y, int w, int h); |
|---|
| 144 | |
|---|
| 145 | protected: |
|---|
| 146 | |
|---|
| 147 | void init(); |
|---|
| 148 | |
|---|
| 149 | void transformMouseXY(float& x, float& y); |
|---|
| 150 | void setupNSWindow(NSWindow* win); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | virtual ~GraphicsWindowCocoa(); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | bool _valid; |
|---|
| 157 | bool _initialized; |
|---|
| 158 | bool _realized; |
|---|
| 159 | bool _useWindowDecoration; |
|---|
| 160 | bool _ownsWindow; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | private: |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | bool _closeRequested; |
|---|
| 168 | MouseCursor _currentCursor; |
|---|
| 169 | GraphicsWindowCocoaWindow* _window; |
|---|
| 170 | NSOpenGLContext* _context; |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | #endif |
|---|
| 176 | #endif |
|---|