]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
Leftover changes for sample doc improvements in ticket #9582 (most of the patch was...
[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{
6bdf5153 42 WX_CLEAR_LIST(wxList, m_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
6bdf5153 50 wxInt32 n = m_doodleSegments.GetCount();
f6bcfd97 51 stream << n << '\n';
958d3a7e 52
6bdf5153 53 wxList::compatibility_iterator node = m_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
6bdf5153 72 wxInt32 n = m_doodleSegments.GetCount();
f6bcfd97 73 text_stream << n << '\n';
958d3a7e 74
6bdf5153 75 wxList::compatibility_iterator node = m_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);
6bdf5153 101 m_doodleSegments.Append(segment);
f6bcfd97 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);
6bdf5153 120 m_doodleSegments.Append(segment);
f6bcfd97 121 }
958d3a7e 122
f6bcfd97 123 return stream;
56d7679d
RR
124}
125#endif
457814b5 126
fbfb8bcc 127DoodleSegment::DoodleSegment(const DoodleSegment& seg):wxObject()
457814b5 128{
6bdf5153 129 wxList::compatibility_iterator node = seg.m_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
6bdf5153 139 m_lines.Append(newLine);
958d3a7e 140
b1d4dd7a 141 node = node->GetNext();
f6bcfd97 142 }
457814b5
JS
143}
144
145DoodleSegment::~DoodleSegment(void)
146{
6bdf5153 147 WX_CLEAR_LIST(wxList, m_lines)
457814b5
JS
148}
149
56d7679d 150#if wxUSE_STD_IOSTREAM
dd107c50 151wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& stream)
457814b5 152{
6bdf5153 153 wxInt32 n = m_lines.GetCount();
f6bcfd97 154 stream << n << '\n';
958d3a7e 155
6bdf5153 156 wxList::compatibility_iterator node = m_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
6bdf5153
VZ
174 wxInt32 n = m_lines.GetCount();
175 text_stream << n << wxT("\n");
958d3a7e 176
6bdf5153 177 wxList::compatibility_iterator node = m_lines.GetFirst();
f6bcfd97
BP
178 while (node)
179 {
6bdf5153
VZ
180 DoodleLine* line = (DoodleLine*)node->GetData();
181 text_stream << line->x1 << wxT(" ") <<
182 line->y1 << wxT(" ") <<
183 line->x2 << wxT(" ") <<
184 line->y2 << wxT("\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 204 line->y2;
6bdf5153 205 m_lines.Append(line);
f6bcfd97 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 224 line->y2;
6bdf5153 225 m_lines.Append(line);
f6bcfd97 226 }
958d3a7e 227
f6bcfd97 228 return stream;
56d7679d
RR
229}
230#endif
457814b5
JS
231
232void DoodleSegment::Draw(wxDC *dc)
233{
6bdf5153 234 wxList::compatibility_iterator node = m_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 246
6bdf5153
VZ
247DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument* doc, DoodleSegment* seg) :
248 wxCommand(true, name)
457814b5 249{
6bdf5153
VZ
250 m_doc = doc;
251 m_segment = seg;
252 m_cmd = command;
457814b5
JS
253}
254
255DrawingCommand::~DrawingCommand(void)
256{
6bdf5153
VZ
257 if (m_segment)
258 delete m_segment;
457814b5
JS
259}
260
261bool DrawingCommand::Do(void)
262{
6bdf5153 263 switch (m_cmd)
457814b5 264 {
f6bcfd97
BP
265 case DOODLE_CUT:
266 {
267 // Cut the last segment
6bdf5153 268 if (m_doc->GetDoodleSegments().GetCount() > 0)
f6bcfd97 269 {
6bdf5153
VZ
270 wxList::compatibility_iterator node = m_doc->GetDoodleSegments().GetLast();
271 if (m_segment)
272 delete m_segment;
958d3a7e 273
6bdf5153
VZ
274 m_segment = (DoodleSegment*)node->GetData();
275 m_doc->GetDoodleSegments().Erase(node);
958d3a7e 276
6bdf5153
VZ
277 m_doc->Modify(true);
278 m_doc->UpdateAllViews();
f6bcfd97
BP
279 }
280 break;
281 }
457814b5 282 case DOODLE_ADD:
f6bcfd97 283 {
6bdf5153
VZ
284 m_doc->GetDoodleSegments().Append(new DoodleSegment(*m_segment));
285 m_doc->Modify(true);
286 m_doc->UpdateAllViews();
f6bcfd97
BP
287 break;
288 }
457814b5 289 }
f2aea0d1 290 return true;
457814b5
JS
291}
292
293bool DrawingCommand::Undo(void)
294{
6bdf5153 295 switch (m_cmd)
457814b5 296 {
f6bcfd97
BP
297 case DOODLE_CUT:
298 {
299 // Paste the segment
6bdf5153 300 if (m_segment)
f6bcfd97 301 {
6bdf5153
VZ
302 m_doc->GetDoodleSegments().Append(m_segment);
303 m_doc->Modify(true);
304 m_doc->UpdateAllViews();
305 m_segment = NULL;
f6bcfd97 306 }
6bdf5153
VZ
307 m_doc->Modify(true);
308 m_doc->UpdateAllViews();
f6bcfd97
BP
309 break;
310 }
457814b5 311 case DOODLE_ADD:
f6bcfd97
BP
312 {
313 // Cut the last segment
6bdf5153 314 if (m_doc->GetDoodleSegments().GetCount() > 0)
f6bcfd97 315 {
6bdf5153
VZ
316 wxList::compatibility_iterator node = m_doc->GetDoodleSegments().GetLast();
317 DoodleSegment* seg = (DoodleSegment*)node->GetData();
f6bcfd97 318 delete seg;
6bdf5153 319 m_doc->GetDoodleSegments().Erase(node);
958d3a7e 320
6bdf5153
VZ
321 m_doc->Modify(true);
322 m_doc->UpdateAllViews();
f6bcfd97
BP
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{
6bdf5153 335 TextEditView* view = GetFirstView();
958d3a7e 336
6bdf5153 337 if (!view->m_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{
6bdf5153
VZ
349 TextEditView* view = GetFirstView();
350 if (!view->m_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{
6bdf5153
VZ
361 TextEditView* view = GetFirstView();
362 return (wxDocument::IsModified() || (view && view->m_textsw->IsModified()));
457814b5
JS
363}
364
365void TextEditDocument::Modify(bool mod)
366{
6bdf5153 367 TextEditView* view = GetFirstView();
958d3a7e 368
f6bcfd97 369 wxDocument::Modify(mod);
958d3a7e 370
6bdf5153
VZ
371 if (!mod && view && view->m_textsw)
372 view->m_textsw->DiscardEdits();
457814b5 373}
6bdf5153
VZ
374
375TextEditView* TextEditDocument::GetFirstView() const
376{
377 wxView* view = wxDocument::GetFirstView();
378 return view ? wxStaticCast(view, TextEditView) : NULL;
379}
380