openscenegraph
GraphicsWindowCocoa
Go to the documentation of this file.
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 NSOpenGLPixelFormat;
29@class NSWindow;
30@class NSView;
31#else
32class GraphicsWindowCocoaGLView;
33class GraphicsWindowCocoaWindow;
34class NSOpenGLContext;
35class NSOpenGLPixelFormat;
36class NSWindow;
37class NSView;
38#endif
39
40#include <osgViewer/GraphicsWindow>
41#include <osgViewer/api/Cocoa/GraphicsHandleCocoa>
42
43// we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors
44
45namespace osgViewer
46{
47
48class GraphicsWindowCocoa : public osgViewer::GraphicsWindow, public osgViewer::GraphicsHandleCocoa
49{
50 public:
51 class Implementation;
52
53 GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits):
54 osgViewer::GraphicsWindow(),
55 osgViewer::GraphicsHandleCocoa(),
56 _valid(false),
57 _initialized(false),
58 _realized(false),
59 _closeRequested(false),
60 _checkForEvents(true),
61 _ownsWindow(true),
62 _currentCursor(RightArrowCursor),
63 _window(NULL),
64 _view(NULL),
65 _context(NULL),
66 _pixelformat(NULL),
67 _updateContext(false),
68 _multiTouchEnabled(false)
69 {
70 _traits = traits;
71
72 init();
73
74 if (valid())
75 {
76 setState( new osg::State );
77 getState()->setGraphicsContext(this);
78
79 if (_traits.valid() && _traits->sharedContext.valid())
80 {
81 getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
82 incrementContextIDUsageCount( getState()->getContextID() );
83 }
84 else
85 {
86 getState()->setContextID( osg::GraphicsContext::createNewContextID() );
87 }
88 }
89 }
90
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"; }
94
95 virtual bool valid() const { return _valid; }
96
97 /** Realise the GraphicsContext.*/
98 virtual bool realizeImplementation();
99
100 /** Return true if the graphics context has been realised and is ready to use.*/
101 virtual bool isRealizedImplementation() const { return _realized; }
102
103 /** Close the graphics context.*/
104 virtual void closeImplementation();
105
106 /** Make this graphics context current.*/
107 virtual bool makeCurrentImplementation();
108
109 /** Release the graphics context.*/
110 virtual bool releaseContextImplementation();
111
112 /** Swap the front and back buffers.*/
113 virtual void swapBuffersImplementation();
114
115 /** Check to see if any events have been generated.*/
116 virtual bool checkEvents();
117
118 /** Set Window decoration.*/
119 virtual bool setWindowDecorationImplementation(bool flag);
120
121 /** Get focus.*/
122 virtual void grabFocus();
123
124 /** Get focus on if the pointer is in this window.*/
125 virtual void grabFocusIfPointerInWindow();
126
127 bool requestClose() { bool b = _closeRequested; _closeRequested = true; return b; }
128
129 virtual void resizedImplementation(int x, int y, int width, int height);
130
131 virtual bool setWindowRectangleImplementation(int x, int y, int width, int height);
132
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);
137
138 /** WindowData is used to pass in the Cocoa window handle attached the GraphicsContext::Traits structure. */
139 class WindowData : public osg::Referenced
140 {
141 public:
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),
148 _view(NULL)
149 {
150 }
151
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; }
157
158 protected:
159 inline void setCreatedNSView(NSView* view) { _view = view; }
160
161 private:
162 bool _createOnlyView, _checkForEvents, _poseAsStandaloneApp, _multiTouchEnabled;
163 NSView* _view;
164
165 friend class GraphicsWindowCocoa;
166
167 };
168
169 NSOpenGLContext* getContext() { return _context; }
170 GraphicsWindowCocoaWindow* getWindow() { return _window; }
171 NSOpenGLPixelFormat* getPixelFormat() { return _pixelformat; }
172
173 virtual void setSyncToVBlank(bool f);
174
175 /** adapts a resize / move of the window, coords in global screen space */
176 void adaptResize(int x, int y, int w, int h);
177
178 bool isMultiTouchEnabled();
179 void setMultiTouchEnabled(bool b);
180
181 protected:
182
183 void init();
184
185 void transformMouseXY(float& x, float& y);
186 void setupNSWindow(NSWindow* win);
187
188
189 virtual ~GraphicsWindowCocoa();
190
191
192 bool _valid;
193 bool _initialized;
194 bool _realized;
195 bool _useWindowDecoration;
196
197
198
199 private:
200
201
202 bool _closeRequested, _checkForEvents,_ownsWindow;
203 MouseCursor _currentCursor;
204 GraphicsWindowCocoaWindow* _window;
205 GraphicsWindowCocoaGLView* _view;
206 NSOpenGLContext* _context;
207 NSOpenGLPixelFormat* _pixelformat;
208 bool _updateContext, _multiTouchEnabled;
209};
210
211}
212
213#endif
214#endif