]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
Don't create a bitmap with dimensions <= 0...
[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{
f6bcfd97 43 doodleSegments.DeleteContents(TRUE);
457814b5
JS
44}
45
56d7679d 46#if wxUSE_STD_IOSTREAM
dd107c50 47wxSTD ostream& DrawingDocument::SaveObject(wxSTD ostream& stream)
457814b5 48{
f6bcfd97 49 wxDocument::SaveObject(stream);
457814b5 50
f6bcfd97
BP
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;
457814b5 65}
56d7679d 66#else
23a54e14 67wxOutputStream& DrawingDocument::SaveObject(wxOutputStream& stream)
56d7679d 68{
f6bcfd97
BP
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 }
56d7679d 85
f6bcfd97 86 return stream;
56d7679d
RR
87}
88#endif
89
90#if wxUSE_STD_IOSTREAM
dd107c50 91wxSTD istream& DrawingDocument::LoadObject(wxSTD istream& stream)
457814b5 92{
f6bcfd97
BP
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;
457814b5 106}
56d7679d 107#else
23a54e14 108wxInputStream& DrawingDocument::LoadObject(wxInputStream& stream)
56d7679d 109{
f6bcfd97
BP
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;
56d7679d
RR
125}
126#endif
457814b5
JS
127
128DoodleSegment::DoodleSegment(void)
129{
130}
131
132DoodleSegment::DoodleSegment(DoodleSegment& seg)
133{
f6bcfd97
BP
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 }
457814b5
JS
148}
149
150DoodleSegment::~DoodleSegment(void)
151{
f6bcfd97 152 lines.DeleteContents(TRUE);
457814b5
JS
153}
154
56d7679d 155#if wxUSE_STD_IOSTREAM
dd107c50 156wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& stream)
457814b5 157{
f6bcfd97
BP
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;
457814b5 173}
56d7679d 174#else
23a54e14 175wxOutputStream &DoodleSegment::SaveObject(wxOutputStream& stream)
56d7679d 176{
f6bcfd97
BP
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;
56d7679d
RR
194}
195#endif
457814b5 196
56d7679d 197#if wxUSE_STD_IOSTREAM
dd107c50 198wxSTD istream& DoodleSegment::LoadObject(wxSTD istream& stream)
457814b5 199{
f6bcfd97
BP
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;
457814b5 214}
56d7679d 215#else
23a54e14 216wxInputStream &DoodleSegment::LoadObject(wxInputStream& stream)
56d7679d 217{
f6bcfd97
BP
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;
56d7679d
RR
234}
235#endif
457814b5
JS
236
237void DoodleSegment::Draw(wxDC *dc)
238{
f6bcfd97
BP
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 }
457814b5
JS
246}
247
248/*
f6bcfd97
BP
249* Implementation of drawing command
250*/
457814b5
JS
251
252DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument *ddoc, DoodleSegment *seg):
f6bcfd97 253wxCommand(TRUE, name)
457814b5 254{
f6bcfd97
BP
255 doc = ddoc;
256 segment = seg;
257 cmd = command;
457814b5
JS
258}
259
260DrawingCommand::~DrawingCommand(void)
261{
f6bcfd97
BP
262 if (segment)
263 delete segment;
457814b5
JS
264}
265
266bool DrawingCommand::Do(void)
267{
f6bcfd97 268 switch (cmd)
457814b5 269 {
f6bcfd97
BP
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 }
457814b5 287 case DOODLE_ADD:
f6bcfd97
BP
288 {
289 doc->GetDoodleSegments().Append(new DoodleSegment(*segment));
290 doc->Modify(TRUE);
291 doc->UpdateAllViews();
292 break;
293 }
457814b5 294 }
f6bcfd97 295 return TRUE;
457814b5
JS
296}
297
298bool DrawingCommand::Undo(void)
299{
f6bcfd97 300 switch (cmd)
457814b5 301 {
f6bcfd97
BP
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 }
457814b5 316 case DOODLE_ADD:
f6bcfd97
BP
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 }
457814b5 330 }
f6bcfd97 331 return TRUE;
457814b5
JS
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();
f6bcfd97 341
457814b5
JS
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;
f6bcfd97 353
457814b5
JS
354 SetFilename(filename, TRUE);
355 Modify(FALSE);
356 UpdateAllViews();
357 return TRUE;
358}
359
360bool TextEditDocument::IsModified(void) const
361{
f6bcfd97
BP
362 TextEditView *view = (TextEditView *)GetFirstView();
363 if (view)
364 {
365 return (wxDocument::IsModified() || view->textsw->IsModified());
366 }
367 else
368 return wxDocument::IsModified();
457814b5
JS
369}
370
371void TextEditDocument::Modify(bool mod)
372{
f6bcfd97
BP
373 TextEditView *view = (TextEditView *)GetFirstView();
374
375 wxDocument::Modify(mod);
376
377 if (!mod && view && view->textsw)
378 view->textsw->DiscardEdits();
457814b5 379}