]> git.saurik.com Git - wxWidgets.git/blame - utils/serialize/sergdi.cpp
added GetMargins()
[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
8ef6a930
GL
93 bitmap->SetWidth(w);
94 bitmap->SetHeight(h);
1d44aaf8
GL
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 }
9fdd8384
GL
106}
107
1d44aaf8
GL
108// ----------------------------------------------------------------------------
109
9fdd8384
GL
110void 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
121void WXSERIAL(wxGDIObject)::LoadObject(wxObjectInputStream& s)
122{
123 wxDataInputStream data_s(s);
124
125 ((wxGDIObject *)Object())->SetVisible( data_s.Read8() );
126}
127
1d44aaf8
GL
128// ----------------------------------------------------------------------------
129
130void 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
146void 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
9fdd8384
GL
164void WXSERIAL(wxColour)::StoreObject(wxObjectOutputStream& s)
165{
9fdd8384
GL
166 if (s.FirstStage())
167 return;
168
169 wxDataOutputStream data_s(s);
170 wxColour *colour = (wxColour *)Object();
171
856d2e52
GL
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
9fdd8384
GL
180 data_s.Write8(colour->Red());
181 data_s.Write8(colour->Green());
182 data_s.Write8(colour->Blue());
183}
184
185void WXSERIAL(wxColour)::LoadObject(wxObjectInputStream& s)
186{
9fdd8384
GL
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
1d44aaf8
GL
198// ----------------------------------------------------------------------------
199
9fdd8384
GL
200void 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
218void WXSERIAL(wxPen)::LoadObject(wxObjectInputStream& s)
219{
220 wxPen *pen = (wxPen *)Object();
8d43638d 221 wxColour *col = (wxColour *) s.GetChild();
9fdd8384
GL
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
1d44aaf8 234// ----------------------------------------------------------------------------
9fdd8384
GL
235void 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
250void WXSERIAL(wxBrush)::LoadObject(wxObjectInputStream& s)
251{
252 wxBrush *brush = (wxBrush *)Object();
8d43638d
GL
253 wxColour *col = (wxColour *)s.GetChild();
254 wxBitmap *bmap = (wxBitmap *)s.GetChild();
9fdd8384
GL
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
1d44aaf8 265// ----------------------------------------------------------------------------
9fdd8384
GL
266void 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
285void 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}
c2dd8380 305
1d44aaf8
GL
306// ----------------------------------------------------------------------------
307
c2dd8380
GL
308void WXSERIAL(wxImageList)::StoreObject(wxObjectOutputStream& s)
309{
310 wxImageList *list = (wxImageList *)Object();
311 int i;
312
313 if (s.FirstStage()) {
856d2e52 314#ifdef __WXGTK__
c2dd8380
GL
315 for (i=0;i<list->GetImageCount();i++)
316 s.AddChild(list->GetBitmap(i));
856d2e52 317#endif
c2dd8380
GL
318 }
319
320 wxDataOutputStream data_s(s);
321
322 data_s.Write32(list->GetImageCount());
323}
324
325void 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++)
8d43638d 333 list->Add(*((wxBitmap *)s.GetChild()));
c2dd8380 334}