]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xti.h
Fix assert when destroying wxDataViewCtrl being edited in wxGTK.
[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
SC
6// Created: 27/07/03
7// RCS-ID: $Id$
8// Copyright: (c) 1997 Julian Smart
9// (c) 2003 Stefan Csomor
65571936 10// Licence: wxWindows licence
a095505c
SC
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef _WX_XTIH__
14#define _WX_XTIH__
15
a095505c
SC
16// We want to support properties, event sources and events sinks through
17// explicit declarations, using templates and specialization to make the
4393b50c 18// effort as painless as possible.
a095505c
SC
19//
20// This means we have the following domains :
21//
22// - Type Information for categorizing built in types as well as custom types
28953245 23// this includes information about enums, their values and names
cbca59a8 24// - Type safe value storage : a kind of wxVariant, called right now wxAny
28953245 25// which will be merged with wxVariant
a095505c 26// - Property Information and Property Accessors providing access to a class'
28953245 27// values and exposed event delegates
a095505c
SC
28// - Information about event handlers
29// - extended Class Information for accessing all these
30
31// ----------------------------------------------------------------------------
32// headers
33// ----------------------------------------------------------------------------
34
35#include "wx/defs.h"
af498247 36
28953245 37#if wxUSE_EXTENDED_RTTI
ab6e4913 38
cbca59a8
SC
39class WXDLLIMPEXP_FWD_BASE wxAny;
40class WXDLLIMPEXP_FWD_BASE wxAnyList;
41class WXDLLIMPEXP_FWD_BASE wxObject;
42class WXDLLIMPEXP_FWD_BASE wxString;
43class WXDLLIMPEXP_FWD_BASE wxClassInfo;
44class WXDLLIMPEXP_FWD_BASE wxHashTable;
45class WXDLLIMPEXP_FWD_BASE wxObject;
46class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
47class WXDLLIMPEXP_FWD_BASE wxHashTable;
48class WXDLLIMPEXP_FWD_BASE wxHashTable_Node;
49
50class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap;
51class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap;
52class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor;
bca6bd38
SC
53class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator;
54class WXDLLIMPEXP_FWD_BASE wxObjectAllocator;
55
cbca59a8
SC
56
57#define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
58
28953245 59#include "wx/xtitypes.h"
28953245 60#include "wx/xtihandler.h"
a095505c 61
a095505c
SC
62// ----------------------------------------------------------------------------
63// wxClassInfo
64// ----------------------------------------------------------------------------
65
cbca59a8
SC
66class WXDLLIMPEXP_BASE wxObjectFunctor
67{
68public:
69 virtual ~wxObjectFunctor();
70
71 // Invoke the actual event handler:
72 virtual void operator()(const wxObject *) = 0;
73};
74
75class WXDLLIMPEXP_FWD_BASE wxPropertyInfo;
76class WXDLLIMPEXP_FWD_BASE wxHandlerInfo;
28953245 77
a095505c 78typedef wxObject *(*wxObjectConstructorFn)(void);
cbca59a8
SC
79typedef wxPropertyInfo *(*wxPropertyInfoFn)(void);
80typedef wxHandlerInfo *(*wxHandlerInfoFn)(void);
81typedef void (*wxVariantToObjectConverter)( const wxAny &data, wxObjectFunctor* fn );
82typedef wxObject* (*wxVariantToObjectPtrConverter) ( const wxAny& data);
83typedef wxAny (*wxObjectToVariantConverter)( wxObject* );
84
85WXDLLIMPEXP_BASE wxString wxAnyGetAsString( const wxAny& data);
86WXDLLIMPEXP_BASE const wxObject* wxAnyGetAsObjectPtr( const wxAny& data);
492f9a9e 87
28953245 88class WXDLLIMPEXP_BASE wxObjectWriter;
cbca59a8 89class WXDLLIMPEXP_BASE wxObjectWriterCallback;
492f9a9e 90
28953245 91typedef bool (*wxObjectStreamingCallback) ( const wxObject *, wxObjectWriter *, \
cbca59a8
SC
92 wxObjectWriterCallback *, const wxStringToAnyHashMap & );
93
94
a095505c
SC
95
96class WXDLLIMPEXP_BASE wxClassInfo
97{
28953245
SC
98 friend class WXDLLIMPEXP_BASE wxPropertyInfo;
99 friend class WXDLLIMPEXP_BASE wxHandlerInfo;
cbca59a8 100 friend wxObject *wxCreateDynamicObject(const wxString& name);
28953245 101
a095505c
SC
102public:
103 wxClassInfo(const wxClassInfo **_Parents,
28953245
SC
104 const wxChar *_UnitName,
105 const wxChar *_ClassName,
106 int size,
107 wxObjectConstructorFn ctor,
cbca59a8
SC
108 wxPropertyInfoFn _Props,
109 wxHandlerInfoFn _Handlers,
28953245
SC
110 wxObjectAllocatorAndCreator* _Constructor,
111 const wxChar ** _ConstructorProperties,
112 const int _ConstructorPropertiesCount,
cbca59a8 113 wxVariantToObjectPtrConverter _PtrConverter1,
28953245
SC
114 wxVariantToObjectConverter _Converter2,
115 wxObjectToVariantConverter _Converter3,
116 wxObjectStreamingCallback _streamingCallback = NULL) :
ab0881c7
VS
117 m_className(_ClassName),
118 m_objectSize(size),
119 m_objectConstructor(ctor),
120 m_next(sm_first),
cbca59a8
SC
121 m_firstPropertyFn(_Props),
122 m_firstHandlerFn(_Handlers),
123 m_firstProperty(NULL),
124 m_firstHandler(NULL),
125 m_firstInited(false),
ab0881c7 126 m_parents(_Parents),
583150e3 127 m_unitName(_UnitName),
ab0881c7
VS
128 m_constructor(_Constructor),
129 m_constructorProperties(_ConstructorProperties),
130 m_constructorPropertiesCount(_ConstructorPropertiesCount),
131 m_variantOfPtrToObjectConverter(_PtrConverter1),
132 m_variantToObjectConverter(_Converter2),
133 m_objectToVariantConverter(_Converter3),
134 m_streamingCallback(_streamingCallback)
ae820c69
SC
135 {
136 sm_first = this;
28953245 137 Register();
ae820c69
SC
138 }
139
ab0881c7 140 wxClassInfo(const wxChar *_UnitName, const wxChar *_ClassName,
583150e3 141 const wxClassInfo **_Parents) :
ab0881c7
VS
142 m_className(_ClassName),
143 m_objectSize(0),
144 m_objectConstructor(NULL),
145 m_next(sm_first),
cbca59a8
SC
146 m_firstPropertyFn(NULL),
147 m_firstHandlerFn(NULL),
ab0881c7
VS
148 m_firstProperty(NULL),
149 m_firstHandler(NULL),
cbca59a8 150 m_firstInited(true),
ab0881c7
VS
151 m_parents(_Parents),
152 m_unitName(_UnitName),
153 m_constructor(NULL),
154 m_constructorProperties(NULL),
155 m_constructorPropertiesCount(0),
156 m_variantOfPtrToObjectConverter(NULL),
157 m_variantToObjectConverter(NULL),
158 m_objectToVariantConverter(NULL),
159 m_streamingCallback(NULL)
ae820c69
SC
160 {
161 sm_first = this;
28953245
SC
162 Register();
163 }
164
165 // ctor compatible with old RTTI system
166 wxClassInfo(const wxChar *_ClassName,
167 const wxClassInfo *_Parent1,
168 const wxClassInfo *_Parent2,
169 int size,
170 wxObjectConstructorFn ctor) :
171 m_className(_ClassName),
172 m_objectSize(size),
173 m_objectConstructor(ctor),
174 m_next(sm_first),
cbca59a8
SC
175 m_firstPropertyFn(NULL),
176 m_firstHandlerFn(NULL),
28953245
SC
177 m_firstProperty(NULL),
178 m_firstHandler(NULL),
cbca59a8 179 m_firstInited(true),
28953245
SC
180 m_parents(NULL),
181 m_unitName(NULL),
182 m_constructor(NULL),
183 m_constructorProperties(NULL),
184 m_constructorPropertiesCount(0),
185 m_variantOfPtrToObjectConverter(NULL),
186 m_variantToObjectConverter(NULL),
187 m_objectToVariantConverter(NULL),
188 m_streamingCallback(NULL)
189 {
190 sm_first = this;
191 m_parents[0] = _Parent1;
192 m_parents[1] = _Parent2;
193 m_parents[2] = NULL;
194 Register();
ae820c69 195 }
2d51f067 196
28953245 197 virtual ~wxClassInfo();
a095505c 198
28953245
SC
199 // allocates an instance of this class, this object does not have to be
200 // initialized or fully constructed as this call will be followed by a call to Create
201 virtual wxObject *AllocateObject() const
202 { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
ab6e4913
SC
203
204 // 'old naming' for AllocateObject staying here for backward compatibility
28953245 205 wxObject *CreateObject() const { return AllocateObject(); }
a095505c 206
583150e3 207 // direct construction call for classes that cannot construct instances via alloc/create
cbca59a8 208 wxObject *ConstructObject(int ParamCount, wxAny *Params) const;
583150e3 209
bca6bd38
SC
210 bool NeedsDirectConstruction() const;
211
28953245
SC
212 const wxChar *GetClassName() const
213 { return m_className; }
79fdfb35
SC
214 const wxChar *GetBaseClassName1() const
215 { return m_parents[0] ? m_parents[0]->GetClassName() : NULL; }
216 const wxChar *GetBaseClassName2() const
217 { return (m_parents[0] && m_parents[1]) ? m_parents[1]->GetClassName() : NULL; }
a095505c 218
28953245
SC
219 const wxClassInfo *GetBaseClass1() const
220 { return m_parents[0]; }
221 const wxClassInfo *GetBaseClass2() const
222 { return m_parents[0] ? m_parents[1] : NULL; }
223
224 const wxChar *GetIncludeName() const
225 { return m_unitName; }
226 const wxClassInfo **GetParents() const
227 { return m_parents; }
228 int GetSize() const
229 { return m_objectSize; }
230 bool IsDynamic() const
231 { return (NULL != m_objectConstructor); }
232
233 wxObjectConstructorFn GetConstructor() const
234 { return m_objectConstructor; }
235 const wxClassInfo *GetNext() const
236 { return m_next; }
237
238 // statics:
239
240 static void CleanUp();
cbca59a8 241 static wxClassInfo *FindClass(const wxString& className);
28953245
SC
242 static const wxClassInfo *GetFirst()
243 { return sm_first; }
244
a095505c 245
ae820c69
SC
246 // Climb upwards through inheritance hierarchy.
247 // Dual inheritance is catered for.
a095505c 248
28953245 249 bool IsKindOf(const wxClassInfo *info) const;
a095505c 250
cbca59a8 251 wxDECLARE_CLASS_INFO_ITERATORS();
644cb537 252
ab0881c7
VS
253 // if there is a callback registered with that class it will be called
254 // before this object will be written to disk, it can veto streaming out
255 // this object by returning false, if this class has not registered a
256 // callback, the search will go up the inheritance tree if no callback has
257 // been registered true will be returned by default
cbca59a8
SC
258 bool BeforeWriteObject( const wxObject *obj, wxObjectWriter *streamer,
259 wxObjectWriterCallback *writercallback, const wxStringToAnyHashMap &metadata) const;
9c8046dd
SC
260
261 // gets the streaming callback from this class or any superclass
28953245 262 wxObjectStreamingCallback GetStreamingCallback() const;
75890a3f 263
517fb871 264 // returns the first property
cbca59a8
SC
265 wxPropertyInfo* GetFirstProperty() const
266 { EnsureInfosInited(); return m_firstProperty; }
a095505c 267
517fb871 268 // returns the first handler
cbca59a8
SC
269 wxHandlerInfo* GetFirstHandler() const
270 { EnsureInfosInited(); return m_firstHandler; }
a095505c 271
ab6e4913
SC
272 // Call the Create upon an instance of the class, in the end the object is fully
273 // initialized
cbca59a8 274 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
a095505c 275
517fb871 276 // get number of parameters for constructor
28953245
SC
277 virtual int GetCreateParamCount() const
278 { return m_constructorPropertiesCount; }
a095505c 279
ab6e4913 280 // get n-th constructor parameter
28953245
SC
281 virtual const wxChar* GetCreateParamName(int n) const
282 { return m_constructorProperties[n]; }
a095505c 283
28953245
SC
284 // Runtime access to objects for simple properties (get/set) by property
285 // name and variant data
286 virtual void SetProperty (wxObject *object, const wxChar *propertyName,
cbca59a8
SC
287 const wxAny &value) const;
288 virtual wxAny GetProperty (wxObject *object, const wxChar *propertyName) const;
ab6e4913
SC
289
290 // Runtime access to objects for collection properties by property name
cbca59a8 291 virtual wxAnyList GetPropertyCollection(wxObject *object,
28953245
SC
292 const wxChar *propertyName) const;
293 virtual void AddToPropertyCollection(wxObject *object, const wxChar *propertyName,
cbca59a8 294 const wxAny& value) const;
a095505c 295
28953245
SC
296 // we must be able to cast variants to wxObject pointers, templates seem
297 // not to be suitable
cbca59a8
SC
298 void CallOnAny( const wxAny &data, wxObjectFunctor* functor ) const;
299
300 wxObject* AnyToObjectPtr( const wxAny &data) const;
fa08490f 301
cbca59a8 302 wxAny ObjectPtrToAny( wxObject *object ) const;
4393b50c 303
517fb871 304 // find property by name
28953245 305 virtual const wxPropertyInfo *FindPropertyInfo (const wxChar *PropertyName) const;
fbbdc52c 306
517fb871 307 // find handler by name
28953245 308 virtual const wxHandlerInfo *FindHandlerInfo (const wxChar *handlerName) const;
a095505c 309
2d51f067 310 // find property by name
28953245 311 virtual wxPropertyInfo *FindPropertyInfoInThisClass (const wxChar *PropertyName) const;
2d51f067
SC
312
313 // find handler by name
28953245
SC
314 virtual wxHandlerInfo *FindHandlerInfoInThisClass (const wxChar *handlerName) const;
315
316 // puts all the properties of this class and its superclasses in the map,
317 // as long as there is not yet an entry with the same name (overriding mechanism)
318 void GetProperties( wxPropertyInfoMap &map ) const;
ae820c69 319
644cb537 320private:
a095505c
SC
321 const wxChar *m_className;
322 int m_objectSize;
28953245 323 wxObjectConstructorFn m_objectConstructor;
a095505c
SC
324
325 // class info object live in a linked list:
326 // pointers to its head and the next element in it
327
328 static wxClassInfo *sm_first;
28953245 329 wxClassInfo *m_next;
a095505c 330
a095505c
SC
331 static wxHashTable *sm_classTable;
332
cbca59a8
SC
333 wxPropertyInfoFn m_firstPropertyFn;
334 wxHandlerInfoFn m_firstHandlerFn;
335
336 mutable bool m_firstInited;
337
28953245 338protected:
cbca59a8
SC
339 void EnsureInfosInited() const
340 {
341 if ( !m_firstInited)
342 {
343 if ( m_firstPropertyFn != NULL)
344 m_firstProperty = (*m_firstPropertyFn)();
345 if ( m_firstHandlerFn != NULL)
346 m_firstHandler = (*m_firstHandlerFn)();
347 m_firstInited = true;
348 }
349 }
350 mutable wxPropertyInfo* m_firstProperty;
351 mutable wxHandlerInfo* m_firstHandler;
28953245 352
a095505c 353private:
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__