]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
Set solid colour for whole control, not pages
[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>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
14f355c2 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
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
b2edef6f
VZ
36 #if defined(__VISAGECPP__)
37 #define DEBUG_PRINTF(NAME) { static int raz=0; \
38 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
39 #else
40 #define DEBUG_PRINTF(NAME)
41 #endif
42#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 43
e1720942
VZ
44// we must disable optimizations for VC.NET because otherwise its too eager
45// linker discards wxClassInfo objects in release build thus breaking many,
46// many things
fbae49ee 47#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
48 #pragma optimize("", off)
49#endif
f6bcfd97 50
30bfc425 51#if wxUSE_EXTENDED_RTTI
05fa251a 52const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
431346ff 53 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 54{ return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; }
431346ff 55 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 56{ return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; }
431346ff 57 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
a095505c 58 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
525d8583 59 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 60 (int) sizeof(wxObject), \
431346ff 61 (wxObjectConstructorFn) 0 ,
7e548f6b
WS
62 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
63 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
a095505c
SC
64 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
65 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
431346ff
SC
66 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
67 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
7e548f6b
WS
68 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
69 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
a095505c 70#else
c0db9626 71wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
72 (int) sizeof(wxObject),
73 (wxObjectConstructorFn) 0 );
a095505c 74#endif
c801d85f 75
e1720942 76// restore optimizations
fbae49ee 77#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
78 #pragma optimize("", on)
79#endif
80
b2edef6f
VZ
81wxClassInfo* wxClassInfo::sm_first = NULL;
82wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 83
14ca93a0
VZ
84wxClassInfo *wxObject::GetClassInfo() const
85{
86 return &wxObject::ms_classInfo;
87}
88
b2edef6f 89// These are here so we can avoid 'always true/false' warnings
7e548f6b
WS
90// by referring to these instead of true/false
91const bool wxTrue = true;
92const bool wxFalse = false;
c801d85f 93
b2edef6f
VZ
94// Is this object a kind of (a subclass of) 'info'?
95// E.g. is wxWindow a kind of wxObject?
96// Go from this class to superclass, taking into account
97// two possible base classes.
341287bf 98bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 99{
afb74891 100 wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 101 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
102}
103
cf760e4c 104#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 105 #undef new
cf760e4c
VZ
106#endif
107
108
b2edef6f 109#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 110void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 111{
7e548f6b 112 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 113}
b2edef6f 114#endif
c801d85f 115
b2edef6f
VZ
116#ifdef _WX_WANT_DELETE_VOID
117void wxObject::operator delete ( void *buf )
415ca026
DW
118{
119 wxDebugFree(buf);
120}
b2edef6f
VZ
121#endif
122
123#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
124void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 125{
afb74891 126 wxDebugFree(buf);
c801d85f 127}
0b9ab0bd
RL
128#endif
129
b2edef6f 130#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 131void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 132{
b2edef6f 133 wxDebugFree(buf);
76626af2
JS
134}
135#endif
136
b2edef6f 137#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 138void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 139{
7e548f6b 140 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 141}
b2edef6f 142#endif
c801d85f 143
b2edef6f
VZ
144#ifdef _WX_WANT_ARRAY_DELETE_VOID
145void wxObject::operator delete[] ( void *buf )
c801d85f 146{
7e548f6b 147 wxDebugFree(buf, true);
c801d85f
KB
148}
149#endif
150
b2edef6f 151#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 152void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 153{
7e548f6b 154 wxDebugFree(buf, true);
b2edef6f
VZ
155}
156#endif
afb74891 157
afb74891 158
0b9ab0bd
RL
159// ----------------------------------------------------------------------------
160// wxClassInfo
161// ----------------------------------------------------------------------------
c801d85f 162
aa4b7ef9
VZ
163wxClassInfo::~wxClassInfo()
164{
165 // remove this object from the linked list of all class infos: if we don't
166 // do it, loading/unloading a DLL containing static wxClassInfo objects is
167 // not going to work
168 if ( this == sm_first )
169 {
170 sm_first = m_next;
171 }
172 else
173 {
174 wxClassInfo *info = sm_first;
175 while (info)
176 {
177 if ( info->m_next == this )
178 {
179 info->m_next = m_next;
180 break;
181 }
182
183 info = info->m_next;
184 }
185 }
7e548f6b 186 Unregister();
aa4b7ef9
VZ
187}
188
0b9ab0bd 189wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 190{
80e8062f
VZ
191 if ( sm_classTable )
192 {
193 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
194 }
195 else
196 {
197 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
198 {
199 if ( wxStrcmp(info->GetClassName(), className) == 0 )
200 return info;
201 }
202
203 return NULL;
204 }
c801d85f
KB
205}
206
cafc76a4 207void wxClassInfo::CleanUp()
c801d85f 208{
cafc76a4 209 if ( sm_classTable )
afb74891 210 {
cafc76a4
VS
211 delete sm_classTable;
212 sm_classTable = NULL;
afb74891 213 }
d1d738f1 214}
afb74891 215
d1d738f1
VS
216void wxClassInfo::Register()
217{
cafc76a4 218 if ( !sm_classTable )
afb74891 219 {
cafc76a4 220 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 221 }
cafc76a4
VS
222
223 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
224 // link any object module twice mistakenly) will break this function
225 // because it will enter an infinite loop and eventually die with "out of
226 // memory" - as this is quite hard to detect if you're unaware of this,
227 // try to do some checks here
228 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
229 _T("class already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
7e548f6b 230
cafc76a4 231 sm_classTable->Put(m_className, (wxObject *)this);
c801d85f
KB
232}
233
d1d738f1 234void wxClassInfo::Unregister()
c801d85f 235{
d1d738f1
VS
236 if ( sm_classTable )
237 {
238 sm_classTable->Delete(m_className);
cafc76a4
VS
239 if ( sm_classTable->GetCount() == 0 )
240 {
241 delete sm_classTable;
242 sm_classTable = NULL;
243 }
d1d738f1 244 }
0c32066b 245}
f4a8c29f 246
cf2f341a 247wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 248{
bbee61e5 249#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 250 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 251#endif
61cca9d2 252
b2edef6f 253 if ( wxClassInfo::sm_classTable )
0c32066b
JS
254 {
255 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 256 return info ? info->CreateObject() : NULL;
0c32066b 257 }
b2edef6f 258 else // no sm_classTable yet
0c32066b 259 {
b2edef6f
VZ
260 for ( wxClassInfo *info = wxClassInfo::sm_first;
261 info;
262 info = info->m_next )
263 {
cf2f341a 264 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 265 return info->CreateObject();
b2edef6f
VZ
266 }
267
268 return NULL;
0c32066b 269 }
c801d85f
KB
270}
271
7a4b9130 272
0b9ab0bd 273// ----------------------------------------------------------------------------
a6391d30 274// wxObject
0b9ab0bd 275// ----------------------------------------------------------------------------
c801d85f 276
a6391d30
GD
277// Initialize ref data from another object (needed for copy constructor and
278// assignment operator)
279void wxObject::InitFrom(const wxObject& other)
280{
281 m_refData = other.m_refData;
282 if ( m_refData )
283 m_refData->m_count++;
284}
285
c801d85f
KB
286void wxObject::Ref(const wxObject& clone)
287{
bbee61e5 288#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 289 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 290#endif
0b9ab0bd 291
c89f5c02
RR
292 // nothing to be done
293 if (m_refData == clone.m_refData)
294 return;
295
0b9ab0bd 296 // delete reference to old data
c801d85f 297 UnRef();
0b9ab0bd 298
c801d85f 299 // reference new data
807d8487 300 if ( clone.m_refData )
0b9ab0bd 301 {
c801d85f
KB
302 m_refData = clone.m_refData;
303 ++(m_refData->m_count);
304 }
305}
306
afb74891 307void wxObject::UnRef()
c801d85f 308{
807d8487 309 if ( m_refData )
4fe5383d
VZ
310 {
311 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
312
313 if ( !--m_refData->m_count )
c801d85f 314 delete m_refData;
807d8487 315 m_refData = NULL;
c801d85f 316 }
c801d85f
KB
317}
318
807d8487
VZ
319void wxObject::AllocExclusive()
320{
321 if ( !m_refData )
322 {
323 m_refData = CreateRefData();
324 }
325 else if ( m_refData->GetRefCount() > 1 )
326 {
327 // note that ref is not going to be destroyed in this case
a9bf8315 328 const wxObjectRefData* ref = m_refData;
807d8487
VZ
329 UnRef();
330
331 // ... so we can still access it
332 m_refData = CloneRefData(ref);
333 }
334 //else: ref count is 1, we are exclusive owners of m_refData anyhow
335
336 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
337 _T("wxObject::AllocExclusive() failed.") );
338}
339
340wxObjectRefData *wxObject::CreateRefData() const
341{
d1870078
VZ
342 // if you use AllocExclusive() you must override this method
343 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
344
807d8487
VZ
345 return NULL;
346}
347
a9bf8315
VZ
348wxObjectRefData *
349wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 350{
d1870078
VZ
351 // if you use AllocExclusive() you must override this method
352 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
353
807d8487
VZ
354 return NULL;
355}