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