]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/object.h
moved wxNotebook::HitTest() to the base book control class; implemented it for wxList...
[wxWidgets.git] / include / wx / object.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/object.h
3// Purpose: wxObject class, plus run-time type information macros
4// Author: Julian Smart
5// Modified by: Ron Lee
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) 1997 Julian Smart
9// (c) 2001 Ron Lee <ron@debian.org>
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef _WX_OBJECTH__
14#define _WX_OBJECTH__
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/defs.h"
21#include "wx/memory.h"
22
23class WXDLLIMPEXP_BASE wxObject;
24
25#ifndef wxUSE_EXTENDED_RTTI
26#define wxUSE_EXTENDED_RTTI 0
27#endif
28
29#if wxUSE_EXTENDED_RTTI
30#include "wx/xti.h"
31#else
32
33// ----------------------------------------------------------------------------
34// conditional compilation
35// ----------------------------------------------------------------------------
36
37class WXDLLIMPEXP_BASE wxClassInfo;
38class WXDLLIMPEXP_BASE wxHashTable;
39class WXDLLIMPEXP_BASE wxObjectRefData;
40
41// ----------------------------------------------------------------------------
42// wxClassInfo
43// ----------------------------------------------------------------------------
44
45typedef wxObject *(*wxObjectConstructorFn)(void);
46
47class WXDLLIMPEXP_BASE wxClassInfo
48{
49public:
50 wxClassInfo( const wxChar *className,
51 const wxClassInfo *baseInfo1,
52 const wxClassInfo *baseInfo2,
53 int size,
54 wxObjectConstructorFn ctor )
55 : m_className(className)
56 , m_objectSize(size)
57 , m_objectConstructor(ctor)
58 , m_baseInfo1(baseInfo1)
59 , m_baseInfo2(baseInfo2)
60 , m_next(sm_first)
61 {
62 sm_first = this;
63 Register();
64 }
65
66 ~wxClassInfo();
67
68 wxObject *CreateObject() const { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
69 bool IsDynamic() const { return (NULL != m_objectConstructor); }
70
71 const wxChar *GetClassName() const { return m_className; }
72 const wxChar *GetBaseClassName1() const
73 { return m_baseInfo1 ? m_baseInfo1->GetClassName() : NULL; }
74 const wxChar *GetBaseClassName2() const
75 { return m_baseInfo2 ? m_baseInfo2->GetClassName() : NULL; }
76 const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; }
77 const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; }
78 int GetSize() const { return m_objectSize; }
79
80 wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
81 static const wxClassInfo *GetFirst() { return sm_first; }
82 const wxClassInfo *GetNext() const { return m_next; }
83 static wxClassInfo *FindClass(const wxChar *className);
84
85 // Climb upwards through inheritance hierarchy.
86 // Dual inheritance is catered for.
87
88 bool IsKindOf(const wxClassInfo *info) const
89 {
90 return info != 0 &&
91 ( info == this ||
92 ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) ||
93 ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) );
94 }
95
96#if WXWIN_COMPATIBILITY_2_4
97 // Initializes parent pointers and hash table for fast searching.
98 wxDEPRECATED( static void InitializeClasses() );
99 // Cleans up hash table used for fast searching.
100 wxDEPRECATED( static void CleanUpClasses() );
101#endif
102
103public:
104 const wxChar *m_className;
105 int m_objectSize;
106 wxObjectConstructorFn m_objectConstructor;
107
108 // Pointers to base wxClassInfos: set in InitializeClasses
109
110 const wxClassInfo *m_baseInfo1;
111 const wxClassInfo *m_baseInfo2;
112
113 // class info object live in a linked list:
114 // pointers to its head and the next element in it
115
116 static wxClassInfo *sm_first;
117 wxClassInfo *m_next;
118
119 // FIXME: this should be private (currently used directly by way too
120 // many clients)
121 static wxHashTable *sm_classTable;
122
123private:
124 // InitializeClasses() helper
125 static wxClassInfo *GetBaseByName(const wxChar *name);
126
127 DECLARE_NO_COPY_CLASS(wxClassInfo)
128
129protected:
130 // registers the class
131 void Register();
132 void Unregister();
133};
134
135WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxChar *name);
136
137#if WXWIN_COMPATIBILITY_2_4
138inline void wxClassInfo::InitializeClasses() {}
139inline void wxClassInfo::CleanUpClasses() {}
140#endif
141
142// ----------------------------------------------------------------------------
143// Dynamic class macros
144// ----------------------------------------------------------------------------
145
146#define DECLARE_ABSTRACT_CLASS(name) \
147 public: \
148 static wxClassInfo ms_classInfo; \
149 virtual wxClassInfo *GetClassInfo() const;
150
151#define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
152 DECLARE_NO_ASSIGN_CLASS(name) \
153 DECLARE_DYNAMIC_CLASS(name)
154
155#define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
156 DECLARE_NO_COPY_CLASS(name) \
157 DECLARE_DYNAMIC_CLASS(name)
158
159#define DECLARE_DYNAMIC_CLASS(name) \
160 DECLARE_ABSTRACT_CLASS(name) \
161 static wxObject* wxCreateObject();
162
163#define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
164
165
166// common part of the macros below
167#define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
168 wxClassInfo name::ms_classInfo(wxT(#name), \
169 &basename::ms_classInfo, \
170 baseclsinfo2, \
171 (int) sizeof(name), \
172 (wxObjectConstructorFn) func); \
173 \
174 wxClassInfo *name::GetClassInfo() const \
175 { return &name::ms_classInfo; }
176
177#define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
178 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
179
180#define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
181 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
182
183// -----------------------------------
184// for concrete classes
185// -----------------------------------
186
187 // Single inheritance with one base class
188#define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
189 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
190 wxObject* name::wxCreateObject() \
191 { return new name; }
192
193 // Multiple inheritance with two base classes
194#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
195 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
196 name::wxCreateObject) \
197 wxObject* name::wxCreateObject() \
198 { return new name; }
199
200// -----------------------------------
201// for abstract classes
202// -----------------------------------
203
204 // Single inheritance with one base class
205
206#define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
207 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
208
209 // Multiple inheritance with two base classes
210
211#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
212 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
213
214#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
215#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
216
217#endif // !wxUSE_EXTENDED_RTTI
218
219
220// -----------------------------------
221// for pluggable classes
222// -----------------------------------
223
224 // NOTE: this should probably be the very first statement
225 // in the class declaration so wxPluginSentinel is
226 // the first member initialised and the last destroyed.
227
228// _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
229
230#if wxUSE_NESTED_CLASSES
231
232#define _DECLARE_DL_SENTINEL(name, exportdecl) \
233class exportdecl name##PluginSentinel { \
234private: \
235 static const wxString sm_className; \
236public: \
237 name##PluginSentinel(); \
238 ~name##PluginSentinel(); \
239}; \
240name##PluginSentinel m_pluginsentinel;
241
242#define _IMPLEMENT_DL_SENTINEL(name) \
243 const wxString name::name##PluginSentinel::sm_className(#name); \
244 name::name##PluginSentinel::name##PluginSentinel() { \
245 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
246 if( e != 0 ) { e->RefObj(); } \
247 } \
248 name::name##PluginSentinel::~name##PluginSentinel() { \
249 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
250 if( e != 0 ) { e->UnrefObj(); } \
251 }
252#else
253
254#define _DECLARE_DL_SENTINEL(name)
255#define _IMPLEMENT_DL_SENTINEL(name)
256
257#endif // wxUSE_NESTED_CLASSES
258
259#define DECLARE_PLUGGABLE_CLASS(name) \
260 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
261#define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
262 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
263
264#define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
265 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
266#define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
267 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
268
269#define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
270 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
271#define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
272 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
273#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
274 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
275#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
276 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
277
278#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
279 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
280#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
281 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
282#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
283 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
284#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
285 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
286
287#define CLASSINFO(name) (&name::ms_classInfo)
288
289#define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
290
291// Just seems a bit nicer-looking (pretend it's not a macro)
292#define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
293
294// this cast does some more checks at compile time as it uses static_cast
295// internally
296//
297// note that it still has different semantics from dynamic_cast<> and so can't
298// be replaced by it as long as there are any compilers not supporting it
299#define wxDynamicCast(obj, className) \
300 ((className *) wxCheckDynamicCast( \
301 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
302 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
303 &className::ms_classInfo))
304
305// The 'this' pointer is always true, so use this version
306// to cast the this pointer and avoid compiler warnings.
307#define wxDynamicCastThis(className) \
308 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
309
310#ifdef __WXDEBUG__
311inline void* wxCheckCast(void *ptr)
312{
313 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
314 return ptr;
315}
316#define wxStaticCast(obj, className) \
317 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
318
319#else // !__WXDEBUG__
320#define wxStaticCast(obj, className) \
321 wx_const_cast(className *, wx_static_cast(const className *, obj))
322
323#endif // __WXDEBUG__
324
325// ----------------------------------------------------------------------------
326// set up memory debugging macros
327// ----------------------------------------------------------------------------
328
329/*
330 Which new/delete operator variants do we want?
331
332 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
333 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
334 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
335 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
336 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
337 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
338 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
339*/
340
341#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
342
343// All compilers get this one
344#define _WX_WANT_NEW_SIZET_WXCHAR_INT
345
346// Everyone except Visage gets the next one
347#ifndef __VISAGECPP__
348 #define _WX_WANT_DELETE_VOID
349#endif
350
351// Only visage gets this one under the correct circumstances
352#if defined(__VISAGECPP__) && __DEBUG_ALLOC__
353 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
354#endif
355
356// Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
357#if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
358 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
359#endif
360
361// Now see who (if anyone) gets the array memory operators
362#if wxUSE_ARRAY_MEMORY_OPERATORS
363
364 // Everyone except Visual C++ (cause problems for VC++ - crashes)
365 #if !defined(__VISUALC__)
366 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
367 #endif
368
369 // Everyone except Visual C++ (cause problems for VC++ - crashes)
370 #if !defined(__VISUALC__)
371 #define _WX_WANT_ARRAY_DELETE_VOID
372 #endif
373
374 // Only CodeWarrior 6 or higher
375 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
376 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
377 #endif
378
379#endif // wxUSE_ARRAY_MEMORY_OPERATORS
380
381#endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
382
383// ----------------------------------------------------------------------------
384// wxObjectRefData: ref counted data meant to be stored in wxObject
385// ----------------------------------------------------------------------------
386
387class WXDLLIMPEXP_BASE wxObjectRefData
388{
389 friend class WXDLLIMPEXP_BASE wxObject;
390
391public:
392 wxObjectRefData() : m_count(1) { }
393 virtual ~wxObjectRefData() { }
394
395 int GetRefCount() const { return m_count; }
396
397private:
398 int m_count;
399};
400
401// ----------------------------------------------------------------------------
402// wxObject: the root class of wxWidgets object hierarchy
403// ----------------------------------------------------------------------------
404
405class WXDLLIMPEXP_BASE wxObject
406{
407 DECLARE_ABSTRACT_CLASS(wxObject)
408
409public:
410 wxObject() { m_refData = NULL; }
411 virtual ~wxObject() { UnRef(); }
412
413 wxObject(const wxObject& other)
414 {
415 m_refData = other.m_refData;
416 if (m_refData)
417 m_refData->m_count++;
418 }
419
420 wxObject& operator=(const wxObject& other)
421 {
422 if ( this != &other )
423 {
424 Ref(other);
425 }
426 return *this;
427 }
428
429 bool IsKindOf(wxClassInfo *info) const;
430
431
432 // Turn on the correct set of new and delete operators
433
434#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
435 void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
436#endif
437
438#ifdef _WX_WANT_DELETE_VOID
439 void operator delete ( void * buf );
440#endif
441
442#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
443 void operator delete ( void *buf, const char *_fname, size_t _line );
444#endif
445
446#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
447 void operator delete ( void *buf, const wxChar*, int );
448#endif
449
450#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
451 void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
452#endif
453
454#ifdef _WX_WANT_ARRAY_DELETE_VOID
455 void operator delete[] ( void *buf );
456#endif
457
458#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
459 void operator delete[] (void* buf, const wxChar*, int );
460#endif
461
462 // ref counted data handling methods
463
464 // get/set
465 wxObjectRefData *GetRefData() const { return m_refData; }
466 void SetRefData(wxObjectRefData *data) { m_refData = data; }
467
468 // make a 'clone' of the object
469 void Ref(const wxObject& clone);
470
471 // destroy a reference
472 void UnRef();
473
474protected:
475 // ensure that our data is not shared with anybody else: if we have no
476 // data, it is created using CreateRefData() below, if we have shared data
477 // it is copied using CloneRefData(), otherwise nothing is done
478 void AllocExclusive();
479
480 // both methods must be implemented if Unshare() is used, not pure virtual
481 // only because of the backwards compatibility reasons
482
483 // create a new m_refData
484 virtual wxObjectRefData *CreateRefData() const;
485
486 // create a new m_refData initialized with the given one
487 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
488
489 wxObjectRefData *m_refData;
490};
491
492inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
493{
494 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL;
495}
496
497#if wxUSE_EXTENDED_RTTI
498class WXDLLIMPEXP_BASE wxDynamicObject : public wxObject
499{
500 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo ;
501public:
502 // instantiates this object with an instance of its superclass
503 wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info) ;
504 ~wxDynamicObject();
505
506 void SetProperty (const wxChar *propertyName, const wxxVariant &value);
507 wxxVariant GetProperty (const wxChar *propertyName) const ;
508
509 // get the runtime identity of this object
510 wxClassInfo *GetClassInfo() const
511 {
512#ifdef _MSC_VER
513 return (wxClassInfo*) m_classInfo;
514#else
515 return wx_const_cast(wxClassInfo *, m_classInfo);
516#endif
517 }
518
519 wxObject* GetSuperClassInstance() const
520 {
521 return m_superClassInstance ;
522 }
523private :
524 // removes an existing runtime-property
525 void RemoveProperty( const wxChar *propertyName ) ;
526
527 // renames an existing runtime-property
528 void RenameProperty( const wxChar *oldPropertyName , const wxChar *newPropertyName ) ;
529
530 wxObject *m_superClassInstance ;
531 const wxDynamicClassInfo *m_classInfo;
532 struct wxDynamicObjectInternal;
533 wxDynamicObjectInternal *m_data;
534};
535#endif
536
537// ----------------------------------------------------------------------------
538// more debugging macros
539// ----------------------------------------------------------------------------
540
541#ifdef __WXDEBUG__
542 #ifndef WXDEBUG_NEW
543 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
544 #endif
545#else // !__WXDEBUG__
546 #define WXDEBUG_NEW new
547#endif
548
549// Redefine new to be the debugging version. This doesn't work with all
550// compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
551// to use the debugging version.
552
553#if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
554 #define new new(__TFILE__,__LINE__)
555#elif (defined(__WXDEBUG__) && defined(__VISUALC__) && !wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS)
556 // Including this file redefines new and allows leak reports to contain line numbers
557 #include "wx/msw/msvcrt.h"
558#endif
559
560#endif // _WX_OBJECTH__