1 /***********************************************************************
2     created:    Mon Jun 13 2005
3     author:     Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "CEGUI/falagard/WidgetLookManager.h"
28 #include "CEGUI/falagard/XMLHandler.h"
29 #include "CEGUI/ResourceProvider.h"
30 #include "CEGUI/XMLParser.h"
31 #include "CEGUI/Exceptions.h"
32 #include "CEGUI/Logger.h"
33 
34 // Start of CEGUI namespace section
35 namespace CEGUI
36 {
37     ////////////////////////////////////////////////////////////////////////////////
38     // Static data definitions.
39     template<> WidgetLookManager* Singleton<WidgetLookManager>::ms_Singleton = 0;
40     const String WidgetLookManager::FalagardSchemaName("Falagard.xsd");
41     String WidgetLookManager::d_defaultResourceGroup;
42     ////////////////////////////////////////////////////////////////////////////////
43 
WidgetLookManager()44     WidgetLookManager::WidgetLookManager()
45     {
46         char addr_buff[32];
47         sprintf(addr_buff, "(%p)", static_cast<void*>(this));
48         Logger::getSingleton().logEvent("CEGUI::WidgetLookManager singleton "
49             "created. " + String(addr_buff));
50     }
51 
~WidgetLookManager()52     WidgetLookManager::~ WidgetLookManager()
53     {
54         char addr_buff[32];
55         sprintf(addr_buff, "(%p)", static_cast<void*>(this));
56         Logger::getSingleton().logEvent("CEGUI::WidgetLookManager singleton "
57             "destroyed. " + String(addr_buff));
58     }
59 
60     /*************************************************************************
61 		Return singleton object
62 	  *************************************************************************/
getSingleton(void)63 	  WidgetLookManager&	WidgetLookManager::getSingleton(void)
64 	  {
65 		  return Singleton<WidgetLookManager>::getSingleton();
66 	  }
67 
68 	  /*************************************************************************
69 		  Return singleton pointer
70 	  *************************************************************************/
getSingletonPtr(void)71 	  WidgetLookManager*	WidgetLookManager::getSingletonPtr(void)
72 	  {
73 		  return Singleton<WidgetLookManager>::getSingletonPtr();
74   	}
75 
parseLookNFeelSpecificationFromContainer(const RawDataContainer & source)76     void WidgetLookManager::parseLookNFeelSpecificationFromContainer(const RawDataContainer& source)
77     {
78         // create handler object
79         Falagard_xmlHandler handler(this);
80 
81         // perform parse of XML data
82         CEGUI_TRY
83         {
84             System::getSingleton().getXMLParser()->parseXML(
85                 handler, source, FalagardSchemaName);
86         }
87         CEGUI_CATCH(...)
88         {
89             Logger::getSingleton().logEvent("WidgetLookManager::parseLookNFeelSpecificationFromContainer - loading of look and feel data from raw data container has failed.", Errors);
90             CEGUI_RETHROW;
91         }
92     }
93 
parseLookNFeelSpecificationFromFile(const String & filename,const String & resourceGroup)94     void WidgetLookManager::parseLookNFeelSpecificationFromFile(const String& filename, const String& resourceGroup)
95     {
96         // valid filenames are required!
97         if (filename.empty())
98         {
99             CEGUI_THROW(InvalidRequestException(
100                 "Filename supplied for look & feel file must be valid"));
101         }
102 
103         // create handler object
104         Falagard_xmlHandler handler(this);
105 
106         // perform parse of XML data
107         CEGUI_TRY
108         {
109             System::getSingleton().getXMLParser()->parseXMLFile(
110                 handler, filename, FalagardSchemaName,
111                 resourceGroup.empty() ? d_defaultResourceGroup : resourceGroup);
112         }
113         CEGUI_CATCH(...)
114         {
115             Logger::getSingleton().logEvent("WidgetLookManager::parseLookNFeelSpecification - loading of look and feel data from file '" + filename +"' has failed.", Errors);
116             CEGUI_RETHROW;
117         }
118     }
119 
parseLookNFeelSpecificationFromString(const String & source)120     void WidgetLookManager::parseLookNFeelSpecificationFromString(const String& source)
121     {
122         // create handler object
123         Falagard_xmlHandler handler(this);
124 
125         // perform parse of XML data
126         CEGUI_TRY
127         {
128             System::getSingleton().getXMLParser()->parseXMLString(
129                 handler, source, FalagardSchemaName);
130         }
131         CEGUI_CATCH(...)
132         {
133             Logger::getSingleton().logEvent("WidgetLookManager::parseLookNFeelSpecification - loading of look and feel data from string has failed.", Errors);
134             CEGUI_RETHROW;
135         }
136     }
137 
isWidgetLookAvailable(const String & widget) const138     bool WidgetLookManager::isWidgetLookAvailable(const String& widget) const
139     {
140         return d_widgetLooks.find(widget) != d_widgetLooks.end();
141     }
142 
getWidgetLook(const String & widget) const143     const WidgetLookFeel& WidgetLookManager::getWidgetLook(const String& widget) const
144     {
145         WidgetLookList::const_iterator wlf = d_widgetLooks.find(widget);
146 
147         if (wlf != d_widgetLooks.end())
148         {
149             return (*wlf).second;
150         }
151 
152         CEGUI_THROW(UnknownObjectException(
153             "WidgetLook '" + widget + "' does not exist."));
154     }
155 
eraseWidgetLook(const String & widget)156     void WidgetLookManager::eraseWidgetLook(const String& widget)
157     {
158         WidgetLookList::iterator wlf = d_widgetLooks.find(widget);
159         if (wlf != d_widgetLooks.end())
160         {
161             d_widgetLooks.erase(wlf);
162         }
163         else
164         {
165             Logger::getSingleton().logEvent(
166                 "WidgetLookManager::eraseWidgetLook - Widget look and feel '" + widget + "' did not exist.");
167         }
168     }
169 
170 
eraseAllWidgetLooks()171     void WidgetLookManager::eraseAllWidgetLooks()
172     {
173         d_widgetLooks.clear();
174     }
175 
addWidgetLook(const WidgetLookFeel & look)176     void WidgetLookManager::addWidgetLook(const WidgetLookFeel& look)
177     {
178         if (isWidgetLookAvailable(look.getName()))
179         {
180             Logger::getSingleton().logEvent(
181                 "WidgetLookManager::addWidgetLook - Widget look and feel '" + look.getName() + "' already exists.  Replacing previous definition.");
182         }
183 
184         d_widgetLooks[look.getName()] = look;
185     }
186 
writeWidgetLookToStream(const String & name,OutStream & out_stream) const187     void WidgetLookManager::writeWidgetLookToStream(const String& name, OutStream& out_stream) const
188     {
189         // start of file
190         // output xml header
191         XMLSerializer xml(out_stream);
192         // output root element
193         xml.openTag(Falagard_xmlHandler::FalagardElement);
194         xml.attribute(Falagard_xmlHandler::VersionAttribute, Falagard_xmlHandler::NativeVersion);
195 
196         CEGUI_TRY
197         {
198             // output the desired widget look data
199             getWidgetLook(name).writeXMLToStream(xml);
200         }
201         CEGUI_CATCH (UnknownObjectException&)
202         {
203             Logger::getSingleton().logEvent("WidgetLookManager::writeWidgetLookToStream - Failed to write widget look XML data to stream.", Errors);
204         }
205 
206         // close the root tags to terminate the file
207         xml.closeTag();
208     }
209 
getWidgetLookAsString(const String & widgetLookName) const210     String WidgetLookManager::getWidgetLookAsString(const String& widgetLookName) const
211     {
212         std::ostringstream str;
213         writeWidgetLookToStream(widgetLookName, str);
214 
215         return String(reinterpret_cast<const encoded_char*>(str.str().c_str()));
216     }
217 
writeWidgetLookSeriesToStream(const String & prefix,OutStream & out_stream) const218     void WidgetLookManager::writeWidgetLookSeriesToStream(const String& prefix, OutStream& out_stream) const
219     {
220         // start of file
221         // output xml header
222         XMLSerializer xml(out_stream);
223         // output root element
224         xml.openTag(Falagard_xmlHandler::FalagardElement);
225         xml.attribute(Falagard_xmlHandler::VersionAttribute, Falagard_xmlHandler::NativeVersion);
226 
227         for (WidgetLookList::const_iterator curr = d_widgetLooks.begin(); curr != d_widgetLooks.end(); ++curr)
228         {
229             if ((*curr).first.compare(0, prefix.length(), prefix) == 0)
230                 (*curr).second.writeXMLToStream(xml);
231         }
232 
233         // close the root tags to terminate the file
234         xml.closeTag();
235     }
236 
writeWidgetLookSetToStream(const WidgetLookNameSet & widgetLookNameSet,OutStream & out_stream) const237     void WidgetLookManager::writeWidgetLookSetToStream(const WidgetLookNameSet& widgetLookNameSet, OutStream& out_stream) const
238     {
239         // start of file
240         // output xml header
241         XMLSerializer xml(out_stream);
242         // output root element
243         xml.openTag(Falagard_xmlHandler::FalagardElement);
244         xml.attribute(Falagard_xmlHandler::VersionAttribute, Falagard_xmlHandler::NativeVersion);
245 
246         for (WidgetLookNameSet::const_iterator iter = widgetLookNameSet.begin(); iter != widgetLookNameSet.end(); ++iter)
247         {
248             const CEGUI::String& currentWidgetLookName = *iter;
249 
250             const WidgetLookFeel& curWidgetLookFeel = this->getWidgetLook(currentWidgetLookName);
251             curWidgetLookFeel.writeXMLToStream(xml);
252         }
253 
254         // close the root tags to terminate the file
255         xml.closeTag();
256     }
257 
getWidgetLookSetAsString(const WidgetLookNameSet & widgetLookNameSet) const258     String WidgetLookManager::getWidgetLookSetAsString(const WidgetLookNameSet& widgetLookNameSet) const
259     {
260         std::ostringstream str;
261         writeWidgetLookSetToStream(widgetLookNameSet, str);
262 
263         return String(reinterpret_cast<const encoded_char*>(str.str().c_str()));
264     }
265 
266     WidgetLookManager::WidgetLookIterator
getWidgetLookIterator() const267     WidgetLookManager::getWidgetLookIterator() const
268     {
269         return WidgetLookIterator(d_widgetLooks.begin(),d_widgetLooks.end());
270     }
271 
getWidgetLookPointerMap()272     WidgetLookManager::WidgetLookPointerMap WidgetLookManager::getWidgetLookPointerMap()
273     {
274         WidgetLookPointerMap pointerMap;
275 
276         WidgetLookList::iterator iter = d_widgetLooks.begin();
277         WidgetLookList::iterator iterEnd = d_widgetLooks.end();
278         while(iter != iterEnd)
279         {
280             pointerMap.insert(std::make_pair(iter->first, &(iter->second)));
281             ++iter;
282         }
283 
284         return pointerMap;
285     }
286 
287 
288 } // End of  CEGUI namespace section
289