]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
Better disabling of toolbars and menubars
[wxWidgets.git] / src / common / object.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: object.cpp
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "object.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/hash.h"
25 #ifdef wxUSE_SERIAL
26 #include "wx/objstrm.h"
27 #include "wx/serbase.h"
28
29 // for error messages
30 #include "wx/log.h"
31 #include "wx/intl.h"
32 #endif // wxUSE_SERIAL
33 #endif // WX_PRECOMP
34
35 #include <string.h>
36 #include <assert.h>
37
38 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
39 #include "wx/memory.h"
40 #endif
41
42 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
43 // for wxObject::Dump
44 #if wxUSE_IOSTREAMH
45 # include <iostream.h>
46 #else
47 # include <iostream>
48 # ifdef _MSC_VER
49 using namespace std;
50 # endif
51 #endif
52 #endif
53
54 #if !USE_SHARED_LIBRARY
55 wxClassInfo wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL, (char *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL);
56 wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL;
57 wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL;
58 #endif
59
60 /*
61 * wxWindows root object.
62 */
63
64 wxObject::wxObject(void)
65 {
66 m_refData = (wxObjectRefData *) NULL;
67 #ifdef wxUSE_SERIAL
68 m_serialObj = (wxObject_Serialize *)NULL;
69 #endif
70 }
71
72 wxObject::~wxObject(void)
73 {
74 UnRef();
75 #ifdef wxUSE_SERIAL
76 if (m_serialObj)
77 delete m_serialObj;
78 #endif
79 }
80
81 /*
82 * Is this object a kind of (a subclass of) 'info'?
83 * E.g. is wxWindow a kind of wxObject?
84 * Go from this class to superclass, taking into account
85 * two possible base classes.
86 */
87
88 bool wxObject::IsKindOf(wxClassInfo *info) const
89 {
90 wxClassInfo *thisInfo = GetClassInfo();
91 if (thisInfo)
92 return thisInfo->IsKindOf(info);
93 else
94 return FALSE;
95 }
96
97 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
98 void wxObject::Dump(ostream& str)
99 {
100 if (GetClassInfo() && GetClassInfo()->GetClassName())
101 str << GetClassInfo()->GetClassName();
102 else
103 str << "unknown object class";
104 }
105 #endif
106
107 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
108
109 #ifdef new
110 #undef new
111 #endif
112
113 void * wxObject::operator new (size_t size, char * fileName, int lineNum)
114 {
115 return wxDebugAlloc(size, fileName, lineNum, TRUE);
116 }
117
118 void wxObject::operator delete (void * buf)
119 {
120 wxDebugFree(buf);
121 }
122
123 // VC++ 6.0
124 #if _MSC_VER >= 1200
125 void wxObject::operator delete(void* pData, char* /* fileName */, int /* lineNum */)
126 {
127 ::operator delete(pData);
128 }
129 #endif
130
131 // Cause problems for VC++ - crashes
132 #if !defined(_MSC_VER) && wxUSE_ARRAY_MEMORY_OPERATORS
133 void * wxObject::operator new[] (size_t size, char * fileName, int lineNum)
134 {
135 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
136 }
137
138 void wxObject::operator delete[] (void * buf)
139 {
140 wxDebugFree(buf, TRUE);
141 }
142 #endif
143
144 #endif
145
146 /*
147 * Class info: provides run-time class type information.
148 */
149
150 wxClassInfo::wxClassInfo(char *cName, char *baseName1, char *baseName2, int sz, wxObjectConstructorFn constr)
151 {
152 m_className = cName;
153 m_baseClassName1 = baseName1;
154 m_baseClassName2 = baseName2;
155
156 m_objectSize = sz;
157 m_objectConstructor = constr;
158
159 m_next = sm_first;
160 sm_first = this;
161
162 m_baseInfo1 = (wxClassInfo *) NULL;
163 m_baseInfo2 = (wxClassInfo *) NULL;
164 }
165
166 wxObject *wxClassInfo::CreateObject(void)
167 {
168 if (m_objectConstructor)
169 return (wxObject *)(*m_objectConstructor)();
170 else
171 return (wxObject *) NULL;
172 }
173
174 wxClassInfo *wxClassInfo::FindClass(char *c)
175 {
176 wxClassInfo *p = sm_first;
177 while (p)
178 {
179 if (p && p->GetClassName() && strcmp(p->GetClassName(), c) == 0)
180 return p;
181 p = p->m_next;
182 }
183 return (wxClassInfo *) NULL;
184 }
185
186 // Climb upwards through inheritance hierarchy.
187 // Dual inheritance is catered for.
188 bool wxClassInfo::IsKindOf(wxClassInfo *info) const
189 {
190 if (info == NULL)
191 return FALSE;
192
193 // For some reason, when making/using a DLL, static data has to be included
194 // in both the DLL and the application. This can lead to duplicate
195 // wxClassInfo objects, so we have to test the name instead of the pointers.
196 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
197 /*
198 #if WXMAKINGDLL
199 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
200 return TRUE;
201 #else
202 */
203 if (this == info)
204 return TRUE;
205
206 if (m_baseInfo1)
207 if (m_baseInfo1->IsKindOf(info))
208 return TRUE;
209
210 if (m_baseInfo2)
211 return m_baseInfo2->IsKindOf(info);
212
213 return FALSE;
214 }
215
216 // Set pointers to base class(es) to speed up IsKindOf
217 void wxClassInfo::InitializeClasses(void)
218 {
219 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
220
221 // Index all class infos by their class name
222 wxClassInfo *info = sm_first;
223 while (info)
224 {
225 if (info->m_className)
226 sm_classTable->Put(info->m_className, (wxObject *)info);
227 info = info->m_next;
228 }
229
230 // Set base pointers for each wxClassInfo
231 info = sm_first;
232 while (info)
233 {
234 if (info->GetBaseClassName1())
235 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
236 if (info->GetBaseClassName2())
237 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
238 info = info->m_next;
239 }
240 }
241
242 void wxClassInfo::CleanUpClasses(void)
243 {
244 delete wxClassInfo::sm_classTable;
245 wxClassInfo::sm_classTable = NULL;
246 }
247
248 wxObject *wxCreateDynamicObject(const char *name)
249 {
250 if (wxClassInfo::sm_classTable)
251 {
252 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
253 if (!info)
254 return (wxObject *)NULL;
255
256 return info->CreateObject();
257 }
258 else
259 {
260 wxClassInfo *info = wxClassInfo::sm_first;
261 while (info)
262 {
263 if (info->m_className && strcmp(info->m_className, name) == 0)
264 return info->CreateObject();
265 info = info->m_next;
266 }
267 return (wxObject*) NULL;
268 }
269 return (wxObject*) NULL;
270 }
271
272 #ifdef wxUSE_SERIAL
273
274 #include "wx/serbase.h"
275 #include "wx/dynlib.h"
276 #include "wx/msgdlg.h"
277
278 wxObject* wxCreateStoredObject( wxInputStream &stream )
279 {
280 wxObjectInputStream obj_s(stream);
281 return obj_s.LoadObject();
282 };
283
284 void wxObject::StoreObject( wxObjectOutputStream& stream )
285 {
286 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
287 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
288
289 if (!lib) {
290 wxLogError(_("Can't load wxSerial dynamic library."));
291 return;
292 }
293 if (!m_serialObj) {
294 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
295
296 if (!m_serialObj) {
297 wxLogError(_("Can't find the serialization object '%s' "
298 "for the object '%s'."),
299 obj_name.c_str(),
300 GetClassInfo()->GetClassName());
301 return;
302 }
303 m_serialObj->SetObject(this);
304 }
305
306 m_serialObj->StoreObject(stream);
307 }
308
309 void wxObject::LoadObject( wxObjectInputStream& stream )
310 {
311 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
312 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
313
314 if (!m_serialObj) {
315 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
316
317 if (!m_serialObj) {
318 wxLogError(_("Can't find the serialization object '%s' "
319 "for the object '%s'."),
320 obj_name.c_str(),
321 GetClassInfo()->GetClassName());
322 return;
323 }
324 m_serialObj->SetObject(this);
325 }
326
327 m_serialObj->LoadObject(stream);
328 }
329
330 #endif
331
332 /*
333 * wxObject: cloning of objects
334 */
335
336 void wxObject::Ref(const wxObject& clone)
337 {
338 // delete reference to old data
339 UnRef();
340 // reference new data
341 if (clone.m_refData) {
342 m_refData = clone.m_refData;
343 ++(m_refData->m_count);
344 }
345 }
346
347 void wxObject::UnRef(void)
348 {
349 if (m_refData) {
350 assert(m_refData->m_count > 0);
351 --(m_refData->m_count);
352 if (m_refData->m_count == 0)
353 delete m_refData;
354 }
355 m_refData = (wxObjectRefData *) NULL;
356 }
357
358 /*
359 * wxObjectData
360 */
361
362 wxObjectRefData::wxObjectRefData(void) : m_count(1)
363 {
364 }
365
366 wxObjectRefData::~wxObjectRefData(void)
367 {
368 }
369
370 // These are here so we can avoid 'always true/false' warnings
371 // by referring to these instead of TRUE/FALSE
372 const bool wxTrue = TRUE;
373 const bool wxFalse = FALSE;