]> git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/doc.cpp
remove unused onsolete response files
[wxWidgets.git] / samples / docvwmdi / doc.cpp
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 // 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
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
25 #endif
26
27 #if wxUSE_STD_IOSTREAM
28 #include "wx/ioswrap.h"
29 #else
30 #include "wx/txtstrm.h"
31 #endif
32
33 #include "doc.h"
34 #include "view.h"
35
36 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
37
38 DrawingDocument::~DrawingDocument(void)
39 {
40 WX_CLEAR_LIST(wxList, m_doodleSegments)
41 }
42
43 #if wxUSE_STD_IOSTREAM
44 wxSTD ostream& DrawingDocument::SaveObject(wxSTD ostream& stream)
45 {
46 wxDocument::SaveObject(stream);
47
48 wxInt32 n = m_doodleSegments.GetCount();
49 stream << n << wxT('\n');
50
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');
57
58 node = node->GetNext();
59 }
60 return stream;
61 }
62 #else
63 wxOutputStream& DrawingDocument::SaveObject(wxOutputStream& stream)
64 {
65 wxDocument::SaveObject(stream);
66
67 wxTextOutputStream text_stream( stream );
68
69 wxInt32 n = m_doodleSegments.GetCount();
70 text_stream << n << wxT('\n');
71
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');
78
79 node = node->GetNext();
80 }
81
82 return stream;
83 }
84 #endif
85
86 #if wxUSE_STD_IOSTREAM
87 wxSTD 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 m_doodleSegments.Append(segment);
99 }
100
101 return stream;
102 }
103 #else
104 wxInputStream& 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 m_doodleSegments.Append(segment);
118 }
119
120 return stream;
121 }
122 #endif
123
124 DoodleSegment::DoodleSegment(const DoodleSegment& seg) : wxObject()
125 {
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 }
140 }
141
142 DoodleSegment::~DoodleSegment(void)
143 {
144 WX_CLEAR_LIST(wxList, m_lines)
145 }
146
147 #if wxUSE_STD_IOSTREAM
148 wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& stream)
149 {
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;
165 }
166 #else
167 wxOutputStream &DoodleSegment::SaveObject(wxOutputStream& stream)
168 {
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;
186 }
187 #endif
188
189 #if wxUSE_STD_IOSTREAM
190 wxSTD 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 m_lines.Append(line);
203 }
204
205 return stream;
206 }
207 #else
208 wxInputStream &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 m_lines.Append(line);
223 }
224
225 return stream;
226 }
227 #endif
228 void DoodleSegment::Draw(wxDC *dc)
229 {
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 }
237 }
238
239 /*
240 * Implementation of drawing command
241 */
242
243 DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument* doc, DoodleSegment* seg) :
244 wxCommand(true, name)
245 {
246 m_doc = doc;
247 m_segment = seg;
248 m_cmd = command;
249 }
250
251 DrawingCommand::~DrawingCommand(void)
252 {
253 if (m_segment)
254 delete m_segment;
255 }
256
257 bool DrawingCommand::Do(void)
258 {
259 switch (m_cmd)
260 {
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;
281 }
282 return true;
283 }
284
285 bool DrawingCommand::Undo(void)
286 {
287 switch (m_cmd)
288 {
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 }
317 }
318 return true;
319 }
320
321 IMPLEMENT_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
325 bool TextEditDocument::OnSaveDocument(const wxString& filename)
326 {
327 TextEditView* view = GetFirstView();
328
329 if (!view->m_textsw->SaveFile(filename))
330 return false;
331 Modify(false);
332 return true;
333 }
334
335 bool TextEditDocument::OnOpenDocument(const wxString& filename)
336 {
337 TextEditView *view = GetFirstView();
338 if (!view->m_textsw->LoadFile(filename))
339 return false;
340
341 SetFilename(filename, true);
342 Modify(false);
343 UpdateAllViews();
344 return true;
345 }
346
347 bool TextEditDocument::IsModified(void) const
348 {
349 TextEditView* view = GetFirstView();
350 return (wxDocument::IsModified() || (view && view->m_textsw->IsModified()));
351 }
352
353 void TextEditDocument::Modify(bool mod)
354 {
355 TextEditView* view = GetFirstView();
356
357 wxDocument::Modify(mod);
358
359 if ((!mod) && view && view->m_textsw)
360 {
361 view->m_textsw->DiscardEdits();
362 }
363 }
364
365 TextEditView* TextEditDocument::GetFirstView() const
366 {
367 wxView* view = wxDocument::GetFirstView();
368 return view ? wxStaticCast(view, TextEditView) : NULL;
369 }
370