]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
generate a text event even if SetValue() didn't really chaneg anything
[wxWidgets.git] / src / common / object.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
b2edef6f 2// Name: src/common/object.cpp
c801d85f
KB
3// Purpose: wxObject implementation
4// Author: Julian Smart
0b9ab0bd 5// Modified by: Ron Lee
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) 1998 Julian Smart
0b9ab0bd 9// (c) 2001 Ron Lee <ron@debian.org>
55d99c7a 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
13#ifdef __GNUG__
14#pragma implementation "object.h"
15#endif
16
b2edef6f 17// For compilers that support precompilation, includes "wx.h".
c801d85f
KB
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
1678ad78 24#ifndef WX_PRECOMP
df5168c4
MB
25 #include "wx/hash.h"
26 #include "wx/object.h"
24eb81cb
DW
27#endif
28
c801d85f 29#include <string.h>
c801d85f 30
ea57084d 31#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
32#include "wx/memory.h"
33#endif
34
ea57084d 35#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
3f4a0c5b 36 // for wxObject::Dump
b2edef6f 37 #include "wx/ioswrap.h"
c801d85f 38
b2edef6f
VZ
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
42 #else
43 #define DEBUG_PRINTF(NAME)
44 #endif
45#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 46
e1720942
VZ
47// we must disable optimizations for VC.NET because otherwise its too eager
48// linker discards wxClassInfo objects in release build thus breaking many,
49// many things
fbae49ee 50#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
51 #pragma optimize("", off)
52#endif
f6bcfd97 53
30bfc425 54#if wxUSE_EXTENDED_RTTI
a095505c
SC
55const wxClassInfo* wxObject::sm_classParentswxObject[] = { NULL } ;
56 wxObject* wxVariantToObjectConverterwxObject ( const wxxVariant &data )
57{ return data.Get<wxObject*>() ; }
58 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
59 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
60 wxClassInfo wxObject::sm_classwxObject(sm_classParentswxObject , wxT("") , wxT("wxObject"),
61 (int) sizeof(wxObject), \
62 (wxObjectConstructorFn) 0 ,
fbbdc52c 63 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
a095505c
SC
64 0 , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
65 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
66 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
67 template<> const wxTypeInfo* wxGetTypeInfo( wxObject ** )
68 { static wxClassTypeInfo s_typeInfo(&wxObject::sm_classwxObject) ; return &s_typeInfo ; }
69#else
0b9ab0bd
RL
70wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
71 (int) sizeof(wxObject),
72 (wxObjectConstructorFn) 0 );
a095505c 73#endif
c801d85f 74
e1720942 75// restore optimizations
fbae49ee 76#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
77 #pragma optimize("", on)
78#endif
79
b2edef6f
VZ
80wxClassInfo* wxClassInfo::sm_first = NULL;
81wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 82
b2edef6f
VZ
83// These are here so we can avoid 'always true/false' warnings
84// by referring to these instead of TRUE/FALSE
0b9ab0bd
RL
85const bool wxTrue = TRUE;
86const bool wxFalse = FALSE;
c801d85f 87
b2edef6f
VZ
88// Is this object a kind of (a subclass of) 'info'?
89// E.g. is wxWindow a kind of wxObject?
90// Go from this class to superclass, taking into account
91// two possible base classes.
341287bf 92bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 93{
afb74891 94 wxClassInfo *thisInfo = GetClassInfo();
0b9ab0bd 95 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
c801d85f
KB
96}
97
38830220 98#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
dd107c50 99void wxObject::Dump(wxSTD ostream& str)
c801d85f 100{
afb74891
VZ
101 if (GetClassInfo() && GetClassInfo()->GetClassName())
102 str << GetClassInfo()->GetClassName();
103 else
b2edef6f 104 str << _T("unknown object class");
c801d85f
KB
105}
106#endif
107
c801d85f 108
cf760e4c
VZ
109#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
110 #undef new
111#endif
112
113
b2edef6f 114#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 115void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 116{
cf760e4c 117 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
c801d85f 118}
b2edef6f 119#endif
c801d85f 120
b2edef6f
VZ
121#ifdef _WX_WANT_DELETE_VOID
122void wxObject::operator delete ( void *buf )
415ca026
DW
123{
124 wxDebugFree(buf);
125}
b2edef6f
VZ
126#endif
127
128#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
129void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 130{
afb74891 131 wxDebugFree(buf);
c801d85f 132}
0b9ab0bd
RL
133#endif
134
b2edef6f 135#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 136void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 137{
b2edef6f 138 wxDebugFree(buf);
76626af2
JS
139}
140#endif
141
b2edef6f 142#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 143void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 144{
cf760e4c 145 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
c801d85f 146}
b2edef6f 147#endif
c801d85f 148
b2edef6f
VZ
149#ifdef _WX_WANT_ARRAY_DELETE_VOID
150void wxObject::operator delete[] ( void *buf )
c801d85f 151{
afb74891 152 wxDebugFree(buf, TRUE);
c801d85f
KB
153}
154#endif
155
b2edef6f 156#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 157void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f
VZ
158{
159 wxDebugFree(buf, TRUE);
160}
161#endif
afb74891 162
afb74891 163
0b9ab0bd
RL
164// ----------------------------------------------------------------------------
165// wxClassInfo
166// ----------------------------------------------------------------------------
c801d85f 167
aa4b7ef9
VZ
168wxClassInfo::~wxClassInfo()
169{
170 // remove this object from the linked list of all class infos: if we don't
171 // do it, loading/unloading a DLL containing static wxClassInfo objects is
172 // not going to work
173 if ( this == sm_first )
174 {
175 sm_first = m_next;
176 }
177 else
178 {
179 wxClassInfo *info = sm_first;
180 while (info)
181 {
182 if ( info->m_next == this )
183 {
184 info->m_next = m_next;
185 break;
186 }
187
188 info = info->m_next;
189 }
190 }
30bfc425 191#if wxUSE_EXTENDED_RTTI
a095505c
SC
192 Unregister( m_className ) ;
193#endif
aa4b7ef9
VZ
194}
195
0b9ab0bd 196wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 197{
80e8062f
VZ
198 if ( sm_classTable )
199 {
200 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
201 }
202 else
203 {
204 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
205 {
206 if ( wxStrcmp(info->GetClassName(), className) == 0 )
207 return info;
208 }
209
210 return NULL;
211 }
c801d85f
KB
212}
213
1f428942
VZ
214// a tiny InitializeClasses() helper
215/* static */
216inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
217{
218 if ( !name )
219 return NULL;
220
221 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
222
27db95f0
VZ
223#ifdef __WXDEBUG__
224 // this must be fixed, other things will work wrongly later if you get this
225 if ( !classInfo )
226 {
227 wxFAIL_MSG( wxString::Format
228 (
229 _T("base class '%s' is unknown to wxWindows RTTI"),
230 name
231 ) );
232 }
233#endif // __WXDEBUG__
1f428942
VZ
234
235 return classInfo;
236}
c801d85f 237
1f428942 238// Set pointers to base class(es) to speed up IsKindOf
afb74891 239void wxClassInfo::InitializeClasses()
c801d85f 240{
309689b2
VZ
241 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
242 // link any object module twice mistakenly) will break this function
243 // because it will enter an infinite loop and eventually die with "out of
244 // memory" - as this is quite hard to detect if you're unaware of this,
245 // try to do some checks here
0b9ab0bd 246
309689b2 247#ifdef __WXDEBUG__
0b9ab0bd 248 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 249 size_t nClass = 0;
0b9ab0bd 250#endif
309689b2 251
edf0993c 252 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 253
0b9ab0bd
RL
254 // Index all class infos by their class name
255
256 wxClassInfo *info;
257 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
258 {
259 if (info->m_className)
309689b2
VZ
260 {
261 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 262 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 263 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 264 }
afb74891
VZ
265 }
266
30bfc425 267#if wxUSE_EXTENDED_RTTI == 0
0b9ab0bd
RL
268 // Set base pointers for each wxClassInfo
269
270 for(info = sm_first; info; info = info->m_next)
afb74891 271 {
1f428942
VZ
272 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
273 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 274 }
a095505c 275#endif
c801d85f
KB
276}
277
afb74891 278void wxClassInfo::CleanUpClasses()
c801d85f 279{
0c32066b 280 delete wxClassInfo::sm_classTable;
b2edef6f 281 wxClassInfo::sm_classTable = NULL;
0c32066b 282}
f4a8c29f 283
cf2f341a 284wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 285{
bbee61e5 286#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 287 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 288#endif
61cca9d2 289
b2edef6f 290 if ( wxClassInfo::sm_classTable )
0c32066b
JS
291 {
292 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 293 return info ? info->CreateObject() : NULL;
0c32066b 294 }
b2edef6f 295 else // no sm_classTable yet
0c32066b 296 {
b2edef6f
VZ
297 for ( wxClassInfo *info = wxClassInfo::sm_first;
298 info;
299 info = info->m_next )
300 {
cf2f341a 301 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 302 return info->CreateObject();
b2edef6f
VZ
303 }
304
305 return NULL;
0c32066b 306 }
c801d85f
KB
307}
308
7a4b9130 309
0b9ab0bd 310// ----------------------------------------------------------------------------
a6391d30 311// wxObject
0b9ab0bd 312// ----------------------------------------------------------------------------
c801d85f 313
a6391d30
GD
314// Initialize ref data from another object (needed for copy constructor and
315// assignment operator)
316void wxObject::InitFrom(const wxObject& other)
317{
318 m_refData = other.m_refData;
319 if ( m_refData )
320 m_refData->m_count++;
321}
322
c801d85f
KB
323void wxObject::Ref(const wxObject& clone)
324{
bbee61e5 325#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 326 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 327#endif
0b9ab0bd 328
c89f5c02
RR
329 // nothing to be done
330 if (m_refData == clone.m_refData)
331 return;
332
0b9ab0bd 333 // delete reference to old data
c801d85f 334 UnRef();
0b9ab0bd 335
c801d85f 336 // reference new data
807d8487 337 if ( clone.m_refData )
0b9ab0bd 338 {
c801d85f
KB
339 m_refData = clone.m_refData;
340 ++(m_refData->m_count);
341 }
342}
343
afb74891 344void wxObject::UnRef()
c801d85f 345{
807d8487 346 if ( m_refData )
4fe5383d
VZ
347 {
348 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
349
350 if ( !--m_refData->m_count )
c801d85f 351 delete m_refData;
807d8487 352 m_refData = NULL;
c801d85f 353 }
c801d85f
KB
354}
355
807d8487
VZ
356void wxObject::AllocExclusive()
357{
358 if ( !m_refData )
359 {
360 m_refData = CreateRefData();
361 }
362 else if ( m_refData->GetRefCount() > 1 )
363 {
364 // note that ref is not going to be destroyed in this case
a9bf8315 365 const wxObjectRefData* ref = m_refData;
807d8487
VZ
366 UnRef();
367
368 // ... so we can still access it
369 m_refData = CloneRefData(ref);
370 }
371 //else: ref count is 1, we are exclusive owners of m_refData anyhow
372
373 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
374 _T("wxObject::AllocExclusive() failed.") );
375}
376
377wxObjectRefData *wxObject::CreateRefData() const
378{
d1870078
VZ
379 // if you use AllocExclusive() you must override this method
380 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
381
807d8487
VZ
382 return NULL;
383}
384
a9bf8315
VZ
385wxObjectRefData *
386wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 387{
d1870078
VZ
388 // if you use AllocExclusive() you must override this method
389 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
390
807d8487
VZ
391 return NULL;
392}
393
394// ----------------------------------------------------------------------------
395// misc
396// ----------------------------------------------------------------------------
ce28b4d7 397
edf0993c 398#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
399
400extern "C" {
401 void __initialize_Cplusplus(void);
402 void wxWindowsDylibInit(void);
403};
404
edf0993c
GD
405// Dynamic shared library (dylib) initialization routine
406// required to initialize static C++ objects bacause of lazy dynamic linking
407// http://developer.apple.com/techpubs/macosx/Essentials/
408// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 409
ce28b4d7
GD
410void wxWindowsDylibInit()
411{
412 // The function __initialize_Cplusplus() must be called from the shared
413 // library initialization routine to cause the static C++ objects in
414 // the library to be initialized (reference number 2441683).
415
edf0993c
GD
416 // This only seems to be necessary if the library initialization routine
417 // needs to use the static C++ objects
ce28b4d7
GD
418 __initialize_Cplusplus();
419}
420
421#endif
0b9ab0bd
RL
422
423// vi:sts=4:sw=4:et