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

CEGUIScheme.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIScheme.cpp
00003         created:        21/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements GUI Scheme 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 "CEGUIScheme.h"
00027 #include "CEGUIExceptions.h"
00028 #include "CEGUISchemeManager.h"
00029 #include "CEGUILogger.h"
00030 #include "CEGUIImagesetManager.h"
00031 #include "CEGUIImageset.h"
00032 #include "CEGUIFontManager.h"
00033 #include "CEGUIFont.h"
00034 #include "CEGUIWindowFactoryManager.h"
00035 #include "CEGUIFactoryModule.h"
00036 #include "CEGUIScheme_xmlHandler.h"
00037 #include "CEGUIDataContainer.h"
00038 #include "CEGUISystem.h"
00039 #include "CEGUIXMLParser.h"
00040 #include "falagard/CEGUIFalWidgetLookManager.h"
00041 
00042 // Start of CEGUI namespace section
00043 namespace CEGUI
00044 {
00045 
00046 /*************************************************************************
00047         Static Data definitions
00048 *************************************************************************/
00049 // name of the xml schema for GUIScheme files
00050 const char Scheme::GUISchemeSchemaName[]                                        = "GUIScheme.xsd";
00051 
00052 
00053 /*************************************************************************
00054         Constructor for scheme objects
00055 *************************************************************************/
00056 Scheme::Scheme(const String& filename, const String& resourceGroup)
00057 {
00058         if (filename.empty() || (filename == (utf8*)""))
00059         {
00060                 throw InvalidRequestException((utf8*)"Scheme::Scheme - Filename supplied for Scheme loading must be valid");
00061         }
00062 
00063     // create handler object
00064     Scheme_xmlHandler handler(this);
00065 
00066     // do parse (which uses handler to create actual data)
00067         try
00068         {
00069         System::getSingleton().getXMLParser()->parseXMLFile(handler, filename, GUISchemeSchemaName, resourceGroup);
00070         }
00071         catch(...)
00072         {
00073         Logger::getSingleton().logEvent("Scheme::Scheme - loading of Scheme from file '" + filename +"' failed.", Errors);
00074         throw;
00075         }
00076 
00077         Logger::getSingleton().logEvent((utf8*)"Loaded GUI scheme '" + d_name + "' from data in file '" + filename + "'.", Informative);
00078 
00079         // attempt to load in resources
00080         loadResources();
00081 }
00082 
00083 
00084 /*************************************************************************
00085         Destructor for scheme objects
00086 *************************************************************************/
00087 Scheme::~Scheme(void)
00088 {
00089         unloadResources();
00090 
00091         Logger::getSingleton().logEvent((utf8*)"GUI scheme '" + d_name + "' has been unloaded.", Informative);
00092 }
00093 
00094 
00095 /*************************************************************************
00096         Load all resources for this scheme
00097 *************************************************************************/
00098 void Scheme::loadResources(void)
00099 {
00100         Logger::getSingleton().logEvent((utf8*)"---- Begining resource loading for GUI scheme '" + d_name + "' ----", Informative);
00101 
00102         ImagesetManager& ismgr          = ImagesetManager::getSingleton();
00103         FontManager& fntmgr                     = FontManager::getSingleton();
00104         WindowFactoryManager& wfmgr = WindowFactoryManager::getSingleton();
00105     WidgetLookManager& wlfMgr   = WidgetLookManager::getSingleton();
00106 
00107         std::vector<LoadableUIElement>::const_iterator  pos;
00108 
00109         // check imagesets
00110         for (pos = d_imagesets.begin(); pos != d_imagesets.end(); ++pos)
00111         {
00112                 if (!ismgr.isImagesetPresent((*pos).name))
00113                 {
00114                         Imageset* iset = ismgr.createImageset((*pos).filename, (*pos).resourceGroup);
00115 
00116                         // check for wrong imageset for specified name
00117                         String realname = iset->getName();
00118 
00119                         if (realname != (*pos).name)
00120                         {
00121                                 ismgr.destroyImageset(iset);
00122                                 throw InvalidRequestException((utf8*)"Scheme::loadResources - The Imageset created by file '" + 
00123                                         (*pos).filename + "' is named '" + realname + "', not '" + (*pos).name + "' as required by Scheme '" + d_name + "'.");
00124                         }
00125 
00126                 }
00127 
00128         }
00129 
00130     // check imagesets that are created directly from image files
00131     for (pos = d_imagesetsFromImages.begin(); pos != d_imagesetsFromImages.end(); ++pos)
00132     {
00133         if (!ismgr.isImagesetPresent((*pos).name))
00134             ismgr.createImagesetFromImageFile((*pos).name, (*pos).filename, (*pos).resourceGroup);
00135     }
00136 
00137         // check fonts
00138         for (pos = d_fonts.begin(); pos != d_fonts.end(); ++pos)
00139         {
00140                 if (!fntmgr.isFontPresent((*pos).name))
00141                 {
00142                         Font* font = fntmgr.createFont((*pos).filename, (*pos).resourceGroup);
00143 
00144                         // check for wrong font for specified name
00145                         String realname = font->getName();
00146 
00147                         if (realname != (*pos).name)
00148                         {
00149                                 fntmgr.destroyFont(font);
00150                                 throw InvalidRequestException((utf8*)"Scheme::loadResources - The Font created by file '" + 
00151                                         (*pos).filename + "' is named '" + realname + "', not '" + (*pos).name + "' as required by Scheme '" + d_name + "'.");
00152                         }
00153 
00154                 }
00155 
00156         }
00157 
00158     // load look'n'feels (can't actually check these, so just re-parse data; it does no harm except maybe wase a bit of time)
00159     for (pos = d_looknfeels.begin(); pos != d_looknfeels.end(); ++pos)
00160     {
00161         wlfMgr.parseLookNFeelSpecification((*pos).filename, (*pos).resourceGroup);
00162     }
00163 
00164         // check factories
00165         std::vector<UIModule>::iterator cmod = d_widgetModules.begin();
00166         for (;cmod != d_widgetModules.end(); ++cmod)
00167         {
00168                 // create and load dynamic module as required
00169                 if ((*cmod).module == NULL)
00170                 {
00171                         (*cmod).module = new FactoryModule((*cmod).name);
00172                 }
00173 
00174         // see if we should just register all factories available in the module (i.e. No factories explicitly specified)
00175         if ((*cmod).factories.size() == 0)
00176         {
00177             Logger::getSingleton().logEvent("No window factories specified for module '" + (*cmod).name + "' - adding all available factories...");
00178             (*cmod).module->registerAllFactories();
00179         }
00180         // some names were explicitly given, so only register those.
00181         else
00182         {
00183             std::vector<UIElementFactory>::const_iterator       elem = (*cmod).factories.begin();
00184             for (; elem != (*cmod).factories.end(); ++elem)
00185             {
00186                 if (!wfmgr.isFactoryPresent((*elem).name))
00187                 {
00188                     (*cmod).module->registerFactory((*elem).name);
00189                 }
00190             }
00191         }
00192         }
00193 
00194         // check aliases
00195         std::vector<AliasMapping>::iterator alias = d_aliasMappings.begin();
00196         for (;alias != d_aliasMappings.end(); ++alias)
00197         {
00198                 // get iterator 
00199                 WindowFactoryManager::TypeAliasIterator iter = wfmgr.getAliasIterator();
00200 
00201                 // look for this alias
00202                 while (!iter.isAtEnd() && (iter.getCurrentKey() != (*alias).aliasName))
00203                         ++iter;
00204 
00205                 // if the alias exists
00206                 if (!iter.isAtEnd())
00207                 {
00208                         // if the current target type matches
00209                         if (iter.getCurrentValue().getActiveTarget() == (*alias).targetName)
00210                         {
00211                                 // assume this mapping is ours and skip to next alias
00212                                 continue;
00213                         }
00214 
00215                 }
00216 
00217                 // create a new alias entry
00218                 wfmgr.addWindowTypeAlias((*alias).aliasName, (*alias).targetName);
00219         }
00220 
00221     // check falagard window mappings.
00222     std::vector<FalagardMapping>::iterator falagard = d_falagardMappings.begin();
00223     for (;falagard != d_falagardMappings.end(); ++falagard)
00224     {
00225         // get iterator
00226         WindowFactoryManager::FalagardMappingIterator iter = wfmgr.getFalagardMappingIterator();
00227 
00228         // look for this mapping
00229         while (!iter.isAtEnd() && (iter.getCurrentKey() != (*falagard).windowName))
00230             ++iter;
00231 
00232         // if the alias exists
00233         if (!iter.isAtEnd())
00234         {
00235             // if the current target and looks match
00236             if ((iter.getCurrentValue().d_baseType == (*falagard).targetName) &&
00237                 (iter.getCurrentValue().d_lookName == (*falagard).lookName))
00238             {
00239                 // assume this mapping is ours and skip to next
00240                 continue;
00241             }
00242         }
00243 
00244         // create a new mapping entry
00245         wfmgr.addFalagardWindowMapping((*falagard).windowName, (*falagard).targetName, (*falagard).lookName);
00246     }
00247 
00248         Logger::getSingleton().logEvent((utf8*)"---- Resource loading for GUI scheme '" + d_name + "' completed ----", Informative);
00249 }
00250 
00251 
00252 /*************************************************************************
00253         Unload all resources for this scheme
00254 *************************************************************************/
00255 void Scheme::unloadResources(void)
00256 {
00257         Logger::getSingleton().logEvent((utf8*)"---- Begining resource cleanup for GUI scheme '" + d_name + "' ----", Informative);
00258 
00259         ImagesetManager& ismgr          = ImagesetManager::getSingleton();
00260         FontManager& fntmgr                     = FontManager::getSingleton();
00261         WindowFactoryManager& wfmgr = WindowFactoryManager::getSingleton();
00262 
00263         std::vector<LoadableUIElement>::const_iterator  pos;
00264 
00265         // check fonts
00266         for (pos = d_fonts.begin(); pos != d_fonts.end(); ++pos)
00267         {
00268                 fntmgr.destroyFont((*pos).name);
00269         }
00270 
00271         // check Imagesets
00272         for (pos = d_imagesets.begin(); pos != d_imagesets.end(); ++pos)
00273         {
00274                 ismgr.destroyImageset((*pos).name);
00275         }
00276 
00277     // check imagesets that are created directly from image files
00278     for (pos = d_imagesetsFromImages.begin(); pos != d_imagesetsFromImages.end(); ++pos)
00279     {
00280         ismgr.destroyImageset((*pos).name);
00281     }
00282 
00283         // check factories
00284         std::vector<UIModule>::iterator cmod = d_widgetModules.begin();
00285         for (;cmod != d_widgetModules.end(); ++cmod)
00286         {
00287                 std::vector<UIElementFactory>::const_iterator   elem = (*cmod).factories.begin();
00288                 for (; elem != (*cmod).factories.end(); ++elem)
00289                 {
00290                         wfmgr.removeFactory((*elem).name);
00291                 }
00292 
00293                 // unload dynamic module as required
00294                 if ((*cmod).module != NULL)
00295                 {
00296                         delete (*cmod).module;
00297                         (*cmod).module = NULL;
00298                 }
00299 
00300         }
00301 
00302         // check aliases
00303         std::vector<AliasMapping>::iterator alias = d_aliasMappings.begin();
00304         for (;alias != d_aliasMappings.end(); ++alias)
00305         {
00306                 // get iterator 
00307                 WindowFactoryManager::TypeAliasIterator iter = wfmgr.getAliasIterator();
00308 
00309                 // look for this alias
00310                 while (!iter.isAtEnd() && (iter.getCurrentKey() != (*alias).aliasName))
00311                         ++iter;
00312 
00313                 // if the alias exists
00314                 if (!iter.isAtEnd())
00315                 {
00316                         // create a new alias entry
00317                         wfmgr.removeWindowTypeAlias((*alias).aliasName, (*alias).targetName);
00318                 }
00319 
00320         }
00321 
00322     // check falagard window mappings.
00323     std::vector<FalagardMapping>::iterator falagard = d_falagardMappings.begin();
00324     for (;falagard != d_falagardMappings.end(); ++falagard)
00325     {
00326         // get iterator
00327         WindowFactoryManager::FalagardMappingIterator iter = wfmgr.getFalagardMappingIterator();
00328 
00329         // look for this mapping
00330         while (!iter.isAtEnd() && (iter.getCurrentKey() != (*falagard).windowName))
00331             ++iter;
00332 
00333         // if the alias exists
00334         if (!iter.isAtEnd())
00335         {
00336             // if the current target and looks match
00337             if ((iter.getCurrentValue().d_baseType == (*falagard).targetName) &&
00338                 (iter.getCurrentValue().d_lookName == (*falagard).lookName))
00339             {
00340                 // assume this mapping is ours and delete it
00341                 wfmgr.removeFalagardWindowMapping((*falagard).targetName);
00342             }
00343         }
00344     }
00345 
00346         Logger::getSingleton().logEvent((utf8*)"---- Resource cleanup for GUI scheme '" + d_name + "' completed ----", Informative);
00347 }
00348 
00349 
00350 /*************************************************************************
00351         Check if all resources for this Scheme are loaded
00352 *************************************************************************/
00353 bool Scheme::resourcesLoaded(void) const
00354 {
00355         ImagesetManager& ismgr          = ImagesetManager::getSingleton();
00356         FontManager& fntmgr                     = FontManager::getSingleton();
00357         WindowFactoryManager& wfmgr = WindowFactoryManager::getSingleton();
00358 
00359         std::vector<LoadableUIElement>::const_iterator  pos;
00360 
00361         // check imagesets
00362         for (pos = d_imagesets.begin(); pos != d_imagesets.end(); ++pos)
00363         {
00364                 if (!ismgr.isImagesetPresent((*pos).name))
00365                 {
00366                         return false;
00367                 }
00368 
00369         }
00370 
00371         // check fonts
00372         for (pos = d_fonts.begin(); pos != d_fonts.end(); ++pos)
00373         {
00374                 if (!fntmgr.isFontPresent((*pos).name))
00375                 {
00376                         return false;
00377                 }
00378 
00379         }
00380 
00381         // check factories
00382         std::vector<UIModule>::const_iterator   cmod = d_widgetModules.begin();
00383         for (;cmod != d_widgetModules.end(); ++cmod)
00384         {
00385                 std::vector<UIElementFactory>::const_iterator   elem = (*cmod).factories.begin();
00386 
00387                 for (; elem != (*cmod).factories.end(); ++elem)
00388                 {
00389                         if (!wfmgr.isFactoryPresent((*elem).name))
00390                         {
00391                                 return false;
00392                         }
00393 
00394                 }
00395 
00396         }
00397 
00398         // check aliases
00399         std::vector<AliasMapping>::const_iterator alias = d_aliasMappings.begin();
00400         for (;alias != d_aliasMappings.end(); ++alias)
00401         {
00402                 // get iterator 
00403                 WindowFactoryManager::TypeAliasIterator iter = wfmgr.getAliasIterator();
00404 
00405                 // look for this alias
00406                 while (!iter.isAtEnd() && (iter.getCurrentKey() != (*alias).aliasName))
00407                         ++iter;
00408 
00409                 // if the alias exists
00410                 if (!iter.isAtEnd())
00411                 {
00412                         // if the current target type matches
00413                         if (iter.getCurrentValue().getActiveTarget() == (*alias).targetName)
00414                         {
00415                                 // target matches, assume we set it and continue to next alias
00416                                 continue;
00417                         }
00418 
00419                 }
00420 
00421                 // no alias or target type does not match
00422                 return false;
00423         }
00424 
00425         return true;
00426 }
00427 
00428 
00429 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3