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