| 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_GRAPHICSWINDOWCARBON |
|---|
| 20 | #define OSGVIEWER_GRAPHICSWINDOWCARBON 1 |
|---|
| 21 | |
|---|
| 22 | #ifdef __APPLE__ |
|---|
| 23 | |
|---|
| 24 | #include <osgViewer/GraphicsWindow> |
|---|
| 25 | #include <Carbon/Carbon.h> |
|---|
| 26 | #include <AGL/agl.h> |
|---|
| 27 | namespace osgViewer |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | class GraphicsWindowCarbon : public osgViewer::GraphicsWindow |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | GraphicsWindowCarbon(osg::GraphicsContext::Traits* traits): |
|---|
| 35 | _valid(false), |
|---|
| 36 | _initialized(false), |
|---|
| 37 | _realized(false), |
|---|
| 38 | _ownsWindow(true), |
|---|
| 39 | _currentCursor(RightArrowCursor) |
|---|
| 40 | { |
|---|
| 41 | _traits = traits; |
|---|
| 42 | |
|---|
| 43 | init(); |
|---|
| 44 | |
|---|
| 45 | if (valid()) |
|---|
| 46 | { |
|---|
| 47 | setState( new osg::State ); |
|---|
| 48 | getState()->setGraphicsContext(this); |
|---|
| 49 | |
|---|
| 50 | if (_traits.valid() && _traits->sharedContext) |
|---|
| 51 | { |
|---|
| 52 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 53 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 54 | } |
|---|
| 55 | else |
|---|
| 56 | { |
|---|
| 57 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowCarbon*>(object)!=0; } |
|---|
| 63 | virtual const char* libraryName() const { return "osgViewer"; } |
|---|
| 64 | virtual const char* className() const { return "GraphicsWindowCarbon"; } |
|---|
| 65 | |
|---|
| 66 | virtual bool valid() const { return _valid; } |
|---|
| 67 | |
|---|
| 68 | /** Realise the GraphicsContext.*/ |
|---|
| 69 | virtual bool realizeImplementation(); |
|---|
| 70 | |
|---|
| 71 | /** Return true if the graphics context has been realised and is ready to use.*/ |
|---|
| 72 | virtual bool isRealizedImplementation() const { return _realized; } |
|---|
| 73 | |
|---|
| 74 | /** Close the graphics context.*/ |
|---|
| 75 | virtual void closeImplementation(); |
|---|
| 76 | |
|---|
| 77 | /** Make this graphics context current.*/ |
|---|
| 78 | virtual bool makeCurrentImplementation(); |
|---|
| 79 | |
|---|
| 80 | /** Release the graphics context.*/ |
|---|
| 81 | virtual bool releaseContextImplementation(); |
|---|
| 82 | |
|---|
| 83 | /** Swap the front and back buffers.*/ |
|---|
| 84 | virtual void swapBuffersImplementation(); |
|---|
| 85 | |
|---|
| 86 | /** Check to see if any events have been generated.*/ |
|---|
| 87 | virtual void checkEvents(); |
|---|
| 88 | |
|---|
| 89 | /** Set the window's position and size.*/ |
|---|
| 90 | virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); |
|---|
| 91 | |
|---|
| 92 | /** Set Window decoration.*/ |
|---|
| 93 | virtual bool setWindowDecorationImplementation(bool flag); |
|---|
| 94 | |
|---|
| 95 | // Override from GUIActionAdapter |
|---|
| 96 | virtual void requestWarpPointer( float x, float y); |
|---|
| 97 | |
|---|
| 98 | /** Get focus.*/ |
|---|
| 99 | virtual void grabFocus(); |
|---|
| 100 | |
|---|
| 101 | /** Get focus on if the pointer is in this window.*/ |
|---|
| 102 | virtual void grabFocusIfPointerInWindow(); |
|---|
| 103 | |
|---|
| 104 | void requestClose() { _closeRequested = true; } |
|---|
| 105 | |
|---|
| 106 | virtual void resizedImplementation(int x, int y, int width, int height); |
|---|
| 107 | |
|---|
| 108 | virtual void setWindowName (const std::string & name); |
|---|
| 109 | virtual void useCursor(bool cursorOn); |
|---|
| 110 | virtual void setCursor(MouseCursor mouseCursor); |
|---|
| 111 | |
|---|
| 112 | WindowRef getNativeWindowRef() { return _window; } |
|---|
| 113 | |
|---|
| 114 | bool handleMouseEvent(EventRef theEvent); |
|---|
| 115 | bool handleKeyboardEvent(EventRef theEvent); |
|---|
| 116 | bool handleModifierKeys(EventRef theEvent); |
|---|
| 117 | |
|---|
| 118 | /** WindowData is used to pass in the Carbon window handle attached the GraphicsContext::Traits structure. */ |
|---|
| 119 | class WindowData : public osg::Referenced |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | WindowData(WindowRef window, AGLDrawable* drawable=NULL ): //ADEGLI |
|---|
| 123 | _window(window), _AGLDrawable(drawable) ,_installEventHandler(false) {} //ADEGLI |
|---|
| 124 | |
|---|
| 125 | WindowRef getNativeWindowRef() { return _window; } |
|---|
| 126 | void setInstallEventHandler(bool flag) { _installEventHandler = flag; } |
|---|
| 127 | bool installEventHandler() { return _installEventHandler; } |
|---|
| 128 | AGLDrawable* getAGLDrawable() { return _AGLDrawable; } //ADEGLI |
|---|
| 129 | |
|---|
| 130 | private: |
|---|
| 131 | WindowRef _window; |
|---|
| 132 | AGLDrawable* _AGLDrawable; //ADEGLI |
|---|
| 133 | bool _installEventHandler; |
|---|
| 134 | |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | /// install the standard os-eventhandler |
|---|
| 138 | void installEventHandler(); |
|---|
| 139 | |
|---|
| 140 | /// get the AGL context |
|---|
| 141 | AGLContext getAGLContext() { return _context; } |
|---|
| 142 | |
|---|
| 143 | // get the pixelformat |
|---|
| 144 | AGLPixelFormat getAGLPixelFormat() { return _pixelFormat; } |
|---|
| 145 | |
|---|
| 146 | protected: |
|---|
| 147 | |
|---|
| 148 | void init(); |
|---|
| 149 | |
|---|
| 150 | void transformMouseXY(float& x, float& y); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | bool _valid; |
|---|
| 157 | bool _initialized; |
|---|
| 158 | bool _realized; |
|---|
| 159 | bool _useWindowDecoration; |
|---|
| 160 | |
|---|
| 161 | bool _ownsWindow; |
|---|
| 162 | WindowRef _window; |
|---|
| 163 | AGLContext _context; |
|---|
| 164 | AGLPixelFormat _pixelFormat; |
|---|
| 165 | |
|---|
| 166 | int _windowTitleHeight; |
|---|
| 167 | private: |
|---|
| 168 | /// computes the window attributes |
|---|
| 169 | WindowAttributes computeWindowAttributes(bool useWindowDecoration, bool supportsResize); |
|---|
| 170 | void handleModifierKey(UInt32 modifierKey, UInt32 modifierMask, osgGA::GUIEventAdapter::KeySymbol keySymbol); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | bool _closeRequested; |
|---|
| 174 | UInt32 _lastModifierKeys; |
|---|
| 175 | MouseCursor _currentCursor; |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | #endif |
|---|
| 181 | #endif |
|---|