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