]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
added SpinCtrl,
[wxWidgets.git] / samples / docview / doc.cpp
CommitLineData
457814b5
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$
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
56d7679d 26#include "wx/txtstrm.h"
457814b5 27
e4b19d9b 28#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 29#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5
JS
30#endif
31
32#include "doc.h"
33#include "view.h"
34
35IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
36
37DrawingDocument::DrawingDocument(void)
38{
39}
40
41DrawingDocument::~DrawingDocument(void)
42{
43 doodleSegments.DeleteContents(TRUE);
44}
45
56d7679d 46#if wxUSE_STD_IOSTREAM
457814b5
JS
47ostream& DrawingDocument::SaveObject(ostream& stream)
48{
49 wxDocument::SaveObject(stream);
23a54e14
RR
50
51 wxInt32 n = doodleSegments.Number();
52 stream << n << '\n';
53
457814b5
JS
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 }
23a54e14 63
457814b5
JS
64 return stream;
65}
56d7679d 66#else
23a54e14 67wxOutputStream& DrawingDocument::SaveObject(wxOutputStream& stream)
56d7679d
RR
68{
69 wxDocument::SaveObject(stream);
23a54e14 70
56d7679d 71 wxTextOutputStream text_stream( stream );
457814b5 72
56d7679d 73 wxInt32 n = doodleSegments.Number();
23a54e14 74 text_stream << n << '\n';
56d7679d
RR
75
76 wxNode *node = doodleSegments.First();
77 while (node)
78 {
79 DoodleSegment *segment = (DoodleSegment *)node->Data();
80 segment->SaveObject(stream);
23a54e14 81 text_stream << '\n';
56d7679d
RR
82
83 node = node->Next();
84 }
23a54e14
RR
85
86 return stream;
56d7679d
RR
87}
88#endif
89
90#if wxUSE_STD_IOSTREAM
457814b5
JS
91istream& DrawingDocument::LoadObject(istream& stream)
92{
93 wxDocument::LoadObject(stream);
23a54e14
RR
94
95 wxInt32 n = 0;
457814b5
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}
56d7679d 107#else
23a54e14 108wxInputStream& DrawingDocument::LoadObject(wxInputStream& stream)
56d7679d
RR
109{
110 wxDocument::LoadObject(stream);
111
112 wxTextInputStream text_stream( stream );
23a54e14 113
56d7679d
RR
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
23a54e14 124 return stream;
56d7679d
RR
125}
126#endif
457814b5
JS
127
128DoodleSegment::DoodleSegment(void)
129{
130}
131
132DoodleSegment::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
150DoodleSegment::~DoodleSegment(void)
151{
152 lines.DeleteContents(TRUE);
153}
154
56d7679d 155#if wxUSE_STD_IOSTREAM
457814b5
JS
156ostream& DoodleSegment::SaveObject(ostream& stream)
157{
23a54e14
RR
158 wxInt32 n = lines.Number();
159 stream << n << '\n';
160
457814b5
JS
161 wxNode *node = lines.First();
162 while (node)
163 {
164 DoodleLine *line = (DoodleLine *)node->Data();
23a54e14
RR
165 stream << line->x1 << " " <<
166 line->y1 << " " <<
167 line->x2 << " " <<
168 line->y2 << "\n";
457814b5
JS
169 node = node->Next();
170 }
23a54e14 171
457814b5
JS
172 return stream;
173}
56d7679d 174#else
23a54e14 175wxOutputStream &DoodleSegment::SaveObject(wxOutputStream& stream)
56d7679d
RR
176{
177 wxTextOutputStream text_stream( stream );
178
179 wxInt32 n = lines.Number();
23a54e14 180 text_stream << n << '\n';
56d7679d
RR
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 }
23a54e14
RR
192
193 return stream;
56d7679d
RR
194}
195#endif
457814b5 196
56d7679d 197#if wxUSE_STD_IOSTREAM
457814b5
JS
198istream& DoodleSegment::LoadObject(istream& stream)
199{
23a54e14 200 wxInt32 n = 0;
457814b5
JS
201 stream >> n;
202
203 for (int i = 0; i < n; i++)
204 {
205 DoodleLine *line = new DoodleLine;
23a54e14
RR
206 stream >> line->x1 >>
207 line->y1 >>
208 line->x2 >>
209 line->y2;
457814b5
JS
210 lines.Append(line);
211 }
23a54e14 212
457814b5
JS
213 return stream;
214}
56d7679d 215#else
23a54e14 216wxInputStream &DoodleSegment::LoadObject(wxInputStream& stream)
56d7679d
RR
217{
218 wxTextInputStream text_stream( stream );
23a54e14 219
56d7679d
RR
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 }
23a54e14
RR
232
233 return stream;
56d7679d
RR
234}
235#endif
457814b5
JS
236
237void 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
252DrawingCommand::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
260DrawingCommand::~DrawingCommand(void)
261{
262 if (segment)
263 delete segment;
264}
265
266bool 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
298bool 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();
c67daf87 310 segment = (DoodleSegment *) NULL;
457814b5
JS
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
334IMPLEMENT_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
338bool 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
348bool 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
360bool TextEditDocument::IsModified(void) const
361{
362 TextEditView *view = (TextEditView *)GetFirstView();
363 if (view)
364 {
112892b9 365 return (wxDocument::IsModified() || view->textsw->IsModified());
457814b5
JS
366 }
367 else
368 return wxDocument::IsModified();
369}
370
371void 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}