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