From 08a793eb8778fe0ca3bb2832f60930340be079e6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 10 Jun 2007 19:53:18 +0000 Subject: [PATCH] From Stephan Huber and Robert Osfield, Stephan: "attached you'll find some modifications to the GraphicsWindow-class and their platform-dependant implementations. The problem: setWindowRectangle and setWindowDecoration do not update the traits-object, so, if you call setWindowRectangle on a not-realized-window it will open with another size when realized later. getWindowRectangle reports possible wrong sizes if setWindowRectangle called before. My solution: split the implementation in two parts: GraphicsWindow::setWindowRectangle will update its traits-object and call afterwards the virtual method setWindowRectangleImplementation (which is implemented by the derived platformspecific classess). For setWindowDecoration I am useing a similar mechanism. I hope you'll find the submission useful, the Win32 and X11 changes are not tested but should work." Changes to this made by Robert are call of resized in setWindowRectangle instead of setting of Traits, and use of a bool return type. --- include/osgViewer/GraphicsWindow | 23 +++++++++++++-- .../osgViewer/api/Carbon/GraphicsWindowCarbon | 4 +-- .../osgViewer/api/Win32/GraphicsWindowWin32 | 4 +-- include/osgViewer/api/X11/GraphicsWindowX11 | 4 +-- src/osgViewer/GraphicsWindowCarbon.cpp | 29 ++++++++++++++----- src/osgViewer/GraphicsWindowWin32.cpp | 23 +++++++++++---- src/osgViewer/GraphicsWindowX11.cpp | 20 ++++++++++--- 7 files changed, 82 insertions(+), 25 deletions(-) diff --git a/include/osgViewer/GraphicsWindow b/include/osgViewer/GraphicsWindow index 85034a27e..e2c0c7e83 100644 --- a/include/osgViewer/GraphicsWindow +++ b/include/osgViewer/GraphicsWindow @@ -49,13 +49,32 @@ class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgG virtual void checkEvents() {} /** Set the window's position and size.*/ - virtual void setWindowRectangle(int /*x*/, int /*y*/, int /*width*/, int /*height*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowRectangle(..) not implemented."<x; y = _traits->y; width = _traits->width; height = _traits->height; } } /** Set Window decoration.*/ - virtual void setWindowDecoration(bool /*flag*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowDecoration(..) not implemented."<windowDecoration = flag; + } + } + + /** implementation of setWindowDecoration, should be implemented by derived classes */ + virtual bool setWindowDecorationImplementation(bool /*flag*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::setWindowDecorationImplementation(..) not implemented."<windowDecoration : false; } diff --git a/include/osgViewer/api/Carbon/GraphicsWindowCarbon b/include/osgViewer/api/Carbon/GraphicsWindowCarbon index 092647ea7..83551ec4a 100644 --- a/include/osgViewer/api/Carbon/GraphicsWindowCarbon +++ b/include/osgViewer/api/Carbon/GraphicsWindowCarbon @@ -82,10 +82,10 @@ class GraphicsWindowCarbon : public osgViewer::GraphicsWindow virtual void checkEvents(); /** Set the window's position and size.*/ - virtual void setWindowRectangle(int x, int y, int width, int height); + virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); /** Set Window decoration.*/ - virtual void setWindowDecoration(bool flag); + virtual bool setWindowDecorationImplementation(bool flag); /** Get focus.*/ virtual void grabFocus(); diff --git a/include/osgViewer/api/Win32/GraphicsWindowWin32 b/include/osgViewer/api/Win32/GraphicsWindowWin32 index 843c2e919..ee96e1444 100644 --- a/include/osgViewer/api/Win32/GraphicsWindowWin32 +++ b/include/osgViewer/api/Win32/GraphicsWindowWin32 @@ -61,10 +61,10 @@ class GraphicsWindowWin32 : public osgViewer::GraphicsWindow virtual void checkEvents(); /** Set the window's position and size.*/ - virtual void setWindowRectangle(int x, int y, int width, int height); + virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); /** Set Window decoration.*/ - virtual void setWindowDecoration(bool flag); + virtual bool setWindowDecorationImplementation(bool flag); /** Get focus.*/ virtual void grabFocus(); diff --git a/include/osgViewer/api/X11/GraphicsWindowX11 b/include/osgViewer/api/X11/GraphicsWindowX11 index a13ba9b57..f863ac0ea 100644 --- a/include/osgViewer/api/X11/GraphicsWindowX11 +++ b/include/osgViewer/api/X11/GraphicsWindowX11 @@ -92,7 +92,7 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow virtual void checkEvents(); /** Set Window decoration.*/ - virtual void setWindowDecoration(bool flag); + virtual bool setWindowDecorationImplementation(bool flag); /** Get focus.*/ virtual void grabFocus(); @@ -104,7 +104,7 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow virtual void requestWarpPointer(float x,float y); /** Set the window's position and size.*/ - virtual void setWindowRectangle(int x, int y, int width, int height); + virtual bool setWindowRectangleImplementation(int x, int y, int width, int height); /** Set mouse cursor to a specific shape.*/ virtual void setCursor(MouseCursor cursor); diff --git a/src/osgViewer/GraphicsWindowCarbon.cpp b/src/osgViewer/GraphicsWindowCarbon.cpp index c7891d5fd..70fed84bf 100644 --- a/src/osgViewer/GraphicsWindowCarbon.cpp +++ b/src/osgViewer/GraphicsWindowCarbon.cpp @@ -611,25 +611,34 @@ void GraphicsWindowCarbon::init() _initialized = true; } -void GraphicsWindowCarbon::setWindowDecoration(bool flag) +bool GraphicsWindowCarbon::setWindowDecorationImplementation(bool flag) { _useWindowDecoration = flag; - if (_realized) { + + if (_realized) + { OSErr err = noErr; Rect bounds; GetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); - if (_useWindowDecoration) { + if (_useWindowDecoration) + { err = ChangeWindowAttributes(getNativeWindowRef(), kWindowStandardDocumentAttributes, kWindowNoTitleBarAttribute | kWindowNoShadowAttribute); SetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); - } else { + } + else + { err = ChangeWindowAttributes(getNativeWindowRef(), kWindowNoTitleBarAttribute | kWindowNoShadowAttribute, kWindowStandardDocumentAttributes); } - if (err != noErr) { + if (err != noErr) + { osg::notify(osg::WARN) << "GraphicsWindowCarbon::setWindowDecoration failed with " << err << std::endl; + return false; } } + + return true; } WindowAttributes GraphicsWindowCarbon::computeWindowAttributes(bool useWindowDecoration, bool supportsResize) { @@ -1180,13 +1189,13 @@ void GraphicsWindowCarbon::checkEvents() } -void GraphicsWindowCarbon::setWindowRectangle(int x, int y, int width, int height) +bool GraphicsWindowCarbon::setWindowRectangleImplementation(int x, int y, int width, int height) { Rect bounds = {y, x, y + height, x + width}; SetWindowBounds(getNativeWindowRef(), kWindowContentRgn, &bounds); aglUpdateContext(_context); MenubarController::instance()->update(); - + return true; } void GraphicsWindowCarbon::grabFocus() @@ -1234,4 +1243,10 @@ struct RegisterWindowingSystemInterfaceProxy RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy; +// declare C entry point for static compilation. +extern "C" void graphicswindow_Carbon(void) +{ + osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::OSXCarbonWindowingSystemInterface()); +} + #endif diff --git a/src/osgViewer/GraphicsWindowWin32.cpp b/src/osgViewer/GraphicsWindowWin32.cpp index d758f7baa..b5eac814f 100644 --- a/src/osgViewer/GraphicsWindowWin32.cpp +++ b/src/osgViewer/GraphicsWindowWin32.cpp @@ -1512,7 +1512,7 @@ bool GraphicsWindowWin32::setPixelFormat() return true; } -void GraphicsWindowWin32::setWindowDecoration( bool decorated ) +bool GraphicsWindowWin32::setWindowDecorationImplementation( bool decorated ) { unsigned int windowStyle; unsigned int extendedStyle; @@ -1527,7 +1527,7 @@ void GraphicsWindowWin32::setWindowDecoration( bool decorated ) if (!determineWindowPositionAndStyle(decorated, x, y, w, h, windowStyle, extendedStyle)) { reportErrorForScreen("GraphicsWindowWin32::setWindowDecoration() - Unable to determine the window position and style", _traits->screenNum, 0); - return; + return false; } // @@ -1540,7 +1540,7 @@ void GraphicsWindowWin32::setWindowDecoration( bool decorated ) if (result==0 && error) { reportErrorForScreen("GraphicsWindowWin32::setWindowDecoration() - Unable to set window style", _traits->screenNum, error); - return; + return false; } // @@ -1553,7 +1553,7 @@ void GraphicsWindowWin32::setWindowDecoration( bool decorated ) if (result==0 && error) { reportErrorForScreen("GraphicsWindowWin32::setWindowDecoration() - Unable to set window extented style", _traits->screenNum, error); - return; + return false; } // @@ -1563,7 +1563,7 @@ void GraphicsWindowWin32::setWindowDecoration( bool decorated ) if (!::SetWindowPos(_hwnd, HWND_TOP, x, y, w, h, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_SHOWWINDOW)) { reportErrorForScreen("GraphicsWindowWin32::setWindowDecoration() - Unable to set new window position and size", _traits->screenNum, ::GetLastError()); - return; + return false; } // @@ -1574,6 +1574,8 @@ void GraphicsWindowWin32::setWindowDecoration( bool decorated ) { ::InvalidateRect(NULL, NULL, TRUE); } + + return true; } bool GraphicsWindowWin32::realizeImplementation() @@ -1760,12 +1762,14 @@ void GraphicsWindowWin32::requestWarpPointer( float x, float y ) getEventQueue()->mouseWarped(x,y); } -void GraphicsWindowWin32::setWindowRectangle(int x, int y, int width, int height) +bool GraphicsWindowWin32::setWindowRectangleImplementation(int x, int y, int width, int height) { if (!::SetWindowPos(_hwnd, HWND_TOP, x, y, width, height, SWP_SHOWWINDOW | SWP_FRAMECHANGED)) { reportErrorForScreen("GraphicsWindowWin32::setWindowRectangle() - Unable to set new window position and size", _traits->screenNum, ::GetLastError()); + return false; } + return true; } void GraphicsWindowWin32::useCursor( bool cursorOn ) @@ -2255,3 +2259,10 @@ struct RegisterWindowingSystemInterfaceProxy static RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy; }; // namespace OsgViewer + + +// declare C entry point for static compilation. +extern "C" void graphicswindow_Win32(void) +{ + osg::GraphicsContext::setWindowingSystemInterface(osgViewer::Win32WindowingSystem::getInterface()); +} diff --git a/src/osgViewer/GraphicsWindowX11.cpp b/src/osgViewer/GraphicsWindowX11.cpp index 3be3680c2..91db38d5d 100644 --- a/src/osgViewer/GraphicsWindowX11.cpp +++ b/src/osgViewer/GraphicsWindowX11.cpp @@ -292,7 +292,7 @@ bool GraphicsWindowX11::createVisualInfo() return _visualInfo != 0; } -void GraphicsWindowX11::setWindowDecoration(bool flag) +bool GraphicsWindowX11::setWindowDecorationImplementation(bool flag) { Display* display = getDisplayToUse(); @@ -361,15 +361,22 @@ void GraphicsWindowX11::setWindowDecoration(bool flag) // we don't add this sleep then any X11 calls right afterwards can produce // X11 errors. usleep(100000); + + return true; } else + { osg::notify(osg::NOTICE)<<"Error: GraphicsWindowX11::setBorder(" << flag << ") - couldn't change decorations." << std::endl; + return false; + } + + } -void GraphicsWindowX11::setWindowRectangle(int x, int y, int width, int height) +bool GraphicsWindowX11::setWindowRectangleImplementation(int x, int y, int width, int height) { - if (!_realized) return; + if (!_realized) return false; Display* display = getDisplayToUse(); @@ -382,6 +389,8 @@ void GraphicsWindowX11::setWindowRectangle(int x, int y, int width, int height) // we don't add this sleep then any X11 calls right afterwards can produce // X11 errors. usleep(100000); + + return true; } void GraphicsWindowX11::setCursor(MouseCursor mouseCursor) @@ -1291,4 +1300,7 @@ struct RegisterWindowingSystemInterfaceProxy RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy; // declare C entry point for static compilation. -extern "C" void graphicswindow_X11(void) {} +extern "C" void graphicswindow_X11(void) +{ + osg::GraphicsContext::setWindowingSystemInterface(new X11WindowingSystemInterface); +}