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#ifndef OSGVIEWER_GRAPHICWINDOW
15#define OSGVIEWER_GRAPHICWINDOW 1
17#include <osg/GraphicsContext>
20#include <osgGA/EventQueue>
21#include <osgGA/GUIActionAdapter>
23#include <osgViewer/Export>
28 typedef void (* CGraphicsWindowFunction) (void);
36/** Base class for providing Windowing API agnostic access to creating and managing graphics window and events.
37 * Note, the GraphicsWindow is subclassed from osg::GraphicsContext, and to provide an implementation you'll need to implement its
38 * range of pure virtual functions, you'll find these all have naming convention methodNameImplementation(..).
39 * GraphicsWindow adds the event queue on top of the GraphicsContext, thereby adding a mechanism for adapting Windowing events
40 * as well as basics graphics context work, you should wire up custom GraphicsWindowImplementation to push their events through
41 * into the EventQueue. */
42class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgGA::GUIActionAdapter
46 GraphicsWindow() { _eventQueue = new osgGA::EventQueue; _eventQueue->setGraphicsContext(this); }
48 virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindow*>(object)!=0; }
49 virtual const char* libraryName() const { return "osgViewer"; }
50 virtual const char* className() const { return "GraphicsWindow"; }
52 void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
53 osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
54 const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
56 /** Check events, return true if events have been received.*/
57 virtual bool checkEvents() { return !(_eventQueue->empty()); }
59 /** Set the window's position and size.*/
60 void setWindowRectangle(int x, int y, int width, int height)
62 if (setWindowRectangleImplementation(x ,y ,width, height) && _traits.valid())
64 resized(x,y,width,height);
68 /** implementation of setWindowRectangle, should be implemented by derived classes */
69 virtual bool setWindowRectangleImplementation(int /*x*/, int /*y*/, int /*width*/, int /*height*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowRectangleImplementation(..) not implemented."<<std::endl; return false; }
71 /** Get the window's position and size.*/
72 virtual void getWindowRectangle(int& x, int& y, int& width, int& height) { if (_traits.valid()) { x = _traits->x; y = _traits->y; width = _traits->width; height = _traits->height; } }
74 /** Set Window decoration.*/
75 void setWindowDecoration(bool flag)
77 if (setWindowDecorationImplementation(flag) && _traits.valid())
79 _traits->windowDecoration = flag;
83 /** implementation of setWindowDecoration, should be implemented by derived classes */
84 virtual bool setWindowDecorationImplementation(bool /*flag*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowDecorationImplementation(..) not implemented."<<std::endl; return false; }
87 /** Set Window decoration.*/
88 virtual bool getWindowDecoration() const { return _traits.valid() ? _traits->windowDecoration : false; }
91 virtual void grabFocus() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocus(..) not implemented."<<std::endl; }
93 /** Get focus on if the pointer is in this window.*/
94 virtual void grabFocusIfPointerInWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::grabFocusIfPointerInWindow(..) not implemented."<<std::endl; }
96 /** Raise the window to the top.*/
97 virtual void raiseWindow() { osg::notify(osg::NOTICE)<<"GraphicsWindow::raiseWindow(..) not implemented."<<std::endl; }
99 /** Mouse cursor types, the same ones already present with ancient glut ... */
126 /** Set the name of the window */
127 virtual void setWindowName(const std::string& /*name*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowName(..) not implemented."<<std::endl; }
129 /** Return the name of the window */
130 virtual std::string getWindowName() { return _traits.valid() ? _traits->windowName : ""; }
132 /** Switch on/off the cursor.*/
133 virtual void useCursor(bool cursorOn) { setCursor(cursorOn ? InheritCursor : NoCursor); }
135 /** Set mouse cursor to a specific shape.*/
136 virtual void setCursor(MouseCursor /*mouseCursor*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setCursor(..) not implemented."<<std::endl; }
138 /** Create a new mouse cursor from the usual bitmap data.*/
139 //virtual MouseCursor createCursor(const char *data, const char *mask, unsigned w, unsigned h, unsigned hotx, unsigned hoty) { osg::notify(osg::NOTICE)<<"GraphicsWindow::createCursor(..) not implemented."<<std::endl; }
141 /** Set sync-to-vblank. */
142 virtual void setSyncToVBlank(bool on)
144 osg::notify(osg::NOTICE) << "GraphicsWindow::setSyncToVBlank(" << on << ") not implemented." << std::endl;
147 bool getSyncToVBlank() const { return _traits.valid() ? _traits->vsync : true; }
149 /** Set swap group. */
150 virtual void setSwapGroup(bool on, GLuint group, GLuint barrier)
152 osg::notify(osg::NOTICE) << "GraphicsWindow::setSwapGroup(" << on << " " << group << " " << barrier << ") not implemented." << std::endl;
155 void getSwapGroup(bool& on, GLuint& group, GLuint& barrier) const { on = _traits->swapGroupEnabled; group = _traits->swapGroup; barrier = _traits->swapBarrier; }
159 /** Return whether a valid and usable GraphicsContext has been created.*/
160 virtual bool valid() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::valid() not implemented."<<std::endl; return false; }
162 /** Realize the GraphicsContext implementation,
163 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
164 virtual bool realizeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::realizeImplementation() not implemented."<<std::endl; return false; }
166 /** Return true if the graphics context has been realized, and is ready to use, implementation.
167 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
168 virtual bool isRealizedImplementation() const { osg::notify(osg::NOTICE)<<"GraphicsWindow::isRealizedImplementation() not implemented."<<std::endl; return false; }
170 /** Close the graphics context implementation.
171 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
172 virtual void closeImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::closeImplementation() not implemented."<<std::endl; }
174 /** Make this graphics context current implementation.
175 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
176 virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false;}
178 /** Make this graphics context current with specified read context implementation.
179 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
180 virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false;}
182 /** Release the graphics context.*/
183 virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; }
185 /** Pure virtual, Bind the graphics context to associated texture implementation.
186 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
187 virtual void bindPBufferToTextureImplementation(GLenum /*buffer*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::bindPBufferToTextureImplementation(..) not implemented."<<std::endl; }
189 /** Swap the front and back buffers implementation.
190 * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
191 virtual void swapBuffersImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow:: swapBuffersImplementation() not implemented."<<std::endl; }
195 typedef std::list<osgViewer::View*> Views;
196 /** Returns the list of views (osgViewer::View) attached to this GraphicsWindow.
197 * Internally, the method walks through all the cameras and collects all the views attached to the cameras.*/
198 void getViews(Views& views);
200 // Override from GUIActionAdapter
201 virtual void requestRedraw();
203 // Override from GUIActionAdapter
204 virtual void requestContinuousUpdate(bool /*needed*/=true) {}
206 // Override from GUIActionAdapter
207 virtual void requestWarpPointer(float /*x*/,float /*y*/) {}
212 osg::ref_ptr<osgGA::EventQueue> _eventQueue;
217class GraphicsWindowEmbedded : public GraphicsWindow
221 GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0)
229 GraphicsWindowEmbedded(int x, int y, int width, int height)
231 _traits = new GraphicsContext::Traits;
234 _traits->width = width;
235 _traits->height = height;
240 virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindowEmbedded*>(object)!=0; }
241 virtual const char* libraryName() const { return "osgViewer"; }
242 virtual const char* className() const { return "GraphicsWindowEmbedded"; }
248 setState( new osg::State );
249 getState()->setGraphicsContext(this);
251 if (_traits.valid() && _traits->sharedContext.valid())
253 getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
254 incrementContextIDUsageCount( getState()->getContextID() );
258 getState()->setContextID( osg::GraphicsContext::createNewContextID() );
263 // dummy implementations, assume that graphics context is *always* current and valid.
264 virtual bool valid() const { return true; }
265 virtual bool realizeImplementation() { return true; }
266 virtual bool isRealizedImplementation() const { return true; }
267 virtual void closeImplementation() {}
268 virtual bool makeCurrentImplementation() { return true; }
269 virtual bool releaseContextImplementation() { return true; }
270 virtual void swapBuffersImplementation() {}
271 virtual void grabFocus() {}
272 virtual void grabFocusIfPointerInWindow() {}
273 virtual void raiseWindow() {}
277struct GraphicsWindowFunctionProxy
279 GraphicsWindowFunctionProxy(CGraphicsWindowFunction function) { (function)(); }
282#define USE_GRAPICSWINDOW_IMPLEMENTATION(ext) \
283 extern "C" void graphicswindow_##ext(void); \
284 static osgViewer::GraphicsWindowFunctionProxy graphicswindowproxy_##ext(graphicswindow_##ext);
287 #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Win32)
288#elif defined(__APPLE__)
289 #if defined(OSG_WINDOWING_SYSTEM_CARBON)
290 #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Carbon)
292 #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(Cocoa)
295 #define USE_GRAPHICSWINDOW() USE_GRAPICSWINDOW_IMPLEMENTATION(X11)