]> git.saurik.com Git - wxWidgets.git/blame - samples/docvwmdi/doc.cpp
non-PCH build fix (according to Tinderbox).
[wxWidgets.git] / samples / docvwmdi / doc.cpp
CommitLineData
2108f33a
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: doc.cpp
3// Purpose: Implements document functionality
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
2108f33a
JS
10/////////////////////////////////////////////////////////////////////////////
11
2108f33a
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
23a54e14 22#include "wx/txtstrm.h"
2108f33a 23
e4b19d9b 24#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 25#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
2108f33a
JS
26#endif
27
28#include "doc.h"
29#include "view.h"
30
31IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
32
2108f33a
JS
33DrawingDocument::~DrawingDocument(void)
34{
bd5206dd 35 WX_CLEAR_LIST(wxList, doodleSegments);
2108f33a
JS
36}
37
23a54e14 38#if wxUSE_STD_IOSTREAM
dd107c50 39wxSTD ostream& DrawingDocument::SaveObject(wxSTD ostream& stream)
2108f33a
JS
40{
41 wxDocument::SaveObject(stream);
958d3a7e 42
b1d4dd7a 43 wxInt32 n = doodleSegments.GetCount();
8325937e 44 stream << n << _T('\n');
958d3a7e 45
bd5206dd 46 wxList::compatibility_iterator node = doodleSegments.GetFirst();
2108f33a
JS
47 while (node)
48 {
b1d4dd7a 49 DoodleSegment *segment = (DoodleSegment *)node->GetData();
2108f33a 50 segment->SaveObject(stream);
8325937e 51 stream << _T('\n');
958d3a7e 52
b1d4dd7a 53 node = node->GetNext();
2108f33a 54 }
958d3a7e 55
2108f33a
JS
56 return stream;
57}
23a54e14
RR
58#else
59wxOutputStream& DrawingDocument::SaveObject(wxOutputStream& stream)
60{
61 wxDocument::SaveObject(stream);
62
63 wxTextOutputStream text_stream( stream );
2108f33a 64
b1d4dd7a 65 wxInt32 n = doodleSegments.GetCount();
8325937e 66 text_stream << n << _T('\n');
958d3a7e 67
bd5206dd 68 wxList::compatibility_iterator node = doodleSegments.GetFirst();
23a54e14
RR
69 while (node)
70 {
b1d4dd7a 71 DoodleSegment *segment = (DoodleSegment *)node->GetData();
23a54e14 72 segment->SaveObject(stream);
8325937e 73 text_stream << _T('\n');
958d3a7e 74
b1d4dd7a 75 node = node->GetNext();
23a54e14 76 }
958d3a7e 77
23a54e14
RR
78 return stream;
79}
80#endif
81
82#if wxUSE_STD_IOSTREAM
dd107c50 83wxSTD istream& DrawingDocument::LoadObject(wxSTD istream& stream)
2108f33a
JS
84{
85 wxDocument::LoadObject(stream);
958d3a7e 86
23a54e14 87 wxInt32 n = 0;
2108f33a
JS
88 stream >> n;
89
90 for (int i = 0; i < n; i++)
91 {
92 DoodleSegment *segment = new DoodleSegment;
93 segment->LoadObject(stream);
94 doodleSegments.Append(segment);
95 }
96
97 return stream;
98}
23a54e14
RR
99#else
100wxInputStream& DrawingDocument::LoadObject(wxInputStream& stream)
101{
102 wxDocument::LoadObject(stream);
103
104 wxTextInputStream text_stream( stream );
105
106 wxInt32 n = 0;
107 text_stream >> n;
108
109 for (int i = 0; i < n; i++)
110 {
111 DoodleSegment *segment = new DoodleSegment;
112 segment->LoadObject(stream);
113 doodleSegments.Append(segment);
114 }
2108f33a 115
23a54e14
RR
116 return stream;
117}
118#endif
2108f33a 119
fbfb8bcc 120DoodleSegment::DoodleSegment(const DoodleSegment& seg)
21fe01e2 121 :wxObject()
2108f33a 122{
bd5206dd 123 wxList::compatibility_iterator node = seg.lines.GetFirst();
2108f33a
JS
124 while (node)
125 {
b1d4dd7a 126 DoodleLine *line = (DoodleLine *)node->GetData();
2108f33a
JS
127 DoodleLine *newLine = new DoodleLine;
128 newLine->x1 = line->x1;
129 newLine->y1 = line->y1;
130 newLine->x2 = line->x2;
131 newLine->y2 = line->y2;
132
133 lines.Append(newLine);
134
b1d4dd7a 135 node = node->GetNext();
2108f33a
JS
136 }
137}
138
139DoodleSegment::~DoodleSegment(void)
140{
bd5206dd 141 WX_CLEAR_LIST(wxList, lines);
2108f33a
JS
142}
143
23a54e14 144#if wxUSE_STD_IOSTREAM
dd107c50 145wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& stream)
2108f33a 146{
b1d4dd7a 147 wxInt32 n = lines.GetCount();
8325937e 148 stream << n << _T('\n');
958d3a7e 149
bd5206dd 150 wxList::compatibility_iterator node = lines.GetFirst();
2108f33a
JS
151 while (node)
152 {
b1d4dd7a 153 DoodleLine *line = (DoodleLine *)node->GetData();
958d3a7e
WS
154 stream << line->x1 << _T(" ") <<
155 line->y1 << _T(" ") <<
156 line->x2 << _T(" ") <<
8325937e 157 line->y2 << _T("\n");
b1d4dd7a 158 node = node->GetNext();
2108f33a 159 }
23a54e14 160
2108f33a
JS
161 return stream;
162}
23a54e14
RR
163#else
164wxOutputStream &DoodleSegment::SaveObject(wxOutputStream& stream)
165{
166 wxTextOutputStream text_stream( stream );
167
b1d4dd7a 168 wxInt32 n = lines.GetCount();
8325937e 169 text_stream << n << _T('\n');
958d3a7e 170
bd5206dd 171 wxList::compatibility_iterator node = lines.GetFirst();
23a54e14
RR
172 while (node)
173 {
b1d4dd7a 174 DoodleLine *line = (DoodleLine *)node->GetData();
958d3a7e
WS
175 text_stream << line->x1 << _T(" ") <<
176 line->y1 << _T(" ") <<
177 line->x2 << _T(" ") <<
8325937e 178 line->y2 << _T("\n");
b1d4dd7a 179 node = node->GetNext();
23a54e14 180 }
2108f33a 181
23a54e14
RR
182 return stream;
183}
184#endif
185
186#if wxUSE_STD_IOSTREAM
dd107c50 187wxSTD istream& DoodleSegment::LoadObject(wxSTD istream& stream)
2108f33a 188{
23a54e14 189 wxInt32 n = 0;
2108f33a
JS
190 stream >> n;
191
192 for (int i = 0; i < n; i++)
193 {
194 DoodleLine *line = new DoodleLine;
958d3a7e
WS
195 stream >> line->x1 >>
196 line->y1 >>
197 line->x2 >>
2f6c54eb 198 line->y2;
2108f33a
JS
199 lines.Append(line);
200 }
958d3a7e 201
2108f33a
JS
202 return stream;
203}
23a54e14
RR
204#else
205wxInputStream &DoodleSegment::LoadObject(wxInputStream& stream)
206{
207 wxTextInputStream text_stream( stream );
2108f33a 208
23a54e14
RR
209 wxInt32 n = 0;
210 text_stream >> n;
211
212 for (int i = 0; i < n; i++)
213 {
214 DoodleLine *line = new DoodleLine;
958d3a7e
WS
215 text_stream >> line->x1 >>
216 line->y1 >>
217 line->x2 >>
2f6c54eb 218 line->y2;
23a54e14
RR
219 lines.Append(line);
220 }
958d3a7e 221
23a54e14
RR
222 return stream;
223}
224#endif
2108f33a
JS
225void DoodleSegment::Draw(wxDC *dc)
226{
bd5206dd 227 wxList::compatibility_iterator node = lines.GetFirst();
2108f33a
JS
228 while (node)
229 {
b1d4dd7a 230 DoodleLine *line = (DoodleLine *)node->GetData();
2108f33a 231 dc->DrawLine(line->x1, line->y1, line->x2, line->y2);
b1d4dd7a 232 node = node->GetNext();
2108f33a
JS
233 }
234}
235
236/*
237 * Implementation of drawing command
238 */
239
240DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument *ddoc, DoodleSegment *seg):
691d944f 241 wxCommand(true, name)
2108f33a
JS
242{
243 doc = ddoc;
244 segment = seg;
245 cmd = command;
246}
247
248DrawingCommand::~DrawingCommand(void)
249{
250 if (segment)
251 delete segment;
252}
253
254bool DrawingCommand::Do(void)
255{
256 switch (cmd)
257 {
258 case DOODLE_CUT:
259 {
260 // Cut the last segment
b1d4dd7a 261 if (doc->GetDoodleSegments().GetCount() > 0)
2108f33a 262 {
bd5206dd 263 wxList::compatibility_iterator node = doc->GetDoodleSegments().GetLast();
2108f33a
JS
264 if (segment)
265 delete segment;
958d3a7e 266
b1d4dd7a 267 segment = (DoodleSegment *)node->GetData();
bd5206dd 268 doc->GetDoodleSegments().Erase(node);
2108f33a 269
691d944f 270 doc->Modify(true);
2108f33a
JS
271 doc->UpdateAllViews();
272 }
273 break;
274 }
275 case DOODLE_ADD:
276 {
277 doc->GetDoodleSegments().Append(new DoodleSegment(*segment));
691d944f 278 doc->Modify(true);
2108f33a
JS
279 doc->UpdateAllViews();
280 break;
281 }
282 }
691d944f 283 return true;
2108f33a
JS
284}
285
286bool DrawingCommand::Undo(void)
287{
288 switch (cmd)
289 {
290 case DOODLE_CUT:
291 {
292 // Paste the segment
293 if (segment)
294 {
295 doc->GetDoodleSegments().Append(segment);
691d944f 296 doc->Modify(true);
2108f33a 297 doc->UpdateAllViews();
c67daf87 298 segment = (DoodleSegment *) NULL;
2108f33a 299 }
691d944f 300 doc->Modify(true);
2108f33a
JS
301 doc->UpdateAllViews();
302 break;
303 }
304 case DOODLE_ADD:
305 {
306 // Cut the last segment
b1d4dd7a 307 if (doc->GetDoodleSegments().GetCount() > 0)
2108f33a 308 {
bd5206dd 309 wxList::compatibility_iterator node = doc->GetDoodleSegments().GetLast();
b1d4dd7a 310 DoodleSegment *seg = (DoodleSegment *)node->GetData();
2108f33a 311 delete seg;
bd5206dd 312 doc->GetDoodleSegments().Erase(node);
2108f33a 313
691d944f 314 doc->Modify(true);
2108f33a
JS
315 doc->UpdateAllViews();
316 }
317 }
318 }
691d944f 319 return true;
2108f33a
JS
320}
321
322IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
323
324// Since text windows have their own method for saving to/loading from files,
325// we override OnSave/OpenDocument instead of Save/LoadObject
326bool TextEditDocument::OnSaveDocument(const wxString& filename)
327{
328 TextEditView *view = (TextEditView *)GetFirstView();
329
330 if (!view->textsw->SaveFile(filename))
691d944f
WS
331 return false;
332 Modify(false);
333 return true;
2108f33a
JS
334}
335
336bool TextEditDocument::OnOpenDocument(const wxString& filename)
337{
338 TextEditView *view = (TextEditView *)GetFirstView();
339 if (!view->textsw->LoadFile(filename))
691d944f 340 return false;
2108f33a 341
691d944f
WS
342 SetFilename(filename, true);
343 Modify(false);
2108f33a 344 UpdateAllViews();
691d944f 345 return true;
2108f33a
JS
346}
347
348bool TextEditDocument::IsModified(void) const
349{
350 TextEditView *view = (TextEditView *)GetFirstView();
351 if (view)
352 {
353 return (wxDocument::IsModified() || view->textsw->IsModified());
354 }
355 else
356 return wxDocument::IsModified();
357}
358
359void TextEditDocument::Modify(bool mod)
360{
361 TextEditView *view = (TextEditView *)GetFirstView();
362
363 wxDocument::Modify(mod);
364
365 if (!mod && view && view->textsw)
366 view->textsw->DiscardEdits();
367}