Registering custom editors for types that are not descended from TObject

By: Arkadi Makarov

Abstract: This article shows how to register custom property editors for property types that aren't descendants of TObject. By Nikolay Antonov.

Property editors are a key element of C++Builder's RAD power. And custom property editors, which allow developers to set properties for objects, make component-based development even better.

To associate a custom property editor with the property type of a components class, the RegisterPropertyEditor function is used:

extern PACKAGE void __fastcall RegisterPropertyEditor(Typinfo::PtypeInfo PropertyType,
	System::TMetaClass* ComponentClass, const AnsiString PropertyName,
	System::TMetaClass* EditorClass);

Specifying the ComponentClass, PropertyName, and EditorClass parameters of this function is simple.

The C++Builder Help file suggests that the __typeinfo macro be used to defined the PropertyType parameter if necessary:

#define __typeinfo(type) (PTypeInfo)TObject::ClassInfo(__classid(type))

As you can see, this macro can be applied only to classes that are descendants of TObject. Therefore, custom editors for properties of other types cant be registered this way.

Or they couldn't...until now.

To solve this problem it is necessary to find an alternate way to get type information pointers for properties of types which are not descendants of TObject. For this purpose the GetPropInfo function can be used. It is defined in the typinfo.hpp as:

extern PACKAGE PPropInfo __fastcall GetPropInfo(PTypeInfo TypeInfo, 
	const AnsiString PropName);

This function returns a pointer to the property information record for the property specified by the PropName parameter. The components class is determined by the TypeInfo parameter. The property information record contains the PropType pointer to the type information pointer.

To get the type information pointer for property of the components class (the PropertyType parameter for call of RegisterPropertyEditor), the following construction can be used:

Typinfo::PTypeInfo PropertyType = *GetPropInfo((Typinfo::TTypeInfo*)
	TObject::ClassInfo(ComponentClass), PropertyName)->PropType;

Now a macro can be used. It is defined as follows:

#define proptypeinfo(ComponentClass, PropertyName)
	(*GetPropInfo((Typinfo::TTypeInfo*) TObject::ClassInfo(ComponentClass),
	PropertyName)->PropType)

The source code can be simplified to register the custom property editor by the following function:

void __fastcall RegisterPropertyEditorEx (System::TMetaClass* ComponentClass,
	const AnsiString PropertyName, System::TMetaClass* EditorClass)
{
	RegisterPropertyEditor(proptypeinfo (ComponentClass, PropertyName),
		ComponentClass, PropertyName, EditorClass);
}

More info can receive on www.mblab.by.ru.

Nikolay Antonov, MBLab


Server Response from: ETNASC04