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
)
332 wxPropertyInfo(wxPropertyInfo
* &iter
,
333 wxClassInfo
* itsClass
,
334 const wxString
& name
,
335 const char* typeName
,
336 wxPropertyAccessor
*accessor
,
338 wxPropertyInfoFlags flags
= 0,
339 const wxString
& helpString
= wxEmptyString
,
340 const wxString
& groupString
= wxEmptyString
) :
341 m_itsClass(itsClass
),
344 m_typeName(wxString::FromAscii(typeName
)),
345 m_collectionElementTypeInfo(NULL
),
346 m_accessor(accessor
),
349 m_helpString(helpString
),
350 m_groupString(groupString
)
355 wxPropertyInfo(wxPropertyInfo
* &iter
,
356 wxClassInfo
* itsClass
,
357 const wxString
& name
,
358 wxEventSourceTypeInfo
* type
,
359 wxPropertyAccessor
*accessor
,
361 wxPropertyInfoFlags flags
= 0,
362 const wxString
& helpString
= wxEmptyString
,
363 const wxString
& groupString
= wxEmptyString
) :
364 m_itsClass(itsClass
),
367 m_collectionElementTypeInfo(NULL
),
368 m_accessor(accessor
),
371 m_helpString(helpString
),
372 m_groupString(groupString
)
377 wxPropertyInfo(wxPropertyInfo
* &iter
,
378 wxClassInfo
* itsClass
, const wxString
& name
,
379 const wxString
& collectionTypeName
,
380 const wxString
& elementTypeName
,
381 wxPropertyAccessor
*accessor
,
382 wxPropertyInfoFlags flags
= 0,
383 const wxString
& helpString
= wxEmptyString
,
384 const wxString
& groupString
= wxEmptyString
) :
385 m_itsClass(itsClass
),
388 m_typeName(collectionTypeName
),
389 m_collectionElementTypeInfo(NULL
),
390 m_collectionElementTypeName(elementTypeName
),
391 m_accessor(accessor
),
393 m_helpString(helpString
),
394 m_groupString(groupString
)
400 wxPropertyInfo(wxPropertyInfo
* &iter
,
401 wxClassInfo
* itsClass
, const wxString
& name
,
402 const char* collectionTypeName
,
403 const char* elementTypeName
,
404 wxPropertyAccessor
*accessor
,
405 wxPropertyInfoFlags flags
= 0,
406 const wxString
& helpString
= wxEmptyString
,
407 const wxString
& groupString
= wxEmptyString
) :
408 m_itsClass(itsClass
),
411 m_typeName(wxString::FromAscii(collectionTypeName
)),
412 m_collectionElementTypeInfo(NULL
),
413 m_collectionElementTypeName(wxString::FromAscii(elementTypeName
)),
414 m_accessor(accessor
),
416 m_helpString(helpString
),
417 m_groupString(groupString
)
425 // return the class this property is declared in
426 const wxClassInfo
* GetDeclaringClass() const { return m_itsClass
; }
428 // return the name of this property
429 const wxString
& GetName() const { return m_name
; }
431 // returns the flags of this property
432 wxPropertyInfoFlags
GetFlags() const { return m_flags
; }
434 // returns the short help string of this property
435 const wxString
& GetHelpString() const { return m_helpString
; }
437 // returns the group string of this property
438 const wxString
& GetGroupString() const { return m_groupString
; }
440 // return the element type info of this property (for collections, otherwise NULL)
441 const wxTypeInfo
* GetCollectionElementTypeInfo() const
443 if ( m_collectionElementTypeInfo
== NULL
)
444 m_collectionElementTypeInfo
= wxTypeInfo::FindType(m_collectionElementTypeName
);
445 return m_collectionElementTypeInfo
;
448 // return the type info of this property
449 const wxTypeInfo
* GetTypeInfo() const
451 if ( m_typeInfo
== NULL
)
452 m_typeInfo
= wxTypeInfo::FindType(m_typeName
);
456 // return the accessor for this property
457 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
459 // returns NULL if this is the last property of this class
460 wxPropertyInfo
* GetNext() const { return m_next
; }
462 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
463 wxAny
GetDefaultValue() const { return m_defaultValue
; }
467 // inserts this property at the end of the linked chain which begins
468 // with "iter" property.
469 void Insert(wxPropertyInfo
* &iter
);
471 // removes this property from the linked chain of the m_itsClass properties.
474 wxClassInfo
* m_itsClass
;
476 mutable wxTypeInfo
* m_typeInfo
;
478 mutable wxTypeInfo
* m_collectionElementTypeInfo
;
479 wxString m_collectionElementTypeName
;
480 wxPropertyAccessor
* m_accessor
;
481 wxAny m_defaultValue
;
482 wxPropertyInfoFlags m_flags
;
483 wxString m_helpString
;
484 wxString m_groupString
;
485 wxPropertyInfo
* m_next
;
487 // FIXME: what's this comment about??
488 // string representation of the default value
489 // to be assigned by the designer to the property
490 // when the component is dropped on the container.
493 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxPropertyInfo
*, wxPropertyInfoMap
,
494 class WXDLLIMPEXP_BASE
);
496 #define wxBEGIN_PROPERTIES_TABLE(theClass) \
497 wxPropertyInfo *theClass::GetPropertiesStatic() \
499 typedef theClass class_t; \
500 static wxPropertyInfo* first = NULL;
502 #define wxEND_PROPERTIES_TABLE() \
505 #define wxHIDE_PROPERTY( pname ) \
506 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
507 wxT(#pname), typeid(void).name(), NULL, wxAny(), wxPROP_DONT_STREAM, \
508 wxEmptyString, wxEmptyString );
510 #define wxPROPERTY( pname, type, setter, getter, defaultValue, flags, help, group) \
511 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
512 static wxPropertySetter##pname _setter##pname; \
513 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
514 static wxPropertyGetter##pname _getter##pname; \
515 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
516 &_getter##pname, NULL, NULL ); \
517 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
518 wxT(#pname), typeid(type).name(), &_accessor##pname, \
519 wxAny(defaultValue), flags, group, help );
521 #define wxPROPERTY_FLAGS( pname, flags, type, setter, getter,defaultValue, \
522 pflags, help, group) \
523 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
524 static wxPropertySetter##pname _setter##pname; \
525 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
526 static wxPropertyGetter##pname _getter##pname; \
527 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
528 &_getter##pname, NULL, NULL ); \
529 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
530 wxT(#pname), typeid(flags).name(), &_accessor##pname, \
531 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
533 #define wxREADONLY_PROPERTY( pname, type, getter,defaultValue, flags, help, group) \
534 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
535 static wxPropertyGetter##pname _getter##pname; \
536 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
537 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
538 wxT(#pname), typeid(type).name(),&_accessor##pname, \
539 wxAny(defaultValue), flags, help, group );
541 #define wxREADONLY_PROPERTY_FLAGS( pname, flags, type, getter,defaultValue, \
542 pflags, help, group) \
543 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
544 static wxPropertyGetter##pname _getter##pname; \
545 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
546 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
547 wxT(#pname), typeid(flags).name(),&_accessor##pname, \
548 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
550 #define wxPROPERTY_COLLECTION( pname, colltype, addelemtype, adder, getter, \
551 flags, help, group ) \
552 wxPROPERTY_COLLECTION_ADDER( pname, class_t, addelemtype, adder ) \
553 static wxPropertyCollectionAdder##pname _adder##pname; \
554 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
555 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
556 static wxPropertyAccessor _accessor##pname( NULL, NULL,&_adder##pname, \
557 &_collectionGetter##pname ); \
558 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
559 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
560 &_accessor##pname, flags, help, group );
562 #define wxREADONLY_PROPERTY_COLLECTION( pname, colltype, addelemtype, getter, \
563 flags, help, group) \
564 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
565 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
566 static wxPropertyAccessor _accessor##pname( NULL, NULL, NULL, \
567 &_collectionGetter##pname ); \
568 static wxPropertyInfo _propertyInfo##pname( first,class_t::GetClassInfoStatic(), \
569 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
570 &_accessor##pname, flags, help, group );
572 #define wxEVENT_PROPERTY( name, eventType, eventClass ) \
573 static wxEventSourceTypeInfo _typeInfo##name( eventType, CLASSINFO( eventClass ) ); \
574 static wxPropertyInfo _propertyInfo##name( first,class_t::GetClassInfoStatic(), \
575 wxT(#name), &_typeInfo##name, NULL, wxAny() );
577 #define wxEVENT_RANGE_PROPERTY( name, eventType, lastEventType, eventClass ) \
578 static wxEventSourceTypeInfo _typeInfo##name( eventType, lastEventType, \
579 CLASSINFO( eventClass ) ); \
580 static wxPropertyInfo _propertyInfo##name( first, class_t::GetClassInfoStatic(), \
581 wxT(#name), &_typeInfo##name, NULL, wxAny() );
583 // ----------------------------------------------------------------------------
584 // Implementation Helper for Simple Properties
585 // ----------------------------------------------------------------------------
587 #define wxIMPLEMENT_PROPERTY(name, type) \
591 void Set##name( type const & p) { m_##name = p; } \
592 type const & Get##name() const { return m_##name; }
594 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxAny
, wxStringToAnyHashMap
,
595 class WXDLLIMPEXP_BASE
);
597 #endif // wxUSE_EXTENDED_RTTI
598 #endif // _XTIPROP_H_