* New function wxObjectInputStream::Recall()
[wxWidgets.git] / utils / serialize / sergdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sergdi.cpp
3 // Purpose: Serialization: GDI classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: July 1998
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "sergdi.h"
14 #endif
15 #include <wx/objstrm.h>
16 #include <wx/datstrm.h>
17 #include <wx/gdicmn.h>
18 #include <wx/bitmap.h>
19 #include <wx/font.h>
20 #include <wx/pen.h>
21 #include <wx/brush.h>
22 #include <wx/serbase.h>
23 #include <wx/imaglist.h>
24 #include <wx/region.h>
25 #include <wx/colour.h>
26 #include <wx/palette.h>
27 #include <wx/dcmemory.h>
28 #include "sergdi.h"
29
30 IMPLEMENT_SERIAL_CLASS(wxBitmap, wxObject)
31 IMPLEMENT_SERIAL_CLASS(wxGDIObject, wxObject)
32 IMPLEMENT_SERIAL_CLASS(wxRegion, wxGDIObject)
33 IMPLEMENT_SERIAL_CLASS(wxColour, wxGDIObject)
34 IMPLEMENT_SERIAL_CLASS(wxFont, wxGDIObject)
35 IMPLEMENT_SERIAL_CLASS(wxPen, wxGDIObject)
36 IMPLEMENT_SERIAL_CLASS(wxBrush, wxGDIObject)
37 IMPLEMENT_SERIAL_CLASS(wxImageList, wxObject)
38
39 IMPLEMENT_ALIAS_SERIAL_CLASS(wxPenList, wxList)
40 IMPLEMENT_ALIAS_SERIAL_CLASS(wxBrushList, wxList)
41 IMPLEMENT_ALIAS_SERIAL_CLASS(wxFontList, wxList)
42 IMPLEMENT_ALIAS_SERIAL_CLASS(wxColourDatabase, wxList)
43 IMPLEMENT_ALIAS_SERIAL_CLASS(wxBitmapList, wxList)
44
45 // ----------------------------------------------------------------------------
46
47 void WXSERIAL(wxBitmap)::StoreObject(wxObjectOutputStream& s)
48 {
49 // TODO
50 // I implemented a basic image saving (maybe I'll need to improve wxWin API).
51
52 int x, y, w, h;
53 wxDataOutputStream data_s(s);
54 wxBitmap *bitmap = (wxBitmap *)Object();
55 wxColour col;
56 wxMemoryDC dc;
57
58 w = bitmap->GetWidth();
59 h = bitmap->GetHeight();
60
61 if (s.FirstStage()) {
62 s.AddChild(bitmap->GetMask());
63 }
64
65 dc.SelectObject(*bitmap);
66
67 data_s.Write16(w);
68 data_s.Write16(h);
69 for (y=0;y<h;y++)
70 for (x=0;x<w;x++) {
71 dc.GetPixel(x, y, &col);
72 data_s.Write8( col.Red() );
73 data_s.Write8( col.Green() );
74 data_s.Write8( col.Blue() );
75 }
76 }
77
78 void WXSERIAL(wxBitmap)::LoadObject(wxObjectInputStream& s)
79 {
80 // TODO
81 // I implemented a basic image loading (maybe I'll need to improve wxWin API).
82 wxDataInputStream data_s(s);
83 wxBitmap *bitmap = (wxBitmap *)Object();
84 wxMemoryDC dc;
85 wxPen pen;
86 int x, y, w, h;
87 int r, g, b;
88
89 w = data_s.Read16();
90 h = data_s.Read16();
91
92 bitmap->Resize(w, h);
93 dc.SelectObject(*bitmap);
94
95 for (y=0;y<h;y++)
96 for (x=0;x<w;x++) {
97 r = data_s.Read8();
98 g = data_s.Read8();
99 b = data_s.Read8();
100 pen.SetColour(r, g, b);
101 dc.SetPen(pen);
102 dc.DrawPoint(x,y);
103 }
104 }
105
106 // ----------------------------------------------------------------------------
107
108 void WXSERIAL(wxGDIObject)::StoreObject(wxObjectOutputStream& s)
109 {
110 if (s.FirstStage())
111 return;
112
113 bool visible = ((wxGDIObject *)Object())->GetVisible();
114
115 wxDataOutputStream data_s(s);
116 data_s.Write8(visible);
117 }
118
119 void WXSERIAL(wxGDIObject)::LoadObject(wxObjectInputStream& s)
120 {
121 wxDataInputStream data_s(s);
122
123 ((wxGDIObject *)Object())->SetVisible( data_s.Read8() );
124 }
125
126 // ----------------------------------------------------------------------------
127
128 void WXSERIAL(wxRegion)::StoreObject(wxObjectOutputStream& s)
129 {
130 WXSERIAL(wxGDIObject)::StoreObject(s);
131
132 if (s.FirstStage())
133 return;
134
135 wxDataOutputStream data_s(s);
136 wxRect rect = ((wxRegion *)Object())->GetBox();
137
138 data_s.Write16( rect.GetX() );
139 data_s.Write16( rect.GetY() );
140 data_s.Write16( rect.GetWidth() );
141 data_s.Write16( rect.GetHeight() );
142 }
143
144 void WXSERIAL(wxRegion)::LoadObject(wxObjectInputStream& s)
145 {
146 WXSERIAL(wxGDIObject)::LoadObject(s);
147
148 wxDataInputStream data_s(s);
149 wxRegion *region = (wxRegion *)Object();
150 wxRect rect;
151
152 rect.SetX( data_s.Read16() );
153 rect.SetY( data_s.Read16() );
154 rect.SetWidth( data_s.Read16() );
155 rect.SetHeight( data_s.Read16() );
156
157 *region = wxRegion(rect);
158 }
159
160 // ----------------------------------------------------------------------------
161
162 void WXSERIAL(wxColour)::StoreObject(wxObjectOutputStream& s)
163 {
164 WXSERIAL(wxGDIObject)::StoreObject(s);
165
166 if (s.FirstStage())
167 return;
168
169 wxDataOutputStream data_s(s);
170 wxColour *colour = (wxColour *)Object();
171
172 data_s.Write8(colour->Red());
173 data_s.Write8(colour->Green());
174 data_s.Write8(colour->Blue());
175 }
176
177 void WXSERIAL(wxColour)::LoadObject(wxObjectInputStream& s)
178 {
179 WXSERIAL(wxGDIObject)::LoadObject(s);
180
181 wxDataInputStream data_s(s);
182 wxColour *colour = (wxColour *)Object();
183 int r, g, b;
184
185 r = data_s.Read8();
186 g = data_s.Read8();
187 b = data_s.Read8();
188
189 colour->Set(r, g, b);
190 }
191
192 // ----------------------------------------------------------------------------
193
194 void WXSERIAL(wxPen)::StoreObject(wxObjectOutputStream& s)
195 {
196 wxPen *pen = (wxPen *)Object();
197 WXSERIAL(wxGDIObject)::StoreObject(s);
198
199 if (s.FirstStage()) {
200 s.AddChild(& (pen->GetColour()) );
201 return;
202 }
203
204 wxDataOutputStream data_s(s);
205
206 data_s.Write8( pen->GetCap() );
207 data_s.Write8( pen->GetJoin() );
208 data_s.Write8( pen->GetStyle() );
209 data_s.Write8( pen->GetWidth() );
210 }
211
212 void WXSERIAL(wxPen)::LoadObject(wxObjectInputStream& s)
213 {
214 wxPen *pen = (wxPen *)Object();
215 wxColour *col = (wxColour *) s.GetChild();
216
217 WXSERIAL(wxGDIObject)::LoadObject(s);
218
219 wxDataInputStream data_s(s);
220
221 pen->SetColour(*col);
222 pen->SetCap( data_s.Read8() );
223 pen->SetJoin( data_s.Read8() );
224 pen->SetStyle( data_s.Read8() );
225 pen->SetWidth( data_s.Read8() );
226 }
227
228 // ----------------------------------------------------------------------------
229 void WXSERIAL(wxBrush)::StoreObject(wxObjectOutputStream& s)
230 {
231 wxBrush *brush = (wxBrush *)Object();
232 WXSERIAL(wxGDIObject)::StoreObject(s);
233
234 if (s.FirstStage()) {
235 s.AddChild( &(brush->GetColour()) );
236 s.AddChild( brush->GetStipple() );
237 return;
238 }
239
240 wxDataOutputStream data_s(s);
241 data_s.Write8( brush->GetStyle() );
242 }
243
244 void WXSERIAL(wxBrush)::LoadObject(wxObjectInputStream& s)
245 {
246 wxBrush *brush = (wxBrush *)Object();
247 wxColour *col = (wxColour *)s.GetChild();
248 wxBitmap *bmap = (wxBitmap *)s.GetChild();
249
250 WXSERIAL(wxGDIObject)::LoadObject(s);
251
252 wxDataInputStream data_s(s);
253 if (bmap)
254 *brush = wxBrush(*col, data_s.Read8());
255 else
256 *brush = wxBrush(bmap);
257 }
258
259 // ----------------------------------------------------------------------------
260 void WXSERIAL(wxFont)::StoreObject(wxObjectOutputStream& s)
261 {
262 wxFont *font = (wxFont *)Object();
263
264 WXSERIAL(wxGDIObject)::StoreObject(s);
265
266 if (s.FirstStage())
267 return;
268
269 wxDataOutputStream data_s(s);
270
271 data_s.Write8( font->GetPointSize() );
272 data_s.WriteString( font->GetFaceName() );
273 data_s.Write8( font->GetFamily() );
274 data_s.Write8( font->GetStyle() );
275 data_s.Write8( font->GetWeight() );
276 data_s.Write8( font->GetUnderlined() );
277 }
278
279 void WXSERIAL(wxFont)::LoadObject(wxObjectInputStream& s)
280 {
281 wxFont *font = (wxFont *)Object();
282
283 WXSERIAL(wxGDIObject)::LoadObject(s);
284
285 wxDataInputStream data_s(s);
286 int psize, family, style, weight;
287 bool underlined;
288 wxString face_name;
289
290 psize = data_s.Read8();
291 face_name = data_s.ReadString();
292 family = data_s.Read8();
293 style = data_s.Read8();
294 weight = data_s.Read8();
295 underlined = data_s.Read8();
296
297 *font = wxFont(psize, face_name, family, style, weight, underlined);
298 }
299
300 // ----------------------------------------------------------------------------
301
302 void WXSERIAL(wxImageList)::StoreObject(wxObjectOutputStream& s)
303 {
304 wxImageList *list = (wxImageList *)Object();
305 int i;
306
307 if (s.FirstStage()) {
308 for (i=0;i<list->GetImageCount();i++)
309 s.AddChild(list->GetBitmap(i));
310 }
311
312 wxDataOutputStream data_s(s);
313
314 data_s.Write32(list->GetImageCount());
315 }
316
317 void WXSERIAL(wxImageList)::LoadObject(wxObjectInputStream& s)
318 {
319 int i, count;
320 wxImageList *list = (wxImageList *)Object();
321 wxDataInputStream data_s(s);
322
323 count = data_s.Read32();
324 for (i=0;i<count;i++)
325 list->Add(*((wxBitmap *)s.GetChild()));
326 }