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
24 class WXDLLIMPEXP_BASE wxObject;
25 class WXDLLIMPEXP_BASE wxClassInfo;
26 class WXDLLIMPEXP_BASE wxDynamicClassInfo;
28 class WXDLLIMPEXP_BASE wxHashTable
;
29 class WXDLLIMPEXP_BASE wxHashTable_Node
;
30 class WXDLLIMPEXP_BASE wxEvent
;
31 class WXDLLIMPEXP_BASE wxEvtHandler
;
33 // ----------------------------------------------------------------------------
36 // wxPropertySetter/Getter/CollectionGetter/CollectionAdder are all property
37 // accessors which are managed by wxPropertyAccessor class which in turn is
38 // handled by wxPropertyInfo.
39 // ----------------------------------------------------------------------------
41 class WXDLLIMPEXP_BASE wxPropertySetter
44 wxPropertySetter( const wxString name
) { m_name
= name
; }
45 virtual ~wxPropertySetter() {}
47 virtual void Set( wxObject
*object
, const wxAny
&variantValue
) const = 0;
48 const wxString
& GetName() const { return m_name
; }
54 class WXDLLIMPEXP_BASE wxPropertyGetter
57 wxPropertyGetter( const wxString name
) { m_name
= name
; }
58 virtual ~wxPropertyGetter() {}
60 virtual void Get( const wxObject
*object
, wxAny
& result
) const = 0;
61 const wxString
& GetName() const { return m_name
; }
67 class WXDLLIMPEXP_BASE wxPropertyCollectionGetter
70 wxPropertyCollectionGetter( const wxString name
) { m_name
= name
; }
71 virtual ~wxPropertyCollectionGetter() {}
73 virtual void Get( const wxObject
*object
, wxAnyList
& result
) const = 0;
74 const wxString
& GetName() const { return m_name
; }
80 template<typename coll_t
> void WXDLLIMPEXP_BASE \
81 wxCollectionToVariantArray( const coll_t
& coll
, wxAnyList
& result
);
83 class WXDLLIMPEXP_BASE wxPropertyCollectionAdder
86 wxPropertyCollectionAdder( const wxString name
) { m_name
= name
; }
87 virtual ~wxPropertyCollectionAdder() {}
89 virtual void Add( wxObject
*object
, const wxAny
&variantValue
) const= 0;
90 const wxString
& GetName() const { return m_name
; }
96 #define wxPROPERTY_SETTER( property, Klass, valueType, setterMethod ) \
97 class wxPropertySetter##property : public wxPropertySetter \
100 wxINFUNC_CLASS_TYPE_FIX(Klass) \
101 wxPropertySetter##property() : wxPropertySetter( wxT(#setterMethod) ) {} \
102 virtual ~wxPropertySetter##property() {} \
104 void Set( wxObject *object, const wxAny &variantValue ) const \
106 Klass *obj = dynamic_cast<Klass*>(object); \
108 if ( variantValue.GetAs(&tempobj) ) \
109 obj->setterMethod(tempobj); \
111 obj->setterMethod(*wxANY_AS(variantValue, valueType*)); \
115 #define wxPROPERTY_GETTER( property, Klass, valueType, gettermethod ) \
116 class wxPropertyGetter##property : public wxPropertyGetter \
119 wxINFUNC_CLASS_TYPE_FIX(Klass) \
120 wxPropertyGetter##property() : wxPropertyGetter( wxT(#gettermethod) ) {} \
121 virtual ~wxPropertyGetter##property() {} \
123 void Get( const wxObject *object, wxAny &result) const \
125 const Klass *obj = dynamic_cast<const Klass*>(object); \
126 result = wxAny( obj->gettermethod() ); \
130 #define wxPROPERTY_COLLECTION_ADDER( property, Klass, valueType, addermethod ) \
131 class wxPropertyCollectionAdder##property : public wxPropertyCollectionAdder \
134 wxINFUNC_CLASS_TYPE_FIX(Klass) \
135 wxPropertyCollectionAdder##property() : wxPropertyCollectionAdder( wxT(#addermethod) ) {} \
136 virtual ~wxPropertyCollectionAdder##property() {} \
138 void Add( wxObject *object, const wxAny &variantValue ) const \
140 Klass *obj = dynamic_cast<Klass*>(object); \
142 if ( variantValue.GetAs(&tempobj) ) \
143 obj->addermethod(tempobj); \
145 obj->addermethod(*wxANY_AS(variantValue, valueType*)); \
149 #define wxPROPERTY_COLLECTION_GETTER( property, Klass, valueType, gettermethod ) \
150 class wxPropertyCollectionGetter##property : public wxPropertyCollectionGetter \
153 wxINFUNC_CLASS_TYPE_FIX(Klass) \
154 wxPropertyCollectionGetter##property() : wxPropertyCollectionGetter( wxT(#gettermethod) ) {} \
155 virtual ~wxPropertyCollectionGetter##property() {} \
157 void Get( const wxObject *object, wxAnyList &result) const \
159 const Klass *obj = dynamic_cast<const Klass*>(object); \
160 wxCollectionToVariantArray( obj->gettermethod(), result ); \
164 class WXDLLIMPEXP_BASE wxPropertyAccessor
167 wxPropertyAccessor( wxPropertySetter
*setter
, wxPropertyGetter
*getter
,
168 wxPropertyCollectionAdder
*adder
, wxPropertyCollectionGetter
*collectionGetter
)
169 { m_setter
= setter
; m_getter
= getter
; m_adder
= adder
;
170 m_collectionGetter
= collectionGetter
; }
172 virtual ~wxPropertyAccessor() {}
174 // Setting a simple property (non-collection)
175 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const
178 m_setter
->Set( object
, value
);
180 wxLogError( _("SetProperty called w/o valid setter") );
183 // Getting a simple property (non-collection)
184 virtual void GetProperty(const wxObject
*object
, wxAny
&result
) const
187 m_getter
->Get( object
, result
);
189 wxLogError( _("GetProperty called w/o valid getter") );
192 // Adding an element to a collection property
193 virtual void AddToPropertyCollection(wxObject
*object
, const wxAny
&value
) const
196 m_adder
->Add( object
, value
);
198 wxLogError( _("AddToPropertyCollection called w/o valid adder") );
201 // Getting a collection property
202 virtual void GetPropertyCollection( const wxObject
*obj
, wxAnyList
&result
) const
204 if ( m_collectionGetter
)
205 m_collectionGetter
->Get( obj
, result
);
207 wxLogError( _("GetPropertyCollection called w/o valid collection getter") );
210 virtual bool HasSetter() const { return m_setter
!= NULL
; }
211 virtual bool HasCollectionGetter() const { return m_collectionGetter
!= NULL
; }
212 virtual bool HasGetter() const { return m_getter
!= NULL
; }
213 virtual bool HasAdder() const { return m_adder
!= NULL
; }
215 virtual const wxString
& GetCollectionGetterName() const
216 { return m_collectionGetter
->GetName(); }
217 virtual const wxString
& GetGetterName() const
218 { return m_getter
->GetName(); }
219 virtual const wxString
& GetSetterName() const
220 { return m_setter
->GetName(); }
221 virtual const wxString
& GetAdderName() const
222 { return m_adder
->GetName(); }
225 wxPropertySetter
*m_setter
;
226 wxPropertyCollectionAdder
*m_adder
;
227 wxPropertyGetter
*m_getter
;
228 wxPropertyCollectionGetter
* m_collectionGetter
;
231 class WXDLLIMPEXP_BASE wxGenericPropertyAccessor
: public wxPropertyAccessor
234 wxGenericPropertyAccessor( const wxString
&propName
);
235 virtual ~wxGenericPropertyAccessor();
237 void RenameProperty( const wxString
& WXUNUSED_UNLESS_DEBUG(oldName
),
238 const wxString
& newName
)
240 wxASSERT( oldName
== m_propertyName
); m_propertyName
= newName
;
243 virtual bool HasSetter() const { return true; }
244 virtual bool HasGetter() const { return true; }
245 virtual bool HasAdder() const { return false; }
246 virtual bool HasCollectionGetter() const { return false; }
248 virtual const wxString
& GetGetterName() const
249 { return m_getterName
; }
250 virtual const wxString
& GetSetterName() const
251 { return m_setterName
; }
253 virtual void SetProperty(wxObject
*object
, const wxAny
&value
) const;
254 virtual void GetProperty(const wxObject
*object
, wxAny
&value
) const;
256 // Adding an element to a collection property
257 virtual void AddToPropertyCollection(wxObject
*WXUNUSED(object
),
258 const wxAny
&WXUNUSED(value
)) const
260 wxLogError( _("AddToPropertyCollection called on a generic accessor") );
263 // Getting a collection property
264 virtual void GetPropertyCollection( const wxObject
*WXUNUSED(obj
),
265 wxAnyList
&WXUNUSED(result
)) const
267 wxLogError ( _("GetPropertyCollection called on a generic accessor") );
271 struct wxGenericPropertyAccessorInternal
;
272 wxGenericPropertyAccessorInternal
* m_data
;
273 wxString m_propertyName
;
274 wxString m_setterName
;
275 wxString m_getterName
;
278 typedef long wxPropertyInfoFlags
;
281 // will be removed in future releases
282 wxPROP_DEPRECATED
= 0x00000001,
284 // object graph property, will be streamed with priority (after constructor properties)
285 wxPROP_OBJECT_GRAPH
= 0x00000002,
287 // this will only be streamed out and in as enum/set, the internal representation
289 wxPROP_ENUM_STORE_LONG
= 0x00000004,
291 // don't stream out this property, needed eg to avoid streaming out children
292 // that are always created by their parents
293 wxPROP_DONT_STREAM
= 0x00000008
297 // ----------------------------------------------------------------------------
300 // wxPropertyInfo is used to inquire of the property by name. It doesn't
301 // provide access to the property, only information about it. If you
302 // want access, look at wxPropertyAccessor.
303 // ----------------------------------------------------------------------------
305 class WXDLLIMPEXP_BASE wxPropertyInfo
307 friend class /* WXDLLIMPEXP_BASE */ wxDynamicClassInfo
;
310 wxPropertyInfo(wxPropertyInfo
* &iter
,
311 wxClassInfo
* itsClass
,
312 const wxString
& name
,
313 const wxString
& typeName
,
314 wxPropertyAccessor
*accessor
,
316 wxPropertyInfoFlags flags
= 0,
317 const wxString
& helpString
= wxEmptyString
,
318 const wxString
& groupString
= wxEmptyString
) :
319 m_itsClass(itsClass
),
322 m_typeName(typeName
),
323 m_collectionElementTypeInfo(NULL
),
324 m_accessor(accessor
),
327 m_helpString(helpString
),
328 m_groupString(groupString
)
333 wxPropertyInfo(wxPropertyInfo
* &iter
,
334 wxClassInfo
* itsClass
,
335 const wxString
& name
,
336 wxEventSourceTypeInfo
* type
,
337 wxPropertyAccessor
*accessor
,
339 wxPropertyInfoFlags flags
= 0,
340 const wxString
& helpString
= wxEmptyString
,
341 const wxString
& groupString
= wxEmptyString
) :
342 m_itsClass(itsClass
),
345 m_collectionElementTypeInfo(NULL
),
346 m_accessor(accessor
),
349 m_helpString(helpString
),
350 m_groupString(groupString
)
355 wxPropertyInfo(wxPropertyInfo
* &iter
,
356 wxClassInfo
* itsClass
, const wxString
& name
,
357 const wxString
& collectionTypeName
,
358 const wxString
& elementTypeName
,
359 wxPropertyAccessor
*accessor
,
360 wxPropertyInfoFlags flags
= 0,
361 const wxString
& helpString
= wxEmptyString
,
362 const wxString
& groupString
= wxEmptyString
) :
363 m_itsClass(itsClass
),
366 m_typeName(collectionTypeName
),
367 m_collectionElementTypeInfo(NULL
),
368 m_collectionElementTypeName(elementTypeName
),
369 m_accessor(accessor
),
371 m_helpString(helpString
),
372 m_groupString(groupString
)
380 // return the class this property is declared in
381 const wxClassInfo
* GetDeclaringClass() const { return m_itsClass
; }
383 // return the name of this property
384 const wxString
& GetName() const { return m_name
; }
386 // returns the flags of this property
387 wxPropertyInfoFlags
GetFlags() const { return m_flags
; }
389 // returns the short help string of this property
390 const wxString
& GetHelpString() const { return m_helpString
; }
392 // returns the group string of this property
393 const wxString
& GetGroupString() const { return m_groupString
; }
395 // return the element type info of this property (for collections, otherwise NULL)
396 const wxTypeInfo
* GetCollectionElementTypeInfo() const
398 if ( m_collectionElementTypeInfo
== NULL
)
399 m_collectionElementTypeInfo
= wxTypeInfo::FindType(m_collectionElementTypeName
);
400 return m_collectionElementTypeInfo
;
403 // return the type info of this property
404 const wxTypeInfo
* GetTypeInfo() const
406 if ( m_typeInfo
== NULL
)
407 m_typeInfo
= wxTypeInfo::FindType(m_typeName
);
411 // return the accessor for this property
412 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
414 // returns NULL if this is the last property of this class
415 wxPropertyInfo
* GetNext() const { return m_next
; }
417 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
418 wxAny
GetDefaultValue() const { return m_defaultValue
; }
422 // inserts this property at the end of the linked chain which begins
423 // with "iter" property.
424 void Insert(wxPropertyInfo
* &iter
);
426 // removes this property from the linked chain of the m_itsClass properties.
429 wxClassInfo
* m_itsClass
;
431 mutable wxTypeInfo
* m_typeInfo
;
433 mutable wxTypeInfo
* m_collectionElementTypeInfo
;
434 wxString m_collectionElementTypeName
;
435 wxPropertyAccessor
* m_accessor
;
436 wxAny m_defaultValue
;
437 wxPropertyInfoFlags m_flags
;
438 wxString m_helpString
;
439 wxString m_groupString
;
440 wxPropertyInfo
* m_next
;
442 // FIXME: what's this comment about??
443 // string representation of the default value
444 // to be assigned by the designer to the property
445 // when the component is dropped on the container.
448 // stl is giving problems when forwarding declarations, therefore we define it as a subclass
450 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxPropertyInfo
*, wxPropertyInfoMapBase
,
451 class WXDLLIMPEXP_BASE
);
453 class WXDLLIMPEXP_BASE wxPropertyInfoMap
: public wxPropertyInfoMapBase
{
456 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxAny
, wxStringToAnyHashMapBase
,
457 class WXDLLIMPEXP_BASE
);
459 class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap
: public wxStringToAnyHashMapBase
{
462 #define wxBEGIN_PROPERTIES_TABLE(theClass) \
463 wxPropertyInfo *theClass::GetPropertiesStatic() \
465 typedef theClass class_t; \
466 static wxPropertyInfo* first = NULL;
468 #define wxEND_PROPERTIES_TABLE() \
471 #define wxHIDE_PROPERTY( pname ) \
472 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
473 wxT(#pname), typeid(void).name(), NULL, wxAny(), wxPROP_DONT_STREAM, \
474 wxEmptyString, wxEmptyString );
476 #define wxPROPERTY( pname, type, setter, getter, defaultValue, flags, help, group) \
477 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
478 static wxPropertySetter##pname _setter##pname; \
479 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
480 static wxPropertyGetter##pname _getter##pname; \
481 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
482 &_getter##pname, NULL, NULL ); \
483 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
484 wxT(#pname), typeid(type).name(), &_accessor##pname, \
485 wxAny(defaultValue), flags, group, help );
487 #define wxPROPERTY_FLAGS( pname, flags, type, setter, getter,defaultValue, \
488 pflags, help, group) \
489 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
490 static wxPropertySetter##pname _setter##pname; \
491 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
492 static wxPropertyGetter##pname _getter##pname; \
493 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
494 &_getter##pname, NULL, NULL ); \
495 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
496 wxT(#pname), typeid(flags).name(), &_accessor##pname, \
497 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
499 #define wxREADONLY_PROPERTY( pname, type, getter,defaultValue, flags, help, group) \
500 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
501 static wxPropertyGetter##pname _getter##pname; \
502 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
503 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
504 wxT(#pname), typeid(type).name(),&_accessor##pname, \
505 wxAny(defaultValue), flags, help, group );
507 #define wxREADONLY_PROPERTY_FLAGS( pname, flags, type, getter,defaultValue, \
508 pflags, help, group) \
509 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
510 static wxPropertyGetter##pname _getter##pname; \
511 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
512 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
513 wxT(#pname), typeid(flags).name(),&_accessor##pname, \
514 wxAny(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
516 #define wxPROPERTY_COLLECTION( pname, colltype, addelemtype, adder, getter, \
517 flags, help, group ) \
518 wxPROPERTY_COLLECTION_ADDER( pname, class_t, addelemtype, adder ) \
519 static wxPropertyCollectionAdder##pname _adder##pname; \
520 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
521 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
522 static wxPropertyAccessor _accessor##pname( NULL, NULL,&_adder##pname, \
523 &_collectionGetter##pname ); \
524 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
525 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
526 &_accessor##pname, flags, help, group );
528 #define wxREADONLY_PROPERTY_COLLECTION( pname, colltype, addelemtype, getter, \
529 flags, help, group) \
530 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
531 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
532 static wxPropertyAccessor _accessor##pname( NULL, NULL, NULL, \
533 &_collectionGetter##pname ); \
534 static wxPropertyInfo _propertyInfo##pname( first,class_t::GetClassInfoStatic(), \
535 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
536 &_accessor##pname, flags, help, group );
538 #define wxEVENT_PROPERTY( name, eventType, eventClass ) \
539 static wxEventSourceTypeInfo _typeInfo##name( eventType, CLASSINFO( eventClass ) ); \
540 static wxPropertyInfo _propertyInfo##name( first,class_t::GetClassInfoStatic(), \
541 wxT(#name), &_typeInfo##name, NULL, wxAny() );
543 #define wxEVENT_RANGE_PROPERTY( name, eventType, lastEventType, eventClass ) \
544 static wxEventSourceTypeInfo _typeInfo##name( eventType, lastEventType, \
545 CLASSINFO( eventClass ) ); \
546 static wxPropertyInfo _propertyInfo##name( first, class_t::GetClassInfoStatic(), \
547 wxT(#name), &_typeInfo##name, NULL, wxAny() );
549 // ----------------------------------------------------------------------------
550 // Implementation Helper for Simple Properties
551 // ----------------------------------------------------------------------------
553 #define wxIMPLEMENT_PROPERTY(name, type) \
557 void Set##name( type const & p) { m_##name = p; } \
558 type const & Get##name() const { return m_##name; }
560 #endif // wxUSE_EXTENDED_RTTI
561 #endif // _XTIPROP_H_