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