]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xti.h
Corrected handling of wxTB_HORZ_TEXT in the toolbar sample.
[wxWidgets.git] / include / wx / xti.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/xti.h
3 // Purpose: runtime metadata information (extended class info)
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
6 // Created: 27/07/03
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_XTIH__
14 #define _WX_XTIH__
15
16 // We want to support properties, event sources and events sinks through
17 // explicit declarations, using templates and specialization to make the
18 // effort as painless as possible.
19 //
20 // This means we have the following domains :
21 //
22 // - Type Information for categorizing built in types as well as custom types
23 // this includes information about enums, their values and names
24 // - Type safe value storage : a kind of wxVariant, called right now wxAny
25 // which will be merged with wxVariant
26 // - Property Information and Property Accessors providing access to a class'
27 // values and exposed event delegates
28 // - Information about event handlers
29 // - extended Class Information for accessing all these
30
31 // ----------------------------------------------------------------------------
32 // headers
33 // ----------------------------------------------------------------------------
34
35 #include "wx/defs.h"
36
37 #if wxUSE_EXTENDED_RTTI
38
39 class WXDLLIMPEXP_FWD_BASE wxAny;
40 class WXDLLIMPEXP_FWD_BASE wxAnyList;
41 class WXDLLIMPEXP_FWD_BASE wxObject;
42 class WXDLLIMPEXP_FWD_BASE wxString;
43 class WXDLLIMPEXP_FWD_BASE wxClassInfo;
44 class WXDLLIMPEXP_FWD_BASE wxHashTable;
45 class WXDLLIMPEXP_FWD_BASE wxObject;
46 class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
47 class WXDLLIMPEXP_FWD_BASE wxHashTable;
48 class WXDLLIMPEXP_FWD_BASE wxHashTable_Node;
49
50 class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap;
51 class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap;
52 class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor;
53 class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator;
54 class WXDLLIMPEXP_FWD_BASE wxObjectAllocator;
55
56
57 #define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
58
59 #include "wx/xtitypes.h"
60 #include "wx/xtihandler.h"
61
62 // ----------------------------------------------------------------------------
63 // wxClassInfo
64 // ----------------------------------------------------------------------------
65
66 class WXDLLIMPEXP_BASE wxObjectFunctor
67 {
68 public:
69 virtual ~wxObjectFunctor();
70
71 // Invoke the actual event handler:
72 virtual void operator()(const wxObject *) = 0;
73 };
74
75 class WXDLLIMPEXP_FWD_BASE wxPropertyInfo;
76 class WXDLLIMPEXP_FWD_BASE wxHandlerInfo;
77
78 typedef wxObject *(*wxObjectConstructorFn)(void);
79 typedef wxPropertyInfo *(*wxPropertyInfoFn)(void);
80 typedef wxHandlerInfo *(*wxHandlerInfoFn)(void);
81 typedef void (*wxVariantToObjectConverter)( const wxAny &data, wxObjectFunctor* fn );
82 typedef wxObject* (*wxVariantToObjectPtrConverter) ( const wxAny& data);
83 typedef wxAny (*wxObjectToVariantConverter)( wxObject* );
84
85 WXDLLIMPEXP_BASE wxString wxAnyGetAsString( const wxAny& data);
86 WXDLLIMPEXP_BASE const wxObject* wxAnyGetAsObjectPtr( const wxAny& data);
87
88 class WXDLLIMPEXP_BASE wxObjectWriter;
89 class WXDLLIMPEXP_BASE wxObjectWriterCallback;
90
91 typedef bool (*wxObjectStreamingCallback) ( const wxObject *, wxObjectWriter *, \
92 wxObjectWriterCallback *, const wxStringToAnyHashMap & );
93
94
95
96 class WXDLLIMPEXP_BASE wxClassInfo
97 {
98 friend class WXDLLIMPEXP_BASE wxPropertyInfo;
99 friend class WXDLLIMPEXP_BASE wxHandlerInfo;
100 friend wxObject *wxCreateDynamicObject(const wxString& name);
101
102 public:
103 wxClassInfo(const wxClassInfo **_Parents,
104 const wxChar *_UnitName,
105 const wxChar *_ClassName,
106 int size,
107 wxObjectConstructorFn ctor,
108 wxPropertyInfoFn _Props,
109 wxHandlerInfoFn _Handlers,
110 wxObjectAllocatorAndCreator* _Constructor,
111 const wxChar ** _ConstructorProperties,
112 const int _ConstructorPropertiesCount,
113 wxVariantToObjectPtrConverter _PtrConverter1,
114 wxVariantToObjectConverter _Converter2,
115 wxObjectToVariantConverter _Converter3,
116 wxObjectStreamingCallback _streamingCallback = NULL) :
117 m_className(_ClassName),
118 m_objectSize(size),
119 m_objectConstructor(ctor),
120 m_next(sm_first),
121 m_firstPropertyFn(_Props),
122 m_firstHandlerFn(_Handlers),
123 m_firstProperty(NULL),
124 m_firstHandler(NULL),
125 m_firstInited(false),
126 m_parents(_Parents),
127 m_unitName(_UnitName),
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)
135 {
136 sm_first = this;
137 Register();
138 }
139
140 wxClassInfo(const wxChar *_UnitName, const wxChar *_ClassName,
141 const wxClassInfo **_Parents) :
142 m_className(_ClassName),
143 m_objectSize(0),
144 m_objectConstructor(NULL),
145 m_next(sm_first),
146 m_firstPropertyFn(NULL),
147 m_firstHandlerFn(NULL),
148 m_firstProperty(NULL),
149 m_firstHandler(NULL),
150 m_firstInited(true),
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)
160 {
161 sm_first = this;
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),
175 m_firstPropertyFn(NULL),
176 m_firstHandlerFn(NULL),
177 m_firstProperty(NULL),
178 m_firstHandler(NULL),
179 m_firstInited(true),
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();
195 }
196
197 virtual ~wxClassInfo();
198
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; }
203
204 // 'old naming' for AllocateObject staying here for backward compatibility
205 wxObject *CreateObject() const { return AllocateObject(); }
206
207 // direct construction call for classes that cannot construct instances via alloc/create
208 wxObject *ConstructObject(int ParamCount, wxAny *Params) const;
209
210 bool NeedsDirectConstruction() const;
211
212 const wxChar *GetClassName() const
213 { return m_className; }
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; }
218
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();
241 static wxClassInfo *FindClass(const wxString& className);
242 static const wxClassInfo *GetFirst()
243 { return sm_first; }
244
245
246 // Climb upwards through inheritance hierarchy.
247 // Dual inheritance is catered for.
248
249 bool IsKindOf(const wxClassInfo *info) const;
250
251 wxDECLARE_CLASS_INFO_ITERATORS();
252
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
258 bool BeforeWriteObject( const wxObject *obj, wxObjectWriter *streamer,
259 wxObjectWriterCallback *writercallback, const wxStringToAnyHashMap &metadata) const;
260
261 // gets the streaming callback from this class or any superclass
262 wxObjectStreamingCallback GetStreamingCallback() const;
263
264 // returns the first property
265 wxPropertyInfo* GetFirstProperty() const
266 { EnsureInfosInited(); return m_firstProperty; }
267
268 // returns the first handler
269 wxHandlerInfo* GetFirstHandler() const
270 { EnsureInfosInited(); return m_firstHandler; }
271
272 // Call the Create upon an instance of the class, in the end the object is fully
273 // initialized
274 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
275
276 // get number of parameters for constructor
277 virtual int GetCreateParamCount() const
278 { return m_constructorPropertiesCount; }
279
280 // get n-th constructor parameter
281 virtual const wxChar* GetCreateParamName(int n) const
282 { return m_constructorProperties[n]; }
283
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,
287 const wxAny &value) const;
288 virtual wxAny GetProperty (wxObject *object, const wxChar *propertyName) const;
289
290 // Runtime access to objects for collection properties by property name
291 virtual wxAnyList GetPropertyCollection(wxObject *object,
292 const wxChar *propertyName) const;
293 virtual void AddToPropertyCollection(wxObject *object, const wxChar *propertyName,
294 const wxAny& value) const;
295
296 // we must be able to cast variants to wxObject pointers, templates seem
297 // not to be suitable
298 void CallOnAny( const wxAny &data, wxObjectFunctor* functor ) const;
299
300 wxObject* AnyToObjectPtr( const wxAny &data) const;
301
302 wxAny ObjectPtrToAny( wxObject *object ) const;
303
304 // find property by name
305 virtual const wxPropertyInfo *FindPropertyInfo (const wxChar *PropertyName) const;
306
307 // find handler by name
308 virtual const wxHandlerInfo *FindHandlerInfo (const wxChar *handlerName) const;
309
310 // find property by name
311 virtual wxPropertyInfo *FindPropertyInfoInThisClass (const wxChar *PropertyName) const;
312
313 // find handler by name
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;
319
320 private:
321 const wxChar *m_className;
322 int m_objectSize;
323 wxObjectConstructorFn m_objectConstructor;
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;
329 wxClassInfo *m_next;
330
331 static wxHashTable *sm_classTable;
332
333 wxPropertyInfoFn m_firstPropertyFn;
334 wxHandlerInfoFn m_firstHandlerFn;
335
336 mutable bool m_firstInited;
337
338 protected:
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;
352
353 private:
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;
360 wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter;
361 wxVariantToObjectConverter m_variantToObjectConverter;
362 wxObjectToVariantConverter m_objectToVariantConverter;
363 wxObjectStreamingCallback m_streamingCallback;
364
365 const wxPropertyAccessor *FindAccessor (const wxChar *propertyName) const;
366
367 protected:
368 // registers the class
369 void Register();
370 void Unregister();
371
372 DECLARE_NO_COPY_CLASS(wxClassInfo)
373 };
374
375 WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name);
376
377 // ----------------------------------------------------------------------------
378 // wxDynamicClassInfo
379 // ----------------------------------------------------------------------------
380
381 // this object leads to having a pure runtime-instantiation
382
383 class WXDLLIMPEXP_BASE wxDynamicClassInfo : public wxClassInfo
384 {
385 friend class WXDLLIMPEXP_BASE wxDynamicObject;
386
387 public:
388 wxDynamicClassInfo( const wxChar *_UnitName, const wxChar *_ClassName,
389 const wxClassInfo* superClass );
390 virtual ~wxDynamicClassInfo();
391
392 // constructs a wxDynamicObject with an instance
393 virtual wxObject *AllocateObject() const;
394
395 // Call the Create method for a class
396 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
397
398 // get number of parameters for constructor
399 virtual int GetCreateParamCount() const;
400
401 // get i-th constructor parameter
402 virtual const wxChar* GetCreateParamName(int i) const;
403
404 // Runtime access to objects by property name, and variant data
405 virtual void SetProperty (wxObject *object, const wxChar *PropertyName,
406 const wxAny &Value) const;
407 virtual wxAny GetProperty (wxObject *object, const wxChar *PropertyName) const;
408
409 // adds a property to this class at runtime
410 void AddProperty( const wxChar *propertyName, const wxTypeInfo* typeInfo );
411
412 // removes an existing runtime-property
413 void RemoveProperty( const wxChar *propertyName );
414
415 // renames an existing runtime-property
416 void RenameProperty( const wxChar *oldPropertyName, const wxChar *newPropertyName );
417
418 // as a handler to this class at runtime
419 void AddHandler( const wxChar *handlerName, wxObjectEventFunction address,
420 const wxClassInfo* eventClassInfo );
421
422 // removes an existing runtime-handler
423 void RemoveHandler( const wxChar *handlerName );
424
425 // renames an existing runtime-handler
426 void RenameHandler( const wxChar *oldHandlerName, const wxChar *newHandlerName );
427
428 private:
429 struct wxDynamicClassInfoInternal;
430 wxDynamicClassInfoInternal* m_data;
431 };
432
433 // ----------------------------------------------------------------------------
434 // wxDECLARE class macros
435 // ----------------------------------------------------------------------------
436
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; \
452 _DECLARE_DYNAMIC_CLASS(name)
453
454 #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
455 wxDECLARE_NO_ASSIGN_CLASS(name); \
456 wxDECLARE_DYNAMIC_CLASS(name)
457
458 #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
459 wxDECLARE_NO_COPY_CLASS(name); \
460 wxDECLARE_DYNAMIC_CLASS(name)
461
462 #define wxDECLARE_CLASS(name) \
463 wxDECLARE_DYNAMIC_CLASS(name)
464
465 #define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
466 #define wxCLASSINFO(name) (&name::ms_classInfo)
467
468 #endif // wxUSE_EXTENDED_RTTI
469 #endif // _WX_XTIH__