1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
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.
19#ifndef OSGVIEWER_GRAPHICSWINDOWCOCOA
20#define OSGVIEWER_GRAPHICSWINDOWCOCOA 1
25@class GraphicsWindowCocoaWindow;
26@class GraphicsWindowCocoaGLView;
27@class NSOpenGLContext;
28@class NSOpenGLPixelFormat;
32class GraphicsWindowCocoaGLView;
33class GraphicsWindowCocoaWindow;
35class NSOpenGLPixelFormat;
40#include <osgViewer/GraphicsWindow>
41#include <osgViewer/api/Cocoa/GraphicsHandleCocoa>
43// we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors
48class GraphicsWindowCocoa : public osgViewer::GraphicsWindow, public osgViewer::GraphicsHandleCocoa
53 GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits):
54 osgViewer::GraphicsWindow(),
55 osgViewer::GraphicsHandleCocoa(),
59 _closeRequested(false),
60 _checkForEvents(true),
62 _currentCursor(RightArrowCursor),
67 _updateContext(false),
68 _multiTouchEnabled(false)
76 setState( new osg::State );
77 getState()->setGraphicsContext(this);
79 if (_traits.valid() && _traits->sharedContext.valid())
81 getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
82 incrementContextIDUsageCount( getState()->getContextID() );
86 getState()->setContextID( osg::GraphicsContext::createNewContextID() );
91 virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowCocoa*>(object)!=0; }
92 virtual const char* libraryName() const { return "osgViewer"; }
93 virtual const char* className() const { return "GraphicsWindowCocoa"; }
95 virtual bool valid() const { return _valid; }
97 /** Realise the GraphicsContext.*/
98 virtual bool realizeImplementation();
100 /** Return true if the graphics context has been realised and is ready to use.*/
101 virtual bool isRealizedImplementation() const { return _realized; }
103 /** Close the graphics context.*/
104 virtual void closeImplementation();
106 /** Make this graphics context current.*/
107 virtual bool makeCurrentImplementation();
109 /** Release the graphics context.*/
110 virtual bool releaseContextImplementation();
112 /** Swap the front and back buffers.*/
113 virtual void swapBuffersImplementation();
115 /** Check to see if any events have been generated.*/
116 virtual bool checkEvents();
118 /** Set Window decoration.*/
119 virtual bool setWindowDecorationImplementation(bool flag);
122 virtual void grabFocus();
124 /** Get focus on if the pointer is in this window.*/
125 virtual void grabFocusIfPointerInWindow();
127 bool requestClose() { bool b = _closeRequested; _closeRequested = true; return b; }
129 virtual void resizedImplementation(int x, int y, int width, int height);
131 virtual bool setWindowRectangleImplementation(int x, int y, int width, int height);
133 virtual void setWindowName (const std::string & name);
134 virtual void requestWarpPointer(float x,float y);
135 virtual void useCursor(bool cursorOn);
136 virtual void setCursor(MouseCursor mouseCursor);
138 /** WindowData is used to pass in the Cocoa window handle attached the GraphicsContext::Traits structure. */
139 class WindowData : public osg::Referenced
142 enum Options { CreateOnlyView = 1, CheckForEvents = 2, PoseAsStandaloneApp = 4, EnableMultiTouch = 8};
143 WindowData(unsigned int options)
144 : _createOnlyView(options & CreateOnlyView),
145 _checkForEvents(options & CheckForEvents),
146 _poseAsStandaloneApp(options & PoseAsStandaloneApp),
147 _multiTouchEnabled(options & EnableMultiTouch),
152 inline NSView* getCreatedNSView() { return _view; }
153 bool createOnlyView() const { return _createOnlyView; }
154 bool checkForEvents() const { return _checkForEvents; }
155 bool poseAsStandaloneApp() const { return _poseAsStandaloneApp; }
156 bool isMultiTouchEnabled() const { return _multiTouchEnabled; }
159 inline void setCreatedNSView(NSView* view) { _view = view; }
162 bool _createOnlyView, _checkForEvents, _poseAsStandaloneApp, _multiTouchEnabled;
165 friend class GraphicsWindowCocoa;
169 NSOpenGLContext* getContext() { return _context; }
170 GraphicsWindowCocoaWindow* getWindow() { return _window; }
171 NSOpenGLPixelFormat* getPixelFormat() { return _pixelformat; }
173 virtual void setSyncToVBlank(bool f);
175 /** adapts a resize / move of the window, coords in global screen space */
176 void adaptResize(int x, int y, int w, int h);
178 bool isMultiTouchEnabled();
179 void setMultiTouchEnabled(bool b);
185 void transformMouseXY(float& x, float& y);
186 void setupNSWindow(NSWindow* win);
189 virtual ~GraphicsWindowCocoa();
195 bool _useWindowDecoration;
202 bool _closeRequested, _checkForEvents,_ownsWindow;
203 MouseCursor _currentCursor;
204 GraphicsWindowCocoaWindow* _window;
205 GraphicsWindowCocoaGLView* _view;
206 NSOpenGLContext* _context;
207 NSOpenGLPixelFormat* _pixelformat;
208 bool _updateContext, _multiTouchEnabled;