1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XTI properties
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
18 #if wxUSE_EXTENDED_RTTI
23 class WXDLLIMPEXP_BASE wxObject
;
24 class WXDLLIMPEXP_BASE wxClassInfo
;
25 class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
26 class WXDLLIMPEXP_BASE wxHashTable
;
27 class WXDLLIMPEXP_BASE wxHashTable_Node
;
28 class WXDLLIMPEXP_BASE wxEvent
;
29 class WXDLLIMPEXP_BASE wxEvtHandler
;
31 // ----------------------------------------------------------------------------
34 // wxPropertySetter/Getter/CollectionGetter/CollectionAdder are all property
35 // accessors which are managed by wxPropertyAccessor class which in turn is
36 // handled by wxPropertyInfo.
37 // ----------------------------------------------------------------------------
39 class WXDLLIMPEXP_BASE wxPropertySetter
42 wxPropertySetter( const wxString name
) { m_name
= name
; }
43 virtual ~wxPropertySetter() {}
45 virtual void Set( wxObject
*object
, const wxAny
&variantValue
) const = 0;
46 const wxString
& GetName() const { return m_name
; }
52 class WXDLLIMPEXP_BASE wxPropertyGetter
55 wxPropertyGetter( const wxString name
) { m_name
= name
; }
56 virtual ~wxPropertyGetter() {}
58 virtual void Get( const wxObject
*object
, wxAny
& result
) const = 0;
59 const wxString
& GetName() const { return m_name
; }
65 class WXDLLIMPEXP_BASE wxPropertyCollectionGetter
68 wxPropertyCollectionGetter( const wxString name
) { m_name
= name
; }
69 virtual ~wxPropertyCollectionGetter() {}
71 virtual void Get( const wxObject
*object
, wxAnyList
& result
) const = 0;
72 const wxString
& GetName() const { return m_name
; }
78 template<typename coll_t
> void WXDLLIMPEXP_BASE \
79 wxCollectionToVariantArray( const coll_t
& coll
, wxAnyList
& result
);
81 class WXDLLIMPEXP_BASE wxPropertyCollectionAdder
84 wxPropertyCollectionAdder( const wxString name
) { m_name
= name
; }
85 virtual ~wxPropertyCollectionAdder() {}
87 virtual void Add( wxObject
*object
, const wxAny
&variantValue
) const= 0;
88 const wxString
& GetName() const { return m_name
; }
94 #define wxPROPERTY_SETTER( property, Klass, valueType, setterMethod ) \
95 class wxPropertySetter##property : public wxPropertySetter \
98 wxINFUNC_CLASS_TYPE_FIX(Klass) \
99 wxPropertySetter##property() : wxPropertySetter( wxT(#setterMethod) ) {} \
100 virtual ~wxPropertySetter##property() {} \
102 void Set( wxObject *object, const wxAny &variantValue ) const \
104 Klass *obj = dynamic_cast<Klass*>(object); \
106 if ( variantValue.GetAs(&tempobj) ) \
107 obj->setterMethod(tempobj); \
109 obj->setterMethod(*wxANY_AS(variantValue, valueType*)); \
113 #define wxPROPERTY_GETTER( property, Klass, valueType, gettermethod ) \
114 class wxPropertyGetter##property : public wxPropertyGetter \
117 wxINFUNC_CLASS_TYPE_FIX(Klass) \
118 wxPropertyGetter##property() : wxPropertyGetter( wxT(#gettermethod) ) {} \
119 virtual ~wxPropertyGetter##property() {} \
121 void Get( const wxObject *object, wxAny &result) const \
123 const Klass *obj = dynamic_cast<const Klass*>(object); \
124 result = wxAny( obj->gettermethod() ); \
128 #define wxPROPERTY_COLLECTION_ADDER( property, Klass, valueType, addermethod ) \
129 class wxPropertyCollectionAdder##property : public wxPropertyCollectionAdder \
132 wxINFUNC_CLASS_TYPE_FIX(Klass) \
133 wxPropertyCollectionAdder##property() : wxPropertyCollectionAdder( wxT(#addermethod) ) {} \
134 virtual ~wxPropertyCollectionAdder##property() {} \
136 void Add( wxObject *object, const wxAny &variantValue ) const \
138 Klass *obj = dynamic_cast<Klass*>(object); \
140 if ( variantValue.GetAs(&tempobj) ) \
141 obj->addermethod(tempobj); \
143 obj->addermethod(*wxANY_AS(variantValue, valueType*)); \
147 #define wxPROPERTY_COLLECTION_GETTER( property, Klass, valueType, gettermethod ) \
148 class wxPropertyCollectionGetter##property : public wxPropertyCollectionGetter \
151 wxINFUNC_CLASS_TYPE_FIX(Klass) \
152 wxPropertyCollectionGetter##property() : wxPropertyCollectionGetter( wxT(#gettermethod) ) {} \
153 virtual ~wxPropertyCollectionGetter##property() {} \
155 void Get( const wxObject *object, wxAnyList &result) const \
157 const Klass *obj = dynamic_cast<const Klass*>(object); \
158 wxCollectionToVariantArray( obj->gettermethod(), result ); \
162 class WXDLLIMPEXP_BASE wxPropertyAccessor
165 wxPropertyAccessor( wxPropertySetter
*setter
, wxPropertyGetter
*getter
,
166 wxPropertyCollectionAdder
*adder
, wxPropertyCollectionGetter
*collectionGetter
)
167 { m_setter
= setter
; m_getter
= getter
; m_adder
= adder
;
168 m_collectionGetter
= collectionGetter
; }
170 virtual ~wxPropertyAccessor() {}
172 // Setting a simple property (non-collection)
173 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const
176 m_setter
->Set( object
, value
);
178 wxLogError( _("SetProperty called w/o valid setter") );
181 // Getting a simple property (non-collection)
182 virtual void GetProperty(const wxObject
*object
, wxAny
&result
) const
185 m_getter
->Get( object
, result
);
187 wxLogError( _("GetProperty called w/o valid getter") );
190 // Adding an element to a collection property
191 virtual void AddToPropertyCollection(wxObject
*object
, const wxAny
&value
) const
194 m_adder
->Add( object
, value
);
196 wxLogError( _("AddToPropertyCollection called w/o valid adder") );
199 // Getting a collection property
200 virtual void GetPropertyCollection( const wxObject
*obj
, wxAnyList
&result
) const
202 if ( m_collectionGetter
)
203 m_collectionGetter
->Get( obj
, result
);
205 wxLogError( _("GetPropertyCollection called w/o valid collection getter") );
208 virtual bool HasSetter() const { return m_setter
!= NULL
; }
209 virtual bool HasCollectionGetter() const { return m_collectionGetter
!= NULL
; }
210 virtual bool HasGetter() const { return m_getter
!= NULL
; }
211 virtual bool HasAdder() const { return m_adder
!= NULL
; }
213 virtual const wxString
& GetCollectionGetterName() const
214 { return m_collectionGetter
->GetName(); }
215 virtual const wxString
& GetGetterName() const
216 { return m_getter
->GetName(); }
217 virtual const wxString
& GetSetterName() const
218 { return m_setter
->GetName(); }
219 virtual const wxString
& GetAdderName() const
220 { return m_adder
->GetName(); }
223 wxPropertySetter
*m_setter
;
224 wxPropertyCollectionAdder
*m_adder
;
225 wxPropertyGetter
*m_getter
;
226 wxPropertyCollectionGetter
* m_collectionGetter
;
229 class WXDLLIMPEXP_BASE wxGenericPropertyAccessor
: public wxPropertyAccessor
232 wxGenericPropertyAccessor( const wxString
&propName
);
233 virtual ~wxGenericPropertyAccessor();
235 void RenameProperty( const wxString
& WXUNUSED_UNLESS_DEBUG(oldName
),
236 const wxString
& newName
)
238 wxASSERT( oldName
== m_propertyName
); m_propertyName
= newName
;
241 virtual bool HasSetter() const { return true; }
242 virtual bool HasGetter() const { return true; }
243 virtual bool HasAdder() const { return false; }
244 virtual bool HasCollectionGetter() const { return false; }
246 virtual const wxString
& GetGetterName() const
247 { return m_getterName
; }
248 virtual const wxString
& GetSetterName() const
249 { return m_setterName
; }
251 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const;
252 virtual void GetProperty(const wxObject
*object
, wxAny
&value
) const;
254 // Adding an element to a collection property
255 virtual void AddToPropertyCollection(wxObject
*WXUNUSED(object
),
256 const wxAny
&WXUNUSED(value
)) const
258 wxLogError( _("AddToPropertyCollection called on a generic accessor") );
261 // Getting a collection property
262 virtual void GetPropertyCollection( const wxObject
*WXUNUSED(obj
),
263 wxAnyList
&WXUNUSED(result
)) const
265 wxLogError ( _("GetPropertyCollection called on a generic accessor") );
269 struct wxGenericPropertyAccessorInternal
;
270 wxGenericPropertyAccessorInternal
* m_data
;
271 wxString m_propertyName
;
272 wxString m_setterName
;
273 wxString m_getterName
;
276 typedef long wxPropertyInfoFlags
;
279 // will be removed in future releases
280 wxPROP_DEPRECATED
= 0x00000001,
282 // object graph property, will be streamed with priority (after constructor properties)
283 wxPROP_OBJECT_GRAPH
= 0x00000002,
285 // this will only be streamed out and in as enum/set, the internal representation
287 wxPROP_ENUM_STORE_LONG
= 0x00000004,
289 // don't stream out this property, needed eg to avoid streaming out children
290 // that are always created by their parents
291 wxPROP_DONT_STREAM
= 0x00000008
295 // ----------------------------------------------------------------------------
298 // wxPropertyInfo is used to inquire of the property by name. It doesn't
299 // provide access to the property, only information about it. If you
300 // want access, look at wxPropertyAccessor.
301 // ----------------------------------------------------------------------------
303 class WXDLLIMPEXP_BASE wxPropertyInfo
305 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
308 wxPropertyInfo(wxPropertyInfo
* &iter
,
309 wxClassInfo
* itsClass
,
310 const wxString
& name
,
311 const wxString
& typeName
,
312 wxPropertyAccessor
*accessor
,
314 wxPropertyInfoFlags flags
= 0,
315 const wxString
& helpString
= wxEmptyString
,
316 const wxString
& groupString
= wxEmptyString
) :
317 m_itsClass(itsClass
),
320 m_typeName(typeName
),
321 m_collectionElementTypeInfo(NULL
),
322 m_accessor(accessor
),
325 m_helpString(helpString
),
326 m_groupString(groupString
)
331 wxPropertyInfo(wxPropertyInfo
* &iter
,
332 wxClassInfo
* itsClass
,
333 const wxString
& name
,
334 wxEventSourceTypeInfo
* type
,
335 wxPropertyAccessor
*accessor
,
337 wxPropertyInfoFlags flags
= 0,
338 const wxString
& helpString
= wxEmptyString
,
339 const wxString
& groupString
= wxEmptyString
) :
340 m_itsClass(itsClass
),
343 m_collectionElementTypeInfo(NULL
),
344 m_accessor(accessor
),
347 m_helpString(helpString
),
348 m_groupString(groupString
)
353 wxPropertyInfo(wxPropertyInfo
* &iter
,
354 wxClassInfo
* itsClass
, const wxString
& name
,
355 const wxString
& collectionTypeName
,
356 const wxString
& elementTypeName
,
357 wxPropertyAccessor
*accessor
,
358 wxPropertyInfoFlags flags
= 0,
359 const wxString
& helpString
= wxEmptyString
,
360 const wxString
& groupString
= wxEmptyString
) :
361 m_itsClass(itsClass
),
364 m_typeName(collectionTypeName
),
365 m_collectionElementTypeInfo(NULL
),
366 m_collectionElementTypeName(elementTypeName
),
367 m_accessor(accessor
),
369 m_helpString(helpString
),
370 m_groupString(groupString
)
378 // return the class this property is declared in
379 const wxClassInfo
* GetDeclaringClass() const { return m_itsClass
; }
381 // return the name of this property
382 const wxString
& GetName() const { return m_name
; }
384 // returns the flags of this property
385 wxPropertyInfoFlags
GetFlags() const { return m_flags
; }
387 // returns the short help string of this property
388 const wxString
& GetHelpString() const { return m_helpString
; }
390 // returns the group string of this property
391 const wxString
& GetGroupString() const { return m_groupString
; }
393 // return the element type info of this property (for collections, otherwise NULL)
394 const wxTypeInfo
* GetCollectionElementTypeInfo() const
396 if ( m_collectionElementTypeInfo
== NULL
)
397 m_collectionElementTypeInfo
= wxTypeInfo::FindType(m_collectionElementTypeName
);
398 return m_collectionElementTypeInfo
;
401 // return the type info of this property
402 const wxTypeInfo
* GetTypeInfo() const
404 if ( m_typeInfo
== NULL
)
405 m_typeInfo
= wxTypeInfo::FindType(m_typeName
);
409 // return the accessor for this property
410 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
412 // returns NULL if this is the last property of this class
413 wxPropertyInfo
* GetNext() const { return m_next
; }
415 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
416 wxAny
GetDefaultValue() const { return m_defaultValue
; }
420 // inserts this property at the end of the linked chain which begins
421 // with "iter" property.
422 void Insert(wxPropertyInfo
* &iter
);
424 // removes this property from the linked chain of the m_itsClass properties.
427 wxClassInfo
* m_itsClass
;
429 mutable wxTypeInfo
* m_typeInfo
;
431 mutable wxTypeInfo
* m_collectionElementTypeInfo
;
432 wxString m_collectionElementTypeName
;
433 wxPropertyAccessor
* m_accessor
;
434 wxAny m_defaultValue
;
435 wxPropertyInfoFlags m_flags
;
436 wxString m_helpString
;
437 wxString m_groupString
;
438 wxPropertyInfo
* m_next
;
440 // FIXME: what's this comment about??
441 // string representation of the default value
442 // to be assigned by the designer to the property
443 // when the component is dropped on the container.
446 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxPropertyInfo
*, wxPropertyInfoMap
,
447 class WXDLLIMPEXP_BASE
);
449 #define wxBEGIN_PROPERTIES_TABLE(theClass) \
450 wxPropertyInfo *theClass::GetPropertiesStatic() \
452 typedef theClass class_t; \
453 static wxPropertyInfo* first = NULL;
455 #define wxEND_PROPERTIES_TABLE() \
458 #define wxHIDE_PROPERTY( pname ) \
459 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
460 wxT(#pname), typeid(void).name(), NULL, wxAny(), wxPROP_DONT_STREAM, \
461 wxEmptyString, wxEmptyString );
463 #define wxPROPERTY( pname, type, setter, getter, defaultValue, flags, help, group) \
464 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
465 static wxPropertySetter##pname _setter##pname; \
466 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
467 static wxPropertyGetter##pname _getter##pname; \
468 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
469 &_getter##pname, NULL, NULL ); \
470 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
471 wxT(#pname), typeid(type).name(), &_accessor##pname, \
472 wxAny(defaultValue), flags, group, help );
474 #define wxPROPERTY_FLAGS( pname, flags, type, setter, getter,defaultValue, \
475 pflags, 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(flags).name(), &_accessor##pname, \
484 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
486 #define wxREADONLY_PROPERTY( pname, type, getter,defaultValue, flags, help, group) \
487 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
488 static wxPropertyGetter##pname _getter##pname; \
489 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
490 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
491 wxT(#pname), typeid(type).name(),&_accessor##pname, \
492 wxAny(defaultValue), flags, help, group );
494 #define wxREADONLY_PROPERTY_FLAGS( pname, flags, type, getter,defaultValue, \
495 pflags, help, group) \
496 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
497 static wxPropertyGetter##pname _getter##pname; \
498 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
499 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
500 wxT(#pname), typeid(flags).name(),&_accessor##pname, \
501 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
503 #define wxPROPERTY_COLLECTION( pname, colltype, addelemtype, adder, getter, \
504 flags, help, group ) \
505 wxPROPERTY_COLLECTION_ADDER( pname, class_t, addelemtype, adder ) \
506 static wxPropertyCollectionAdder##pname _adder##pname; \
507 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
508 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
509 static wxPropertyAccessor _accessor##pname( NULL, NULL,&_adder##pname, \
510 &_collectionGetter##pname ); \
511 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
512 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
513 &_accessor##pname, flags, help, group );
515 #define wxREADONLY_PROPERTY_COLLECTION( pname, colltype, addelemtype, getter, \
516 flags, help, group) \
517 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
518 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
519 static wxPropertyAccessor _accessor##pname( NULL, NULL, NULL, \
520 &_collectionGetter##pname ); \
521 static wxPropertyInfo _propertyInfo##pname( first,class_t::GetClassInfoStatic(), \
522 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
523 &_accessor##pname, flags, help, group );
525 #define wxEVENT_PROPERTY( name, eventType, eventClass ) \
526 static wxEventSourceTypeInfo _typeInfo##name( eventType, CLASSINFO( eventClass ) ); \
527 static wxPropertyInfo _propertyInfo##name( first,class_t::GetClassInfoStatic(), \
528 wxT(#name), &_typeInfo##name, NULL, wxAny() );
530 #define wxEVENT_RANGE_PROPERTY( name, eventType, lastEventType, eventClass ) \
531 static wxEventSourceTypeInfo _typeInfo##name( eventType, lastEventType, \
532 CLASSINFO( eventClass ) ); \
533 static wxPropertyInfo _propertyInfo##name( first, class_t::GetClassInfoStatic(), \
534 wxT(#name), &_typeInfo##name, NULL, wxAny() );
536 // ----------------------------------------------------------------------------
537 // Implementation Helper for Simple Properties
538 // ----------------------------------------------------------------------------
540 #define wxIMPLEMENT_PROPERTY(name, type) \
544 void Set##name( type const & p) { m_##name = p; } \
545 type const & Get##name() const { return m_##name; }
547 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxAny
, wxStringToAnyHashMap
,
548 class WXDLLIMPEXP_BASE
);
550 #endif // wxUSE_EXTENDED_RTTI
551 #endif // _XTIPROP_H_