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