| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 | (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "OgreGTKWindow.h" | 
|---|
| 31 | #include "OgreGTKGLSupport.h" | 
|---|
| 32 | #include "OgreRenderSystem.h" | 
|---|
| 33 | #include "OgreRoot.h" | 
|---|
| 34 | #include "OgreLogManager.h" | 
|---|
| 35 |  | 
|---|
| 36 | using namespace Ogre; | 
|---|
| 37 |  | 
|---|
| 38 | OGREWidget::OGREWidget(bool useDepthBuffer) :  | 
|---|
| 39 |         Gtk::GL::DrawingArea() | 
|---|
| 40 | { | 
|---|
| 41 |     Glib::RefPtr<Gdk::GL::Config> glconfig; | 
|---|
| 42 |  | 
|---|
| 43 |     Gdk::GL::ConfigMode mode = Gdk::GL::MODE_RGBA | Gdk::GL::MODE_DOUBLE; | 
|---|
| 44 |     if (useDepthBuffer) | 
|---|
| 45 |         mode |= Gdk::GL::MODE_DEPTH; | 
|---|
| 46 |  | 
|---|
| 47 |     glconfig = Gdk::GL::Config::create(mode); | 
|---|
| 48 |     if (glconfig.is_null()) | 
|---|
| 49 |     { | 
|---|
| 50 |         LogManager::getSingleton().logMessage("[gtk] GLCONFIG BLOWUP"); | 
|---|
| 51 |     } | 
|---|
| 52 |  | 
|---|
| 53 |     // Inherit GL context from Ogre main context | 
|---|
| 54 |     set_gl_capability(glconfig, GTKGLSupport::getSingleton().getMainContext()); | 
|---|
| 55 |  | 
|---|
| 56 |     add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | // OGREWidget TODO:  | 
|---|
| 60 | // - resize events et al | 
|---|
| 61 | // - Change aspect ratio | 
|---|
| 62 |  | 
|---|
| 63 | GTKWindow::GTKWindow(): | 
|---|
| 64 |         mGtkWindow(0) | 
|---|
| 65 | { | 
|---|
| 66 |         //kit = Gtk::Main::instance(); | 
|---|
| 67 |          | 
|---|
| 68 |         // Should  this move to GTKGLSupport? | 
|---|
| 69 |         // Gtk::GL::init(0, NULL); | 
|---|
| 70 |         // Already done in GTKGLSupport  | 
|---|
| 71 |  | 
|---|
| 72 |         mWidth = 0; | 
|---|
| 73 |         mHeight  = 0; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | GTKWindow::~GTKWindow() | 
|---|
| 77 | { | 
|---|
| 78 | } | 
|---|
| 79 | bool GTKWindow::pump_events() | 
|---|
| 80 | { | 
|---|
| 81 |     Gtk::Main *kit = Gtk::Main::instance(); | 
|---|
| 82 |     if (kit->events_pending()) | 
|---|
| 83 |     { | 
|---|
| 84 |         kit->iteration(false); | 
|---|
| 85 |         return true; | 
|---|
| 86 |     } | 
|---|
| 87 |     return false; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | OGREWidget* GTKWindow::get_ogre_widget() | 
|---|
| 91 | { | 
|---|
| 92 |     return ogre; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void GTKWindow::create(const String& name, unsigned int width, unsigned int height, unsigned int colourDepth,  | 
|---|
| 96 |                        bool fullScreen, int left, int top, bool depthBuffer,  | 
|---|
| 97 |                        void* miscParam, ...) | 
|---|
| 98 | { | 
|---|
| 99 |         mName = name; | 
|---|
| 100 |         mWidth = width; | 
|---|
| 101 |         mHeight = height; | 
|---|
| 102 |  | 
|---|
| 103 |         if(!miscParam) { | 
|---|
| 104 |                 mGtkWindow = new Gtk::Window(); | 
|---|
| 105 |                 mGtkWindow->set_title(mName); | 
|---|
| 106 |  | 
|---|
| 107 |                 if (fullScreen) | 
|---|
| 108 |                 { | 
|---|
| 109 |                         mGtkWindow->set_decorated(false); | 
|---|
| 110 |                         mGtkWindow->fullscreen(); | 
|---|
| 111 |                 } | 
|---|
| 112 |                 else | 
|---|
| 113 |                 { | 
|---|
| 114 |                         mGtkWindow->set_default_size(mWidth, mHeight); | 
|---|
| 115 |                         mGtkWindow->move(left, top);  | 
|---|
| 116 |                 } | 
|---|
| 117 |         } else { | 
|---|
| 118 |                 // If miscParam is not 0, a parent widget has been passed in, | 
|---|
| 119 |                 // we will handle this later on after the widget has been created. | 
|---|
| 120 |         } | 
|---|
| 121 |  | 
|---|
| 122 |         ogre = Gtk::manage(new OGREWidget(depthBuffer)); | 
|---|
| 123 |         ogre->set_size_request(width, height); | 
|---|
| 124 |  | 
|---|
| 125 |         ogre->signal_delete_event().connect(SigC::slot(*this, >KWindow::on_delete_event)); | 
|---|
| 126 |         ogre->signal_expose_event().connect(SigC::slot(*this, >KWindow::on_expose_event)); | 
|---|
| 127 |  | 
|---|
| 128 |         if(mGtkWindow) { | 
|---|
| 129 |                 mGtkWindow->add(*ogre); | 
|---|
| 130 |                 mGtkWindow->show_all(); | 
|---|
| 131 |         } | 
|---|
| 132 |         if(miscParam) { | 
|---|
| 133 |                 // Attach it! | 
|---|
| 134 |                 // Note that the parent widget *must* be visible already at this point, | 
|---|
| 135 |                 // or the widget won't get realized in time for the GLinit that follows | 
|---|
| 136 |                 // this call. This is usually the case for Glade generated windows, anyway. | 
|---|
| 137 |                 reinterpret_cast<Gtk::Container*>(miscParam)->add(*ogre); | 
|---|
| 138 |                 ogre->show(); | 
|---|
| 139 |         } | 
|---|
| 140 |         //ogre->realize(); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | void GTKWindow::destroy() | 
|---|
| 144 | { | 
|---|
| 145 |         Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() ); | 
|---|
| 146 |         // We could detach the widget from its parent and destroy it here too, | 
|---|
| 147 |         // but then again, it is managed so we rely on GTK to destroy it. | 
|---|
| 148 |         delete mGtkWindow; | 
|---|
| 149 |         mGtkWindow = 0; | 
|---|
| 150 |  | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | bool GTKWindow::isActive() const | 
|---|
| 154 | { | 
|---|
| 155 |     return ogre->is_realized(); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | bool GTKWindow::isClosed() const | 
|---|
| 159 | { | 
|---|
| 160 |     return ogre->is_visible(); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void GTKWindow::reposition(int left, int top) | 
|---|
| 164 | { | 
|---|
| 165 |         if(mGtkWindow) | 
|---|
| 166 |                 mGtkWindow->move(left, top); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void GTKWindow::resize(unsigned int width, unsigned int height) | 
|---|
| 170 | { | 
|---|
| 171 |         if(mGtkWindow) | 
|---|
| 172 |                 mGtkWindow->resize(width, height); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | void GTKWindow::swapBuffers(bool waitForVSync) | 
|---|
| 176 | { | 
|---|
| 177 |         Glib::RefPtr<Gdk::GL::Window> glwindow = ogre->get_gl_window(); | 
|---|
| 178 |         glwindow->swap_buffers(); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | void GTKWindow::writeContentsToFile(const String& filename) | 
|---|
| 182 | { | 
|---|
| 183 |         // XXX impl me | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | void GTKWindow::getCustomAttribute( const String& name, void* pData ) | 
|---|
| 187 | { | 
|---|
| 188 |         if( name == "GTKMMWINDOW" ) | 
|---|
| 189 |         { | 
|---|
| 190 |                 Gtk::Window **win = static_cast<Gtk::Window **>(pData); | 
|---|
| 191 |                 // Oh, the burdens of multiple inheritance | 
|---|
| 192 |                 *win = mGtkWindow; | 
|---|
| 193 |                 return; | 
|---|
| 194 |         } | 
|---|
| 195 |         else if( name == "GTKGLMMWIDGET" ) | 
|---|
| 196 |         { | 
|---|
| 197 |                 Gtk::GL::DrawingArea **widget = static_cast<Gtk::GL::DrawingArea **>(pData); | 
|---|
| 198 |                 *widget = ogre; | 
|---|
| 199 |                 return; | 
|---|
| 200 |         } | 
|---|
| 201 |         else if( name == "isTexture" ) | 
|---|
| 202 |         { | 
|---|
| 203 |                 bool *b = reinterpret_cast< bool * >( pData ); | 
|---|
| 204 |                 *b = false; | 
|---|
| 205 |                 return; | 
|---|
| 206 |         } | 
|---|
| 207 |         RenderWindow::getCustomAttribute(name, pData); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | bool GTKWindow::on_delete_event(GdkEventAny* event) | 
|---|
| 212 | { | 
|---|
| 213 |     Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() ); | 
|---|
| 214 |     return false; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | bool GTKWindow::on_expose_event(GdkEventExpose* event) | 
|---|
| 218 | { | 
|---|
| 219 |     // Window exposed, update interior | 
|---|
| 220 |     //std::cout << "Window exposed, update interior" << std::endl; | 
|---|
| 221 |     // TODO: time between events, as expose events can be sent crazily fast | 
|---|
| 222 |     update(); | 
|---|
| 223 |     return false; | 
|---|
| 224 | } | 
|---|