1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/txtstrm.h"
24 #if !wxUSE_DOC_VIEW_ARCHITECTURE
25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
31 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
33 DrawingDocument::~DrawingDocument(void)
35 WX_CLEAR_LIST(wxList
, doodleSegments
);
38 #if wxUSE_STD_IOSTREAM
39 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
41 wxDocument::SaveObject(stream
);
43 wxInt32 n
= doodleSegments
.GetCount();
44 stream
<< n
<< _T('\n');
46 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
49 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
50 segment
->SaveObject(stream
);
53 node
= node
->GetNext();
59 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
61 wxDocument::SaveObject(stream
);
63 wxTextOutputStream
text_stream( stream
);
65 wxInt32 n
= doodleSegments
.GetCount();
66 text_stream
<< n
<< _T('\n');
68 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
71 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
72 segment
->SaveObject(stream
);
73 text_stream
<< _T('\n');
75 node
= node
->GetNext();
82 #if wxUSE_STD_IOSTREAM
83 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
85 wxDocument::LoadObject(stream
);
90 for (int i
= 0; i
< n
; i
++)
92 DoodleSegment
*segment
= new DoodleSegment
;
93 segment
->LoadObject(stream
);
94 doodleSegments
.Append(segment
);
100 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
102 wxDocument::LoadObject(stream
);
104 wxTextInputStream
text_stream( stream
);
109 for (int i
= 0; i
< n
; i
++)
111 DoodleSegment
*segment
= new DoodleSegment
;
112 segment
->LoadObject(stream
);
113 doodleSegments
.Append(segment
);
120 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
)
123 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
126 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
127 DoodleLine
*newLine
= new DoodleLine
;
128 newLine
->x1
= line
->x1
;
129 newLine
->y1
= line
->y1
;
130 newLine
->x2
= line
->x2
;
131 newLine
->y2
= line
->y2
;
133 lines
.Append(newLine
);
135 node
= node
->GetNext();
139 DoodleSegment::~DoodleSegment(void)
141 WX_CLEAR_LIST(wxList
, lines
);
144 #if wxUSE_STD_IOSTREAM
145 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
147 wxInt32 n
= lines
.GetCount();
148 stream
<< n
<< _T('\n');
150 wxList::compatibility_iterator node
= lines
.GetFirst();
153 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
154 stream
<< line
->x1
<< _T(" ") <<
155 line
->y1
<< _T(" ") <<
156 line
->x2
<< _T(" ") <<
157 line
->y2
<< _T("\n");
158 node
= node
->GetNext();
164 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
166 wxTextOutputStream
text_stream( stream
);
168 wxInt32 n
= lines
.GetCount();
169 text_stream
<< n
<< _T('\n');
171 wxList::compatibility_iterator node
= lines
.GetFirst();
174 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
175 text_stream
<< line
->x1
<< _T(" ") <<
176 line
->y1
<< _T(" ") <<
177 line
->x2
<< _T(" ") <<
178 line
->y2
<< _T("\n");
179 node
= node
->GetNext();
186 #if wxUSE_STD_IOSTREAM
187 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
192 for (int i
= 0; i
< n
; i
++)
194 DoodleLine
*line
= new DoodleLine
;
195 stream
>> line
->x1
>>
205 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
207 wxTextInputStream
text_stream( stream
);
212 for (int i
= 0; i
< n
; i
++)
214 DoodleLine
*line
= new DoodleLine
;
215 text_stream
>> line
->x1
>>
225 void DoodleSegment::Draw(wxDC
*dc
)
227 wxList::compatibility_iterator node
= lines
.GetFirst();
230 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
231 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
232 node
= node
->GetNext();
237 * Implementation of drawing command
240 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
241 wxCommand(true, name
)
248 DrawingCommand::~DrawingCommand(void)
254 bool DrawingCommand::Do(void)
260 // Cut the last segment
261 if (doc
->GetDoodleSegments().GetCount() > 0)
263 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
267 segment
= (DoodleSegment
*)node
->GetData();
268 doc
->GetDoodleSegments().Erase(node
);
271 doc
->UpdateAllViews();
277 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
279 doc
->UpdateAllViews();
286 bool DrawingCommand::Undo(void)
295 doc
->GetDoodleSegments().Append(segment
);
297 doc
->UpdateAllViews();
298 segment
= (DoodleSegment
*) NULL
;
301 doc
->UpdateAllViews();
306 // Cut the last segment
307 if (doc
->GetDoodleSegments().GetCount() > 0)
309 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
310 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
312 doc
->GetDoodleSegments().Erase(node
);
315 doc
->UpdateAllViews();
322 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
324 // Since text windows have their own method for saving to/loading from files,
325 // we override OnSave/OpenDocument instead of Save/LoadObject
326 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
328 TextEditView
*view
= (TextEditView
*)GetFirstView();
330 if (!view
->textsw
->SaveFile(filename
))
336 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
338 TextEditView
*view
= (TextEditView
*)GetFirstView();
339 if (!view
->textsw
->LoadFile(filename
))
342 SetFilename(filename
, true);
348 bool TextEditDocument::IsModified(void) const
350 TextEditView
*view
= (TextEditView
*)GetFirstView();
353 return (wxDocument::IsModified() || view
->textsw
->IsModified());
356 return wxDocument::IsModified();
359 void TextEditDocument::Modify(bool mod
)
361 TextEditView
*view
= (TextEditView
*)GetFirstView();
363 wxDocument::Modify(mod
);
365 if (!mod
&& view
&& view
->textsw
)
366 view
->textsw
->DiscardEdits();