Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIWindowManager.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIWindowManager.cpp
00003         created:        21/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements the WindowManager class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIWindowManager.h"
00027 #include "CEGUIWindowFactoryManager.h"
00028 #include "CEGUIWindowFactory.h"
00029 #include "CEGUIWindow.h"
00030 #include "CEGUIExceptions.h"
00031 #include "CEGUIGUILayout_xmlHandler.h"
00032 
00033 #include <xercesc/sax2/SAX2XMLReader.hpp>
00034 #include <xercesc/sax2/XMLReaderFactory.hpp>
00035 #include "xercesc/framework/MemBufInputSource.hpp"
00036 
00037 
00038 
00039 // Start of CEGUI namespace section
00040 namespace CEGUI
00041 {
00042 /*************************************************************************
00043         Static Data Definitions
00044 *************************************************************************/
00045 // singleton instance pointer
00046 template<> WindowManager* Singleton<WindowManager>::ms_Singleton        = NULL;
00047 
00048 
00049 /*************************************************************************
00050         Definition of constant data for WindowManager
00051 *************************************************************************/
00052 // Declared in WindowManager
00053 const char      WindowManager::GUILayoutSchemaName[]    = "GUILayout.xsd";
00054 
00055 
00056 /*************************************************************************
00057         Destructor
00058 *************************************************************************/
00059 WindowManager::~WindowManager(void)
00060 {
00061         destroyAllWindows();
00062 
00063         Logger::getSingleton().logEvent((utf8*)"CEGUI::WindowManager singleton destroyed");
00064 }
00065 
00066 
00067 /*************************************************************************
00068         Create a new window of the specified type
00069 *************************************************************************/
00070 Window* WindowManager::createWindow(const String& type, const String& name)
00071 {
00072         if (isWindowPresent(name))
00073         {
00074                 throw AlreadyExistsException("WindowManager::createWindow - A Window object with the name '" + name +"' already exists within the system.");
00075         }
00076 
00077         WindowFactory* factory = WindowFactoryManager::getSingleton().getFactory(type);
00078 
00079         Window* newWindow = factory->createWindow(name);
00080         d_windowRegistry[name] = newWindow;
00081 
00082         Logger::getSingleton().logEvent((utf8*)"Window '" + name +"' of type '" + type + "' has been created.", Informative);
00083 
00084         return newWindow;
00085 }
00086 
00087 
00088 /*************************************************************************
00089         Destroy the given window by pointer
00090 *************************************************************************/
00091 void WindowManager::destroyWindow(Window* window)
00092 {
00093         if (window != NULL)
00094         {
00095                 // this is done because the name is used for the log after the window is destroyed,
00096                 // if we just did getName() we would get a const ref to the Window's internal name
00097                 // string which is destroyed along with the window so wouldn't exist when the log tried
00098                 // to use it (as I soon discovered).
00099                 String name = window->getName();
00100 
00101                 destroyWindow(name);
00102         }
00103 
00104 }
00105 
00106 
00107 /*************************************************************************
00108         Destroy the given window by name
00109 *************************************************************************/
00110 void WindowManager::destroyWindow(const String& window)
00111 {
00112         WindowRegistry::iterator wndpos = d_windowRegistry.find(window);
00113 
00114         if (wndpos != d_windowRegistry.end())
00115         {
00116                 WindowFactory* factory = WindowFactoryManager::getSingleton().getFactory(wndpos->second->getType());
00117                 factory->destroyWindow(wndpos->second);
00118 
00119                 // notify system object of the window destruction
00120                 System::getSingleton().notifyWindowDestroyed(wndpos->second);
00121 
00122                 // remove entry from the WindowRegistry.
00123                 d_windowRegistry.erase(wndpos);
00124 
00125                 Logger::getSingleton().logEvent((utf8*)"Window '" + window + "' has been destroyed.", Informative);
00126         }
00127 
00128 }
00129 
00130 
00131 /*************************************************************************
00132         Return a pointer to the named window
00133 *************************************************************************/
00134 Window* WindowManager::getWindow(const String& name) const
00135 {
00136         WindowRegistry::const_iterator pos = d_windowRegistry.find(name);
00137 
00138         if (pos == d_windowRegistry.end())
00139         {
00140                 throw UnknownObjectException("WindowManager::getWindow - A Window object with the name '" + name +"' does not exist within the system");
00141         }
00142 
00143         return pos->second;
00144 }
00145 
00146 
00147 /*************************************************************************
00148         Return true if a window with the given name is present
00149 *************************************************************************/
00150 bool WindowManager::isWindowPresent(const String& name) const
00151 {
00152         return (d_windowRegistry.find(name) != d_windowRegistry.end());
00153 }
00154 
00155 
00156 /*************************************************************************
00157         Destroy all Window objects
00158 *************************************************************************/
00159 void WindowManager::destroyAllWindows(void)
00160 {
00161         String window_name;
00162         while (!d_windowRegistry.empty())
00163         {
00164                 window_name = d_windowRegistry.begin()->first;
00165                 destroyWindow(window_name);
00166         }
00167 
00168 }
00169 
00170 
00171 /*************************************************************************
00172         Creates a set of windows (a Gui layout) from the information in the
00173         specified XML file.     
00174 *************************************************************************/
00175 Window* WindowManager::loadWindowLayout(const String& filename, const String& name_prefix, const String& resourceGroup, PropertyCallback* callback, void* userdata)
00176 {
00177         XERCES_CPP_NAMESPACE_USE
00178 
00179         if (filename.empty() || (filename == (utf8*)""))
00180         {
00181                 throw InvalidRequestException((utf8*)"WindowManager::loadWindowLayout - Filename supplied for gui-layout loading must be valid.");
00182         }
00183 
00184         // log the fact we are about to load a layout
00185         Logger::getSingleton().logEvent((utf8*)"---- Beginning loading of GUI layout from '" + filename + "' ----", Informative);
00186 
00187         SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00188 
00189         // set basic settings we want from parser
00190         parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
00191         parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00192         parser->setFeature(XMLUni::fgXercesSchema, true);
00193         parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
00194 
00195 //    InputSourceContainer layoutSchemaData;
00196 //    System::getSingleton().getResourceProvider()->loadInputSourceContainer(GUILayoutSchemaName, layoutSchemaData);
00197 //    parser->loadGrammar(*(layoutSchemaData.getDataPtr()), Grammar::SchemaGrammarType, true);
00198 
00199     RawDataContainer rawSchemaData;
00200     System::getSingleton().getResourceProvider()->loadRawDataContainer(GUILayoutSchemaName, rawSchemaData, resourceGroup);
00201     MemBufInputSource  layoutSchemaData(rawSchemaData.getDataPtr(), rawSchemaData.getSize(), GUILayoutSchemaName, false);
00202     parser->loadGrammar(layoutSchemaData, Grammar::SchemaGrammarType, true);
00203 
00204     // enable grammar reuse
00205     parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
00206 
00207         // setup schema for gui-layout data
00208         XMLCh* pval = XMLString::transcode(GUILayoutSchemaName);
00209         parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, pval);
00210         XMLString::release(&pval);
00211 
00212         // setup handler object
00213         GUILayout_xmlHandler handler(name_prefix, callback, userdata);
00214         parser->setContentHandler(&handler);
00215         parser->setErrorHandler(&handler);
00216 
00217 //    InputSourceContainer layoutData;
00218 //    System::getSingleton().getResourceProvider()->loadInputSourceContainer(filename, layoutData);
00219 
00220     RawDataContainer rawXMLData;
00221     System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, rawXMLData, resourceGroup);
00222     MemBufInputSource  layoutData(rawXMLData.getDataPtr(), rawXMLData.getSize(), filename.c_str(), false);
00223 
00224         // do parse (which uses handler to create actual data)
00225         try
00226         {
00227 //        parser->parse(*(layoutData.getDataPtr()));
00228         parser->parse(layoutData);
00229 
00230                 // log the completion of loading
00231                 Logger::getSingleton().logEvent((utf8*)"---- Successfully completed loading of GUI layout from '" + filename + "' ----", Standard);
00232         }
00233         catch(const XMLException& exc)
00234         {
00235                 if (exc.getCode() != XMLExcepts::NoError)
00236                 {
00237                         delete parser;
00238 
00239                         char* excmsg = XMLString::transcode(exc.getMessage());
00240                         String message((utf8*)"WindowManager::loadWindowLayout - An error occurred while parsing gui-layout file '" + filename + "'.  Additional information: ");
00241                         message += (utf8*)excmsg;
00242                         XMLString::release(&excmsg);
00243 
00244                         throw FileIOException(message);
00245                 }
00246 
00247         }
00248         catch(const SAXParseException& exc)
00249         {
00250                 delete parser;
00251 
00252                 char* excmsg = XMLString::transcode(exc.getMessage());
00253                 String message((utf8*)"WindowManager::loadWindowLayout - An error occurred while parsing gui-layout file '" + filename + "'.  Additional information: ");
00254                 message += (utf8*)excmsg;
00255                 XMLString::release(&excmsg);
00256 
00257                 throw FileIOException(message);
00258         }
00259         catch(const CEGUI::Exception&)
00260         {
00261                 delete parser;
00262                 throw;
00263         }
00264         catch(...)
00265         {
00266                 delete parser;
00267 
00268                 throw FileIOException((utf8*)"WindowManager::loadWindowLayout - An unexpected error occurred while parsing gui-layout file '" + filename + "'.");
00269         }
00270 
00271         // cleanup
00272         delete parser;
00273 
00274         return handler.getLayoutRootWindow();
00275 }
00276 
00277 
00278 WindowManager& WindowManager::getSingleton(void)
00279 {
00280         return Singleton<WindowManager>::getSingleton();
00281 }
00282 
00283 
00284 WindowManager* WindowManager::getSingletonPtr(void)
00285 {
00286         return Singleton<WindowManager>::getSingletonPtr();
00287 }
00288 
00289 
00290 /*************************************************************************
00291         Return a WindowManager::WindowIterator object to iterate over the
00292         currently defined Windows.
00293 *************************************************************************/
00294 WindowManager::WindowIterator WindowManager::getIterator(void) const
00295 {
00296         return WindowIterator(d_windowRegistry.begin(), d_windowRegistry.end());
00297 }
00298 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:08 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1