]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xti.h
adding SetLabel -> SetTitle redirects, solves missing title updates using wxDocument...
[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 98 friend class WXDLLIMPEXP_BASE wxPropertyInfo;
9ae4b67f 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
cbca59a8 336
28953245 337protected:
cbca59a8
SC
338 void EnsureInfosInited() const
339 {
340 if ( !m_firstInited)
341 {
342 if ( m_firstPropertyFn != NULL)
343 m_firstProperty = (*m_firstPropertyFn)();
344 if ( m_firstHandlerFn != NULL)
345 m_firstHandler = (*m_firstHandlerFn)();
346 m_firstInited = true;
347 }
348 }
349 mutable wxPropertyInfo* m_firstProperty;
350 mutable wxHandlerInfo* m_firstHandler;
28953245 351
a095505c 352private:
9ae4b67f
SC
353 mutable bool m_firstInited;
354
28953245
SC
355 const wxClassInfo** m_parents;
356 const wxChar* m_unitName;
357
358 wxObjectAllocatorAndCreator* m_constructor;
359 const wxChar ** m_constructorProperties;
360 const int m_constructorPropertiesCount;
cbca59a8 361 wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter;
28953245
SC
362 wxVariantToObjectConverter m_variantToObjectConverter;
363 wxObjectToVariantConverter m_objectToVariantConverter;
364 wxObjectStreamingCallback m_streamingCallback;
365
366 const wxPropertyAccessor *FindAccessor (const wxChar *propertyName) const;
a095505c 367
d1d738f1
VS
368protected:
369 // registers the class
370 void Register();
371 void Unregister();
a095505c 372
28953245 373 DECLARE_NO_COPY_CLASS(wxClassInfo)
a095505c
SC
374};
375
cbca59a8 376WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name);
a095505c 377
2d51f067 378// ----------------------------------------------------------------------------
28953245 379// wxDynamicClassInfo
2d51f067 380// ----------------------------------------------------------------------------
28953245 381
2d51f067
SC
382// this object leads to having a pure runtime-instantiation
383
492f9a9e 384class WXDLLIMPEXP_BASE wxDynamicClassInfo : public wxClassInfo
2d51f067 385{
28953245
SC
386 friend class WXDLLIMPEXP_BASE wxDynamicObject;
387
388public:
389 wxDynamicClassInfo( const wxChar *_UnitName, const wxChar *_ClassName,
390 const wxClassInfo* superClass );
391 virtual ~wxDynamicClassInfo();
2d51f067
SC
392
393 // constructs a wxDynamicObject with an instance
28953245 394 virtual wxObject *AllocateObject() const;
2d51f067
SC
395
396 // Call the Create method for a class
cbca59a8 397 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
2d51f067
SC
398
399 // get number of parameters for constructor
28953245 400 virtual int GetCreateParamCount() const;
2d51f067
SC
401
402 // get i-th constructor parameter
28953245 403 virtual const wxChar* GetCreateParamName(int i) const;
2d51f067
SC
404
405 // Runtime access to objects by property name, and variant data
28953245 406 virtual void SetProperty (wxObject *object, const wxChar *PropertyName,
cbca59a8
SC
407 const wxAny &Value) const;
408 virtual wxAny GetProperty (wxObject *object, const wxChar *PropertyName) const;
2d51f067 409
2abce515 410 // adds a property to this class at runtime
28953245 411 void AddProperty( const wxChar *propertyName, const wxTypeInfo* typeInfo );
2abce515
SC
412
413 // removes an existing runtime-property
28953245 414 void RemoveProperty( const wxChar *propertyName );
2abce515 415
492f9a9e 416 // renames an existing runtime-property
28953245 417 void RenameProperty( const wxChar *oldPropertyName, const wxChar *newPropertyName );
492f9a9e 418
2abce515 419 // as a handler to this class at runtime
28953245
SC
420 void AddHandler( const wxChar *handlerName, wxObjectEventFunction address,
421 const wxClassInfo* eventClassInfo );
2abce515
SC
422
423 // removes an existing runtime-handler
28953245 424 void RemoveHandler( const wxChar *handlerName );
492f9a9e
SC
425
426 // renames an existing runtime-handler
28953245
SC
427 void RenameHandler( const wxChar *oldHandlerName, const wxChar *newHandlerName );
428
429private:
430 struct wxDynamicClassInfoInternal;
431 wxDynamicClassInfoInternal* m_data;
432};
433
a095505c 434// ----------------------------------------------------------------------------
28953245 435// wxDECLARE class macros
a095505c
SC
436// ----------------------------------------------------------------------------
437
28953245
SC
438#define _DECLARE_DYNAMIC_CLASS(name) \
439 public: \
440 static wxClassInfo ms_classInfo; \
441 static const wxClassInfo* ms_classParents[]; \
442 static wxPropertyInfo* GetPropertiesStatic(); \
443 static wxHandlerInfo* GetHandlersStatic(); \
444 static wxClassInfo *GetClassInfoStatic() \
445 { return &name::ms_classInfo; } \
446 virtual wxClassInfo *GetClassInfo() const \
447 { return &name::ms_classInfo; }
448
449#define wxDECLARE_DYNAMIC_CLASS(name) \
450 static wxObjectAllocatorAndCreator* ms_constructor; \
451 static const wxChar * ms_constructorProperties[]; \
452 static const int ms_constructorPropertiesCount; \
f0b7eadf 453 _DECLARE_DYNAMIC_CLASS(name)
a095505c 454
28953245 455#define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
cbca59a8 456 wxDECLARE_NO_ASSIGN_CLASS(name); \
28953245 457 wxDECLARE_DYNAMIC_CLASS(name)
a095505c 458
28953245 459#define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
cbca59a8
SC
460 wxDECLARE_NO_COPY_CLASS(name); \
461 wxDECLARE_DYNAMIC_CLASS(name)
462
463#define wxDECLARE_CLASS(name) \
28953245 464 wxDECLARE_DYNAMIC_CLASS(name)
a095505c 465
28953245
SC
466#define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
467#define wxCLASSINFO(name) (&name::ms_classInfo)
a095505c 468
28953245 469#endif // wxUSE_EXTENDED_RTTI
c0089c96 470#endif // _WX_XTIH__