00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00040 namespace CEGUI
00041 {
00042
00043
00044
00045
00046 template<> WindowManager* Singleton<WindowManager>::ms_Singleton = NULL;
00047
00048
00049
00050
00051
00052
00053 const char WindowManager::GUILayoutSchemaName[] = "GUILayout.xsd";
00054
00055
00056
00057
00058
00059 WindowManager::~WindowManager(void)
00060 {
00061 destroyAllWindows();
00062
00063 Logger::getSingleton().logEvent((utf8*)"CEGUI::WindowManager singleton destroyed");
00064 }
00065
00066
00067
00068
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
00090
00091 void WindowManager::destroyWindow(Window* window)
00092 {
00093 if (window != NULL)
00094 {
00095
00096
00097
00098
00099 String name = window->getName();
00100
00101 destroyWindow(name);
00102 }
00103
00104 }
00105
00106
00107
00108
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
00120 System::getSingleton().notifyWindowDestroyed(wndpos->second);
00121
00122
00123 d_windowRegistry.erase(wndpos);
00124
00125 Logger::getSingleton().logEvent((utf8*)"Window '" + window + "' has been destroyed.", Informative);
00126 }
00127
00128 }
00129
00130
00131
00132
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
00149
00150 bool WindowManager::isWindowPresent(const String& name) const
00151 {
00152 return (d_windowRegistry.find(name) != d_windowRegistry.end());
00153 }
00154
00155
00156
00157
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
00173
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
00185 Logger::getSingleton().logEvent((utf8*)"---- Beginning loading of GUI layout from '" + filename + "' ----", Informative);
00186
00187 SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00188
00189
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
00196
00197
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
00205 parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
00206
00207
00208 XMLCh* pval = XMLString::transcode(GUILayoutSchemaName);
00209 parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, pval);
00210 XMLString::release(&pval);
00211
00212
00213 GUILayout_xmlHandler handler(name_prefix, callback, userdata);
00214 parser->setContentHandler(&handler);
00215 parser->setErrorHandler(&handler);
00216
00217
00218
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
00225 try
00226 {
00227
00228 parser->parse(layoutData);
00229
00230
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
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
00292
00293
00294 WindowManager::WindowIterator WindowManager::getIterator(void) const
00295 {
00296 return WindowIterator(d_windowRegistry.begin(), d_windowRegistry.end());
00297 }
00298 }