1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XTI properties
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) 1997 Julian Smart
8 // (c) 2003 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #if wxUSE_EXTENDED_RTTI
23 class WXDLLIMPEXP_BASE wxObject;
24 class WXDLLIMPEXP_BASE wxClassInfo;
25 class WXDLLIMPEXP_BASE wxDynamicClassInfo;
27 class WXDLLIMPEXP_BASE wxHashTable
;
28 class WXDLLIMPEXP_BASE wxHashTable_Node
;
29 class WXDLLIMPEXP_BASE wxEvent
;
30 class WXDLLIMPEXP_BASE wxEvtHandler
;
32 // ----------------------------------------------------------------------------
35 // wxPropertySetter/Getter/CollectionGetter/CollectionAdder are all property
36 // accessors which are managed by wxPropertyAccessor class which in turn is
37 // handled by wxPropertyInfo.
38 // ----------------------------------------------------------------------------
40 class WXDLLIMPEXP_BASE wxPropertySetter
43 wxPropertySetter( const wxString name
) { m_name
= name
; }
44 virtual ~wxPropertySetter() {}
46 virtual void Set( wxObject
*object
, const wxAny
&variantValue
) const = 0;
47 const wxString
& GetName() const { return m_name
; }
53 class WXDLLIMPEXP_BASE wxPropertyGetter
56 wxPropertyGetter( const wxString name
) { m_name
= name
; }
57 virtual ~wxPropertyGetter() {}
59 virtual void Get( const wxObject
*object
, wxAny
& result
) const = 0;
60 const wxString
& GetName() const { return m_name
; }
66 class WXDLLIMPEXP_BASE wxPropertyCollectionGetter
69 wxPropertyCollectionGetter( const wxString name
) { m_name
= name
; }
70 virtual ~wxPropertyCollectionGetter() {}
72 virtual void Get( const wxObject
*object
, wxAnyList
& result
) const = 0;
73 const wxString
& GetName() const { return m_name
; }
79 template<typename coll_t
> void WXDLLIMPEXP_BASE \
80 wxCollectionToVariantArray( const coll_t
& coll
, wxAnyList
& result
);
82 class WXDLLIMPEXP_BASE wxPropertyCollectionAdder
85 wxPropertyCollectionAdder( const wxString name
) { m_name
= name
; }
86 virtual ~wxPropertyCollectionAdder() {}
88 virtual void Add( wxObject
*object
, const wxAny
&variantValue
) const= 0;
89 const wxString
& GetName() const { return m_name
; }
95 #define wxPROPERTY_SETTER( property, Klass, valueType, setterMethod ) \
96 class wxPropertySetter##property : public wxPropertySetter \
99 wxINFUNC_CLASS_TYPE_FIX(Klass) \
100 wxPropertySetter##property() : wxPropertySetter( wxT(#setterMethod) ) {} \
101 virtual ~wxPropertySetter##property() {} \
103 void Set( wxObject *object, const wxAny &variantValue ) const \
105 Klass *obj = dynamic_cast<Klass*>(object); \
107 if ( variantValue.GetAs(&tempobj) ) \
108 obj->setterMethod(tempobj); \
110 obj->setterMethod(*wxANY_AS(variantValue, valueType*)); \
114 #define wxPROPERTY_GETTER( property, Klass, valueType, gettermethod ) \
115 class wxPropertyGetter##property : public wxPropertyGetter \
118 wxINFUNC_CLASS_TYPE_FIX(Klass) \
119 wxPropertyGetter##property() : wxPropertyGetter( wxT(#gettermethod) ) {} \
120 virtual ~wxPropertyGetter##property() {} \
122 void Get( const wxObject *object, wxAny &result) const \
124 const Klass *obj = dynamic_cast<const Klass*>(object); \
125 result = wxAny( obj->gettermethod() ); \
129 #define wxPROPERTY_COLLECTION_ADDER( property, Klass, valueType, addermethod ) \
130 class wxPropertyCollectionAdder##property : public wxPropertyCollectionAdder \
133 wxINFUNC_CLASS_TYPE_FIX(Klass) \
134 wxPropertyCollectionAdder##property() : wxPropertyCollectionAdder( wxT(#addermethod) ) {} \
135 virtual ~wxPropertyCollectionAdder##property() {} \
137 void Add( wxObject *object, const wxAny &variantValue ) const \
139 Klass *obj = dynamic_cast<Klass*>(object); \
141 if ( variantValue.GetAs(&tempobj) ) \
142 obj->addermethod(tempobj); \
144 obj->addermethod(*wxANY_AS(variantValue, valueType*)); \
148 #define wxPROPERTY_COLLECTION_GETTER( property, Klass, valueType, gettermethod ) \
149 class wxPropertyCollectionGetter##property : public wxPropertyCollectionGetter \
152 wxINFUNC_CLASS_TYPE_FIX(Klass) \
153 wxPropertyCollectionGetter##property() : wxPropertyCollectionGetter( wxT(#gettermethod) ) {} \
154 virtual ~wxPropertyCollectionGetter##property() {} \
156 void Get( const wxObject *object, wxAnyList &result) const \
158 const Klass *obj = dynamic_cast<const Klass*>(object); \
159 wxCollectionToVariantArray( obj->gettermethod(), result ); \
163 class WXDLLIMPEXP_BASE wxPropertyAccessor
166 wxPropertyAccessor( wxPropertySetter
*setter
, wxPropertyGetter
*getter
,
167 wxPropertyCollectionAdder
*adder
, wxPropertyCollectionGetter
*collectionGetter
)
168 { m_setter
= setter
; m_getter
= getter
; m_adder
= adder
;
169 m_collectionGetter
= collectionGetter
; }
171 virtual ~wxPropertyAccessor() {}
173 // Setting a simple property (non-collection)
174 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const
177 m_setter
->Set( object
, value
);
179 wxLogError( wxGetTranslation("SetProperty called w/o valid setter") );
182 // Getting a simple property (non-collection)
183 virtual void GetProperty(const wxObject
*object
, wxAny
&result
) const
186 m_getter
->Get( object
, result
);
188 wxLogError( wxGetTranslation("GetProperty called w/o valid getter") );
191 // Adding an element to a collection property
192 virtual void AddToPropertyCollection(wxObject
*object
, const wxAny
&value
) const
195 m_adder
->Add( object
, value
);
197 wxLogError( wxGetTranslation("AddToPropertyCollection called w/o valid adder") );
200 // Getting a collection property
201 virtual void GetPropertyCollection( const wxObject
*obj
, wxAnyList
&result
) const
203 if ( m_collectionGetter
)
204 m_collectionGetter
->Get( obj
, result
);
206 wxLogError( wxGetTranslation("GetPropertyCollection called w/o valid collection getter") );
209 virtual bool HasSetter() const { return m_setter
!= NULL
; }
210 virtual bool HasCollectionGetter() const { return m_collectionGetter
!= NULL
; }
211 virtual bool HasGetter() const { return m_getter
!= NULL
; }
212 virtual bool HasAdder() const { return m_adder
!= NULL
; }
214 virtual const wxString
& GetCollectionGetterName() const
215 { return m_collectionGetter
->GetName(); }
216 virtual const wxString
& GetGetterName() const
217 { return m_getter
->GetName(); }
218 virtual const wxString
& GetSetterName() const
219 { return m_setter
->GetName(); }
220 virtual const wxString
& GetAdderName() const
221 { return m_adder
->GetName(); }
224 wxPropertySetter
*m_setter
;
225 wxPropertyCollectionAdder
*m_adder
;
226 wxPropertyGetter
*m_getter
;
227 wxPropertyCollectionGetter
* m_collectionGetter
;
230 class WXDLLIMPEXP_BASE wxGenericPropertyAccessor
: public wxPropertyAccessor
233 wxGenericPropertyAccessor( const wxString
&propName
);
234 virtual ~wxGenericPropertyAccessor();
236 void RenameProperty( const wxString
& WXUNUSED_UNLESS_DEBUG(oldName
),
237 const wxString
& newName
)
239 wxASSERT( oldName
== m_propertyName
); m_propertyName
= newName
;
242 virtual bool HasSetter() const { return true; }
243 virtual bool HasGetter() const { return true; }
244 virtual bool HasAdder() const { return false; }
245 virtual bool HasCollectionGetter() const { return false; }
247 virtual const wxString
& GetGetterName() const
248 { return m_getterName
; }
249 virtual const wxString
& GetSetterName() const
250 { return m_setterName
; }
252 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const;
253 virtual void GetProperty(const wxObject
*object
, wxAny
&value
) const;
255 // Adding an element to a collection property
256 virtual void AddToPropertyCollection(wxObject
*WXUNUSED(object
),
257 const wxAny
&WXUNUSED(value
)) const
259 wxLogError( wxGetTranslation("AddToPropertyCollection called on a generic accessor") );
262 // Getting a collection property
263 virtual void GetPropertyCollection( const wxObject
*WXUNUSED(obj
),
264 wxAnyList
&WXUNUSED(result
)) const
266 wxLogError ( wxGetTranslation("GetPropertyCollection called on a generic accessor") );
270 struct wxGenericPropertyAccessorInternal
;
271 wxGenericPropertyAccessorInternal
* m_data
;
272 wxString m_propertyName
;
273 wxString m_setterName
;
274 wxString m_getterName
;
277 typedef long wxPropertyInfoFlags
;
280 // will be removed in future releases
281 wxPROP_DEPRECATED
= 0x00000001,
283 // object graph property, will be streamed with priority (after constructor properties)
284 wxPROP_OBJECT_GRAPH
= 0x00000002,
286 // this will only be streamed out and in as enum/set, the internal representation
288 wxPROP_ENUM_STORE_LONG
= 0x00000004,
290 // don't stream out this property, needed eg to avoid streaming out children
291 // that are always created by their parents
292 wxPROP_DONT_STREAM
= 0x00000008
296 // ----------------------------------------------------------------------------
299 // wxPropertyInfo is used to inquire of the property by name. It doesn't
300 // provide access to the property, only information about it. If you
301 // want access, look at wxPropertyAccessor.
302 // ----------------------------------------------------------------------------
304 class WXDLLIMPEXP_BASE wxPropertyInfo
306 friend class /* WXDLLIMPEXP_BASE */ wxDynamicClassInfo
;
309 wxPropertyInfo(wxPropertyInfo
* &iter
,
310 wxClassInfo
* itsClass
,
311 const wxString
& name
,
312 const wxString
& typeName
,
313 wxPropertyAccessor
*accessor
,
315 wxPropertyInfoFlags flags
= 0,
316 const wxString
& helpString
= wxEmptyString
,
317 const wxString
& groupString
= wxEmptyString
) :
318 m_itsClass(itsClass
),
321 m_typeName(typeName
),
322 m_collectionElementTypeInfo(NULL
),
323 m_accessor(accessor
),
326 m_helpString(helpString
),
327 m_groupString(groupString
)
332 wxPropertyInfo(wxPropertyInfo
* &iter
,
333 wxClassInfo
* itsClass
,
334 const wxString
& name
,
335 wxEventSourceTypeInfo
* type
,
336 wxPropertyAccessor
*accessor
,
338 wxPropertyInfoFlags flags
= 0,
339 const wxString
& helpString
= wxEmptyString
,
340 const wxString
& groupString
= wxEmptyString
) :
341 m_itsClass(itsClass
),
344 m_collectionElementTypeInfo(NULL
),
345 m_accessor(accessor
),
348 m_helpString(helpString
),
349 m_groupString(groupString
)
354 wxPropertyInfo(wxPropertyInfo
* &iter
,
355 wxClassInfo
* itsClass
, const wxString
& name
,
356 const wxString
& collectionTypeName
,
357 const wxString
& elementTypeName
,
358 wxPropertyAccessor
*accessor
,
359 wxPropertyInfoFlags flags
= 0,
360 const wxString
& helpString
= wxEmptyString
,
361 const wxString
& groupString
= wxEmptyString
) :
362 m_itsClass(itsClass
),
365 m_typeName(collectionTypeName
),
366 m_collectionElementTypeInfo(NULL
),
367 m_collectionElementTypeName(elementTypeName
),
368 m_accessor(accessor
),
370 m_helpString(helpString
),
371 m_groupString(groupString
)
379 // return the class this property is declared in
380 const wxClassInfo
* GetDeclaringClass() const { return m_itsClass
; }
382 // return the name of this property
383 const wxString
& GetName() const { return m_name
; }
385 // returns the flags of this property
386 wxPropertyInfoFlags
GetFlags() const { return m_flags
; }
388 // returns the short help string of this property
389 const wxString
& GetHelpString() const { return m_helpString
; }
391 // returns the group string of this property
392 const wxString
& GetGroupString() const { return m_groupString
; }
394 // return the element type info of this property (for collections, otherwise NULL)
395 const wxTypeInfo
* GetCollectionElementTypeInfo() const
397 if ( m_collectionElementTypeInfo
== NULL
)
398 m_collectionElementTypeInfo
= wxTypeInfo::FindType(m_collectionElementTypeName
);
399 return m_collectionElementTypeInfo
;
402 // return the type info of this property
403 const wxTypeInfo
* GetTypeInfo() const
405 if ( m_typeInfo
== NULL
)
406 m_typeInfo
= wxTypeInfo::FindType(m_typeName
);
410 // return the accessor for this property
411 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
413 // returns NULL if this is the last property of this class
414 wxPropertyInfo
* GetNext() const { return m_next
; }
416 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
417 wxAny
GetDefaultValue() const { return m_defaultValue
; }
421 // inserts this property at the end of the linked chain which begins
422 // with "iter" property.
423 void Insert(wxPropertyInfo
* &iter
);
425 // removes this property from the linked chain of the m_itsClass properties.
428 wxClassInfo
* m_itsClass
;
430 mutable wxTypeInfo
* m_typeInfo
;
432 mutable wxTypeInfo
* m_collectionElementTypeInfo
;
433 wxString m_collectionElementTypeName
;
434 wxPropertyAccessor
* m_accessor
;
435 wxAny m_defaultValue
;
436 wxPropertyInfoFlags m_flags
;
437 wxString m_helpString
;
438 wxString m_groupString
;
439 wxPropertyInfo
* m_next
;
441 // FIXME: what's this comment about??
442 // string representation of the default value
443 // to be assigned by the designer to the property
444 // when the component is dropped on the container.
447 // stl is giving problems when forwarding declarations, therefore we define it as a subclass
449 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxPropertyInfo
*, wxPropertyInfoMapBase
,
450 class WXDLLIMPEXP_BASE
);
452 class WXDLLIMPEXP_BASE wxPropertyInfoMap
: public wxPropertyInfoMapBase
{
455 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxAny
, wxStringToAnyHashMapBase
,
456 class WXDLLIMPEXP_BASE
);
458 class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap
: public wxStringToAnyHashMapBase
{
461 #define wxBEGIN_PROPERTIES_TABLE(theClass) \
462 wxPropertyInfo *theClass::GetPropertiesStatic() \
464 typedef theClass class_t; \
465 static wxPropertyInfo* first = NULL;
467 #define wxEND_PROPERTIES_TABLE() \
470 #define wxHIDE_PROPERTY( pname ) \
471 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
472 wxT(#pname), typeid(void).name(), NULL, wxAny(), wxPROP_DONT_STREAM, \
473 wxEmptyString, wxEmptyString );
475 #define wxPROPERTY( pname, type, setter, getter, defaultValue, flags, help, group) \
476 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
477 static wxPropertySetter##pname _setter##pname; \
478 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
479 static wxPropertyGetter##pname _getter##pname; \
480 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
481 &_getter##pname, NULL, NULL ); \
482 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
483 wxT(#pname), typeid(type).name(), &_accessor##pname, \
484 wxAny(defaultValue), flags, group, help );
486 #define wxPROPERTY_FLAGS( pname, flags, type, setter, getter,defaultValue, \
487 pflags, help, group) \
488 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
489 static wxPropertySetter##pname _setter##pname; \
490 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
491 static wxPropertyGetter##pname _getter##pname; \
492 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
493 &_getter##pname, NULL, NULL ); \
494 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
495 wxT(#pname), typeid(flags).name(), &_accessor##pname, \
496 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
498 #define wxREADONLY_PROPERTY( pname, type, getter,defaultValue, flags, help, group) \
499 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
500 static wxPropertyGetter##pname _getter##pname; \
501 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
502 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
503 wxT(#pname), typeid(type).name(),&_accessor##pname, \
504 wxAny(defaultValue), flags, help, group );
506 #define wxREADONLY_PROPERTY_FLAGS( pname, flags, type, getter,defaultValue, \
507 pflags, help, group) \
508 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
509 static wxPropertyGetter##pname _getter##pname; \
510 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
511 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
512 wxT(#pname), typeid(flags).name(),&_accessor##pname, \
513 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
515 #define wxPROPERTY_COLLECTION( pname, colltype, addelemtype, adder, getter, \
516 flags, help, group ) \
517 wxPROPERTY_COLLECTION_ADDER( pname, class_t, addelemtype, adder ) \
518 static wxPropertyCollectionAdder##pname _adder##pname; \
519 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
520 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
521 static wxPropertyAccessor _accessor##pname( NULL, NULL,&_adder##pname, \
522 &_collectionGetter##pname ); \
523 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
524 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
525 &_accessor##pname, flags, help, group );
527 #define wxREADONLY_PROPERTY_COLLECTION( pname, colltype, addelemtype, getter, \
528 flags, help, group) \
529 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
530 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
531 static wxPropertyAccessor _accessor##pname( NULL, NULL, NULL, \
532 &_collectionGetter##pname ); \
533 static wxPropertyInfo _propertyInfo##pname( first,class_t::GetClassInfoStatic(), \
534 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
535 &_accessor##pname, flags, help, group );
537 #define wxEVENT_PROPERTY( name, eventType, eventClass ) \
538 static wxEventSourceTypeInfo _typeInfo##name( eventType, wxCLASSINFO( eventClass ) ); \
539 static wxPropertyInfo _propertyInfo##name( first,class_t::GetClassInfoStatic(), \
540 wxT(#name), &_typeInfo##name, NULL, wxAny() );
542 #define wxEVENT_RANGE_PROPERTY( name, eventType, lastEventType, eventClass ) \
543 static wxEventSourceTypeInfo _typeInfo##name( eventType, lastEventType, \
544 wxCLASSINFO( eventClass ) ); \
545 static wxPropertyInfo _propertyInfo##name( first, class_t::GetClassInfoStatic(), \
546 wxT(#name), &_typeInfo##name, NULL, wxAny() );
548 // ----------------------------------------------------------------------------
549 // Implementation Helper for Simple Properties
550 // ----------------------------------------------------------------------------
552 #define wxIMPLEMENT_PROPERTY(name, type) \
556 void Set##name( type const & p) { m_##name = p; } \
557 type const & Get##name() const { return m_##name; }
559 #endif // wxUSE_EXTENDED_RTTI
560 #endif // _XTIPROP_H_