]>
Commit | Line | Data |
---|---|---|
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 | ||
35 | IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument) | |
36 | ||
2108f33a JS |
37 | DrawingDocument::~DrawingDocument(void) |
38 | { | |
bd5206dd | 39 | WX_CLEAR_LIST(wxList, doodleSegments); |
2108f33a JS |
40 | } |
41 | ||
23a54e14 | 42 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 43 | wxSTD 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 |
63 | wxOutputStream& 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 | 87 | wxSTD 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 |
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 | doodleSegments.Append(segment); | |
118 | } | |
2108f33a | 119 | |
23a54e14 RR |
120 | return stream; |
121 | } | |
122 | #endif | |
2108f33a JS |
123 | |
124 | DoodleSegment::DoodleSegment(DoodleSegment& seg) | |
125 | { | |
bd5206dd | 126 | wxList::compatibility_iterator node = seg.lines.GetFirst(); |
2108f33a JS |
127 | while (node) |
128 | { | |
b1d4dd7a | 129 | DoodleLine *line = (DoodleLine *)node->GetData(); |
2108f33a JS |
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 | ||
b1d4dd7a | 138 | node = node->GetNext(); |
2108f33a JS |
139 | } |
140 | } | |
141 | ||
142 | DoodleSegment::~DoodleSegment(void) | |
143 | { | |
bd5206dd | 144 | WX_CLEAR_LIST(wxList, lines); |
2108f33a JS |
145 | } |
146 | ||
23a54e14 | 147 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 148 | wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& stream) |
2108f33a | 149 | { |
b1d4dd7a | 150 | wxInt32 n = lines.GetCount(); |
8325937e | 151 | stream << n << _T('\n'); |
958d3a7e | 152 | |
bd5206dd | 153 | wxList::compatibility_iterator node = lines.GetFirst(); |
2108f33a JS |
154 | while (node) |
155 | { | |
b1d4dd7a | 156 | DoodleLine *line = (DoodleLine *)node->GetData(); |
958d3a7e WS |
157 | stream << line->x1 << _T(" ") << |
158 | line->y1 << _T(" ") << | |
159 | line->x2 << _T(" ") << | |
8325937e | 160 | line->y2 << _T("\n"); |
b1d4dd7a | 161 | node = node->GetNext(); |
2108f33a | 162 | } |
23a54e14 | 163 | |
2108f33a JS |
164 | return stream; |
165 | } | |
23a54e14 RR |
166 | #else |
167 | wxOutputStream &DoodleSegment::SaveObject(wxOutputStream& stream) | |
168 | { | |
169 | wxTextOutputStream text_stream( stream ); | |
170 | ||
b1d4dd7a | 171 | wxInt32 n = lines.GetCount(); |
8325937e | 172 | text_stream << n << _T('\n'); |
958d3a7e | 173 | |
bd5206dd | 174 | wxList::compatibility_iterator node = lines.GetFirst(); |
23a54e14 RR |
175 | while (node) |
176 | { | |
b1d4dd7a | 177 | DoodleLine *line = (DoodleLine *)node->GetData(); |
958d3a7e WS |
178 | text_stream << line->x1 << _T(" ") << |
179 | line->y1 << _T(" ") << | |
180 | line->x2 << _T(" ") << | |
8325937e | 181 | line->y2 << _T("\n"); |
b1d4dd7a | 182 | node = node->GetNext(); |
23a54e14 | 183 | } |
2108f33a | 184 | |
23a54e14 RR |
185 | return stream; |
186 | } | |
187 | #endif | |
188 | ||
189 | #if wxUSE_STD_IOSTREAM | |
dd107c50 | 190 | wxSTD istream& DoodleSegment::LoadObject(wxSTD istream& stream) |
2108f33a | 191 | { |
23a54e14 | 192 | wxInt32 n = 0; |
2108f33a JS |
193 | stream >> n; |
194 | ||
195 | for (int i = 0; i < n; i++) | |
196 | { | |
197 | DoodleLine *line = new DoodleLine; | |
958d3a7e WS |
198 | stream >> line->x1 >> |
199 | line->y1 >> | |
200 | line->x2 >> | |
2f6c54eb | 201 | line->y2; |
2108f33a JS |
202 | lines.Append(line); |
203 | } | |
958d3a7e | 204 | |
2108f33a JS |
205 | return stream; |
206 | } | |
23a54e14 RR |
207 | #else |
208 | wxInputStream &DoodleSegment::LoadObject(wxInputStream& stream) | |
209 | { | |
210 | wxTextInputStream text_stream( stream ); | |
2108f33a | 211 | |
23a54e14 RR |
212 | wxInt32 n = 0; |
213 | text_stream >> n; | |
214 | ||
215 | for (int i = 0; i < n; i++) | |
216 | { | |
217 | DoodleLine *line = new DoodleLine; | |
958d3a7e WS |
218 | text_stream >> line->x1 >> |
219 | line->y1 >> | |
220 | line->x2 >> | |
2f6c54eb | 221 | line->y2; |
23a54e14 RR |
222 | lines.Append(line); |
223 | } | |
958d3a7e | 224 | |
23a54e14 RR |
225 | return stream; |
226 | } | |
227 | #endif | |
2108f33a JS |
228 | void DoodleSegment::Draw(wxDC *dc) |
229 | { | |
bd5206dd | 230 | wxList::compatibility_iterator node = lines.GetFirst(); |
2108f33a JS |
231 | while (node) |
232 | { | |
b1d4dd7a | 233 | DoodleLine *line = (DoodleLine *)node->GetData(); |
2108f33a | 234 | dc->DrawLine(line->x1, line->y1, line->x2, line->y2); |
b1d4dd7a | 235 | node = node->GetNext(); |
2108f33a JS |
236 | } |
237 | } | |
238 | ||
239 | /* | |
240 | * Implementation of drawing command | |
241 | */ | |
242 | ||
243 | DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument *ddoc, DoodleSegment *seg): | |
691d944f | 244 | wxCommand(true, name) |
2108f33a JS |
245 | { |
246 | doc = ddoc; | |
247 | segment = seg; | |
248 | cmd = command; | |
249 | } | |
250 | ||
251 | DrawingCommand::~DrawingCommand(void) | |
252 | { | |
253 | if (segment) | |
254 | delete segment; | |
255 | } | |
256 | ||
257 | bool DrawingCommand::Do(void) | |
258 | { | |
259 | switch (cmd) | |
260 | { | |
261 | case DOODLE_CUT: | |
262 | { | |
263 | // Cut the last segment | |
b1d4dd7a | 264 | if (doc->GetDoodleSegments().GetCount() > 0) |
2108f33a | 265 | { |
bd5206dd | 266 | wxList::compatibility_iterator node = doc->GetDoodleSegments().GetLast(); |
2108f33a JS |
267 | if (segment) |
268 | delete segment; | |
958d3a7e | 269 | |
b1d4dd7a | 270 | segment = (DoodleSegment *)node->GetData(); |
bd5206dd | 271 | doc->GetDoodleSegments().Erase(node); |
2108f33a | 272 | |
691d944f | 273 | doc->Modify(true); |
2108f33a JS |
274 | doc->UpdateAllViews(); |
275 | } | |
276 | break; | |
277 | } | |
278 | case DOODLE_ADD: | |
279 | { | |
280 | doc->GetDoodleSegments().Append(new DoodleSegment(*segment)); | |
691d944f | 281 | doc->Modify(true); |
2108f33a JS |
282 | doc->UpdateAllViews(); |
283 | break; | |
284 | } | |
285 | } | |
691d944f | 286 | return true; |
2108f33a JS |
287 | } |
288 | ||
289 | bool 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); | |
691d944f | 299 | doc->Modify(true); |
2108f33a | 300 | doc->UpdateAllViews(); |
c67daf87 | 301 | segment = (DoodleSegment *) NULL; |
2108f33a | 302 | } |
691d944f | 303 | doc->Modify(true); |
2108f33a JS |
304 | doc->UpdateAllViews(); |
305 | break; | |
306 | } | |
307 | case DOODLE_ADD: | |
308 | { | |
309 | // Cut the last segment | |
b1d4dd7a | 310 | if (doc->GetDoodleSegments().GetCount() > 0) |
2108f33a | 311 | { |
bd5206dd | 312 | wxList::compatibility_iterator node = doc->GetDoodleSegments().GetLast(); |
b1d4dd7a | 313 | DoodleSegment *seg = (DoodleSegment *)node->GetData(); |
2108f33a | 314 | delete seg; |
bd5206dd | 315 | doc->GetDoodleSegments().Erase(node); |
2108f33a | 316 | |
691d944f | 317 | doc->Modify(true); |
2108f33a JS |
318 | doc->UpdateAllViews(); |
319 | } | |
320 | } | |
321 | } | |
691d944f | 322 | return true; |
2108f33a JS |
323 | } |
324 | ||
325 | IMPLEMENT_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 | |
329 | bool TextEditDocument::OnSaveDocument(const wxString& filename) | |
330 | { | |
331 | TextEditView *view = (TextEditView *)GetFirstView(); | |
332 | ||
333 | if (!view->textsw->SaveFile(filename)) | |
691d944f WS |
334 | return false; |
335 | Modify(false); | |
336 | return true; | |
2108f33a JS |
337 | } |
338 | ||
339 | bool TextEditDocument::OnOpenDocument(const wxString& filename) | |
340 | { | |
341 | TextEditView *view = (TextEditView *)GetFirstView(); | |
342 | if (!view->textsw->LoadFile(filename)) | |
691d944f | 343 | return false; |
2108f33a | 344 | |
691d944f WS |
345 | SetFilename(filename, true); |
346 | Modify(false); | |
2108f33a | 347 | UpdateAllViews(); |
691d944f | 348 | return true; |
2108f33a JS |
349 | } |
350 | ||
351 | bool 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 | ||
362 | void 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 | } |