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