]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xti.h
Export recently added wxRichTextXMLHelper to fix link errors.
[wxWidgets.git] / include / wx / xti.h
CommitLineData
a095505c 1/////////////////////////////////////////////////////////////////////////////
bb7eff4c 2// Name: wx/xti.h
a095505c
SC
3// Purpose: runtime metadata information (extended class info)
4// Author: Stefan Csomor
28953245 5// Modified by: Francesco Montorsi
a095505c 6// Created: 27/07/03
a095505c
SC
7// Copyright: (c) 1997 Julian Smart
8// (c) 2003 Stefan Csomor
65571936 9// Licence: wxWindows licence
a095505c
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_XTIH__
13#define _WX_XTIH__
14
a095505c
SC
15// We want to support properties, event sources and events sinks through
16// explicit declarations, using templates and specialization to make the
4393b50c 17// effort as painless as possible.
a095505c
SC
18//
19// This means we have the following domains :
20//
21// - Type Information for categorizing built in types as well as custom types
28953245 22// this includes information about enums, their values and names
cbca59a8 23// - Type safe value storage : a kind of wxVariant, called right now wxAny
28953245 24// which will be merged with wxVariant
a095505c 25// - Property Information and Property Accessors providing access to a class'
28953245 26// values and exposed event delegates
a095505c
SC
27// - Information about event handlers
28// - extended Class Information for accessing all these
29
30// ----------------------------------------------------------------------------
31// headers
32// ----------------------------------------------------------------------------
33
34#include "wx/defs.h"
af498247 35
28953245 36#if wxUSE_EXTENDED_RTTI
ab6e4913 37
cbca59a8
SC
38class WXDLLIMPEXP_FWD_BASE wxAny;
39class WXDLLIMPEXP_FWD_BASE wxAnyList;
40class WXDLLIMPEXP_FWD_BASE wxObject;
41class WXDLLIMPEXP_FWD_BASE wxString;
42class WXDLLIMPEXP_FWD_BASE wxClassInfo;
43class WXDLLIMPEXP_FWD_BASE wxHashTable;
44class WXDLLIMPEXP_FWD_BASE wxObject;
45class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
46class WXDLLIMPEXP_FWD_BASE wxHashTable;
47class WXDLLIMPEXP_FWD_BASE wxHashTable_Node;
48
49class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap;
50class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap;
51class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor;
bca6bd38
SC
52class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator;
53class WXDLLIMPEXP_FWD_BASE wxObjectAllocator;
54
cbca59a8
SC
55
56#define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
57
28953245 58#include "wx/xtitypes.h"
28953245 59#include "wx/xtihandler.h"
a095505c 60
a095505c
SC
61// ----------------------------------------------------------------------------
62// wxClassInfo
63// ----------------------------------------------------------------------------
64
cbca59a8
SC
65class WXDLLIMPEXP_BASE wxObjectFunctor
66{
67public:
68 virtual ~wxObjectFunctor();
69
70 // Invoke the actual event handler:
71 virtual void operator()(const wxObject *) = 0;
72};
73
74class WXDLLIMPEXP_FWD_BASE wxPropertyInfo;
75class WXDLLIMPEXP_FWD_BASE wxHandlerInfo;
28953245 76
a095505c 77typedef wxObject *(*wxObjectConstructorFn)(void);
cbca59a8
SC
78typedef wxPropertyInfo *(*wxPropertyInfoFn)(void);
79typedef wxHandlerInfo *(*wxHandlerInfoFn)(void);
80typedef void (*wxVariantToObjectConverter)( const wxAny &data, wxObjectFunctor* fn );
81typedef wxObject* (*wxVariantToObjectPtrConverter) ( const wxAny& data);
82typedef wxAny (*wxObjectToVariantConverter)( wxObject* );
83
84WXDLLIMPEXP_BASE wxString wxAnyGetAsString( const wxAny& data);
85WXDLLIMPEXP_BASE const wxObject* wxAnyGetAsObjectPtr( const wxAny& data);
492f9a9e 86
28953245 87class WXDLLIMPEXP_BASE wxObjectWriter;
cbca59a8 88class WXDLLIMPEXP_BASE wxObjectWriterCallback;
492f9a9e 89
28953245 90typedef bool (*wxObjectStreamingCallback) ( const wxObject *, wxObjectWriter *, \
cbca59a8
SC
91 wxObjectWriterCallback *, const wxStringToAnyHashMap & );
92
93
a095505c
SC
94
95class WXDLLIMPEXP_BASE wxClassInfo
96{
28953245 97 friend class WXDLLIMPEXP_BASE wxPropertyInfo;
9ae4b67f 98 friend class /* WXDLLIMPEXP_BASE */ wxHandlerInfo;
cbca59a8 99 friend wxObject *wxCreateDynamicObject(const wxString& name);
28953245 100
a095505c
SC
101public:
102 wxClassInfo(const wxClassInfo **_Parents,
28953245
SC
103 const wxChar *_UnitName,
104 const wxChar *_ClassName,
105 int size,
106 wxObjectConstructorFn ctor,
cbca59a8
SC
107 wxPropertyInfoFn _Props,
108 wxHandlerInfoFn _Handlers,
28953245
SC
109 wxObjectAllocatorAndCreator* _Constructor,
110 const wxChar ** _ConstructorProperties,
111 const int _ConstructorPropertiesCount,
cbca59a8 112 wxVariantToObjectPtrConverter _PtrConverter1,
28953245
SC
113 wxVariantToObjectConverter _Converter2,
114 wxObjectToVariantConverter _Converter3,
115 wxObjectStreamingCallback _streamingCallback = NULL) :
ab0881c7
VS
116 m_className(_ClassName),
117 m_objectSize(size),
118 m_objectConstructor(ctor),
119 m_next(sm_first),
cbca59a8
SC
120 m_firstPropertyFn(_Props),
121 m_firstHandlerFn(_Handlers),
122 m_firstProperty(NULL),
123 m_firstHandler(NULL),
124 m_firstInited(false),
ab0881c7 125 m_parents(_Parents),
583150e3 126 m_unitName(_UnitName),
ab0881c7
VS
127 m_constructor(_Constructor),
128 m_constructorProperties(_ConstructorProperties),
129 m_constructorPropertiesCount(_ConstructorPropertiesCount),
130 m_variantOfPtrToObjectConverter(_PtrConverter1),
131 m_variantToObjectConverter(_Converter2),
132 m_objectToVariantConverter(_Converter3),
133 m_streamingCallback(_streamingCallback)
ae820c69
SC
134 {
135 sm_first = this;
28953245 136 Register();
ae820c69
SC
137 }
138
ab0881c7 139 wxClassInfo(const wxChar *_UnitName, const wxChar *_ClassName,
583150e3 140 const wxClassInfo **_Parents) :
ab0881c7
VS
141 m_className(_ClassName),
142 m_objectSize(0),
143 m_objectConstructor(NULL),
144 m_next(sm_first),
cbca59a8
SC
145 m_firstPropertyFn(NULL),
146 m_firstHandlerFn(NULL),
ab0881c7
VS
147 m_firstProperty(NULL),
148 m_firstHandler(NULL),
cbca59a8 149 m_firstInited(true),
ab0881c7
VS
150 m_parents(_Parents),
151 m_unitName(_UnitName),
152 m_constructor(NULL),
153 m_constructorProperties(NULL),
154 m_constructorPropertiesCount(0),
155 m_variantOfPtrToObjectConverter(NULL),
156 m_variantToObjectConverter(NULL),
157 m_objectToVariantConverter(NULL),
158 m_streamingCallback(NULL)
ae820c69
SC
159 {
160 sm_first = this;
28953245
SC
161 Register();
162 }
163
164 // ctor compatible with old RTTI system
165 wxClassInfo(const wxChar *_ClassName,
166 const wxClassInfo *_Parent1,
167 const wxClassInfo *_Parent2,
168 int size,
169 wxObjectConstructorFn ctor) :
170 m_className(_ClassName),
171 m_objectSize(size),
172 m_objectConstructor(ctor),
173 m_next(sm_first),
cbca59a8
SC
174 m_firstPropertyFn(NULL),
175 m_firstHandlerFn(NULL),
28953245
SC
176 m_firstProperty(NULL),
177 m_firstHandler(NULL),
cbca59a8 178 m_firstInited(true),
28953245
SC
179 m_parents(NULL),
180 m_unitName(NULL),
181 m_constructor(NULL),
182 m_constructorProperties(NULL),
183 m_constructorPropertiesCount(0),
184 m_variantOfPtrToObjectConverter(NULL),
185 m_variantToObjectConverter(NULL),
186 m_objectToVariantConverter(NULL),
187 m_streamingCallback(NULL)
188 {
189 sm_first = this;
190 m_parents[0] = _Parent1;
191 m_parents[1] = _Parent2;
192 m_parents[2] = NULL;
193 Register();
ae820c69 194 }
2d51f067 195
28953245 196 virtual ~wxClassInfo();
a095505c 197
28953245
SC
198 // allocates an instance of this class, this object does not have to be
199 // initialized or fully constructed as this call will be followed by a call to Create
200 virtual wxObject *AllocateObject() const
201 { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
ab6e4913
SC
202
203 // 'old naming' for AllocateObject staying here for backward compatibility
28953245 204 wxObject *CreateObject() const { return AllocateObject(); }
a095505c 205
583150e3 206 // direct construction call for classes that cannot construct instances via alloc/create
cbca59a8 207 wxObject *ConstructObject(int ParamCount, wxAny *Params) const;
583150e3 208
bca6bd38
SC
209 bool NeedsDirectConstruction() const;
210
28953245
SC
211 const wxChar *GetClassName() const
212 { return m_className; }
79fdfb35
SC
213 const wxChar *GetBaseClassName1() const
214 { return m_parents[0] ? m_parents[0]->GetClassName() : NULL; }
215 const wxChar *GetBaseClassName2() const
216 { return (m_parents[0] && m_parents[1]) ? m_parents[1]->GetClassName() : NULL; }
a095505c 217
28953245
SC
218 const wxClassInfo *GetBaseClass1() const
219 { return m_parents[0]; }
220 const wxClassInfo *GetBaseClass2() const
221 { return m_parents[0] ? m_parents[1] : NULL; }
222
223 const wxChar *GetIncludeName() const
224 { return m_unitName; }
225 const wxClassInfo **GetParents() const
226 { return m_parents; }
227 int GetSize() const
228 { return m_objectSize; }
229 bool IsDynamic() const
230 { return (NULL != m_objectConstructor); }
231
232 wxObjectConstructorFn GetConstructor() const
233 { return m_objectConstructor; }
234 const wxClassInfo *GetNext() const
235 { return m_next; }
236
237 // statics:
238
239 static void CleanUp();
cbca59a8 240 static wxClassInfo *FindClass(const wxString& className);
28953245
SC
241 static const wxClassInfo *GetFirst()
242 { return sm_first; }
243
a095505c 244
ae820c69
SC
245 // Climb upwards through inheritance hierarchy.
246 // Dual inheritance is catered for.
a095505c 247
28953245 248 bool IsKindOf(const wxClassInfo *info) const;
a095505c 249
cbca59a8 250 wxDECLARE_CLASS_INFO_ITERATORS();
644cb537 251
ab0881c7
VS
252 // if there is a callback registered with that class it will be called
253 // before this object will be written to disk, it can veto streaming out
254 // this object by returning false, if this class has not registered a
255 // callback, the search will go up the inheritance tree if no callback has
256 // been registered true will be returned by default
cbca59a8
SC
257 bool BeforeWriteObject( const wxObject *obj, wxObjectWriter *streamer,
258 wxObjectWriterCallback *writercallback, const wxStringToAnyHashMap &metadata) const;
9c8046dd
SC
259
260 // gets the streaming callback from this class or any superclass
28953245 261 wxObjectStreamingCallback GetStreamingCallback() const;
75890a3f 262
517fb871 263 // returns the first property
cbca59a8
SC
264 wxPropertyInfo* GetFirstProperty() const
265 { EnsureInfosInited(); return m_firstProperty; }
a095505c 266
517fb871 267 // returns the first handler
cbca59a8
SC
268 wxHandlerInfo* GetFirstHandler() const
269 { EnsureInfosInited(); return m_firstHandler; }
a095505c 270
ab6e4913
SC
271 // Call the Create upon an instance of the class, in the end the object is fully
272 // initialized
cbca59a8 273 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
a095505c 274
517fb871 275 // get number of parameters for constructor
28953245
SC
276 virtual int GetCreateParamCount() const
277 { return m_constructorPropertiesCount; }
a095505c 278
ab6e4913 279 // get n-th constructor parameter
28953245
SC
280 virtual const wxChar* GetCreateParamName(int n) const
281 { return m_constructorProperties[n]; }
a095505c 282
28953245
SC
283 // Runtime access to objects for simple properties (get/set) by property
284 // name and variant data
285 virtual void SetProperty (wxObject *object, const wxChar *propertyName,
cbca59a8
SC
286 const wxAny &value) const;
287 virtual wxAny GetProperty (wxObject *object, const wxChar *propertyName) const;
ab6e4913
SC
288
289 // Runtime access to objects for collection properties by property name
cbca59a8 290 virtual wxAnyList GetPropertyCollection(wxObject *object,
28953245
SC
291 const wxChar *propertyName) const;
292 virtual void AddToPropertyCollection(wxObject *object, const wxChar *propertyName,
cbca59a8 293 const wxAny& value) const;
a095505c 294
28953245
SC
295 // we must be able to cast variants to wxObject pointers, templates seem
296 // not to be suitable
cbca59a8
SC
297 void CallOnAny( const wxAny &data, wxObjectFunctor* functor ) const;
298
299 wxObject* AnyToObjectPtr( const wxAny &data) const;
fa08490f 300
cbca59a8 301 wxAny ObjectPtrToAny( wxObject *object ) const;
4393b50c 302
517fb871 303 // find property by name
28953245 304 virtual const wxPropertyInfo *FindPropertyInfo (const wxChar *PropertyName) const;
fbbdc52c 305
517fb871 306 // find handler by name
28953245 307 virtual const wxHandlerInfo *FindHandlerInfo (const wxChar *handlerName) const;
a095505c 308
2d51f067 309 // find property by name
28953245 310 virtual wxPropertyInfo *FindPropertyInfoInThisClass (const wxChar *PropertyName) const;
2d51f067
SC
311
312 // find handler by name
28953245
SC
313 virtual wxHandlerInfo *FindHandlerInfoInThisClass (const wxChar *handlerName) const;
314
315 // puts all the properties of this class and its superclasses in the map,
316 // as long as there is not yet an entry with the same name (overriding mechanism)
317 void GetProperties( wxPropertyInfoMap &map ) const;
ae820c69 318
644cb537 319private:
a095505c
SC
320 const wxChar *m_className;
321 int m_objectSize;
28953245 322 wxObjectConstructorFn m_objectConstructor;
a095505c
SC
323
324 // class info object live in a linked list:
325 // pointers to its head and the next element in it
326
327 static wxClassInfo *sm_first;
28953245 328 wxClassInfo *m_next;
a095505c 329
a095505c
SC
330 static wxHashTable *sm_classTable;
331
cbca59a8
SC
332 wxPropertyInfoFn m_firstPropertyFn;
333 wxHandlerInfoFn m_firstHandlerFn;
334
cbca59a8 335
28953245 336protected:
cbca59a8
SC
337 void EnsureInfosInited() const
338 {
339 if ( !m_firstInited)
340 {
341 if ( m_firstPropertyFn != NULL)
342 m_firstProperty = (*m_firstPropertyFn)();
343 if ( m_firstHandlerFn != NULL)
344 m_firstHandler = (*m_firstHandlerFn)();
345 m_firstInited = true;
346 }
347 }
348 mutable wxPropertyInfo* m_firstProperty;
349 mutable wxHandlerInfo* m_firstHandler;
28953245 350
a095505c 351private:
9ae4b67f
SC
352 mutable bool m_firstInited;
353
28953245
SC
354 const wxClassInfo** m_parents;
355 const wxChar* m_unitName;
356
357 wxObjectAllocatorAndCreator* m_constructor;
358 const wxChar ** m_constructorProperties;
359 const int m_constructorPropertiesCount;
cbca59a8 360 wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter;
28953245
SC
361 wxVariantToObjectConverter m_variantToObjectConverter;
362 wxObjectToVariantConverter m_objectToVariantConverter;
363 wxObjectStreamingCallback m_streamingCallback;
364
365 const wxPropertyAccessor *FindAccessor (const wxChar *propertyName) const;
a095505c 366
d1d738f1
VS
367protected:
368 // registers the class
369 void Register();
370 void Unregister();
a095505c 371
28953245 372 DECLARE_NO_COPY_CLASS(wxClassInfo)
a095505c
SC
373};
374
cbca59a8 375WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name);
a095505c 376
2d51f067 377// ----------------------------------------------------------------------------
28953245 378// wxDynamicClassInfo
2d51f067 379// ----------------------------------------------------------------------------
28953245 380
2d51f067
SC
381// this object leads to having a pure runtime-instantiation
382
492f9a9e 383class WXDLLIMPEXP_BASE wxDynamicClassInfo : public wxClassInfo
2d51f067 384{
28953245
SC
385 friend class WXDLLIMPEXP_BASE wxDynamicObject;
386
387public:
388 wxDynamicClassInfo( const wxChar *_UnitName, const wxChar *_ClassName,
389 const wxClassInfo* superClass );
390 virtual ~wxDynamicClassInfo();
2d51f067
SC
391
392 // constructs a wxDynamicObject with an instance
28953245 393 virtual wxObject *AllocateObject() const;
2d51f067
SC
394
395 // Call the Create method for a class
cbca59a8 396 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
2d51f067
SC
397
398 // get number of parameters for constructor
28953245 399 virtual int GetCreateParamCount() const;
2d51f067
SC
400
401 // get i-th constructor parameter
28953245 402 virtual const wxChar* GetCreateParamName(int i) const;
2d51f067
SC
403
404 // Runtime access to objects by property name, and variant data
28953245 405 virtual void SetProperty (wxObject *object, const wxChar *PropertyName,
cbca59a8
SC
406 const wxAny &Value) const;
407 virtual wxAny GetProperty (wxObject *object, const wxChar *PropertyName) const;
2d51f067 408
2abce515 409 // adds a property to this class at runtime
28953245 410 void AddProperty( const wxChar *propertyName, const wxTypeInfo* typeInfo );
2abce515
SC
411
412 // removes an existing runtime-property
28953245 413 void RemoveProperty( const wxChar *propertyName );
2abce515 414
492f9a9e 415 // renames an existing runtime-property
28953245 416 void RenameProperty( const wxChar *oldPropertyName, const wxChar *newPropertyName );
492f9a9e 417
2abce515 418 // as a handler to this class at runtime
28953245
SC
419 void AddHandler( const wxChar *handlerName, wxObjectEventFunction address,
420 const wxClassInfo* eventClassInfo );
2abce515
SC
421
422 // removes an existing runtime-handler
28953245 423 void RemoveHandler( const wxChar *handlerName );
492f9a9e
SC
424
425 // renames an existing runtime-handler
28953245
SC
426 void RenameHandler( const wxChar *oldHandlerName, const wxChar *newHandlerName );
427
428private:
429 struct wxDynamicClassInfoInternal;
430 wxDynamicClassInfoInternal* m_data;
431};
432
a095505c 433// ----------------------------------------------------------------------------
28953245 434// wxDECLARE class macros
a095505c
SC
435// ----------------------------------------------------------------------------
436
28953245
SC
437#define _DECLARE_DYNAMIC_CLASS(name) \
438 public: \
439 static wxClassInfo ms_classInfo; \
440 static const wxClassInfo* ms_classParents[]; \
441 static wxPropertyInfo* GetPropertiesStatic(); \
442 static wxHandlerInfo* GetHandlersStatic(); \
443 static wxClassInfo *GetClassInfoStatic() \
444 { return &name::ms_classInfo; } \
445 virtual wxClassInfo *GetClassInfo() const \
446 { return &name::ms_classInfo; }
447
448#define wxDECLARE_DYNAMIC_CLASS(name) \
449 static wxObjectAllocatorAndCreator* ms_constructor; \
450 static const wxChar * ms_constructorProperties[]; \
451 static const int ms_constructorPropertiesCount; \
f0b7eadf 452 _DECLARE_DYNAMIC_CLASS(name)
a095505c 453
28953245 454#define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
cbca59a8 455 wxDECLARE_NO_ASSIGN_CLASS(name); \
28953245 456 wxDECLARE_DYNAMIC_CLASS(name)
a095505c 457
28953245 458#define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
cbca59a8
SC
459 wxDECLARE_NO_COPY_CLASS(name); \
460 wxDECLARE_DYNAMIC_CLASS(name)
461
462#define wxDECLARE_CLASS(name) \
28953245 463 wxDECLARE_DYNAMIC_CLASS(name)
a095505c 464
28953245
SC
465#define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
466#define wxCLASSINFO(name) (&name::ms_classInfo)
a095505c 467
28953245 468#endif // wxUSE_EXTENDED_RTTI
c0089c96 469#endif // _WX_XTIH__