]> git.saurik.com Git - wxWidgets.git/blob - utils/serialize/sergdi.cpp
Added sys/types.h needed for Solaris.
[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 #ifdef __WXGTK__
94 bitmap->Resize(w, h);
95 #else
96 bitmap->Create(w, h);
97 #endif
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 }
109 }
110
111 // ----------------------------------------------------------------------------
112
113 void 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
124 void WXSERIAL(wxGDIObject)::LoadObject(wxObjectInputStream& s)
125 {
126 wxDataInputStream data_s(s);
127
128 ((wxGDIObject *)Object())->SetVisible( data_s.Read8() );
129 }
130
131 // ----------------------------------------------------------------------------
132
133 void 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
149 void 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
167 void WXSERIAL(wxColour)::StoreObject(wxObjectOutputStream& s)
168 {
169 if (s.FirstStage())
170 return;
171
172 wxDataOutputStream data_s(s);
173 wxColour *colour = (wxColour *)Object();
174
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
183 data_s.Write8(colour->Red());
184 data_s.Write8(colour->Green());
185 data_s.Write8(colour->Blue());
186 }
187
188 void WXSERIAL(wxColour)::LoadObject(wxObjectInputStream& s)
189 {
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
201 // ----------------------------------------------------------------------------
202
203 void 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
221 void WXSERIAL(wxPen)::LoadObject(wxObjectInputStream& s)
222 {
223 wxPen *pen = (wxPen *)Object();
224 wxColour *col = (wxColour *) s.GetChild();
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
237 // ----------------------------------------------------------------------------
238 void 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
253 void WXSERIAL(wxBrush)::LoadObject(wxObjectInputStream& s)
254 {
255 wxBrush *brush = (wxBrush *)Object();
256 wxColour *col = (wxColour *)s.GetChild();
257 wxBitmap *bmap = (wxBitmap *)s.GetChild();
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
268 // ----------------------------------------------------------------------------
269 void 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
288 void 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 }
308
309 // ----------------------------------------------------------------------------
310
311 void WXSERIAL(wxImageList)::StoreObject(wxObjectOutputStream& s)
312 {
313 wxImageList *list = (wxImageList *)Object();
314 int i;
315
316 if (s.FirstStage()) {
317 #ifdef __WXGTK__
318 for (i=0;i<list->GetImageCount();i++)
319 s.AddChild(list->GetBitmap(i));
320 #endif
321 }
322
323 wxDataOutputStream data_s(s);
324
325 data_s.Write32(list->GetImageCount());
326 }
327
328 void 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++)
336 list->Add(*((wxBitmap *)s.GetChild()));
337 }