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