]> git.saurik.com Git - wxWidgets.git/blob - src/common/objstrm.cpp
wxImage now uses GTK 1.2's fast rendering code if possible
[wxWidgets.git] / src / common / objstrm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: objstrm.cpp
3 // Purpose: wxObjectStream classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 16/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11 #ifdef __GNUG__
12 #pragma implementation "objstrm.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/defs.h"
24 #include "wx/setup.h"
25 #endif
26
27 #if wxUSE_SERIAL
28
29 #include "wx/object.h"
30 #include "wx/objstrm.h"
31 #include "wx/datstrm.h"
32
33 #define WXOBJ_BEGIN "OBEGIN"
34 #define WXOBJ_BEG_LEN 6
35
36 #define TAG_EMPTY_OBJECT "NULL"
37 #define TAG_DUPLICATE_OBJECT "DUPLIC"
38
39 // ----------------------------------------------------------------------------
40 // wxObjectOutputStream
41 // ----------------------------------------------------------------------------
42
43 wxObjectOutputStream::wxObjectOutputStream(wxOutputStream& s)
44 : wxFilterOutputStream(s)
45 {
46 m_saving = FALSE;
47 }
48
49 wxString wxObjectOutputStream::GetObjectName(wxObject *obj)
50 {
51 wxString name;
52
53 name.Printf(_T("%x"), (unsigned long)obj);
54 return name;
55 }
56
57 void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo& info)
58 {
59 wxDataOutputStream data_s(*this);
60
61 Write(WXOBJ_BEGIN, WXOBJ_BEG_LEN);
62
63 if (info.duplicate) {
64 data_s.WriteString(TAG_DUPLICATE_OBJECT);
65 data_s.WriteString(GetObjectName(info.object));
66 wxPrintf(_T("info.object (dup %s)\n"), info.object->GetClassInfo()->GetClassName());
67 return;
68 }
69
70 if (info.object) {
71 data_s.WriteString(info.object->GetClassInfo()->GetClassName());
72 wxPrintf(_T("info.object (%s)\n"), info.object->GetClassInfo()->GetClassName());
73 } else {
74 data_s.WriteString(TAG_EMPTY_OBJECT);
75 wxPrintf(_T("info.object (NULL)\n"));
76 return;
77 }
78
79 data_s.WriteString(GetObjectName(info.object));
80
81 // I assume an object will not have millions of children
82 // Hmmm ... it could have (for example wxGrid)
83 data_s.Write32(info.children.Number());
84 }
85
86 void wxObjectOutputStream::AddChild(wxObject *obj)
87 {
88 wxObjectStreamInfo *info;
89
90 if (!FirstStage())
91 return;
92
93 info = new wxObjectStreamInfo;
94
95 if (m_saved_objs.Member(obj) != NULL) {
96 info->duplicate = TRUE;
97 } else {
98 info->duplicate = FALSE;
99 m_saved_objs.Append(obj);
100 }
101 if (!obj)
102 info->duplicate = FALSE;
103
104 info->n_children = 0;
105 info->object = obj;
106 info->parent = m_current_info; // Not useful here.
107 m_current_info->n_children++;
108 m_current_info->children.Append(info);
109 }
110
111 void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo *info)
112 {
113 wxNode *node;
114
115 m_current_info = info;
116 // First stage: get children of obj
117 if (info->object && !info->duplicate)
118 info->object->StoreObject(*this);
119
120 // Prepare and write the sub-entry about the child obj.
121 WriteObjectDef(*info);
122
123 node = info->children.First();
124
125 while (node) {
126 ProcessObjectDef((wxObjectStreamInfo *)node->Data());
127 node = node->Next();
128 }
129 }
130
131 void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo *info)
132 {
133 wxNode *node = info->children.First();
134
135 m_current_info = info;
136
137 if (info->object && !info->duplicate)
138 info->object->StoreObject(*this);
139
140 while (node) {
141 ProcessObjectData((wxObjectStreamInfo *)node->Data());
142 node = node->Next();
143 }
144 }
145
146 bool wxObjectOutputStream::SaveObject(wxObject& obj)
147 {
148 wxObjectStreamInfo info;
149
150 if (m_saving)
151 return FALSE;
152
153 m_saving = TRUE;
154
155 // First stage
156 m_stage = 0;
157 info.object = &obj;
158 info.n_children = 0;
159 info.duplicate = FALSE;
160 ProcessObjectDef(&info);
161
162 m_stage = 1;
163 ProcessObjectData(&info);
164
165 info.children.Clear();
166 m_saved_objs.Clear();
167
168 m_saving = FALSE;
169
170 return TRUE;
171 }
172
173 // ----------------------------------------------------------------------------
174 // wxObjectInputStream
175 // ----------------------------------------------------------------------------
176
177 wxObjectInputStream::wxObjectInputStream(wxInputStream& s)
178 : wxFilterInputStream(s)
179 {
180 m_secondcall = FALSE;
181 }
182
183 wxObject *wxObjectInputStream::SolveName(const wxString& name) const
184 {
185 wxNode *node = m_solver.First();
186 wxObjectStreamInfo *info;
187
188 while (node) {
189 info = (wxObjectStreamInfo *)node->Data();
190 if (info->object_name == name)
191 return info->object;
192
193 node = node->Next();
194 }
195 return (wxObject *) NULL;
196 }
197
198 wxObject *wxObjectInputStream::GetParent() const
199 {
200 if (!m_current_info->parent)
201 return (wxObject *) NULL;
202
203 return m_current_info->parent->object;
204 }
205
206 wxObject *wxObjectInputStream::GetChild()
207 {
208 wxObject *obj = GetChild(0);
209
210 m_current_info->children_removed++;
211
212 return obj;
213 }
214
215 wxObject *wxObjectInputStream::GetChild(int no) const
216 {
217 wxNode *node;
218 wxObjectStreamInfo *info;
219
220 if (m_current_info->children_removed >= m_current_info->n_children)
221 return (wxObject *) NULL;
222
223 node = m_current_info->children.Nth(m_current_info->children_removed+no);
224
225 if (!node)
226 return (wxObject *) NULL;
227
228 info = (wxObjectStreamInfo *)node->Data();
229
230 return info->object;
231 }
232
233 void wxObjectInputStream::RemoveChildren(int nb)
234 {
235 m_current_info->children_removed += nb;
236 }
237
238 bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo *info)
239 {
240 wxDataInputStream data_s(*this);
241 char sig[WXOBJ_BEG_LEN+1];
242 wxString class_name;
243
244 Read(sig, WXOBJ_BEG_LEN);
245 sig[WXOBJ_BEG_LEN] = 0;
246 if (wxString(sig) != WXOBJ_BEGIN)
247 return FALSE;
248
249 class_name = data_s.ReadString();
250 info->children_removed = 0;
251 info->n_children = 0;
252
253 if (class_name == TAG_EMPTY_OBJECT)
254 info->object = (wxObject *) NULL;
255 else if (class_name == TAG_DUPLICATE_OBJECT) {
256 info->object_name = data_s.ReadString();
257 info->object = SolveName(info->object_name);
258 } else {
259 info->object_name = data_s.ReadString();
260 info->object = wxCreateDynamicObject( WXSTRINGCAST class_name);
261 info->n_children = data_s.Read32();
262 }
263 return TRUE;
264 }
265
266 wxObjectStreamInfo *wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo *parent)
267 {
268 wxObjectStreamInfo *info, *c_info;
269 int c;
270
271 info = new wxObjectStreamInfo;
272 info->parent = parent;
273 info->children.DeleteContents(TRUE);
274
275 m_solver.Append(info);
276
277 if (!ReadObjectDef(info))
278 return (wxObjectStreamInfo *) NULL;
279
280 for (c=0;c<info->n_children;c++) {
281 c_info = ProcessObjectDef(info);
282 if (!c_info)
283 return (wxObjectStreamInfo *) NULL;
284 info->children.Append(c_info);
285 }
286
287 return info;
288 }
289
290 void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo *info)
291 {
292 wxNode *node = info->children.First();
293
294 m_current_info = info;
295
296 if (info->object)
297 info->object->LoadObject(*this);
298 while (node) {
299 ProcessObjectData((wxObjectStreamInfo *)node->Data());
300 node = node->Next();
301 }
302
303 m_current_info = info;
304
305 if (info->recall) {
306 m_secondcall = TRUE;
307 info->object->LoadObject(*this);
308 m_secondcall = FALSE;
309 }
310 }
311
312 wxObject *wxObjectInputStream::LoadObject()
313 {
314 wxObjectStreamInfo *info;
315 wxObject *object;
316
317 info = ProcessObjectDef((wxObjectStreamInfo *) NULL);
318 if (!info)
319 return (wxObject *) NULL;
320 ProcessObjectData(info);
321
322 object = info->object;
323
324 delete info; // It's magic ! The whole tree is destroyed.
325
326 return object;
327 }
328
329 #endif
330