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