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