1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XTI properties
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
7 // RCS-ID: $Id: xti.h 47299 2007-07-10 15:58:27Z FM $
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
18 #if wxUSE_EXTENDED_RTTI
20 #include "wx/string.h"
21 #include "wx/variant.h"
24 #include "wx/xtitypes.h"
26 class WXDLLIMPEXP_BASE wxObject
;
27 class WXDLLIMPEXP_BASE wxClassInfo
;
28 class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
29 class WXDLLIMPEXP_BASE wxHashTable
;
30 class WXDLLIMPEXP_BASE wxHashTable_Node
;
31 class WXDLLIMPEXP_BASE wxObjectRefData
;
32 class WXDLLIMPEXP_BASE wxEvent
;
33 class WXDLLIMPEXP_BASE wxEvtHandler
;
35 // ----------------------------------------------------------------------------
38 // wxPropertySetter/Getter/CollectionGetter/CollectionAdder are all property
39 // accessors which are managed by wxPropertyAccessor class which in turn is
40 // handled by wxPropertyInfo.
41 // ----------------------------------------------------------------------------
43 class WXDLLIMPEXP_BASE wxPropertySetter
46 wxPropertySetter( const wxString name
) { m_name
= name
; }
47 virtual ~wxPropertySetter() {}
49 virtual void Set( wxObject
*object
, const wxVariantBase
&variantValue
) const = 0;
50 const wxString
& GetName() const { return m_name
; }
56 class WXDLLIMPEXP_BASE wxPropertyGetter
59 wxPropertyGetter( const wxString name
) { m_name
= name
; }
60 virtual ~wxPropertyGetter() {}
62 virtual void Get( const wxObject
*object
, wxVariantBase
& result
) const = 0;
63 const wxString
& GetName() const { return m_name
; }
69 class WXDLLIMPEXP_BASE wxPropertyCollectionGetter
72 wxPropertyCollectionGetter( const wxString name
) { m_name
= name
; }
73 virtual ~wxPropertyCollectionGetter() {}
75 virtual void Get( const wxObject
*object
, wxVariantBaseArray
& result
) const = 0;
76 const wxString
& GetName() const { return m_name
; }
82 template<typename coll_t
> void WXDLLIMPEXP_BASE \
83 wxCollectionToVariantArray( const coll_t
& coll
, wxVariantBaseArray
& result
);
85 class WXDLLIMPEXP_BASE wxPropertyCollectionAdder
88 wxPropertyCollectionAdder( const wxString name
) { m_name
= name
; }
89 virtual ~wxPropertyCollectionAdder() {}
91 virtual void Add( wxObject
*object
, const wxVariantBase
&variantValue
) const= 0;
92 const wxString
& GetName() const { return m_name
; }
98 #define wxPROPERTY_SETTER( property, Klass, valueType, setterMethod ) \
99 class wxPropertySetter##property : public wxPropertySetter \
102 wxINFUNC_CLASS_TYPE_FIX(Klass) \
103 wxPropertySetter##property() : wxPropertySetter( wxT(#setterMethod) ) {} \
104 virtual ~wxPropertySetter##property() {} \
106 void Set( wxObject *object, const wxVariantBase &variantValue ) const \
108 Klass *obj = dynamic_cast<Klass*>(object); \
109 if ( variantValue.wxTEMPLATED_MEMBER_CALL(HasData, valueType) ) \
110 obj->setterMethod(variantValue.wxTEMPLATED_MEMBER_CALL(Get, valueType)); \
112 obj->setterMethod(*variantValue.wxTEMPLATED_MEMBER_CALL(Get, valueType*)); \
116 #define wxPROPERTY_GETTER( property, Klass, valueType, gettermethod ) \
117 class wxPropertyGetter##property : public wxPropertyGetter \
120 wxINFUNC_CLASS_TYPE_FIX(Klass) \
121 wxPropertyGetter##property() : wxPropertyGetter( wxT(#gettermethod) ) {} \
122 virtual ~wxPropertyGetter##property() {} \
124 void Get( const wxObject *object, wxVariantBase &result) const \
126 const Klass *obj = dynamic_cast<const Klass*>(object); \
127 result = wxVariantBase( obj->gettermethod() ); \
131 #define wxPROPERTY_COLLECTION_ADDER( property, Klass, valueType, addermethod ) \
132 class wxPropertyCollectionAdder##property : public wxPropertyCollectionAdder \
135 wxINFUNC_CLASS_TYPE_FIX(Klass) \
136 wxPropertyCollectionAdder##property() : wxPropertyCollectionAdder( wxT(#addermethod) ) {} \
137 virtual ~wxPropertyCollectionAdder##property() {} \
139 void Add( wxObject *object, const wxVariantBase &variantValue ) const \
141 Klass *obj = dynamic_cast<Klass*>(object); \
142 if ( variantValue.wxTEMPLATED_MEMBER_CALL(HasData, valueType) ) \
143 obj->addermethod(variantValue.wxTEMPLATED_MEMBER_CALL(Get, valueType)); \
145 obj->addermethod(*variantValue.wxTEMPLATED_MEMBER_CALL(Get, 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, wxVariantBaseArray &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 wxVariantBase
&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
, wxVariantBase
&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 wxVariantBase
&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
, wxVariantBaseArray
&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 wxVariantBase
&value
) const;
254 virtual void GetProperty(const wxObject
*object
, wxVariantBase
&value
) const;
256 // Adding an element to a collection property
257 virtual void AddToPropertyCollection(wxObject
*WXUNUSED(object
),
258 const wxVariantBase
&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 wxVariantBaseArray
&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
)
334 wxPropertyInfo(wxPropertyInfo
* &iter
,
335 wxClassInfo
* itsClass
,
336 const wxString
& name
,
337 const char* typeName
,
338 wxPropertyAccessor
*accessor
,
340 wxPropertyInfoFlags flags
= 0,
341 const wxString
& helpString
= wxEmptyString
,
342 const wxString
& groupString
= wxEmptyString
) :
343 m_itsClass(itsClass
),
346 m_typeName(wxString::FromAscii(typeName
)),
347 m_collectionElementTypeInfo(NULL
),
348 m_accessor(accessor
),
351 m_helpString(helpString
),
352 m_groupString(groupString
)
357 wxPropertyInfo(wxPropertyInfo
* &iter
,
358 wxClassInfo
* itsClass
,
359 const wxString
& name
,
360 wxEventSourceTypeInfo
* type
,
361 wxPropertyAccessor
*accessor
,
363 wxPropertyInfoFlags flags
= 0,
364 const wxString
& helpString
= wxEmptyString
,
365 const wxString
& groupString
= wxEmptyString
) :
366 m_itsClass(itsClass
),
369 m_collectionElementTypeInfo(NULL
),
370 m_accessor(accessor
),
373 m_helpString(helpString
),
374 m_groupString(groupString
)
379 wxPropertyInfo(wxPropertyInfo
* &iter
,
380 wxClassInfo
* itsClass
, const wxString
& name
,
381 const wxString
& collectionTypeName
,
382 const wxString
& elementTypeName
,
383 wxPropertyAccessor
*accessor
,
384 wxPropertyInfoFlags flags
= 0,
385 const wxString
& helpString
= wxEmptyString
,
386 const wxString
& groupString
= wxEmptyString
) :
387 m_itsClass(itsClass
),
390 m_typeName(collectionTypeName
),
391 m_collectionElementTypeInfo(NULL
),
392 m_collectionElementTypeName(elementTypeName
),
393 m_accessor(accessor
),
395 m_helpString(helpString
),
396 m_groupString(groupString
)
402 wxPropertyInfo(wxPropertyInfo
* &iter
,
403 wxClassInfo
* itsClass
, const wxString
& name
,
404 const char* collectionTypeName
,
405 const char* elementTypeName
,
406 wxPropertyAccessor
*accessor
,
407 wxPropertyInfoFlags flags
= 0,
408 const wxString
& helpString
= wxEmptyString
,
409 const wxString
& groupString
= wxEmptyString
) :
410 m_itsClass(itsClass
),
413 m_typeName(wxString::FromAscii(collectionTypeName
)),
414 m_collectionElementTypeInfo(NULL
),
415 m_collectionElementTypeName(wxString::FromAscii(elementTypeName
)),
416 m_accessor(accessor
),
418 m_helpString(helpString
),
419 m_groupString(groupString
)
427 // return the class this property is declared in
428 const wxClassInfo
* GetDeclaringClass() const { return m_itsClass
; }
430 // return the name of this property
431 const wxString
& GetName() const { return m_name
; }
433 // returns the flags of this property
434 wxPropertyInfoFlags
GetFlags() const { return m_flags
; }
436 // returns the short help string of this property
437 const wxString
& GetHelpString() const { return m_helpString
; }
439 // returns the group string of this property
440 const wxString
& GetGroupString() const { return m_groupString
; }
442 // return the element type info of this property (for collections, otherwise NULL)
443 const wxTypeInfo
* GetCollectionElementTypeInfo() const
445 if ( m_collectionElementTypeInfo
== NULL
)
446 m_collectionElementTypeInfo
= wxTypeInfo::FindType(m_collectionElementTypeName
);
447 return m_collectionElementTypeInfo
;
450 // return the type info of this property
451 const wxTypeInfo
* GetTypeInfo() const
453 if ( m_typeInfo
== NULL
)
454 m_typeInfo
= wxTypeInfo::FindType(m_typeName
);
458 // return the accessor for this property
459 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
461 // returns NULL if this is the last property of this class
462 wxPropertyInfo
* GetNext() const { return m_next
; }
464 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
465 wxVariantBase
GetDefaultValue() const { return m_defaultValue
; }
469 // inserts this property at the end of the linked chain which begins
470 // with "iter" property.
471 void Insert(wxPropertyInfo
* &iter
);
473 // removes this property from the linked chain of the m_itsClass properties.
476 wxClassInfo
* m_itsClass
;
478 mutable wxTypeInfo
* m_typeInfo
;
480 mutable wxTypeInfo
* m_collectionElementTypeInfo
;
481 wxString m_collectionElementTypeName
;
482 wxPropertyAccessor
* m_accessor
;
483 wxVariantBase m_defaultValue
;
484 wxPropertyInfoFlags m_flags
;
485 wxString m_helpString
;
486 wxString m_groupString
;
487 wxPropertyInfo
* m_next
;
489 // FIXME: what's this comment about??
490 // string representation of the default value
491 // to be assigned by the designer to the property
492 // when the component is dropped on the container.
495 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxPropertyInfo
*, wxPropertyInfoMap
,
496 class WXDLLIMPEXP_BASE
);
498 #define wxBEGIN_PROPERTIES_TABLE(theClass) \
499 wxPropertyInfo *theClass::GetPropertiesStatic() \
501 typedef theClass class_t; \
502 static wxPropertyInfo* first = NULL;
504 #define wxEND_PROPERTIES_TABLE() \
507 #define wxHIDE_PROPERTY( pname ) \
508 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
509 wxT(#pname), typeid(void).name(), NULL, wxVariantBase(), wxPROP_DONT_STREAM, \
510 wxEmptyString, wxEmptyString );
512 #define wxPROPERTY( pname, type, setter, getter, defaultValue, flags, help, group) \
513 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
514 static wxPropertySetter##pname _setter##pname; \
515 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
516 static wxPropertyGetter##pname _getter##pname; \
517 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
518 &_getter##pname, NULL, NULL ); \
519 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
520 wxT(#pname), typeid(type).name(), &_accessor##pname, \
521 wxVariantBase(defaultValue), flags, group, help );
523 #define wxPROPERTY_FLAGS( pname, flags, type, setter, getter,defaultValue, \
524 pflags, help, group) \
525 wxPROPERTY_SETTER( pname, class_t, type, setter ) \
526 static wxPropertySetter##pname _setter##pname; \
527 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
528 static wxPropertyGetter##pname _getter##pname; \
529 static wxPropertyAccessor _accessor##pname( &_setter##pname, \
530 &_getter##pname, NULL, NULL ); \
531 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
532 wxT(#pname), typeid(flags).name(), &_accessor##pname, \
533 wxVariantBase(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
535 #define wxREADONLY_PROPERTY( pname, type, getter,defaultValue, flags, help, group) \
536 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
537 static wxPropertyGetter##pname _getter##pname; \
538 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
539 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
540 wxT(#pname), typeid(type).name(),&_accessor##pname, \
541 wxVariantBase(defaultValue), flags, help, group );
543 #define wxREADONLY_PROPERTY_FLAGS( pname, flags, type, getter,defaultValue, \
544 pflags, help, group) \
545 wxPROPERTY_GETTER( pname, class_t, type, getter ) \
546 static wxPropertyGetter##pname _getter##pname; \
547 static wxPropertyAccessor _accessor##pname( NULL, &_getter##pname, NULL, NULL ); \
548 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
549 wxT(#pname), typeid(flags).name(),&_accessor##pname, \
550 wxVariantBase(defaultValue), wxPROP_ENUM_STORE_LONG | pflags, help, group );
552 #define wxPROPERTY_COLLECTION( pname, colltype, addelemtype, adder, getter, \
553 flags, help, group ) \
554 wxPROPERTY_COLLECTION_ADDER( pname, class_t, addelemtype, adder ) \
555 static wxPropertyCollectionAdder##pname _adder##pname; \
556 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
557 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
558 static wxPropertyAccessor _accessor##pname( NULL, NULL,&_adder##pname, \
559 &_collectionGetter##pname ); \
560 static wxPropertyInfo _propertyInfo##pname( first, class_t::GetClassInfoStatic(), \
561 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
562 &_accessor##pname, flags, help, group );
564 #define wxREADONLY_PROPERTY_COLLECTION( pname, colltype, addelemtype, getter, \
565 flags, help, group) \
566 wxPROPERTY_COLLECTION_GETTER( pname, class_t, colltype, getter ) \
567 static wxPropertyCollectionGetter##pname _collectionGetter##pname; \
568 static wxPropertyAccessor _accessor##pname( NULL, NULL, NULL, \
569 &_collectionGetter##pname ); \
570 static wxPropertyInfo _propertyInfo##pname( first,class_t::GetClassInfoStatic(), \
571 wxT(#pname), typeid(colltype).name(),typeid(addelemtype).name(), \
572 &_accessor##pname, flags, help, group );
574 #define wxEVENT_PROPERTY( name, eventType, eventClass ) \
575 static wxEventSourceTypeInfo _typeInfo##name( eventType, CLASSINFO( eventClass ) ); \
576 static wxPropertyInfo _propertyInfo##name( first,class_t::GetClassInfoStatic(), \
577 wxT(#name), &_typeInfo##name, NULL, wxVariantBase() );
579 #define wxEVENT_RANGE_PROPERTY( name, eventType, lastEventType, eventClass ) \
580 static wxEventSourceTypeInfo _typeInfo##name( eventType, lastEventType, \
581 CLASSINFO( eventClass ) ); \
582 static wxPropertyInfo _propertyInfo##name( first, class_t::GetClassInfoStatic(), \
583 wxT(#name), &_typeInfo##name, NULL, wxVariantBase() );
585 // ----------------------------------------------------------------------------
586 // Implementation Helper for Simple Properties
587 // ----------------------------------------------------------------------------
589 #define wxIMPLEMENT_PROPERTY(name, type) \
593 void Set##name( type const & p) { m_##name = p; } \
594 type const & Get##name() const { return m_##name; }
596 #endif // wxUSE_EXTENDED_RTTI
597 #endif // _XTIPROP_H_