Crazy Eddie's GUI System  0.8.5
PropertyLinkDefinition.h
1 /***********************************************************************
2  created: Sat Oct 8 2005
3  author: Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  * Copyright (C) 2004 - 2010 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 _CEGUIFalPropertyLinkDefinition_h_
28 #define _CEGUIFalPropertyLinkDefinition_h_
29 
30 #include "CEGUI/falagard/FalagardPropertyBase.h"
31 #include "CEGUI/falagard/XMLHandler.h"
32 #include "CEGUI/IteratorBase.h"
33 #include <vector>
34 
35 #if defined (_MSC_VER)
36 # pragma warning(push)
37 # pragma warning(disable : 4251)
38 #endif
39 
40 namespace CEGUI
41 {
43 extern const String S_parentIdentifier;
44 
50 template <typename T>
52 {
53 public:
54  //------------------------------------------------------------------------//
55  typedef typename TypedProperty<T>::Helper Helper;
56 
57  //------------------------------------------------------------------------//
58  PropertyLinkDefinition(const String& propertyName, const String& widgetName,
59  const String& targetProperty, const String& initialValue,
60  const String& origin,
61  bool redrawOnWrite, bool layoutOnWrite,
62  const String& fireEvent, const String& eventNamespace) :
63  FalagardPropertyBase<T>(propertyName,
65  initialValue, origin,
66  redrawOnWrite, layoutOnWrite,
67  fireEvent, eventNamespace)
68  {
69  // add initial target if it was specified via constructor
70  // (typically meaning it came via XML attributes)
71  if (!widgetName.empty() || !targetProperty.empty())
72  addLinkTarget(widgetName, targetProperty);
73  }
74 
76 
77  //------------------------------------------------------------------------//
79  void addLinkTarget(const String& widget, const String& property)
80  {
81  d_targets.push_back(std::make_pair(widget,property));
82  }
83 
84  //------------------------------------------------------------------------//
87  {
88  d_targets.clear();
89  }
90 
91  //------------------------------------------------------------------------//
92  // return whether a the given widget / property pair is a target of this
93  // property link.
94  bool isTargetProperty(const String& widget, const String& property) const
95  {
96  LinkTargetCollection::const_iterator i = d_targets.begin();
97  for (; i != d_targets.end(); ++i)
98  {
99  if (property == i->second && widget == i->first)
100  return true;
101  }
102 
103  return false;
104  }
105 
106  //------------------------------------------------------------------------//
108  {
109  updateLinkTargets(receiver, Helper::fromString(FalagardPropertyBase<T>::d_initialValue));
110  }
111 
112  //------------------------------------------------------------------------//
113  Property* clone() const
114  {
115  return CEGUI_NEW_AO PropertyLinkDefinition<T>(*this);
116  }
117 
118 protected:
119  // override members from FalagardPropertyBase
120  //------------------------------------------------------------------------//
121  typename Helper::safe_method_return_type
122  getNative_impl(const PropertyReceiver* receiver) const
123  {
124  const LinkTargetCollection::const_iterator i(d_targets.begin());
125 
126  const Window* const target_wnd =
127  getTargetWindow(receiver, i->first);
128 
129  // if no target, or target (currently) invalid, return the default value
130  if (d_targets.empty() || !target_wnd)
131  return Helper::fromString(FalagardPropertyBase<T>::d_initialValue);
132 
133  // otherwise return the value of the property for first target, since
134  // this is considered the 'master' target for get operations.
135  return Helper::fromString(target_wnd->getProperty(i->second.empty() ?
136  TypedProperty<T>::d_name : i->second));
137  }
138 
139  //------------------------------------------------------------------------//
140  void setNative_impl(PropertyReceiver* receiver,
141  typename Helper::pass_type value)
142  {
143  updateLinkTargets(receiver, value);
144 
145  // base handles things like ensuring redraws and such happen
146  FalagardPropertyBase<T>::setNative_impl(receiver, value);
147  }
148 
149  //------------------------------------------------------------------------//
150  void updateLinkTargets(PropertyReceiver* receiver,
151  typename Helper::pass_type value) const
152  {
153  LinkTargetCollection::const_iterator i = d_targets.begin();
154  for ( ; i != d_targets.end(); ++i)
155  {
156  Window* target_wnd = getTargetWindow(receiver, i->first);
157 
158  // only try to set property if target is currently valid.
159  if (target_wnd)
160  {
161  const CEGUI::String& propertyName = i->second.empty() ? TypedProperty<T>::d_name : i->second;
162  CEGUI::String propertyValue = Helper::toString(value);
163  target_wnd->setProperty(propertyName, propertyValue);
164  target_wnd->banPropertyFromXML(propertyName);
165  }
166  }
167  }
168 
169  //------------------------------------------------------------------------//
171  {
173  writeFalagardXMLAttributes(xml_stream);
174  writeDefinitionXMLAdditionalAttributes(xml_stream);
175  }
176 
177  //------------------------------------------------------------------------//
178  void writeDefinitionXMLAdditionalAttributes(XMLSerializer& xml_stream) const
179  {
182 
185  }
186 
187  //------------------------------------------------------------------------//
188  void writeFalagardXMLAttributes(XMLSerializer& xml_stream) const
189  {
190  // HACK: Here we abuse some intimate knowledge in that we know it's
191  // safe to write our sub-elements out although the function is named
192  // for writing attributes. The alternative was to repeat code from the
193  // base class, also demonstrating intimate knowledge ;)
194 
195  LinkTargetCollection::const_iterator i(d_targets.begin());
196 
197  // if there is one target only, write it out as attributes
198  if (d_targets.size() == 1)
199  {
200  if (!i->first.empty())
201  xml_stream.attribute(Falagard_xmlHandler::WidgetAttribute, i->first);
202 
203  if (!i->second.empty())
205  }
206  // we have multiple targets, so write them as PropertyLinkTarget tags
207  else
208  {
209  for ( ; i != d_targets.end(); ++i)
210  {
212 
213  if (!i->first.empty())
214  xml_stream.attribute(Falagard_xmlHandler::WidgetAttribute, i->first);
215 
216  if (!i->second.empty())
217  xml_stream.attribute(Falagard_xmlHandler::PropertyAttribute, i->second);
218 
219  xml_stream.closeTag();
220  }
221  }
222  }
223 
224  //------------------------------------------------------------------------//
226  const Window* getTargetWindow(const PropertyReceiver* receiver,
227  const String& name) const
228  {
229  if (name.empty())
230  return static_cast<const Window*>(receiver);
231 
232  // handle link back to parent. Return receiver if no parent.
234  return static_cast<const Window*>(receiver)->getParent();
235 
236  return static_cast<const Window*>(receiver)->getChild(name);
237  }
238 
239  //------------------------------------------------------------------------//
242  const String& name) const
243  {
244  return const_cast<Window*>(
245  getTargetWindow(static_cast<const PropertyReceiver*>(receiver), name));
246  }
247 
248  //------------------------------------------------------------------------//
249  typedef std::pair<String,String> StringPair;
251  typedef std::vector<StringPair CEGUI_VECTOR_ALLOC(StringPair)> LinkTargetCollection;
252 
254  LinkTargetCollection d_targets;
255 
256 public:
258 
259  LinkTargetIterator getLinkTargetIterator() const
260  {
261  return LinkTargetIterator(d_targets.begin(),d_targets.end());
262  }
263 };
264 
265 }
266 
267 #if defined (_MSC_VER)
268 # pragma warning(pop)
269 #endif
270 
271 #endif
272 
static const String GenericDataType
Default or unspecified value for the "dataType" attribute.
Definition: falagard/XMLHandler.h:89
static const String TargetPropertyAttribute
Attribute name that stores a name of a target property.
Definition: falagard/XMLHandler.h:172
static const String HelpStringAttribute
Attribute name that stores a help string.
Definition: falagard/XMLHandler.h:179
static const String TypeAttribute
Attribute name that stores a type string.
Definition: falagard/XMLHandler.h:152
XMLSerializer & openTag(const String &name)
Start a new tag in the xml document.
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
bool empty(void) const
Returns true if the String is empty.
Definition: String.h:633
iterator for vectors
Definition: IteratorBase.h:287
XMLSerializer & attribute(const String &name, const String &value)
After an opening tag you can populate attribute list with this function.
Window * getTargetWindow(PropertyReceiver *receiver, const String &name) const
Return a pointer to the target window with the given name.
Definition: PropertyLinkDefinition.h:241
Class representing a property that links to another property defined on an attached child widget...
Definition: PropertyLinkDefinition.h:51
Dummy base class to ensure correct casting of receivers.
Definition: Property.h:45
static const String PropertyLinkDefinitionElement
Tag name for property link elements.
Definition: falagard/XMLHandler.h:130
XMLSerializer & closeTag(void)
Close the current tag.
void clearLinkTargets()
clear all link targets from this link definition.
Definition: PropertyLinkDefinition.h:86
std::vector< StringPair CEGUI_VECTOR_ALLOC(StringPair)> LinkTargetCollection
type used for the collection of targets.
Definition: PropertyLinkDefinition.h:251
An abstract base class providing common functionality and specifying the required interface for deriv...
Definition: Window.h:149
const String S_parentIdentifier
Helper class used to convert various data types to and from the format expected in Property strings...
Definition: ForwardRefs.h:84
static const String PropertyAttribute
Attribute name that stores the name of a property.
Definition: falagard/XMLHandler.h:175
void initialisePropertyReceiver(PropertyReceiver *receiver) const
function to allow initialisation of a PropertyReceiver.
Definition: PropertyLinkDefinition.h:107
const Window * getTargetWindow(const PropertyReceiver *receiver, const String &name) const
Return a pointer to the target window with the given name.
Definition: PropertyLinkDefinition.h:226
static const String PropertyLinkDefinitionHelpDefaultValue
Default value for the "type" attribute of PropertyLinkDefinition elements.
Definition: falagard/XMLHandler.h:86
static const String WidgetAttribute
Attribute name that stores the name of a widget (suffix).
Definition: falagard/XMLHandler.h:163
An abstract class that defines the interface to access object properties by name. ...
Definition: Property.h:60
Definition: FalagardPropertyBase.h:36
Class used to create XML Document.
Definition: XMLSerializer.h:85
void addLinkTarget(const String &widget, const String &property)
add a new link target to property on widget (name).
Definition: PropertyLinkDefinition.h:79
LinkTargetCollection d_targets
collection of targets for this PropertyLinkDefinition.
Definition: PropertyLinkDefinition.h:254
void writeDefinitionXMLElementType(XMLSerializer &xml_stream) const
Write out the text of the XML element type. Note that you should not write the opening '<' character...
Definition: PropertyLinkDefinition.h:170
static const String ParentIdentifier
String value representing a parent link identifier.
Definition: falagard/XMLHandler.h:93
static const String PropertyLinkTargetElement
Tag name for property link target elements.
Definition: falagard/XMLHandler.h:131
String class used within the GUI system.
Definition: String.h:62