Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
PropertyDefinition.h
1 /***********************************************************************
2  created: Sun Jun 26 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 #ifndef _CEGUIFalPropertyDefinition_h_
28 #define _CEGUIFalPropertyDefinition_h_
29 
30 #include "CEGUI/falagard/FalagardPropertyBase.h"
31 #include "CEGUI/falagard/XMLHandler.h"
32 #include "CEGUI/Logger.h"
33 
34 namespace CEGUI
35 {
36 template <typename T>
38 {
39 public:
40  typedef typename TypedProperty<T>::Helper Helper;
41 
42  //------------------------------------------------------------------------//
43  PropertyDefinition(const String& name, const String& initialValue,
44  const String& help, const String& origin,
45  bool redrawOnWrite, bool layoutOnWrite,
46  const String& fireEvent, const String& eventNamespace) :
47  FalagardPropertyBase<T>(name, help, initialValue, origin,
48  redrawOnWrite, layoutOnWrite,
49  fireEvent, eventNamespace),
50  d_userStringName(name + PropertyDefinitionBase::UserStringNameSuffix)
51  {
52  }
53 
54  //------------------------------------------------------------------------//
55  ~PropertyDefinition() {}
56 
57  //------------------------------------------------------------------------//
59  {
60  setWindowUserString(static_cast<Window*>(receiver), this->d_default);
61  }
62 
63  //------------------------------------------------------------------------//
64  Property* clone() const
65  {
66  return CEGUI_NEW_AO PropertyDefinition<T>(*this);
67  }
68 
69 protected:
70  //------------------------------------------------------------------------//
71  typename Helper::safe_method_return_type
72  getNative_impl(const PropertyReceiver* receiver) const
73  {
74  const Window* const wnd = static_cast<const Window*>(receiver);
75 
76  // the try/catch is used instead of querying the existence of the user
77  // string in order that for the 'usual' case - where the user string
78  // exists - there is basically no additional overhead, and that any
79  // overhead is incurred only for the initial creation of the user
80  // string.
81  // Maybe the only negative here is that an error gets logged, though
82  // this can be treated as a 'soft' error.
83  CEGUI_TRY
84  {
85  return Helper::fromString(wnd->getUserString(d_userStringName));
86  }
87  CEGUI_CATCH (UnknownObjectException&)
88  {
89  Logger::getSingleton().logEvent(
90  "PropertyDefiniton::get: Defining new user string: " +
91  d_userStringName);
92 
93  // HACK: FIXME: TODO: This const_cast is basically to allow the
94  // above mentioned performance measure; the alternative would be
95  // to just return d_default, and while technically correct, it
96  // would be very slow.
97  const_cast<Window*>(wnd)->
98  setUserString(d_userStringName, TypedProperty<T>::d_default);
99 
100  return Helper::fromString(TypedProperty<T>::d_default);
101  }
102  }
103 
104  //------------------------------------------------------------------------//
105  void setNative_impl(PropertyReceiver* receiver,typename Helper::pass_type value)
106  {
107  setWindowUserString(static_cast<Window*>(receiver), Helper::toString(value));
108  FalagardPropertyBase<T>::setNative_impl(receiver, value);
109  }
110 
111  //------------------------------------------------------------------------//
112  void setWindowUserString(Window* window, const String& value) const
113  {
114  window->setUserString(d_userStringName, value);
115  }
116 
117  //------------------------------------------------------------------------//
119  {
121  writeDefinitionXMLAdditionalAttributes(xml_stream);
122  }
123  //------------------------------------------------------------------------//
124  void writeDefinitionXMLAdditionalAttributes(XMLSerializer& xml_stream) const
125  {
128 
129  if (!PropertyDefinitionBase::d_helpString.empty() && PropertyDefinitionBase::d_helpString.compare(CEGUI::Falagard_xmlHandler::PropertyDefinitionHelpDefaultValue) != 0)
130  xml_stream.attribute(Falagard_xmlHandler::HelpStringAttribute, PropertyDefinitionBase::d_helpString);
131  }
132 
133 
134  //------------------------------------------------------------------------//
135 
136  String d_userStringName;
137 };
138 
139 }
140 
141 #endif
142