1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/txtstrm.h"
28 #if !wxUSE_DOC_VIEW_ARCHITECTURE
29 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
37 DrawingDocument::~DrawingDocument(void)
39 WX_CLEAR_LIST(wxList
, doodleSegments
);
42 #if wxUSE_STD_IOSTREAM
43 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
45 wxDocument::SaveObject(stream
);
47 wxInt32 n
= doodleSegments
.GetCount();
48 stream
<< n
<< _T('\n');
50 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
53 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
54 segment
->SaveObject(stream
);
57 node
= node
->GetNext();
63 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
65 wxDocument::SaveObject(stream
);
67 wxTextOutputStream
text_stream( stream
);
69 wxInt32 n
= doodleSegments
.GetCount();
70 text_stream
<< n
<< _T('\n');
72 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
75 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
76 segment
->SaveObject(stream
);
77 text_stream
<< _T('\n');
79 node
= node
->GetNext();
86 #if wxUSE_STD_IOSTREAM
87 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
89 wxDocument::LoadObject(stream
);
94 for (int i
= 0; i
< n
; i
++)
96 DoodleSegment
*segment
= new DoodleSegment
;
97 segment
->LoadObject(stream
);
98 doodleSegments
.Append(segment
);
104 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
106 wxDocument::LoadObject(stream
);
108 wxTextInputStream
text_stream( stream
);
113 for (int i
= 0; i
< n
; i
++)
115 DoodleSegment
*segment
= new DoodleSegment
;
116 segment
->LoadObject(stream
);
117 doodleSegments
.Append(segment
);
124 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
127 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
130 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
131 DoodleLine
*newLine
= new DoodleLine
;
132 newLine
->x1
= line
->x1
;
133 newLine
->y1
= line
->y1
;
134 newLine
->x2
= line
->x2
;
135 newLine
->y2
= line
->y2
;
137 lines
.Append(newLine
);
139 node
= node
->GetNext();
143 DoodleSegment::~DoodleSegment(void)
145 WX_CLEAR_LIST(wxList
, lines
);
148 #if wxUSE_STD_IOSTREAM
149 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
151 wxInt32 n
= lines
.GetCount();
152 stream
<< n
<< _T('\n');
154 wxList::compatibility_iterator node
= lines
.GetFirst();
157 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
158 stream
<< line
->x1
<< _T(" ") <<
159 line
->y1
<< _T(" ") <<
160 line
->x2
<< _T(" ") <<
161 line
->y2
<< _T("\n");
162 node
= node
->GetNext();
168 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
170 wxTextOutputStream
text_stream( stream
);
172 wxInt32 n
= lines
.GetCount();
173 text_stream
<< n
<< _T('\n');
175 wxList::compatibility_iterator node
= lines
.GetFirst();
178 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
179 text_stream
<< line
->x1
<< _T(" ") <<
180 line
->y1
<< _T(" ") <<
181 line
->x2
<< _T(" ") <<
182 line
->y2
<< _T("\n");
183 node
= node
->GetNext();
190 #if wxUSE_STD_IOSTREAM
191 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
196 for (int i
= 0; i
< n
; i
++)
198 DoodleLine
*line
= new DoodleLine
;
199 stream
>> line
->x1
>>
209 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
211 wxTextInputStream
text_stream( stream
);
216 for (int i
= 0; i
< n
; i
++)
218 DoodleLine
*line
= new DoodleLine
;
219 text_stream
>> line
->x1
>>
229 void DoodleSegment::Draw(wxDC
*dc
)
231 wxList::compatibility_iterator node
= lines
.GetFirst();
234 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
235 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
236 node
= node
->GetNext();
241 * Implementation of drawing command
244 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
245 wxCommand(true, name
)
252 DrawingCommand::~DrawingCommand(void)
258 bool DrawingCommand::Do(void)
264 // Cut the last segment
265 if (doc
->GetDoodleSegments().GetCount() > 0)
267 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
271 segment
= (DoodleSegment
*)node
->GetData();
272 doc
->GetDoodleSegments().Erase(node
);
275 doc
->UpdateAllViews();
281 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
283 doc
->UpdateAllViews();
290 bool DrawingCommand::Undo(void)
299 doc
->GetDoodleSegments().Append(segment
);
301 doc
->UpdateAllViews();
302 segment
= (DoodleSegment
*) NULL
;
305 doc
->UpdateAllViews();
310 // Cut the last segment
311 if (doc
->GetDoodleSegments().GetCount() > 0)
313 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
314 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
316 doc
->GetDoodleSegments().Erase(node
);
319 doc
->UpdateAllViews();
326 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
328 // Since text windows have their own method for saving to/loading from files,
329 // we override OnSave/OpenDocument instead of Save/LoadObject
330 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
332 TextEditView
*view
= (TextEditView
*)GetFirstView();
334 if (!view
->textsw
->SaveFile(filename
))
340 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
342 TextEditView
*view
= (TextEditView
*)GetFirstView();
343 if (!view
->textsw
->LoadFile(filename
))
346 SetFilename(filename
, true);
352 bool TextEditDocument::IsModified(void) const
354 TextEditView
*view
= (TextEditView
*)GetFirstView();
357 return (wxDocument::IsModified() || view
->textsw
->IsModified());
360 return wxDocument::IsModified();
363 void TextEditDocument::Modify(bool mod
)
365 TextEditView
*view
= (TextEditView
*)GetFirstView();
367 wxDocument::Modify(mod
);
369 if (!mod
&& view
&& view
->textsw
)
370 view
->textsw
->DiscardEdits();